C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
mathtests.cpp
Go to the documentation of this file.
1 #include "../math/math.h"
2 #include "../tests/testutils.h"
3 
4 #include <cppunit/TestFixture.h>
5 #include <cppunit/extensions/HelperMacros.h>
6 
7 using namespace std;
8 using namespace MathUtilities;
9 using namespace TestUtilities::Literals;
10 
11 using namespace CPPUNIT_NS;
12 
13 namespace MathUtilities {
14 
15 static_assert(min(1, 2, 3) == 1, "min");
16 static_assert(min(3, 2, 1) == 1, "min");
17 static_assert(min(3, 4, 2, 1) == 1, "min");
18 static_assert(min(3, 4, -2, 2, 1) == -2, "min");
19 static_assert(max(1, 2, 3) == 3, "max");
20 static_assert(max(3, 2, 1) == 3, "max");
21 static_assert(max(3, 4, 2, 1) == 4, "max");
22 static_assert(max(3, -2, 4, 2, 1) == 4, "max");
23 
24 } // namespace MathUtilities
25 
29 class MathTests : public TestFixture {
30  CPPUNIT_TEST_SUITE(MathTests);
31  CPPUNIT_TEST(testRandom);
32  CPPUNIT_TEST(testDigitsum);
33  CPPUNIT_TEST(testFactorial);
34  CPPUNIT_TEST(testPowerModulo);
35  CPPUNIT_TEST(testInverseModulo);
36  CPPUNIT_TEST(testOrderModulo);
37  CPPUNIT_TEST_SUITE_END();
38 
39 public:
40  void setUp()
41  {
42  }
43  void tearDown()
44  {
45  }
46 
47  void testRandom();
48  void testDigitsum();
49  void testFactorial();
50  void testPowerModulo();
51  void testInverseModulo();
52  void testOrderModulo();
53 };
54 
56 
58 {
59 #ifndef PLATFORM_WINDOWS
60  CPPUNIT_ASSERT_EQUAL(6, random(5, 7));
61 #endif
62 }
63 
65 {
66  CPPUNIT_ASSERT_EQUAL(0, digitsum(0));
67  CPPUNIT_ASSERT_EQUAL(7, digitsum(16));
68  CPPUNIT_ASSERT_EQUAL(1, digitsum(16, 16));
69 }
70 
72 {
73  CPPUNIT_ASSERT_EQUAL(6, factorial(3));
74 }
75 
77 {
78  CPPUNIT_ASSERT_EQUAL(25_uint64, powerModulo(5, 2, 30));
79  CPPUNIT_ASSERT_EQUAL(5_uint64, powerModulo(5, 2, 20));
80 }
81 
83 {
84  CPPUNIT_ASSERT_EQUAL(-12_int64, inverseModulo(2, 25));
85  CPPUNIT_ASSERT_EQUAL(-8_int64, inverseModulo(3, 25));
86 }
87 
89 {
90  CPPUNIT_ASSERT_EQUAL(20_uint64, orderModulo(2, 25));
91  CPPUNIT_ASSERT_EQUAL(5_uint64, orderModulo(6, 25));
92  CPPUNIT_ASSERT_EQUAL(0_uint64, orderModulo(5, 25));
93 }
CPP_UTILITIES_EXPORT int factorial(int number)
Returns the factorial of the given number.
Definition: math.cpp:42
CPP_UTILITIES_EXPORT uint64 orderModulo(uint64 number, uint64 module)
Computes the order of number modulo module.
Definition: math.cpp:92
CPP_UTILITIES_EXPORT int64 inverseModulo(int64 number, int64 module)
Computes the inverse of number modulo module.
Definition: math.cpp:74
void setUp()
Definition: mathtests.cpp:40
Contains various mathematical functions.
Definition: math.h:7
The MathTests class tests functions of the MathUtilities namespace.
Definition: mathtests.cpp:29
void testRandom()
Definition: mathtests.cpp:57
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:268
void testPowerModulo()
Definition: mathtests.cpp:76
CPP_UTILITIES_EXPORT int random(int lowerbounds, int upperbounds)
Returns a pseudo random number between lowerbounds and upperbounds.
Definition: math.cpp:18
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:29
void testInverseModulo()
Definition: mathtests.cpp:82
CPPUNIT_TEST_SUITE_REGISTRATION(MathTests)
void testDigitsum()
Definition: mathtests.cpp:64
CPP_UTILITIES_EXPORT int digitsum(int number, int base=10)
Returns the digitsum of the given number using the specified base.
Definition: math.cpp:28
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:17
void testFactorial()
Definition: mathtests.cpp:71
void tearDown()
Definition: mathtests.cpp:43
void testOrderModulo()
Definition: mathtests.cpp:88
CPP_UTILITIES_EXPORT uint64 powerModulo(uint64 base, uint64 expontent, uint64 module)
Computes base power exponent modulo module.
Definition: math.cpp:55