-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_big_int.cpp
144 lines (114 loc) · 4.96 KB
/
test_big_int.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "big_int.h"
#include "test_utils.h"
using test_utils_t = TestUtils<unsigned int, 2048>;
void test_right_shift(const auto &test_arr, test_utils_t &testUtils) {
std::cout << "\tTesting >>:\n";
for (const auto &el : test_arr) {
auto shift{testUtils.generate_number() %
test_utils_t::test_big_int_t::max_bit_length};
test_utils_t::check_binary_operation<test_utils_t::test_big_int_t, int>(
el, shift, ">>", "%3E%3E",
&test_utils_t::test_big_int_t::operator>>);
sleep(0.5);
}
}
void test_left_shift(const auto &test_arr, test_utils_t &testUtils) {
std::cout << "\tTesting <<:\n";
// auto check left shift
for (const auto &el : test_arr) {
auto shift{testUtils.generate_number() %
test_utils_t::test_big_int_t::max_bit_length};
test_utils_t::check_binary_operation<test_utils_t::test_big_int_t, int>(
el, shift, "<<", "%3C%3C",
&test_utils_t::test_big_int_t::operator<<);
sleep(0.5);
}
}
void test_addition(const auto &test_arr) {
std::cout << "\tTesting addition:\n";
// go from both ends of the array converging at the middle
for (auto l_to_r{(test_arr).begin()}, r_to_l{(test_arr).end() - 1};
l_to_r <= r_to_l; ++l_to_r, --r_to_l) {
test_utils_t::check_binary_operation<test_utils_t::test_big_int_t,
test_utils_t::test_big_int_t>(
*l_to_r, *r_to_l, "+", "%2B",
&test_utils_t::test_big_int_t::operator+);
sleep(0.5);
}
}
void test_subtraction(const auto &test_arr) {
std::cout << "\tTesting subtraction:\n";
for (auto l_to_r{(test_arr).begin()}, r_to_l{(test_arr).end() - 1};
l_to_r <= r_to_l; ++l_to_r, --r_to_l) {
const test_utils_t::test_big_int_t *smaller_ptr, *greater_ptr;
// defuse.ca's - works with signed arithmetic, my - doesnt, so if a<b ->
// just swap them
if (*l_to_r > *r_to_l)
greater_ptr = &(*l_to_r), smaller_ptr = &(*r_to_l);
else
smaller_ptr = &(*l_to_r), greater_ptr = &(*r_to_l);
test_utils_t::check_binary_operation<test_utils_t::test_big_int_t,
test_utils_t::test_big_int_t>(
*greater_ptr, *smaller_ptr, "-", "-",
&test_utils_t::test_big_int_t::operator-);
sleep(0.5);
}
}
void test_multiplication(const auto &test_arr) {
std::cout << "\tTesting multiplication:\n";
for (auto l_to_r{(test_arr).begin()}, r_to_l{(test_arr).end() - 1};
l_to_r <= r_to_l; ++l_to_r, --r_to_l) {
test_utils_t::check_binary_operation<test_utils_t::test_big_int_t,
test_utils_t::test_big_int_t>(
*l_to_r, *r_to_l, "*", "*",
&test_utils_t::test_big_int_t::operator*);
sleep(0.5);
}
}
void test_division(const auto &test_arr) {
std::cout << "\tTesting division:\n";
for (auto l_to_r{(test_arr).begin()}, r_to_l{(test_arr).end() - 1};
l_to_r <= r_to_l; ++l_to_r, --r_to_l) {
const test_utils_t::test_big_int_t *smaller_ptr, *greater_ptr;
if (*l_to_r > *r_to_l)
greater_ptr = &(*l_to_r), smaller_ptr = &(*r_to_l);
else
smaller_ptr = &(*l_to_r), greater_ptr = &(*r_to_l);
test_utils_t::check_binary_operation<test_utils_t::test_big_int_t,
test_utils_t::test_big_int_t>(
*greater_ptr, *smaller_ptr, "/", "%2F",
&test_utils_t::test_big_int_t::operator/);
sleep(0.5);
}
}
// can test this with python
void test_exponentation(const auto &test_arr) {
std::cout << "\tTesting exponentation:\n";
for (auto l_to_r{(test_arr).begin()}, r_to_l{(test_arr).end() - 1};
l_to_r <= r_to_l; ++l_to_r, --r_to_l) {
test_utils_t::check_binary_operation<test_utils_t::test_big_int_t,
test_utils_t::test_big_int_t>(
*l_to_r, *r_to_l, "^", "%5E", &test_utils_t::test_big_int_t::pow);
sleep(0.5);
}
}
int main() {
test_utils_t test_utils{};
std::cout << "size of BigInt::STORAGE_TYPE = "
<< sizeof(test_utils_t::test_big_int_t::STORAGE_TYPE) << '\n';
auto test_data_vec{
test_utils.generate_test_data_vec<test_utils_t::test_big_int_t>(10)};
// for(auto l_to_r{(test_data_vec).begin()},
// r_to_l{(test_data_vec).end()-1}; l_to_r <= r_to_l; ++l_to_r, --r_to_l){
// std::cout << l_to_r->pow(*r_to_l).get_as_string() << "\n\n";
// }
// test_right_shift(test_data_vec, test_utils);
// test_left_shift(test_data_vec, test_utils);
// test_addition(test_data_vec);
// test_subtraction(test_data_vec);
// test_multiplication(test_data_vec);
// test_division(test_data_vec);
// test_exponentation(test_data_vec);
test_utils_t::test_big_int_t{"0x12345"} /
test_utils_t::test_big_int_t{"0x0"};
}