text:
//********************************************************************************
// インクルードガード
//********************************************************************************

#ifndef STATE_MACHINE_HPP
#define STATE_MACHINE_HPP

//********************************************************************************
// 必要なヘッダファイルのインクルード
//********************************************************************************

#include "state.hpp"
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <functional>
#include <iterator>
#include <optional>
#include <utility>

//********************************************************************************
// クラスの定義
//********************************************************************************

/**
 * @brief shared_ptr に this ポインタを渡すときは、enable_shared_from_this
 * を継承してshared_from_this()を呼び出してください。
 */
template <typename T> struct state_machine : private boost::noncopyable {
public:
  state_machine(std::shared_ptr<T> owner, const state<T> &init)
      : owner_(std::move(owner)), current_(init) {}

  void change_state(const state<T> &next) {
    previous_ = current_;
    if (current_ && owner_) {
      current_.exit(*owner_.get());
    }
    current_ = next;
    if (current_ && owner_) {
      current_.enter(*owner_.get());
    }
  }

  void update() {
    decltype(auto) states = {current_, global_};
    for (auto &&state : states) {
      if (state && owner_) {
        state.exec(*owner_.get());
      }
    }
  }

  void set_global_state(state<T> next) { global_ = next; }

private:
  std::shared_ptr<T> owner_ = nullptr;
  std::optional<state<T>> current_ = std::nullopt;
  std::optional<state<T>> previous_ = std::nullopt;
  std::optional<state<T>> global_ = std::nullopt;
};

#endif

max_stars_repo_name: mnrn/game-memo

max_issues_repo_name: mnrn/game-memo

max_forks_repo_name: mnrn/game-memo


text:
#ifndef W_INTEGRALS_HPP
#define W_INTEGRALS_HPP

#include <vector>
#include <libint2.hpp>
#include <armadillo>

namespace willow { namespace qcmol {

struct QAtom {
  double charge;
  double x, y, z;
};

#define INDEX(i,j) ( (i >= j) ? (((i)*((i)+1)/2) + (j)) : (((j)*((j)+1)/2) + (i)))

class Integrals {
public:
  // Constructor
  Integrals (const std::vector<libint2::Atom>& atoms,
	     const libint2::BasisSet& bs,
	     const std::vector<QAtom>& atoms_Q=std::vector<QAtom>() );

  // Data members
  arma::mat Sm;  // Overlap
  arma::mat Tm;  // Kinetic-energy
  arma::mat Vm;  // Nuclear-Attraction
  arma::mat SmInvh;  // Orthogonalize Basis
  arma::mat Km;  // Schwartz screening
  arma::vec TEI;  // Store 2-e integrals
  bool    l_eri_direct; //

};

extern arma::mat compute_2body_fock_memory (const arma::vec& TEI,
					    const arma::mat& Dm);

extern arma::mat compute_shellblock_norm(const libint2::BasisSet& obs,
					 const arma::mat& A);

extern arma::mat compute_2body_fock (const libint2::BasisSet& bs,
				     const arma::mat& D,
				     const arma::mat& Schwartz);

extern arma::mat compute_2body_fock_general (const libint2::BasisSet& bs,
					     const arma::mat& Dm,
					     const libint2::BasisSet& Dm_bs,
					     bool Dm_is_diagonal=false);

} } // namespace willow::qcmol

#endif

ext: hpp

max_stars_repo_name: swillow/w-qcmol

max_issues_repo_name: swillow/w-qcmol

max_forks_repo_name: swillow/w-qcmol


text:
//==============================================================================
//         Copyright 2003 - 2011   LASMEA UMR 6602 CNRS/Univ. Clermont II
//         Copyright 2009 - 2011   LRI    UMR 8623 CNRS/Univ Paris Sud XI
//
//          Distributed under the Boost Software License, Version 1.0.
//                 See accompanying file LICENSE.txt or copy at
//                     http://www.boost.org/LICENSE_1_0.txt
//==============================================================================
#ifndef BOOST_DISPATCH_META_DETAILS_HIERARCHY_OF_HPP_INCLUDED
#define BOOST_DISPATCH_META_DETAILS_HIERARCHY_OF_HPP_INCLUDED

