C++ Utilities  5.10.5
Useful C++ classes and routines such as argument parser, IO and conversion utilities
mathtests.cpp
Go to the documentation of this file.
1 #include "../misc/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 CppUtilities;
9 using namespace CppUtilities::Literals;
10 
11 using namespace CPPUNIT_NS;
12 
13 namespace CppUtilities {
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 CppUtilities
25 
29 class MathTests : public TestFixture {
30  CPPUNIT_TEST_SUITE(MathTests);
31  CPPUNIT_TEST(testDigitsum);
32  CPPUNIT_TEST(testFactorial);
33  CPPUNIT_TEST(testPowerModulo);
34  CPPUNIT_TEST(testInverseModulo);
35  CPPUNIT_TEST(testOrderModulo);
36  CPPUNIT_TEST_SUITE_END();
37 
38 public:
39  void setUp()
40  {
41  }
42  void tearDown()
43  {
44  }
45 
46  void testDigitsum();
47  void testFactorial();
48  void testPowerModulo();
49  void testInverseModulo();
50  void testOrderModulo();
51 };
52 
54 
56 {
57  CPPUNIT_ASSERT_EQUAL(0, digitsum(0));
58  CPPUNIT_ASSERT_EQUAL(7, digitsum(16));
59  CPPUNIT_ASSERT_EQUAL(1, digitsum(16, 16));
60 }
61 
63 {
64  CPPUNIT_ASSERT_EQUAL(6, factorial(3));
65 }
66 
68 {
69  CPPUNIT_ASSERT_EQUAL(25u, powerModulo(5u, 2u, 30u));
70  CPPUNIT_ASSERT_EQUAL(5u, powerModulo(5u, 2u, 20u));
71 }
72 
74 {
75  CPPUNIT_ASSERT_EQUAL(-12u, inverseModulo(2u, 25u));
76  CPPUNIT_ASSERT_EQUAL(-8u, inverseModulo(3u, 25u));
77 }
78 
80 {
81  CPPUNIT_ASSERT_EQUAL(20u, orderModulo(2u, 25u));
82  CPPUNIT_ASSERT_EQUAL(5u, orderModulo(6u, 25u));
83  CPPUNIT_ASSERT_EQUAL(0u, orderModulo(5u, 25u));
84 }
The MathTests class tests functions provided by misc/math.h.
Definition: mathtests.cpp:29
void testPowerModulo()
Definition: mathtests.cpp:67
void testOrderModulo()
Definition: mathtests.cpp:79
void tearDown()
Definition: mathtests.cpp:42
void testDigitsum()
Definition: mathtests.cpp:55
void setUp()
Definition: mathtests.cpp:39
void testFactorial()
Definition: mathtests.cpp:62
void testInverseModulo()
Definition: mathtests.cpp:73
CPPUNIT_TEST_SUITE_REGISTRATION(MathTests)
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:317
Contains all utilities provides by the c++utilities library.
constexpr IntegralType factorial(IntegralType number)
Returns the factorial of the given number.
Definition: math.h:29
constexpr IntegralType inverseModulo(IntegralType number, IntegralType module)
Computes the inverse of number modulo module.
Definition: math.h:61
constexpr IntegralType digitsum(IntegralType number, IntegralType base=10)
Returns the digitsum of the given number using the specified base.
Definition: math.h:16
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:100
constexpr IntegralType powerModulo(const IntegralType base, const IntegralType exponent, const IntegralType module)
Computes base power exponent modulo module.
Definition: math.h:42
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:88
constexpr IntegralType orderModulo(const IntegralType number, const IntegralType module)
Computes the order of number modulo module.
Definition: math.h:79