C++ Utilities  4.11.0
Common C++ classes and routines used by my applications 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 
16 class MathTests : public TestFixture {
17  CPPUNIT_TEST_SUITE(MathTests);
18  CPPUNIT_TEST(testRandom);
19  CPPUNIT_TEST(testDigitsum);
20  CPPUNIT_TEST(testFactorial);
21  CPPUNIT_TEST(testPowerModulo);
22  CPPUNIT_TEST(testInverseModulo);
23  CPPUNIT_TEST(testOrderModulo);
24  CPPUNIT_TEST_SUITE_END();
25 
26 public:
27  void setUp()
28  {
29  }
30  void tearDown()
31  {
32  }
33 
34  void testRandom();
35  void testDigitsum();
36  void testFactorial();
37  void testPowerModulo();
38  void testInverseModulo();
39  void testOrderModulo();
40 };
41 
43 
45 {
46  CPPUNIT_ASSERT_EQUAL(6, random(5, 7));
47 }
48 
50 {
51  CPPUNIT_ASSERT_EQUAL(0, digitsum(0));
52  CPPUNIT_ASSERT_EQUAL(7, digitsum(16));
53  CPPUNIT_ASSERT_EQUAL(1, digitsum(16, 16));
54 }
55 
57 {
58  CPPUNIT_ASSERT_EQUAL(6, factorial(3));
59 }
60 
62 {
63  CPPUNIT_ASSERT_EQUAL(25_uint64, powerModulo(5, 2, 30));
64  CPPUNIT_ASSERT_EQUAL(5_uint64, powerModulo(5, 2, 20));
65 }
66 
68 {
69  CPPUNIT_ASSERT_EQUAL(-12_int64, inverseModulo(2, 25));
70  CPPUNIT_ASSERT_EQUAL(-8_int64, inverseModulo(3, 25));
71 }
72 
74 {
75  CPPUNIT_ASSERT_EQUAL(20_uint64, orderModulo(2, 25));
76  CPPUNIT_ASSERT_EQUAL(5_uint64, orderModulo(6, 25));
77  CPPUNIT_ASSERT_EQUAL(0_uint64, orderModulo(5, 25));
78 }
CPP_UTILITIES_EXPORT int factorial(int number)
Returns the factorial of the given number.
Definition: math.cpp:39
CPP_UTILITIES_EXPORT uint64 orderModulo(uint64 number, uint64 module)
Computes the order of number modulo module.
Definition: math.cpp:86
CPP_UTILITIES_EXPORT int64 inverseModulo(int64 number, int64 module)
Computes the inverse of number modulo module.
Definition: math.cpp:69
void setUp()
Definition: mathtests.cpp:27
Contains various mathematical functions.
Definition: math.h:7
STL namespace.
The MathTests class tests functions of the MathUtilities namespace.
Definition: mathtests.cpp:16
void testRandom()
Definition: mathtests.cpp:44
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:210
void testPowerModulo()
Definition: mathtests.cpp:61
CPP_UTILITIES_EXPORT int random(int lowerbounds, int upperbounds)
Returns a pseudo random number between lowerbounds and upperbounds.
Definition: math.cpp:17
void testInverseModulo()
Definition: mathtests.cpp:67
CPPUNIT_TEST_SUITE_REGISTRATION(MathTests)
void testDigitsum()
Definition: mathtests.cpp:49
CPP_UTILITIES_EXPORT int digitsum(int number, int base=10)
Returns the digitsum of the given number using the specified base.
Definition: math.cpp:26
void testFactorial()
Definition: mathtests.cpp:56
void tearDown()
Definition: mathtests.cpp:30
void testOrderModulo()
Definition: mathtests.cpp:73
CPP_UTILITIES_EXPORT uint64 powerModulo(uint64 base, uint64 expontent, uint64 module)
Computes base power exponent modulo module.
Definition: math.cpp:51