#include <boost/dispatch/meta/property_of.hpp>
#include <boost/dispatch/meta/details/scalar.hpp>
#include <boost/dispatch/meta/enable_if_type.hpp>
#include <boost/type_traits/is_fundamental.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/if.hpp>
#include <boost/utility/enable_if.hpp>

namespace boost { namespace dispatch { namespace details
{
  //============================================================================
  // Overload for fundamental types
  //============================================================================
  template<class T,class Origin>
  struct  hierarchy_of< T
                      , Origin
                      , typename
                        boost::enable_if< boost::is_fundamental<T> >::type
                      >
  {
    typedef typename remove_const<Origin>::type stripped;
    typedef typename mpl::if_< is_same< T, stripped >, stripped, Origin>::type origin_;
    typedef meta::scalar_<typename meta::property_of<T, origin_>::type>  type;
  };

  //============================================================================
  // Overload for hierarchies
  //============================================================================
  template<class T>
  struct  hierarchy_of< T
                      , T
                      , typename
                        meta::enable_if_type< typename T::hierarchy_tag >::type
                      >
  {
    typedef T type;
  };

  template<class T>
  struct  hierarchy_of< T
                      , T const
                      , typename
                        meta::enable_if_type< typename T::hierarchy_tag >::type
                      >
  {
    typedef T type;
  };
} } }

#endif


text:
#pragma warning (disable : 4250)

#include "spectra_di_ns.h"

#include <Eigen/Core>
#include <Eigen/SparseCore>
#include <Spectra/GenEigsSolver.h>
#include <Spectra/GenEigsRealShiftSolver.h>
#include <Spectra/GenEigsComplexShiftSolver.h>
#include <Spectra/MatOp/SparseGenMatProd.h>
#include <Spectra/MatOp/SparseGenRealShiftSolve.h>

using namespace Spectra;

using MapVec = Eigen::Map<Eigen::VectorXcd>;
using MapMat = Eigen::Map<Eigen::MatrixXcd>;
using SpMatrix = Eigen::SparseMatrix<double>;

template <typename TypeA, typename Solver>
int solve_di_ns(const TypeA& A, Solver& eigs, int maxit, double tol, SortRule selection, spectra_result* result)
{
    eigs.init();

    int nconv = eigs.compute(selection, maxit, tol, SortRule::SmallestAlge);

    CompInfo info = eigs.info();

    if (info == CompInfo::Successful)
    {
        auto evals = static_cast<Eigen::dcomplex*>(result->eigval);
        MapVec v(evals, nconv, 1);
        v.noalias() = eigs.eigenvalues();

        if (result->eigvec != NULL)
        {
            auto evecs = static_cast<Eigen::dcomplex*>(result->eigvec);
            MapMat m(evecs, A.rows(), nconv);
            m.noalias() = eigs.eigenvectors();
        }
    }

    result->iterations = eigs.num_iterations();
    result->info = static_cast<int>(info);

    return nconv;
}
int spectra_di_ns(int which, int k, int ncv, int maxit, double tol,
    spectra_spmat* A, spectra_result* result)
{
    try
    {
        SortRule selection;

        SELECTION_RULE_FROM_INT(which)

        // We are going to calculate the eigenvalues of M
        Eigen::Map<const SpMatrix> M(A->n, A->n, A->nnz, A->p, A->i, (double*)A->x);

        SparseGenMatProd<double> op(M);
        GenEigsSolver<double, SparseGenMatProd<double>> eigs(op, k, ncv);

        return solve_di_ns(M, eigs, maxit, tol, selection, result);
    }
    catch (std::exception& e)
    {
        result->info = -1001;
    }
    catch (...)
    {
        result->info = -1000;
    }

    return 0;
}

