diff --git a/include/fintamath/numbers/IInteger.hpp b/include/fintamath/numbers/IInteger.hpp deleted file mode 100644 index 93f4fe2db..000000000 --- a/include/fintamath/numbers/IInteger.hpp +++ /dev/null @@ -1,183 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "fintamath/core/MathObjectClass.hpp" -#include "fintamath/core/MathObjectUtils.hpp" -#include "fintamath/core/Parser.hpp" -#include "fintamath/numbers/INumber.hpp" - -namespace fintamath { - -class IInteger : public INumber { - FINTAMATH_PARENT_CLASS_BODY(IInteger) - -public: - friend std::unique_ptr operator%(const IInteger &lhs, const IInteger &rhs) { - return lhs.modAbstract(rhs); - } - - friend std::unique_ptr operator&(const IInteger &lhs, const IInteger &rhs) { - return lhs.bitAndAbstract(rhs); - } - - friend std::unique_ptr operator|(const IInteger &lhs, const IInteger &rhs) { - return lhs.bitOrAbstract(rhs); - } - - friend std::unique_ptr operator^(const IInteger &lhs, const IInteger &rhs) { - return lhs.bitXorAbstract(rhs); - } - - friend std::unique_ptr operator<<(const IInteger &lhs, const IInteger &rhs) { - return lhs.bitLeftShiftAbstract(rhs); - } - - friend std::unique_ptr operator>>(const IInteger &lhs, const IInteger &rhs) { - return lhs.bitRightShiftAbstract(rhs); - } - - friend std::unique_ptr operator~(const IInteger &rhs) { - return rhs.bitNotAbstract(); - } - - friend IInteger &operator++(IInteger &rhs) { - return rhs.increaseAbstract(); - } - - friend IInteger &operator--(IInteger &rhs) { - return rhs.decreaseAbstract(); - } - - friend std::unique_ptr operator++(IInteger &lhs, int) { - auto res = cast(lhs.clone()); - lhs.increaseAbstract(); - return res; - } - - friend std::unique_ptr operator--(IInteger &lhs, int) { - auto res = cast(lhs.clone()); - lhs.decreaseAbstract(); - return res; - } - -protected: - virtual std::unique_ptr modAbstract(const IInteger &rhs) const = 0; - - virtual std::unique_ptr bitAndAbstract(const IInteger &rhs) const = 0; - - virtual std::unique_ptr bitOrAbstract(const IInteger &rhs) const = 0; - - virtual std::unique_ptr bitXorAbstract(const IInteger &rhs) const = 0; - - virtual std::unique_ptr bitLeftShiftAbstract(const IInteger &rhs) const = 0; - - virtual std::unique_ptr bitRightShiftAbstract(const IInteger &rhs) const = 0; - - virtual std::unique_ptr bitNotAbstract() const = 0; - - virtual IInteger &increaseAbstract() = 0; - - virtual IInteger &decreaseAbstract() = 0; -}; - -template -class IIntegerCRTP : public IInteger { -#define I_INTEGER_CRTP IIntegerCRTP -#include "fintamath/numbers/IIntegerCRTP.hpp" -#undef I_INTEGER_CRTP -}; - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs &operator%=(Lhs &lhs, const Rhs &rhs) { - return lhs %= Lhs(rhs); -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs &operator&=(Lhs &lhs, const Rhs &rhs) { - return lhs &= Lhs(rhs); -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs &operator|=(Lhs &lhs, const Rhs &rhs) { - return lhs |= Lhs(rhs); -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs &operator^=(Lhs &lhs, const Rhs &rhs) { - return lhs ^= Lhs(rhs); -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs &operator<<=(Lhs &lhs, const Rhs &rhs) { - return lhs <<= Lhs(rhs); -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs &operator>>=(Lhs &lhs, const Rhs &rhs) { - return lhs >>= Lhs(rhs); -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs operator%(const Lhs &lhs, const Rhs &rhs) { - return lhs % Lhs(rhs); -} - -template Rhs, ConvertibleToAndNotSameAs Lhs> -Rhs operator%(const Lhs &lhs, const Rhs &rhs) { - return Rhs(lhs) % rhs; -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs operator&(const Lhs &lhs, const Rhs &rhs) { - return lhs & Lhs(rhs); -} - -template Rhs, ConvertibleToAndNotSameAs Lhs> -Rhs operator&(const Lhs &lhs, const Rhs &rhs) { - return Rhs(lhs) & rhs; -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs operator|(const Lhs &lhs, const Rhs &rhs) { - return lhs | Lhs(rhs); -} - -template Rhs, ConvertibleToAndNotSameAs Lhs> -Rhs operator|(const Lhs &lhs, const Rhs &rhs) { - return Rhs(lhs) | rhs; -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs operator^(const Lhs &lhs, const Rhs &rhs) { - return lhs ^ Lhs(rhs); -} - -template Rhs, ConvertibleToAndNotSameAs Lhs> -Rhs operator^(const Lhs &lhs, const Rhs &rhs) { - return Rhs(lhs) ^ rhs; -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs operator<<(const Lhs &lhs, const Rhs &rhs) { - return lhs << Lhs(rhs); -} - -template Rhs, ConvertibleToAndNotSameAs Lhs> -Rhs operator<<(const Lhs &lhs, const Rhs &rhs) { - return Rhs(lhs) << rhs; -} - -template Lhs, ConvertibleToAndNotSameAs Rhs> -Lhs operator>>(const Lhs &lhs, const Rhs &rhs) { - return lhs >> Lhs(rhs); -} - -template Rhs, ConvertibleToAndNotSameAs Lhs> -Rhs operator>>(const Lhs &lhs, const Rhs &rhs) { - return Rhs(lhs) >> rhs; -} - -} diff --git a/include/fintamath/numbers/IIntegerCRTP.hpp b/include/fintamath/numbers/IIntegerCRTP.hpp deleted file mode 100644 index 2bb9c02d7..000000000 --- a/include/fintamath/numbers/IIntegerCRTP.hpp +++ /dev/null @@ -1,220 +0,0 @@ -#if !defined(I_INTEGER_CRTP) && !defined(NDEBUG) - -#include "fintamath/numbers/IInteger.hpp" - -namespace fintamath { - -template -class IIntegerCRTP_ : public IInteger { - -#endif // I_INTEGER_CRTP - -#define I_NUMBER_CRTP I_INTEGER_CRTP -#include "fintamath/numbers/INumberCRTP.hpp" -#undef I_NUMBER_CRTP - -public: - Derived &operator%=(const Derived &rhs) { - return mod(rhs); - } - - Derived operator%(const Derived &rhs) const { - return Derived(cast(*this)) %= rhs; - } - - Derived &operator&=(const Derived &rhs) { - return bitAnd(rhs); - } - - Derived operator&(const Derived &rhs) const { - return Derived(cast(*this)) &= rhs; - } - - Derived &operator|=(const Derived &rhs) { - return bitOr(rhs); - } - - Derived operator|(const Derived &rhs) const { - return Derived(cast(*this)) |= rhs; - } - - Derived &operator^=(const Derived &rhs) { - return bitXor(rhs); - } - - Derived operator^(const Derived &rhs) const { - return Derived(cast(*this)) ^= rhs; - } - - Derived &operator<<=(const Derived &rhs) { - return bitLeftShift(rhs); - } - - Derived operator<<(const Derived &rhs) const { - return Derived(cast(*this)) <<= rhs; - } - - Derived &operator>>=(const Derived &rhs) { - return bitRightShift(rhs); - } - - Derived operator>>(const Derived &rhs) const { - return Derived(cast(*this)) >>= rhs; - } - - Derived operator~() const { - Derived tmp = Derived(cast(*this)); - return cast(tmp).bitNot(); - } - - Derived &operator++() { - return increase(); - } - - Derived &operator--() { - return decrease(); - } - - Derived operator++(int) { - auto res = Derived(cast(*this)); - increase(); - return res; - } - - Derived operator--(int) { - auto res = Derived(cast(*this)); - decrease(); - return res; - } - -protected: - virtual Derived &mod(const Derived &rhs) = 0; - - virtual Derived &bitAnd(const Derived &rhs) = 0; - - virtual Derived &bitOr(const Derived &rhs) = 0; - - virtual Derived &bitXor(const Derived &rhs) = 0; - - virtual Derived &bitLeftShift(const Derived &rhs) = 0; - - virtual Derived &bitRightShift(const Derived &rhs) = 0; - - virtual Derived &bitNot() = 0; - - virtual Derived &increase() = 0; - - virtual Derived &decrease() = 0; - - std::unique_ptr modAbstract(const IInteger &inRhs) const override { - return executeAbstract( - "%", inRhs, - [](I_INTEGER_CRTP &lhs, const Derived &rhs) { - return lhs.mod(rhs); - }, - [](const IInteger &lhs, const IInteger &rhs) { - return lhs % rhs; - }); - } - - std::unique_ptr bitAndAbstract(const IInteger &inRhs) const override { - return executeAbstract( - "&", inRhs, - [](I_INTEGER_CRTP &lhs, const Derived &rhs) { - return lhs.bitAnd(rhs); - }, - [](const IInteger &lhs, const IInteger &rhs) { - return lhs & rhs; - }); - } - - std::unique_ptr bitOrAbstract(const IInteger &inRhs) const override { - return executeAbstract( - "|", inRhs, - [](I_INTEGER_CRTP &lhs, const Derived &rhs) { - return lhs.bitOr(rhs); - }, - [](const IInteger &lhs, const IInteger &rhs) { - return lhs | rhs; - }); - } - - std::unique_ptr bitXorAbstract(const IInteger &inRhs) const override { - return executeAbstract( - "^", inRhs, - [](I_INTEGER_CRTP &lhs, const Derived &rhs) { - return lhs.bitXor(rhs); - }, - [](const IInteger &lhs, const IInteger &rhs) { - return lhs ^ rhs; - }); - } - - std::unique_ptr bitLeftShiftAbstract(const IInteger &inRhs) const override { - return executeAbstract( - "<<", inRhs, - [](I_INTEGER_CRTP &lhs, const Derived &rhs) { - return lhs.bitLeftShift(rhs); - }, - [](const IInteger &lhs, const IInteger &rhs) { - return lhs << rhs; - }); - } - - std::unique_ptr bitRightShiftAbstract(const IInteger &inRhs) const override { - return executeAbstract( - ">>", inRhs, - [](I_INTEGER_CRTP &lhs, const Derived &rhs) { - return lhs.bitRightShift(rhs); - }, - [](const IInteger &lhs, const IInteger &rhs) { - return lhs >> rhs; - }); - } - - std::unique_ptr bitNotAbstract() const override { - return std::make_unique(~(*this)); - } - - IInteger &increaseAbstract() override { - increase(); - return *this; - } - - IInteger &decreaseAbstract() override { - decrease(); - return *this; - } - -private: - std::unique_ptr executeAbstract(const std::string &operStr, - const IInteger &rhs, - std::invocable auto callFunc, - std::invocable auto callOper) const { - - if (const auto *rhsPtr = cast(&rhs)) { - auto lhsPtr = cast(clone()); - auto res = callFunc(*lhsPtr, *rhsPtr); - return cast(res.toMinimalObject()); - } - - if (const auto rhsPtr = cast(convert(*this, rhs))) { - auto lhsPtr = cast(clone()); - auto res = callFunc(*lhsPtr, *rhsPtr); - return cast(res.toMinimalObject()); - } - - if (const auto lhsPtr = cast(convert(rhs, *this))) { - auto res = callOper(*lhsPtr, rhs); - return cast(res->toMinimalObject()); - } - - throw InvalidInputBinaryOperatorException(operStr, toString(), rhs.toString()); - } - -private: -#if !defined(I_INTEGER_CRTP) && !defined(NDEBUG) -}; -} - -#endif // I_INTEGER_CRTP diff --git a/include/fintamath/numbers/Integer.hpp b/include/fintamath/numbers/Integer.hpp index 22690e45a..27e13929b 100644 --- a/include/fintamath/numbers/Integer.hpp +++ b/include/fintamath/numbers/Integer.hpp @@ -12,11 +12,11 @@ #include "fintamath/core/IArithmetic.hpp" #include "fintamath/core/MathObjectClass.hpp" -#include "fintamath/numbers/IInteger.hpp" +#include "fintamath/numbers/INumber.hpp" namespace fintamath { -class Integer final : public IIntegerCRTP { +class Integer final : public INumberCRTP { FINTAMATH_CLASS_BODY(Integer) public: @@ -45,6 +45,40 @@ class Integer final : public IIntegerCRTP { return backend.convert_to(); } + Integer &operator%=(const Integer &rhs); + + Integer &operator&=(const Integer &rhs); + + Integer &operator|=(const Integer &rhs); + + Integer &operator^=(const Integer &rhs); + + Integer &operator<<=(const Integer &rhs); + + Integer &operator>>=(const Integer &rhs); + + Integer operator%(const Integer &rhs) const; + + Integer operator&(const Integer &rhs) const; + + Integer operator|(const Integer &rhs) const; + + Integer operator^(const Integer &rhs) const; + + Integer operator<<(const Integer &rhs) const; + + Integer operator>>(const Integer &rhs) const; + + Integer operator~() const; + + Integer &operator++(); + + Integer &operator--(); + + Integer operator++(int); + + Integer operator--(int); + protected: bool equals(const Integer &rhs) const override; @@ -62,30 +96,120 @@ class Integer final : public IIntegerCRTP { std::unique_ptr divideAbstract(const IArithmetic &rhs) const override; - Integer &mod(const Integer &rhs) override; + Integer &negate() override; - Integer &bitAnd(const Integer &rhs) override; + Integer &mod(const Integer &rhs); - Integer &bitOr(const Integer &rhs) override; + Integer &bitAnd(const Integer &rhs); - Integer &bitXor(const Integer &rhs) override; + Integer &bitOr(const Integer &rhs); - Integer &bitLeftShift(const Integer &rhs) override; + Integer &bitXor(const Integer &rhs); - Integer &bitRightShift(const Integer &rhs) override; + Integer &bitLeftShift(const Integer &rhs); - Integer &bitNot() override; + Integer &bitRightShift(const Integer &rhs); - Integer &negate() override; + Integer &bitNot(); - Integer &increase() override; + Integer &increase(); - Integer &decrease() override; + Integer &decrease(); private: Backend backend; }; +template Rhs> +Integer &operator%=(Integer &lhs, const Rhs &rhs) { + return lhs %= Integer(rhs); +} + +template Rhs> +Integer &operator&=(Integer &lhs, const Rhs &rhs) { + return lhs &= Integer(rhs); +} + +template Rhs> +Integer &operator|=(Integer &lhs, const Rhs &rhs) { + return lhs |= Integer(rhs); +} + +template Rhs> +Integer &operator^=(Integer &lhs, const Rhs &rhs) { + return lhs ^= Integer(rhs); +} + +template Rhs> +Integer &operator<<=(Integer &lhs, const Rhs &rhs) { + return lhs <<= Integer(rhs); +} + +template Rhs> +Integer &operator>>=(Integer &lhs, const Rhs &rhs) { + return lhs >>= Integer(rhs); +} + +template Rhs> +Integer operator%(const Integer &lhs, const Rhs &rhs) { + return lhs % Integer(rhs); +} + +template Lhs> +Integer operator%(const Lhs &lhs, const Integer &rhs) { + return Integer(lhs) % rhs; +} + +template Rhs> +Integer operator&(const Integer &lhs, const Rhs &rhs) { + return lhs & Integer(rhs); +} + +template Lhs> +Integer operator&(const Lhs &lhs, const Integer &rhs) { + return Integer(lhs) & rhs; +} + +template Rhs> +Integer operator|(const Integer &lhs, const Rhs &rhs) { + return lhs | Integer(rhs); +} + +template Lhs> +Integer operator|(const Lhs &lhs, const Integer &rhs) { + return Integer(lhs) | rhs; +} + +template Rhs> +Integer operator^(const Integer &lhs, const Rhs &rhs) { + return lhs ^ Integer(rhs); +} + +template Lhs> +Integer operator^(const Lhs &lhs, const Integer &rhs) { + return Integer(lhs) ^ rhs; +} + +template Rhs> +Integer operator<<(const Integer &lhs, const Rhs &rhs) { + return lhs << Integer(rhs); +} + +template Lhs> +Integer operator<<(const Lhs &lhs, const Integer &rhs) { + return Integer(lhs) << rhs; +} + +template Rhs> +Integer operator>>(const Integer &lhs, const Rhs &rhs) { + return lhs >> Integer(rhs); +} + +template Lhs> +Integer operator>>(const Lhs &lhs, const Integer &rhs) { + return Integer(lhs) >> rhs; +} + } template <> diff --git a/src/fintamath/config/TypeConfig.cpp b/src/fintamath/config/TypeConfig.cpp index c59f312b4..cdfa74557 100644 --- a/src/fintamath/config/TypeConfig.cpp +++ b/src/fintamath/config/TypeConfig.cpp @@ -111,7 +111,6 @@ #include "fintamath/literals/constants/Pi.hpp" #include "fintamath/literals/constants/True.hpp" #include "fintamath/literals/constants/Undefined.hpp" -#include "fintamath/numbers/IInteger.hpp" #include "fintamath/numbers/INumber.hpp" #include "fintamath/numbers/Integer.hpp" #include "fintamath/numbers/Rational.hpp" @@ -128,13 +127,11 @@ TypeConfig::TypeConfig() { IComparable::registerType(); - INumber::registerType(); + INumber::registerType(); INumber::registerType(); INumber::registerType(); INumber::registerType(); - IInteger::registerType(); - ILiteral::registerType(); ILiteral::registerType(); ILiteral::registerType(); diff --git a/src/fintamath/numbers/IInteger.cpp b/src/fintamath/numbers/IInteger.cpp deleted file mode 100644 index e3f972ec4..000000000 --- a/src/fintamath/numbers/IInteger.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "fintamath/numbers/IInteger.hpp" - -namespace fintamath { - -FINTAMATH_PARENT_CLASS_IMPLEMENTATION(IInteger) - -} diff --git a/src/fintamath/numbers/Integer.cpp b/src/fintamath/numbers/Integer.cpp index 52aba98eb..6caffe5bf 100644 --- a/src/fintamath/numbers/Integer.cpp +++ b/src/fintamath/numbers/Integer.cpp @@ -79,6 +79,11 @@ Integer &Integer::divide(const Integer &rhs) { return *this; } +Integer &Integer::negate() { + backend = -backend; + return *this; +} + Integer &Integer::mod(const Integer &rhs) { if (rhs == 0) { throw UndefinedBinaryOperatorException("mod", toString(), rhs.toString()); @@ -128,11 +133,6 @@ Integer &Integer::bitNot() { return *this; } -Integer &Integer::negate() { - backend = -backend; - return *this; -} - Integer &Integer::increase() { ++backend; return *this; @@ -143,4 +143,77 @@ Integer &Integer::decrease() { return *this; } +Integer &Integer::operator%=(const Integer &rhs) { + return mod(rhs); +} + +Integer &Integer::operator&=(const Integer &rhs) { + return bitAnd(rhs); +} + +Integer &Integer::operator|=(const Integer &rhs) { + return bitOr(rhs); +} + +Integer &Integer::operator^=(const Integer &rhs) { + return bitXor(rhs); +} + +Integer &Integer::operator<<=(const Integer &rhs) { + return bitLeftShift(rhs); +} + +Integer &Integer::operator>>=(const Integer &rhs) { + return bitRightShift(rhs); +} + +Integer Integer::operator%(const Integer &rhs) const { + return Integer(*this) %= rhs; +} + +Integer Integer::operator&(const Integer &rhs) const { + return Integer(*this) &= rhs; +} + +Integer Integer::operator|(const Integer &rhs) const { + return Integer(*this) |= rhs; +} + +Integer Integer::operator^(const Integer &rhs) const { + return Integer(*this) ^= rhs; +} + +Integer Integer::operator<<(const Integer &rhs) const { + return Integer(*this) <<= rhs; +} + +Integer Integer::operator>>(const Integer &rhs) const { + return Integer(*this) >>= rhs; +} + +Integer Integer::operator~() const { + Integer tmp = Integer(*this); + return tmp.bitNot(); +} + +Integer &Integer::operator++() { + return increase(); +} + +Integer &Integer::operator--() { + return decrease(); +} + +Integer Integer::operator++(int) { + Integer res = *this; + increase(); + return res; +} + +Integer Integer::operator--(int) { + Integer res = *this; + decrease(); + return res; +} + } diff --git a/src/fintamath/numbers/NumberAbstract.cpp b/src/fintamath/numbers/NumberAbstract.cpp index 9ad847284..bb48f1413 100644 --- a/src/fintamath/numbers/NumberAbstract.cpp +++ b/src/fintamath/numbers/NumberAbstract.cpp @@ -7,7 +7,6 @@ #include "fintamath/core/IComparable.hpp" #include "fintamath/core/IMathObject.hpp" #include "fintamath/core/MathObjectUtils.hpp" -#include "fintamath/numbers/IInteger.hpp" #include "fintamath/numbers/Integer.hpp" #include "fintamath/numbers/Rational.hpp" #include "fintamath/numbers/Real.hpp" @@ -19,7 +18,7 @@ std::unique_ptr Integer::multiplyAbstract(const IArithmetic &rhs) c return cast(Integer(0).clone()); } - return IIntegerCRTP::multiplyAbstract(rhs); + return INumberCRTP::multiplyAbstract(rhs); } std::unique_ptr Integer::divideAbstract(const IArithmetic &rhs) const { @@ -33,7 +32,7 @@ std::unique_ptr Integer::divideAbstract(const IArithmetic &rhs) con } } - return IIntegerCRTP::divideAbstract(rhs); + return INumberCRTP::divideAbstract(rhs); } //-------------------------------------------------------------------------------------// diff --git a/tests/src/numbers/IIntegerTests.cpp b/tests/src/numbers/IIntegerTests.cpp deleted file mode 100644 index e3ebacfcf..000000000 --- a/tests/src/numbers/IIntegerTests.cpp +++ /dev/null @@ -1,291 +0,0 @@ -#include - -#include "fintamath/numbers/IInteger.hpp" - -#include "fintamath/exceptions/InvalidInputException.hpp" -#include "fintamath/numbers/Integer.hpp" - -using namespace fintamath; -using namespace detail; - -namespace { - -class TestInteger : public IIntegerCRTP { - FINTAMATH_PARENT_CLASS_BODY(TestInteger) - -protected: - TestInteger &mod(const TestInteger & /* rhs */) override { - return *this; - } - - TestInteger &bitAnd(const TestInteger &rhs) override { - return *this; - } - - TestInteger &bitOr(const TestInteger &rhs) override { - return *this; - } - - TestInteger &bitXor(const TestInteger &rhs) override { - return *this; - } - - TestInteger &bitLeftShift(const TestInteger &rhs) override { - return *this; - } - - TestInteger &bitRightShift(const TestInteger &rhs) override { - return *this; - } - - TestInteger &bitNot() override { - return *this; - } - - TestInteger &add(const TestInteger &rhs) override { - return *this; - } - - TestInteger &substract(const TestInteger &rhs) override { - return *this; - } - - TestInteger &multiply(const TestInteger &rhs) override { - return *this; - } - - TestInteger ÷(const TestInteger &rhs) override { - return *this; - } - - TestInteger &negate() override { - return *this; - } - - std::strong_ordering compare(const TestInteger &rhs) const override { - return std::strong_ordering::less; - } - - TestInteger &increase() override { - return *this; - } - - TestInteger &decrease() override { - return *this; - } -}; - -FINTAMATH_PARENT_CLASS_IMPLEMENTATION(TestInteger) - -class TestIntegerConvertible final : public TestInteger { - FINTAMATH_CLASS_BODY(TestIntegerConvertible) - -public: - TestIntegerConvertible() : TestInteger() { - } - - TestIntegerConvertible(const Integer &) : TestIntegerConvertible() { - } - - MathObjectClass getClass() const override { - return getClassStatic(); - } -}; - -[[maybe_unused]] const auto config = [] { - IInteger::registerType(); - TestInteger::registerType(); - - Converter::add( - [](const TestIntegerConvertible & /*type*/, const TestIntegerConvertible &value) { - return std::make_unique(value); - }); - Converter::add( - [](const TestIntegerConvertible & /*type*/, const Integer &value) { - return std::make_unique(value); - }); - - return 0; -}(); - -} - -TEST(IIntegerTests, parseTest) { - EXPECT_TRUE(is(*IInteger::parseFirst("TestInteger"))); -} - -TEST(IIntegerTests, modTest) { - std::unique_ptr m1 = std::make_unique(10); - std::unique_ptr m2 = std::make_unique(3); - - EXPECT_EQ((*m1 % *m1)->toString(), "0"); - EXPECT_EQ((*m2 % *m2)->toString(), "0"); - EXPECT_EQ((*m1 % *m2)->toString(), "1"); - EXPECT_EQ((*m2 % *m1)->toString(), "3"); - - EXPECT_TRUE(is(*m1 % *m1)); - EXPECT_TRUE(is(*m2 % *m2)); - EXPECT_TRUE(is(*m1 % *m2)); - EXPECT_TRUE(is(*m2 % *m1)); - - EXPECT_TRUE(is(*m1 % TestIntegerConvertible())); - EXPECT_TRUE(is(TestIntegerConvertible() % *m1)); - - EXPECT_THROW(*m1 % TestInteger(), InvalidInputBinaryOperatorException); - EXPECT_THROW(TestInteger() % *m1, InvalidInputBinaryOperatorException); - - Integer a; - EXPECT_EQ((a %= 3).toString(), "0"); -} - -TEST(IIntegerTests, bitAndTest) { - std::unique_ptr m1 = std::make_unique(10); - std::unique_ptr m2 = std::make_unique(3); - - EXPECT_EQ((*m1 & *m1)->toString(), "10"); - EXPECT_EQ((*m2 & *m2)->toString(), "3"); - EXPECT_EQ((*m1 & *m2)->toString(), "2"); - EXPECT_EQ((*m2 & *m1)->toString(), "2"); - - EXPECT_TRUE(is(*m1 & *m1)); - EXPECT_TRUE(is(*m2 & *m2)); - EXPECT_TRUE(is(*m1 & *m2)); - EXPECT_TRUE(is(*m2 & *m1)); - - EXPECT_TRUE(is(*m1 & TestIntegerConvertible())); - EXPECT_TRUE(is(TestIntegerConvertible() & *m1)); - - EXPECT_THROW(*m1 & TestInteger(), InvalidInputBinaryOperatorException); - EXPECT_THROW(TestInteger() & *m1, InvalidInputBinaryOperatorException); - - Integer a; - EXPECT_EQ((a &= 3).toString(), "0"); -} - -TEST(IIntegerTests, bitOrTest) { - std::unique_ptr m1 = std::make_unique(10); - std::unique_ptr m2 = std::make_unique(3); - - EXPECT_EQ((*m1 | *m1)->toString(), "10"); - EXPECT_EQ((*m2 | *m2)->toString(), "3"); - EXPECT_EQ((*m1 | *m2)->toString(), "11"); - EXPECT_EQ((*m2 | *m1)->toString(), "11"); - - EXPECT_TRUE(is(*m1 | *m1)); - EXPECT_TRUE(is(*m2 | *m2)); - EXPECT_TRUE(is(*m1 | *m2)); - EXPECT_TRUE(is(*m2 | *m1)); - - EXPECT_TRUE(is(*m1 | TestIntegerConvertible())); - EXPECT_TRUE(is(TestIntegerConvertible() | *m1)); - - EXPECT_THROW(*m1 | TestInteger(), InvalidInputBinaryOperatorException); - EXPECT_THROW(TestInteger() | *m1, InvalidInputBinaryOperatorException); - - Integer a; - EXPECT_EQ((a |= 3).toString(), "3"); -} - -TEST(IIntegerTests, bitXorTest) { - std::unique_ptr m1 = std::make_unique(10); - std::unique_ptr m2 = std::make_unique(3); - - EXPECT_EQ((*m1 ^ *m1)->toString(), "0"); - EXPECT_EQ((*m2 ^ *m2)->toString(), "0"); - EXPECT_EQ((*m1 ^ *m2)->toString(), "9"); - EXPECT_EQ((*m2 ^ *m1)->toString(), "9"); - - EXPECT_TRUE(is(*m1 ^ *m1)); - EXPECT_TRUE(is(*m2 ^ *m2)); - EXPECT_TRUE(is(*m1 ^ *m2)); - EXPECT_TRUE(is(*m2 ^ *m1)); - - EXPECT_TRUE(is(*m1 ^ TestIntegerConvertible())); - EXPECT_TRUE(is(TestIntegerConvertible() ^ *m1)); - - EXPECT_THROW(*m1 ^ TestInteger(), InvalidInputBinaryOperatorException); - EXPECT_THROW(TestInteger() ^ *m1, InvalidInputBinaryOperatorException); - - Integer a; - EXPECT_EQ((a ^= 3).toString(), "3"); -} - -TEST(IIntegerTests, bitLeftShiftTest) { - std::unique_ptr m1 = std::make_unique(10); - std::unique_ptr m2 = std::make_unique(3); - - EXPECT_EQ((*m1 << *m1)->toString(), "10240"); - EXPECT_EQ((*m2 << *m2)->toString(), "24"); - EXPECT_EQ((*m1 << *m2)->toString(), "80"); - EXPECT_EQ((*m2 << *m1)->toString(), "3072"); - - EXPECT_TRUE(is(*m1 << *m1)); - EXPECT_TRUE(is(*m2 << *m2)); - EXPECT_TRUE(is(*m1 << *m2)); - EXPECT_TRUE(is(*m2 << *m1)); - - EXPECT_TRUE(is(*m1 << TestIntegerConvertible())); - EXPECT_TRUE(is(TestIntegerConvertible() << *m1)); - - EXPECT_THROW(*m1 << TestInteger(), InvalidInputBinaryOperatorException); - EXPECT_THROW(TestInteger() << *m1, InvalidInputBinaryOperatorException); - - Integer a; - EXPECT_EQ((a <<= 3).toString(), "0"); -} - -TEST(IIntegerTests, bitRightShiftTest) { - std::unique_ptr m1 = std::make_unique(10); - std::unique_ptr m2 = std::make_unique(3); - - EXPECT_EQ((*m1 >> *m1)->toString(), "0"); - EXPECT_EQ((*m2 >> *m2)->toString(), "0"); - EXPECT_EQ((*m1 >> *m2)->toString(), "1"); - EXPECT_EQ((*m2 >> *m1)->toString(), "0"); - - EXPECT_TRUE(is(*m1 >> *m1)); - EXPECT_TRUE(is(*m2 >> *m2)); - EXPECT_TRUE(is(*m1 >> *m2)); - EXPECT_TRUE(is(*m2 >> *m1)); - - EXPECT_TRUE(is(*m1 >> TestIntegerConvertible())); - EXPECT_TRUE(is(TestIntegerConvertible() >> *m1)); - - EXPECT_THROW(*m1 >> TestInteger(), InvalidInputBinaryOperatorException); - EXPECT_THROW(TestInteger() >> *m1, InvalidInputBinaryOperatorException); - - Integer a; - EXPECT_EQ((a >>= 3).toString(), "0"); -} - -TEST(IIntegerTests, bitNotTest) { - const std::unique_ptr m1 = std::make_unique(44); - EXPECT_EQ((~*m1)->toString(), "-45"); - - EXPECT_EQ((~Integer(1)).toString(), "-2"); -} - -TEST(IIntegerTests, incTest) { - const std::unique_ptr m1 = std::make_unique(1); - EXPECT_EQ((++*m1).toString(), "2"); - EXPECT_EQ(((*m1)++)->toString(), "2"); - EXPECT_EQ(m1->toString(), "3"); - - EXPECT_EQ((++Integer(1)).toString(), "2"); - EXPECT_EQ((Integer(1)++).toString(), "1"); -} - -TEST(IIntegerTests, decTest) { - const std::unique_ptr m1 = std::make_unique(1); - EXPECT_EQ((--*m1).toString(), "0"); - EXPECT_EQ(((*m1)--)->toString(), "0"); - EXPECT_EQ(m1->toString(), "-1"); - - EXPECT_EQ((--Integer(1)).toString(), "0"); - EXPECT_EQ((Integer(1)--).toString(), "1"); -} - -TEST(IIntegerTests, getClassTest) { - EXPECT_EQ(IInteger::getClassStatic(), MathObjectClass("IInteger")); - EXPECT_EQ(IInteger::getClassStatic().getParent(), INumber::getClassStatic()); -} diff --git a/tests/src/numbers/IntegerTests.cpp b/tests/src/numbers/IntegerTests.cpp index 6bbf95218..e1dc2d504 100644 --- a/tests/src/numbers/IntegerTests.cpp +++ b/tests/src/numbers/IntegerTests.cpp @@ -610,7 +610,7 @@ TEST(IntegerTests, equalsTest) { TEST(IntegerTests, getClassTest) { EXPECT_EQ(Integer().getClass(), MathObjectClass("Integer")); - EXPECT_EQ(Integer().getClass().getParent(), IInteger::getClassStatic()); + EXPECT_EQ(Integer().getClass().getParent(), INumber::getClassStatic()); } TEST(IntegerTests, hashTest) {