-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fixed_bignum.cpp
108 lines (97 loc) · 3.72 KB
/
test_fixed_bignum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "fixed_bignum.h"
#include "test_helpers.h"
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/generators/catch_generators_adapters.hpp>
#include <catch2/generators/catch_generators_all.hpp>
#include <compare>
#include <cstdint>
#include <string>
using TestFixed = FixedBigNum<2>;
TEST_CASE("Test FixedBigNum constructor works as expected", "[fixbig_ctor]") {
auto a = GENERATE(take(100, random<long>(INT64_MIN, INT64_MAX)));
TestFixed b{a};
std::stringstream ss;
ss << a;
std::string expected = ss.str();
ss = {};
ss << b;
std::string result = ss.str();
INFO("Expected = " << expected << " Result = " << result);
CHECK(expected == result);
}
TEST_CASE("Check FixedBigNum Addition works", "[fixbig_add]") {
auto testVals = GENERATE(take(100, pair_random<std::uint64_t>(0U, UINT64_MAX)));
std::uint64_t a = testVals.first;
std::uint64_t b = testVals.second;
std::uint64_t res = a + b;
TestFixed tv1{a};
TestFixed tv2{b};
auto result = tv1 + tv2;
INFO("a = " << a << " b = " << b);
CHECK(result == res);
}
TEST_CASE("Check FixedBigNum Subtraction works", "[fixbig_sub]") {
auto testVals = GENERATE(take(1000, pair_random<std::uint64_t>(0U, UINT64_MAX)));
std::uint64_t a = std::max(testVals.first, testVals.second);
std::uint64_t b = std::min(testVals.second, testVals.first);
std::uint64_t res = a - b;
TestFixed tv1{a};
TestFixed tv2{b};
auto result = tv1 - tv2;
INFO("a = " << a << " b = " << b);
CHECK(result == res);
}
TEST_CASE("Check FixedBigNum spaceship operator works as expected", "[fixbig_spaceship]") {
auto testVals = GENERATE(take(1000, pair_random<std::int64_t>(INT64_MIN,INT64_MAX)));
std::int64_t a = testVals.first;
std::int64_t b = testVals.second;
std::partial_ordering expected = a <=> b;
TestFixed tv1{a};
TestFixed tv2{b};
std::partial_ordering result = tv1<=>tv2;
INFO("a = " << a << " b = " << b << "\nExpected: " << comparisonString(expected) << " Got: " << comparisonString(result));
CHECK(expected == result);
}
TEST_CASE("Check FixedBigNum bitshift left works as expected", "[fixbig_leftshift]") {
auto testVals = GENERATE(take(1000, pair_random<std::uint64_t>(0U,UINT64_MAX)));
std::uint64_t a = testVals.first;
std::uint64_t displacement = testVals.second % 64;
auto res = a << displacement;
TestFixed tv1{a};
auto result = tv1 << displacement;
INFO("a = " << a << " Offset = " << displacement);
CHECK(result == res);
}
TEST_CASE("Check FixedBigNum bitshift right works as expected", "[fixbig_rightshift]") {
auto testVals = GENERATE(take(1000, pair_random<std::uint64_t>(0U,UINT64_MAX)));
std::uint64_t a = testVals.first;
std::uint64_t displacement = testVals.second % 64;
auto res = a >> displacement;
TestFixed tv1{a};
auto result = tv1 >> displacement;
INFO("a = " << a << " Offset = " << displacement);
CHECK(result == res);
}
TEST_CASE("Check FixedBigNum division operator works as expected", "[fixbig_div]") {
auto testVals = GENERATE(take(1000, pair_random<std::int64_t>(INT64_MIN, INT64_MAX)));
std::uint64_t a = std::max(testVals.first,testVals.second);
std::uint64_t b = std::min(testVals.first, testVals.second);
std::uint64_t expected = a / b;
TestFixed tv1{a};
TestFixed tv2{b};
auto result = tv1 / tv2;
INFO("a = " << a << " b = " << b);
CHECK(result == expected);
}
TEST_CASE("Check FixedBigNum modulo operator works as expected", "[fixbig_mod]") {
auto testVals = GENERATE(take(1000, pair_random<std::int64_t>(INT64_MIN, INT64_MAX)));
std::uint64_t a = std::max(testVals.first,testVals.second);
std::uint64_t b = std::min(testVals.first, testVals.second);
std::uint64_t expected = a % b;
TestFixed tv1{a};
TestFixed tv2{b};
auto result = tv1 % tv2;
INFO("a = " << a << " b = " << b);
CHECK(result == expected);
}