int spectra_di_ns_shift(int which, int k, int ncv, int maxit, double tol, double sigma,
    spectra_spmat* A, spectra_result* result)
{
    try
    {
        SortRule selection;

        SELECTION_RULE_FROM_INT(which)

        // We are going to calculate the eigenvalues of M
        Eigen::Map<const SpMatrix> M(A->n, A->n, A->nnz, A->p, A->i, (double*)A->x);

        SparseGenRealShiftSolve<double> op(M);
        GenEigsRealShiftSolver<double, SparseGenRealShiftSolve<double>> eigs(op, k, ncv, sigma);

        return solve_di_ns(M, eigs, maxit, tol, selection, result);
    }
    catch (std::exception& e)
    {
        result->info = -1001;
    }
    catch (...)
    {
        result->info = -1000;
    }

    return 0;
}

ext: cpp


text:
#include <b0/logger/logger.h>
#include <b0/publisher.h>
#include <b0/message/log/log_entry.h>
#include <b0/node.h>
#include <b0/exception/argument_error.h>
#include <b0/utils/thread_name.h>
#include <b0/utils/env.h>

#include <iostream>
#include <iomanip>
#include <chrono>

#include <boost/lexical_cast.hpp>

namespace b0
{

namespace logger
{

void LogInterface::log_helper(Level level, boost::format &format) const
{
    return log(level, format.str());
}

LocalLogger::LocalLogger(b0::Node *node)
    : node_(node),
      color_(false)
{
    outputLevel_ = getConsoleLogLevel();

    std::string term = b0::env::get("TERM");
    if(term == "xterm-color" || term == "xterm-256color") color_ = true;
}

LocalLogger::~LocalLogger()
{
}

void LocalLogger::log(Level level, const std::string &message) const
{
    if(level < outputLevel_) return;

    LevelInfo info = levelInfo(level);
    std::stringstream ss;
    if(color_) ss << info.ansiEscape();

    auto now = std::chrono::system_clock::now();
    std::time_t time = std::chrono::system_clock::to_time_t(now);
    ss << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S ");

    if(node_)
    {
        std::string name = node_->getName();
        if(!name.empty()) ss << "[" << name << "] ";
    }

    ss << info.str << ": ";

    ss << message;

    if(color_) ss << info.ansiReset();

    std::cout << ss.str() << std::endl;
}

struct Logger::Private
{
    Private(Node *node)
        : pub_(node, "log", false, false)
    {
    }

    Publisher pub_;
};

Logger::Logger(b0::Node *node)
    : LocalLogger(node),
      private_(new Private(node))
{
}

Logger::~Logger()
{
}

void Logger::connect(const std::string &addr)
{
    private_->pub_.setRemoteAddress(addr);
    private_->pub_.init();
}

void Logger::log(Level level, const std::string &message) const
{
    LocalLogger::log(level, message);

    remoteLog(level, message);
}

void Logger::remoteLog(Level level, const std::string &message) const
{
    b0::message::log::LogEntry e;

    if(node_)
    {
        e.node_name = node_->getName();
        e.time_usec = node_->timeUSec();
    }

    e.level = levelInfo(level).str;
    e.message = message;

    private_->pub_.publish(e);
}

} // namespace logger

} // namespace b0

max_stars_repo_name: YueErro/ModernRobotics

max_issues_repo_name: YueErro/ModernRobotics

max_forks_repo_name: YueErro/ModernRobotics


