Skip to content

Commit

Permalink
Add hash tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 6, 2024
1 parent 20dc2ca commit e868dca
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/src/core/MathObjectTypeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>{}(MathObjectType::IMathObject), static_cast<size_t>(MathObjectType::IMathObject));
EXPECT_EQ(boost::hash<MathObjectType>{}(MathObjectType::INumber), static_cast<size_t>(MathObjectType::INumber));
EXPECT_EQ(boost::hash<MathObjectType>{}(MathObjectType::Integer), static_cast<size_t>(MathObjectType::Integer));
EXPECT_EQ(boost::hash<MathObjectType>{}(MathObjectType::ILiteral), static_cast<size_t>(MathObjectType::ILiteral));
EXPECT_EQ(boost::hash<MathObjectType>{}(MathObjectType::IConstant), static_cast<size_t>(MathObjectType::IConstant));
EXPECT_EQ(boost::hash<MathObjectType>{}(MathObjectType::Boolean), static_cast<size_t>(MathObjectType::Boolean));
EXPECT_EQ(boost::hash<MathObjectType>{}(MathObjectType::None), static_cast<size_t>(MathObjectType::None));
}
18 changes: 18 additions & 0 deletions tests/src/numbers/IntegerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "fintamath/numbers/Integer.hpp"

#include <unordered_set>

#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/numbers/Rational.hpp"
Expand Down Expand Up @@ -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<Integer>{};

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")));
}
22 changes: 22 additions & 0 deletions tests/src/numbers/RationalTests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <gtest/gtest.h>

#include <unordered_set>

#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/numbers/Rational.hpp"
Expand Down Expand Up @@ -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<Rational>{};

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")));
}
34 changes: 34 additions & 0 deletions tests/src/numbers/RealTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "fintamath/numbers/Real.hpp"

#include <unordered_set>

#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/exceptions/UndefinedException.hpp"

Expand Down Expand Up @@ -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<Real>{};

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")));
}

0 comments on commit e868dca

Please sign in to comment.