From e868dca255c2bf56f756d6df934eafda1669b8dc Mon Sep 17 00:00:00 2001 From: fintarin Date: Mon, 5 Feb 2024 17:49:05 +0300 Subject: [PATCH] Add hash tests --- tests/src/core/MathObjectTypeTests.cpp | 10 ++++++++ tests/src/numbers/IntegerTests.cpp | 18 ++++++++++++++ tests/src/numbers/RationalTests.cpp | 22 +++++++++++++++++ tests/src/numbers/RealTests.cpp | 34 ++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/tests/src/core/MathObjectTypeTests.cpp b/tests/src/core/MathObjectTypeTests.cpp index d108ccec1..211aecbd6 100644 --- a/tests/src/core/MathObjectTypeTests.cpp +++ b/tests/src/core/MathObjectTypeTests.cpp @@ -72,3 +72,13 @@ TEST(MathObjectTypesTests, isBaseOfTest) { EXPECT_FALSE(isBaseOf(IConstant::getTypeStatic(), INumber::getTypeStatic())); EXPECT_FALSE(isBaseOf(Boolean::getTypeStatic(), INumber::getTypeStatic())); } + +TEST(MathObjectTypesTests, hashTest) { + EXPECT_EQ(boost::hash{}(MathObjectType::IMathObject), static_cast(MathObjectType::IMathObject)); + EXPECT_EQ(boost::hash{}(MathObjectType::INumber), static_cast(MathObjectType::INumber)); + EXPECT_EQ(boost::hash{}(MathObjectType::Integer), static_cast(MathObjectType::Integer)); + EXPECT_EQ(boost::hash{}(MathObjectType::ILiteral), static_cast(MathObjectType::ILiteral)); + EXPECT_EQ(boost::hash{}(MathObjectType::IConstant), static_cast(MathObjectType::IConstant)); + EXPECT_EQ(boost::hash{}(MathObjectType::Boolean), static_cast(MathObjectType::Boolean)); + EXPECT_EQ(boost::hash{}(MathObjectType::None), static_cast(MathObjectType::None)); +} diff --git a/tests/src/numbers/IntegerTests.cpp b/tests/src/numbers/IntegerTests.cpp index d446cb8dd..65da10593 100644 --- a/tests/src/numbers/IntegerTests.cpp +++ b/tests/src/numbers/IntegerTests.cpp @@ -2,6 +2,8 @@ #include "fintamath/numbers/Integer.hpp" +#include + #include "fintamath/exceptions/InvalidInputException.hpp" #include "fintamath/exceptions/UndefinedException.hpp" #include "fintamath/numbers/Rational.hpp" @@ -878,3 +880,19 @@ TEST(IntegerTests, getTypeTest) { EXPECT_EQ(Integer::getTypeStatic(), MathObjectType::Integer); EXPECT_EQ(Integer().getType(), MathObjectType::Integer); } + +TEST(IntegerTests, hashTest) { + constexpr auto hash = boost::hash{}; + + EXPECT_EQ(hash(Integer(0)), hash(Integer(0))); + EXPECT_EQ(hash(Integer(12)), hash(Integer(12))); + EXPECT_EQ(hash(Integer(-12)), hash(Integer(-12))); + EXPECT_EQ(hash(Integer("452734865298734659873246238756987435")), hash(Integer("452734865298734659873246238756987435"))); + EXPECT_EQ(hash(Integer("-452734865298734659873246238756987435")), hash(Integer("-452734865298734659873246238756987435"))); + + EXPECT_NE(hash(Integer(0)), hash(Integer(1))); + EXPECT_NE(hash(Integer(12)), hash(Integer(13))); + EXPECT_NE(hash(Integer(-12)), hash(Integer(-13))); + EXPECT_NE(hash(Integer("452734865298734659873246238756987435")), hash(Integer("452734865298734659873246238756987436"))); + EXPECT_NE(hash(Integer("-452734865298734659873246238756987435")), hash(Integer("-452734865298734659873246238756987436"))); +} diff --git a/tests/src/numbers/RationalTests.cpp b/tests/src/numbers/RationalTests.cpp index 61584d2c8..95e538cc2 100644 --- a/tests/src/numbers/RationalTests.cpp +++ b/tests/src/numbers/RationalTests.cpp @@ -1,5 +1,7 @@ #include +#include + #include "fintamath/exceptions/InvalidInputException.hpp" #include "fintamath/exceptions/UndefinedException.hpp" #include "fintamath/numbers/Rational.hpp" @@ -521,3 +523,23 @@ TEST(RationalTests, getTypeTest) { EXPECT_EQ(Rational::getTypeStatic(), MathObjectType::Rational); EXPECT_EQ(Rational().getType(), MathObjectType::Rational); } + +TEST(RationalTests, hashTest) { + constexpr auto hash = boost::hash{}; + + EXPECT_EQ(hash(Rational(0)), hash(Rational(0))); + EXPECT_EQ(hash(Rational(12)), hash(Rational(12))); + EXPECT_EQ(hash(Rational(-12)), hash(Rational(-12))); + EXPECT_EQ(hash(Rational(3, 2)), hash(Rational(3, 2))); + EXPECT_EQ(hash(Rational(-3, 2)), hash(Rational(-3, 2))); + EXPECT_EQ(hash(Rational(Integer("452734865298734659873246238756987435"), Integer("39842732658275642342352642634234234"))), hash(Rational(Integer("452734865298734659873246238756987435"), Integer("39842732658275642342352642634234234")))); + EXPECT_EQ(hash(Rational(-Integer("452734865298734659873246238756987435"), Integer("39842732658275642342352642634234234"))), hash(Rational(-Integer("452734865298734659873246238756987435"), Integer("39842732658275642342352642634234234")))); + + EXPECT_NE(hash(Rational(0)), hash(Rational(1))); + EXPECT_NE(hash(Rational(12)), hash(Rational(13))); + EXPECT_NE(hash(Rational(-12)), hash(Rational(-13))); + EXPECT_NE(hash(Rational(3, 2)), hash(Rational(2, 3))); + EXPECT_NE(hash(Rational(-3, 2)), hash(Rational(-2, 3))); + EXPECT_NE(hash(Rational(Integer("39842732658275642342352642634234234"), Integer("452734865298734659873246238756987435"))), Rational(Integer("452734865298734659873246238756987435"), Integer("39842732658275642342352642634234234"))); + EXPECT_NE(hash(Rational(-Integer("39842732658275642342352642634234234"), Integer("452734865298734659873246238756987435"))), Rational(-Integer("452734865298734659873246238756987435"), Integer("39842732658275642342352642634234234"))); +} diff --git a/tests/src/numbers/RealTests.cpp b/tests/src/numbers/RealTests.cpp index 0ab57776d..ae5a6d25e 100644 --- a/tests/src/numbers/RealTests.cpp +++ b/tests/src/numbers/RealTests.cpp @@ -2,6 +2,8 @@ #include "fintamath/numbers/Real.hpp" +#include + #include "fintamath/exceptions/InvalidInputException.hpp" #include "fintamath/exceptions/UndefinedException.hpp" @@ -1053,3 +1055,35 @@ TEST(RealTests, getTypeTest) { EXPECT_EQ(Real::getTypeStatic(), MathObjectType::Real); EXPECT_EQ(Real().getType(), MathObjectType::Real); } + +TEST(RealTests, hashTest) { + constexpr auto hash = boost::hash{}; + + EXPECT_EQ(hash(Real(0)), hash(Real(0))); + EXPECT_EQ(hash(Real(12)), hash(Real(12))); + EXPECT_EQ(hash(Real(-12)), hash(Real(-12))); + EXPECT_EQ(hash(Real("452734865298734659873246238756987435")), hash(Real("452734865298734659873246238756987435"))); + EXPECT_EQ(hash(Real("-452734865298734659873246238756987435")), hash(Real("-452734865298734659873246238756987435"))); + EXPECT_EQ(hash(Real("1.4357")), hash(Real("1.4357"))); + EXPECT_EQ(hash(Real("-1.4357")), hash(Real("-1.4357"))); + EXPECT_EQ(hash(Real("3.3333")), hash(Real("3.3333"))); + EXPECT_EQ(hash(Real("-3.3333")), hash(Real("-3.3333"))); + EXPECT_EQ(hash(Real("123.00001")), hash(Real("123.00001"))); + EXPECT_EQ(hash(Real("-123.00001")), hash(Real("-123.00001"))); + EXPECT_EQ(hash(Real("897259832648723648327648273463287.48732648273652873")), hash(Real("897259832648723648327648273463287.48732648273652873"))); + EXPECT_EQ(hash(Real("-897259832648723648327648273463287.48732648273652873")), hash(Real("-897259832648723648327648273463287.48732648273652873"))); + + EXPECT_NE(hash(Real(0)), hash(Real(1))); + EXPECT_NE(hash(Real(12)), hash(Real(13))); + EXPECT_NE(hash(Real(-12)), hash(Real(-13))); + EXPECT_NE(hash(Real("452734865298734659873246238756987435")), hash(Real("452734865298734659873246238756987436"))); + EXPECT_NE(hash(Real("-452734865298734659873246238756987435")), hash(Real("-452734865298734659873246238756987436"))); + EXPECT_NE(hash(Real("1.4357")), hash(Real("1.4358"))); + EXPECT_NE(hash(Real("-1.4357")), hash(Real("-1.4358"))); + EXPECT_NE(hash(Real("3.3333")), hash(Real("3.33333"))); + EXPECT_NE(hash(Real("-3.3333")), hash(Real("-3.33333"))); + EXPECT_NE(hash(Real("123.00001")), hash(Real("123.000001"))); + EXPECT_NE(hash(Real("-123.00001")), hash(Real("-123.000001"))); + EXPECT_NE(hash(Real("897259832648723648327648273463287.48732648273652873")), hash(Real("897259832648723648327648273463287.48732648273652874"))); + EXPECT_NE(hash(Real("-897259832648723648327648273463287.48732648273652873")), hash(Real("-897259832648723648327648273463287.48732648273652874"))); +}