text:
/******************************************************************************
Copyright (c) 2021 Dmitriy Korchemkin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef POLYNOMIALS_DENSE_POLYNOMIAL_HPP
#define POLYNOMIALS_DENSE_POLYNOMIAL_HPP

#include "polynomials/assert.hpp"
#include "polynomials/types.hpp"

#include <Eigen/Dense>

namespace Eigen {
namespace internal {
template <typename Scalar_, Index DegreeAtCompileTime,
          Index MaxDegreeAtCompileTime, int Options_>
struct traits<polynomials::DensePoly<Scalar_, DegreeAtCompileTime,
                                     MaxDegreeAtCompileTime, Options_>> {
  static constexpr int Options = Options_;
  using Scalar = Scalar_;
};

template <typename Scalar_, Index DegreeAtCompileTime,
          Index MaxDegreeAtCompileTime, int Options_>
struct traits<Map<polynomials::DensePoly<Scalar_, DegreeAtCompileTime,
                                         MaxDegreeAtCompileTime>,
                  Options_>> {

  static constexpr int Options = Options_;
  using Scalar = Scalar_;
};
template <typename Scalar_, Index DegreeAtCompileTime,
          Index MaxDegreeAtCompileTime, int Options_>
struct traits<Map<const polynomials::DensePoly<Scalar_, DegreeAtCompileTime,
                                               MaxDegreeAtCompileTime>,
                  Options_>> {

  static constexpr int Options = Options_;
  using Scalar = Scalar_;
};

} // namespace internal
} // namespace Eigen

namespace polynomials {

template <typename Derived_> struct DensePolyBase {
  using Derived = Derived_;
  using Scalar = typename Eigen::internal::traits<Derived>::Scalar;
  template <typename T>
  using OpType = typename Eigen::ScalarBinaryOpTraits<Scalar, T>::ReturnType;
  static constexpr Index DegreeAtCompileTime = Derived::DegreeAtCompileTime;
  static constexpr Index MaxDegreeAtCompileTime =
      Derived::MaxDegreeAtCompileTime;
  static constexpr Index CoeffsCompileTime = Derived::CoeffsCompileTime;
  static constexpr Index MaxCoeffsCompileTime = Derived::MaxCoeffsCompileTime;

  template <typename T = Scalar>
  auto operator()(const T &at) const -> OpType<T> {
    using Res = OpType<T>;
    const auto &C = coeffs();

    const Index n_coeffs = total_coeffs();
    Res result(C[n_coeffs - 1]);

    for (Index i = n_coeffs - 2; i >= 0; --i) {
      result *= at;
      result += C[i];
    }

    return result;
  }

  template <typename T = Scalar> auto df(const T &at) const -> OpType<T> {
    using Res = OpType<T>;
    const auto &C = coeffs();

    const Index n_coeffs = total_coeffs();
    Res result(C[n_coeffs - 1] * T(n_coeffs - 1));

    for (Index i = n_coeffs - 2; i > 0; --i) {
      result *= at;
      result += C[i] * T(i);
    }

    return result;
  }

  auto operator<<(const Scalar &s) { return coeffs().operator<<(s); }

  template <template <typename> typename Algorithm = QuotientRingMulX>
  auto roots() const {
    return RootFinder<Derived, Algorithm>::complex_roots(*this);
  }

  template <template <typename> typename Algorithm = QuotientRingMulX>
  auto real_roots() const {
    return RootFinder<Derived, Algorithm>::real_roots(*this);
  }

  template <template <typename> typename Algorithm = QuotientRingMulX>
  auto positive_real_roots() const {
    return RootFinder<Derived, Algorithm>::positive_real_roots(*this);
  }

  auto operator[](const Index &i) const { return coeffs()[i]; }
  auto &operator[](const Index &i) { return coeffs()[i]; }

  const auto &coeffs() const { return derived().coeffs(); }
  auto &coeffs() { return derived().coeffs(); }

  constexpr Index degree() const {
    return DegreeAtCompileTime == Dynamic ? derived().degree()
                                          : DegreeAtCompileTime;
  }
  constexpr Index total_coeffs() const {
    return CoeffsCompileTime == Dynamic ? coeffs().size() : CoeffsCompileTime;
  }

  void resize(const Index degree) { derived().resize(degree); }

  template <typename OtherDerived>
  Derived &operator=(const DensePolyBase<OtherDerived> &other);

protected:
  auto &derived() const { return *static_cast<const Derived *>(this); }
  auto &derived() { return *static_cast<Derived *>(this); }
};

template <typename T> struct is_dense_poly : std::false_type {};

template <typename T>
struct is_dense_poly<DensePolyBase<T>> : std::true_type {};

constexpr Index max_num_coeffs(const Index MaxDegreeAtCompileTime) {
  if (MaxDegreeAtCompileTime == Dynamic)
    return Dynamic;
  return MaxDegreeAtCompileTime + 1;
}
constexpr Index num_coeffs_compile_time(const Index DegreeAtCompileTime) {

  if (DegreeAtCompileTime != Dynamic)
    return DegreeAtCompileTime + 1;
  return Dynamic;
}

constexpr Index max_or_dynamic(const Index &a, const Index &b) {
  if (a == Dynamic || b == Dynamic)
    return Dynamic;
  return std::max(a, b);
}
constexpr Index min_or_dynamic(const Index &a, const Index &b) {
  if (a == Dynamic || b == Dynamic)
    return Dynamic;
  return std::min(a, b);
}

template <typename T, Index DegreeAtCompileTime_, Index MaxDegreeAtCompileTime_,
          int Options>
struct DensePoly : DensePolyBase<DensePoly<T, DegreeAtCompileTime_,
                                           MaxDegreeAtCompileTime_, Options>> {
  using Scalar = T;
  using Base = DensePolyBase<
      DensePoly<T, DegreeAtCompileTime_, MaxDegreeAtCompileTime_>>;
  static constexpr Index DegreeAtCompileTime = DegreeAtCompileTime_;
  static constexpr Index MaxDegreeAtCompileTime = MaxDegreeAtCompileTime_;

  static constexpr Index CoeffsCompileTime =
      num_coeffs_compile_time(DegreeAtCompileTime);
  static constexpr Index MaxCoeffsCompileTime =
      max_num_coeffs(MaxDegreeAtCompileTime);
  using Coeffs = Eigen::Matrix<Scalar, CoeffsCompileTime, 1, Options,
                               MaxCoeffsCompileTime, 1>;
  using Base::operator=;

  DensePoly(const Index degree = DegreeAtCompileTime)
      : coeffs_(degree + 1, 1) {}

  template <typename OtherDerived>
  DensePoly(const DensePolyBase<OtherDerived> &other)
      : DensePoly(DegreeAtCompileTime == Dynamic ? other.degree()
                                                 : DegreeAtCompileTime) {
    using Other = DensePolyBase<OtherDerived>;
    static_assert(std::is_same_v<Scalar, typename Other::Scalar>,
                  "Incompatible scalar types for copy ctor");
    static_assert(DegreeAtCompileTime == Dynamic ||
                  Other::DegreeAtCompileTime <= DegreeAtCompileTime);
    if constexpr (MaxDegreeAtCompileTime != Dynamic &&
                  Other::DegreeAtCompileTime == Dynamic) {
      POLYNOMIALS_ASSERT(MaxDegreeAtCompileTime >= other.degree(),
                         "Not enough storage to assign "
                             << other.degree() << "-degree polynomial");
    }

    const Index head = other.total_coeffs();
    const Index tail = Base::total_coeffs() - head;
    if (head) {
      coeffs_.head(head) = other.coeffs();
    }
    if (tail) {
      coeffs_.tail(tail).setZero();
    }
  }

  DensePoly(const DensePoly &) = default;
  DensePoly(DensePoly &&) = default;

  const Coeffs &coeffs() const { return coeffs_; }
  Coeffs &coeffs() { return coeffs_; }

  constexpr Index degree() const {
    return DegreeAtCompileTime == Dynamic ? coeffs_.size() - 1
                                          : DegreeAtCompileTime;
  }

  void resize(const Index degree) { coeffs_.resize(degree + 1, 1); }

  const Scalar *data() const { return coeffs_.data(); }
  Scalar *data() { return coeffs_.data(); }

  DensePoly &operator=(const DensePoly &) = default;
  DensePoly &operator=(DensePoly &&) = default;

protected:
  Coeffs coeffs_;
};
} // namespace polynomials

namespace Eigen {

template <typename T, Index DegreeAtCompileTime_, Index MaxDegreeAtCompileTime_,
          int Options>
class Map<polynomials::DensePoly<T, DegreeAtCompileTime_,
                                 MaxDegreeAtCompileTime_, Options>>
    : public polynomials::DensePolyBase<Map<polynomials::DensePoly<
          T, DegreeAtCompileTime_, MaxDegreeAtCompileTime_, Options>>> {
public:
  using Scalar = T;
  using Mapped =
      typename polynomials::DensePoly<T, DegreeAtCompileTime_,
                                      MaxDegreeAtCompileTime_, Options>;
  using Base = polynomials::DensePolyBase<Map<polynomials::DensePoly<
      T, DegreeAtCompileTime_, MaxDegreeAtCompileTime_, Options>>>;
  static constexpr Index DegreeAtCompileTime = Mapped::DegreeAtCompileTime;
  static constexpr Index MaxDegreeAtCompileTime =
      Mapped::MaxDegreeAtCompileTime;
  static constexpr Index CoeffsCompileTime = Mapped::CoeffsCompileTime;
  static constexpr Index MaxCoeffsCompileTime = Mapped::MaxCoeffsCompileTime;

  using Base::operator=;
  using Coeffs = Eigen::Map<typename Mapped::Coeffs>;

  Map(T *data, const Index degree = DegreeAtCompileTime)
      : coeffs_(data, degree + 1, 1) {}

  const Coeffs &coeffs() const { return coeffs_; }
  Coeffs &coeffs() { return coeffs_; }

  constexpr Index degree() const {
    return DegreeAtCompileTime == polynomials::Dynamic ? coeffs_.size() - 1
                                                       : DegreeAtCompileTime;
  }

  const Scalar *data() const { return coeffs_.data(); }
  Scalar *data() { return coeffs_.data(); }

  void resize(const Index degree) { coeffs_.resize(degree + 1); }

  Map &operator=(const Map &map) {
    Base::template operator=<Map>(map);
    return *this;
  }

protected:
  Coeffs coeffs_;
};

template <typename T, Index DegreeAtCompileTime_, Index MaxDegreeAtCompileTime_,
          int Options>
class Map<const polynomials::DensePoly<T, DegreeAtCompileTime_,
                                       MaxDegreeAtCompileTime_, Options>>
    : public polynomials::DensePolyBase<Map<const polynomials::DensePoly<
          T, DegreeAtCompileTime_, MaxDegreeAtCompileTime_, Options>>> {
public:
  using Scalar = T;
  using Base = polynomials::DensePolyBase<Map<const polynomials::DensePoly<
      T, DegreeAtCompileTime_, MaxDegreeAtCompileTime_, Options>>>;
  using Mapped =
      typename polynomials::DensePoly<T, DegreeAtCompileTime_,
                                      MaxDegreeAtCompileTime_, Options>;
  static constexpr Index DegreeAtCompileTime = Mapped::DegreeAtCompileTime;
  static constexpr Index MaxDegreeAtCompileTime =
      Mapped::MaxDegreeAtCompileTime;
  static constexpr Index CoeffsCompileTime = Mapped::CoeffsCompileTime;
  static constexpr Index MaxCoeffsCompileTime = Mapped::MaxCoeffsCompileTime;

  using Coeffs = Eigen::Map<const typename Mapped::Coeffs>;

  Map(const T *data, const Index degree = DegreeAtCompileTime)
      : coeffs_(data, degree + 1, 1) {}

  const Coeffs &coeffs() const { return coeffs_; }

  constexpr Index degree() const {
    return DegreeAtCompileTime == polynomials::Dynamic ? coeffs_.size() - 1
                                                       : DegreeAtCompileTime;
  }

  const Scalar *data() const { return coeffs_.data(); }

protected:
  const Coeffs coeffs_;
};

} // namespace Eigen

#include "src/dense_operators.hpp"
#include "src/dense_scalar_operators.hpp"
#include "src/roots.hpp"
#endif

max_stars_repo_name: DmitriyKorchemkin/polynomials

max_issues_repo_name: DmitriyKorchemkin/polynomials

max_forks_repo_name: DmitriyKorchemkin/polynomials


text:
// Wheels - various C++ utilities
//
// Written in 2013 by Martinho Fernandes <martinho.fernandes@gmail.com>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>

// tribool tests

#include <wheels/tribool.h++>

#include <boost/logic/tribool.hpp>

#include <catch.hpp>

#define CHECK_TRIBOOL_TRUE(x) {                         \
        CHECK(::wheels::is_true((x)));                  \
        CHECK_FALSE(::wheels::is_false((x)));           \
        CHECK_FALSE(::wheels::is_indeterminate((x)));   \
    }
#define CHECK_TRIBOOL_FALSE(x) {                        \
        CHECK_FALSE(::wheels::is_true((x)));            \
        CHECK(::wheels::is_false((x)));                 \
        CHECK_FALSE(::wheels::is_indeterminate((x)));   \
    }
#define CHECK_TRIBOOL_INDETERMINATE(x) {        \
        CHECK_FALSE(::wheels::is_true((x)));    \
        CHECK_FALSE(::wheels::is_false((x)));   \
        CHECK(::wheels::is_indeterminate((x))); \
    }

TEST_CASE("tribool", "tribool tests") {
    using wheels::indeterminate;
    static_assert(!std::is_convertible<wheels::tribool, bool>(), "tribool cannot be implicitly convertible to bool");

    wheels::tribool xxx;
    SECTION("default-ctor", "") {
        wheels::tribool a;
        CHECK_TRIBOOL_FALSE(a);
    }
    SECTION("bool-ctor", "") {
        {
            wheels::tribool a = true;
            CHECK_TRIBOOL_TRUE(a);
        }
        {
            wheels::tribool a = false;
            CHECK_TRIBOOL_FALSE(a);
        }
        {
            wheels::tribool a = indeterminate;
            CHECK_TRIBOOL_INDETERMINATE(a);
        }
    }
    SECTION("negation", "") {
        {
            wheels::tribool a(true);
            CHECK_TRIBOOL_FALSE(!a);
        }
        {
            wheels::tribool a(false);
            CHECK_TRIBOOL_TRUE(!a);
        }
        {
            wheels::tribool a(indeterminate);
            CHECK_TRIBOOL_INDETERMINATE(!a);
        }
    }
    SECTION("equal", "") {
        {
            wheels::tribool a(true);
            wheels::tribool b(true);
            CHECK_TRIBOOL_TRUE(a == b);
        }
        {
            wheels::tribool a(true);
            wheels::tribool b(false);
            CHECK_TRIBOOL_FALSE(a == b);
        }
        {
            wheels::tribool a(true);
            wheels::tribool b(indeterminate);
            CHECK_TRIBOOL_INDETERMINATE(a == b);
        }
        {
            wheels::tribool a(false);
            wheels::tribool b(true);
            CHECK_TRIBOOL_FALSE(a == b);
        }
        {
            wheels::tribool a(false);
            wheels::tribool b(false);
            CHECK_TRIBOOL_TRUE(a == b);
        }
        {
            wheels::tribool a(false);
            wheels::tribool b(indeterminate);
            CHECK_TRIBOOL_INDETERMINATE(a == b);
        }
        {
            wheels::tribool a(indeterminate);
            wheels::tribool b(true);
            CHECK_TRIBOOL_INDETERMINATE(a == b);
        }
        {
            wheels::tribool a(indeterminate);
            wheels::tribool b(false);
            CHECK_TRIBOOL_INDETERMINATE(a == b);
        }
        {
            wheels::tribool a(indeterminate);
            wheels::tribool b(indeterminate);
            CHECK_TRIBOOL_INDETERMINATE(a == b);
        }
    }
    SECTION("not-equal", "") {
        {
            wheels::tribool a(true);
            wheels::tribool b(true);
            CHECK_TRIBOOL_FALSE(a != b);
        }
        {
            wheels::tribool a(true);
            wheels::tribool b(false);
            CHECK_TRIBOOL_TRUE(a != b);
        }
        {
            wheels::tribool a(true);
            wheels::tribool b(indeterminate);
            CHECK_TRIBOOL_INDETERMINATE(a != b);
        }
        {
            wheels::tribool a(false);
            wheels::tribool b(true);
            CHECK_TRIBOOL_TRUE(a != b);
        }
        {
            wheels::tribool a(false);
            wheels::tribool b(false);
            CHECK_TRIBOOL_FALSE(a != b);
        }
        {
            wheels::tribool a(false);
            wheels::tribool b(indeterminate);
            CHECK_TRIBOOL_INDETERMINATE(a != b);
        }
        {
            wheels::tribool a(indeterminate);
            wheels::tribool b(true);
            CHECK_TRIBOOL_INDETERMINATE(a != b);
        }
        {
            wheels::tribool a(indeterminate);
            wheels::tribool b(false);
            CHECK_TRIBOOL_INDETERMINATE(a != b);
        }
        {
            wheels::tribool a(indeterminate);
            wheels::tribool b(indeterminate);
            CHECK_TRIBOOL_INDETERMINATE(a != b);
        }
    }
    SECTION("boost-interop", "") {
        {
            boost::tribool b = true;
            wheels::tribool a = b;
            CHECK_TRIBOOL_TRUE(a);
        }
        {
            boost::tribool b = false;
            wheels::tribool a = b;
            CHECK_TRIBOOL_FALSE(a);
        }
        {
            boost::tribool b = boost::indeterminate;
            wheels::tribool a = b;
            CHECK_TRIBOOL_INDETERMINATE(a);
        }
        {
            wheels::tribool a = true;
            boost::tribool b = a;
            CHECK(bool(b));
        }
        {
            wheels::tribool a = false;
            boost::tribool b = a;
            CHECK(bool(!b));
        }
        {
            wheels::tribool a = indeterminate;
            boost::tribool b = a;
            CHECK(boost::indeterminate(b));
        }
    }
    SECTION("and", "") {
        {
            wheels::tribool a = true;
            wheels::tribool b = true;
            CHECK_TRIBOOL_TRUE(a && b);
        }
        {
            wheels::tribool a = true;
            wheels::tribool b = false;
            CHECK_TRIBOOL_FALSE(a && b);
        }
        {
            wheels::tribool a = true;
            wheels::tribool b = indeterminate;
            CHECK_TRIBOOL_INDETERMINATE(a && b);
        }
        {
            wheels::tribool a = false;
            wheels::tribool b = true;
            CHECK_TRIBOOL_FALSE(a && b);
        }
        {
            wheels::tribool a = false;
            wheels::tribool b = false;
            CHECK_TRIBOOL_FALSE(a && b);
        }
        {
            wheels::tribool a = false;
            wheels::tribool b = indeterminate;
            CHECK_TRIBOOL_FALSE(a && b);
        }
        {
            wheels::tribool a = indeterminate;
            wheels::tribool b = true;
            CHECK_TRIBOOL_INDETERMINATE(a && b);
        }
        {
            wheels::tribool a = indeterminate;
            wheels::tribool b = false;
            CHECK_TRIBOOL_FALSE(a && b);
        }
        {
            wheels::tribool a = indeterminate;
            wheels::tribool b = indeterminate;
            CHECK_TRIBOOL_INDETERMINATE(a && b);
        }
    }
    SECTION("or", "") {
        {
            wheels::tribool a = true;
            wheels::tribool b = true;
            CHECK_TRIBOOL_TRUE(a || b);
        }
        {
            wheels::tribool a = true;
            wheels::tribool b = false;
            CHECK_TRIBOOL_TRUE(a || b);
        }
        {
            wheels::tribool a = true;
            wheels::tribool b = indeterminate;
            CHECK_TRIBOOL_TRUE(a || b);
        }
        {
            wheels::tribool a = false;
            wheels::tribool b = true;
            CHECK_TRIBOOL_TRUE(a || b);
        }
        {
            wheels::tribool a = false;
            wheels::tribool b = false;
            CHECK_TRIBOOL_FALSE(a || b);
        }
        {
            wheels::tribool a = false;
            wheels::tribool b = indeterminate;
            CHECK_TRIBOOL_INDETERMINATE(a || b);
        }
        {
            wheels::tribool a = indeterminate;
            wheels::tribool b = true;
            CHECK_TRIBOOL_TRUE(a || b);
        }
        {
            wheels::tribool a = indeterminate;
            wheels::tribool b = false;
            CHECK_TRIBOOL_INDETERMINATE(a || b);
        }
        {
            wheels::tribool a = indeterminate;
            wheels::tribool b = indeterminate;
            CHECK_TRIBOOL_INDETERMINATE(a || b);
        }
    }
}

max_stars_repo_name: dendisuhubdy/wheels

max_issues_repo_name: dendisuhubdy/wheels

max_forks_repo_name: dendisuhubdy/wheels