C++ Utilities  4.9.2
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 
3 #include <cppunit/TestFixture.h>
4 #include <cppunit/extensions/HelperMacros.h>
5 
6 using namespace std;
7 using namespace MathUtilities;
8 
9 using namespace CPPUNIT_NS;
10 
14 class MathTests : public TestFixture {
15  CPPUNIT_TEST_SUITE(MathTests);
16  CPPUNIT_TEST(testRandom);
17  CPPUNIT_TEST(testDigitsum);
18  CPPUNIT_TEST(testFactorial);
19  CPPUNIT_TEST(testPowerModulo);
20  CPPUNIT_TEST(testInverseModulo);
21  CPPUNIT_TEST(testOrderModulo);
22  CPPUNIT_TEST_SUITE_END();
23 
24 public:
25  void setUp()
26  {
27  }
28  void tearDown()
29  {
30  }
31 
32  void testRandom();
33  void testDigitsum();
34  void testFactorial();
35  void testPowerModulo();
36  void testInverseModulo();
37  void testOrderModulo();
38 };
39 
41 
43 {
44  CPPUNIT_ASSERT_EQUAL(6, random(5, 7));
45 }
46 
48 {
49  CPPUNIT_ASSERT_EQUAL(0, digitsum(0));
50  CPPUNIT_ASSERT_EQUAL(7, digitsum(16));
51  CPPUNIT_ASSERT_EQUAL(1, digitsum(16, 16));
52 }
53 
55 {
56  CPPUNIT_ASSERT_EQUAL(6, factorial(3));
57 }
58 
60 {
61  CPPUNIT_ASSERT_EQUAL(25ul, powerModulo(5, 2, 30));
62  CPPUNIT_ASSERT_EQUAL(5ul, powerModulo(5, 2, 20));
63 }
64 
66 {
67  CPPUNIT_ASSERT_EQUAL(-12l, inverseModulo(2, 25));
68  CPPUNIT_ASSERT_EQUAL(-8l, inverseModulo(3, 25));
69 }
70 
72 {
73  CPPUNIT_ASSERT_EQUAL(20ul, orderModulo(2, 25));
74  CPPUNIT_ASSERT_EQUAL(5ul, orderModulo(6, 25));
75  CPPUNIT_ASSERT_EQUAL(0ul, orderModulo(5, 25));
76 }
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:25
Contains various mathematical functions.
Definition: math.h:7
STL namespace.
The MathTests class tests functions of the MathUtilities namespace.
Definition: mathtests.cpp:14
void testRandom()
Definition: mathtests.cpp:42
void testPowerModulo()
Definition: mathtests.cpp:59
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:65
CPPUNIT_TEST_SUITE_REGISTRATION(MathTests)
void testDigitsum()
Definition: mathtests.cpp:47
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:54
void tearDown()
Definition: mathtests.cpp:28
void testOrderModulo()
Definition: mathtests.cpp:71
CPP_UTILITIES_EXPORT uint64 powerModulo(uint64 base, uint64 expontent, uint64 module)
Computes base power exponent modulo module.
Definition: math.cpp:51