C++ Utilities: tests/mathtests.cpp Source File

C++ Utilities 5.35.0 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
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
7using namespace std;
8using namespace CppUtilities;
9using namespace CppUtilities::Literals;
10
11using namespace CPPUNIT_NS;
12
13namespace CppUtilities {
14
15static_assert(min(1, 2, 3) == 1, "min");
16static_assert(min(3, 2, 1) == 1, "min");
17static_assert(min(3, 4, 2, 1) == 1, "min");
18static_assert(min(3, 4, -2, 2, 1) == -2, "min");
19static_assert(max(1, 2, 3) == 3, "max");
20static_assert(max(3, 2, 1) == 3, "max");
21static_assert(max(3, 4, 2, 1) == 4, "max");
22static_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
38public:
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
53CPPUNIT_TEST_SUITE_REGISTRATION(MathTests);
54
55 void MathTests::testDigitsum()
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
62 void MathTests::testFactorial()
63{
64 CPPUNIT_ASSERT_EQUAL(6, factorial(3));
65}
66
67 void MathTests::testPowerModulo()
68{
69 CPPUNIT_ASSERT_EQUAL(25u, powerModulo(5u, 2u, 30u));
70 CPPUNIT_ASSERT_EQUAL(5u, powerModulo(5u, 2u, 20u));
71}
72
73 void MathTests::testInverseModulo()
74{
75 CPPUNIT_ASSERT_EQUAL(-12u, inverseModulo(2u, 25u));
76 CPPUNIT_ASSERT_EQUAL(-8u, inverseModulo(3u, 25u));
77}
78
79 void MathTests::testOrderModulo()
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}
MathTests
The MathTests class tests functions provided by misc/math.h.
Definition mathtests.cpp:29
MathTests::testPowerModulo
void testPowerModulo()
Definition mathtests.cpp:67
MathTests::testOrderModulo
void testOrderModulo()
Definition mathtests.cpp:79
MathTests::tearDown
void tearDown()
Definition mathtests.cpp:42
MathTests::testDigitsum
void testDigitsum()
Definition mathtests.cpp:55
MathTests::setUp
void setUp()
Definition mathtests.cpp:39
MathTests::testFactorial
void testFactorial()
Definition mathtests.cpp:62
MathTests::testInverseModulo
void testInverseModulo()
Definition mathtests.cpp:73
math.h
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE_REGISTRATION(MathTests)
CppUtilities::Literals
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Definition testutils.h:366
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::factorial
constexpr IntegralType factorial(IntegralType number)
Returns the factorial of the given number.
Definition math.h:25
CppUtilities::inverseModulo
constexpr IntegralType inverseModulo(IntegralType number, IntegralType module)
Computes the inverse of number modulo module.
Definition math.h:57
CppUtilities::digitsum
constexpr IntegralType digitsum(IntegralType number, IntegralType base=10)
Returns the digitsum of the given number using the specified base.
Definition math.h:12
CppUtilities::max
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition math.h:96
CppUtilities::powerModulo
constexpr IntegralType powerModulo(const IntegralType base, const IntegralType exponent, const IntegralType module)
Computes base power exponent modulo module.
Definition math.h:38
CppUtilities::min
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition math.h:84
CppUtilities::orderModulo
constexpr IntegralType orderModulo(const IntegralType number, const IntegralType module)
Computes the order of number modulo module.
Definition math.h:75
std
STL namespace.
testutils.h