This repository was archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.c++
355 lines (303 loc) · 12.6 KB
/
tests.c++
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// main.cpp
// tests
//
// Created by Andy Pilate on 22/01/15.
// Copyright (c) 2015 Andy Pilate. All rights reserved.
//
#include <iostream>
#include <string>
#include <cassert>
#include <random>
#include <cstdint>
#include <chrono>
//#include <gmpxx.h>
#include <bigcub.h++>
#define cassert(x) assert(x)
void initTests() {
BigCub tmp;
cassert(static_cast<int>(tmp) == 0);
for (uintmax_t i = 0; i < ULLONG_MAX; ++i) {
BigCub tmp1(i);
BigCub tmp2(-i);
cassert(static_cast<uintmax_t>(tmp1) == i);
cassert(static_cast<uintmax_t>(tmp2) == -i);
}
}
void arithTests() {
// std::cout << "Starting range arith tests" << std::endl;
//
// for (int32_t i = 0; i < INT_MAX; ++i ) {
// BigCub tmp1(i);
// BigCub tmp2(i);
//
// cassert(static_cast<int32_t>(tmp1 + tmp2) == static_cast<int32_t>(i + i));
// cassert(static_cast<int32_t>(tmp1 + (tmp2 + 10)) == static_cast<int32_t>(i + (i + 10)));
// cassert(static_cast<int32_t>(++tmp1) == static_cast<int32_t>(++i));
// cassert(static_cast<int32_t>(--tmp1) == static_cast<int32_t>(--i));
// cassert(static_cast<int32_t>(-tmp1) == static_cast<int32_t>(-i));
//
// cassert(static_cast<int32_t>(BigCub(i) * BigCub(i)) == static_cast<int32_t>(i * i));
//
// cassert(static_cast<int32_t>(BigCub(i) * BigCub(-i)) == static_cast<int32_t>(i * -i));
// }
std::random_device rd;
std::cout << "Starting random arith tests" << std::endl;
for (auto i = 0; i < 100000; i++) {
intmax_t i1 = rd();
intmax_t i2 = -rd();
cassert(static_cast<int64_t>(BigCub(i1) + BigCub(i2)) == static_cast<int64_t>(i1 + i2));
cassert(static_cast<int64_t>(BigCub(i1) - BigCub(i2)) == static_cast<int64_t>(i1 - i2));
cassert(static_cast<int64_t>(-BigCub(i1) + BigCub(i2)) == static_cast<int64_t>(-i1 + i2));
cassert(static_cast<int64_t>(BigCub(i1) + -BigCub(i2)) == static_cast<int64_t>(i1 + -i2));
cassert(static_cast<int64_t>(BigCub(-i1) + BigCub(i2)) == static_cast<int64_t>(-i1 + i2));
cassert(static_cast<int64_t>(BigCub(-i1) - BigCub(i2)) == static_cast<int64_t>(-i1 - i2));
cassert(static_cast<int64_t>(-BigCub(-i1) + BigCub(i2)) == static_cast<int64_t>(i1 + i2));
cassert(static_cast<int64_t>(BigCub(-i1) + -BigCub(i2)) == static_cast<int64_t>(-i1 + -i2));
cassert(static_cast<int64_t>(BigCub(i1) + BigCub(-i2)) == static_cast<int64_t>(i1 + -i2));
cassert(static_cast<int64_t>(BigCub(i1) - BigCub(-i2)) == static_cast<int64_t>(i1 - -i2));
cassert(static_cast<int64_t>(-BigCub(i1) + BigCub(-i2)) == static_cast<int64_t>(-i1 + -i2));
cassert(static_cast<int64_t>(BigCub(i1) + -BigCub(-i2)) == static_cast<int64_t>(i1 + i2));
cassert(static_cast<int64_t>(BigCub(-i1) + BigCub(-i2)) == static_cast<int64_t>(-i1 + -i2));
cassert(static_cast<int64_t>(BigCub(-i1) - BigCub(-i2)) == static_cast<int64_t>(-i1 - -i2));
cassert(static_cast<int64_t>(-BigCub(-i1) + BigCub(-i2)) == static_cast<int64_t>(i1 + -i2));
cassert(static_cast<int64_t>(BigCub(-i1) + -BigCub(-i2)) == static_cast<int64_t>(-i1 + i2));
cassert(static_cast<int64_t>(++BigCub(i1)) == ++i1);
cassert(static_cast<int64_t>(--BigCub(i1)) == --i1);
cassert(static_cast<int64_t>(++BigCub(i2)) == ++i2);
cassert(static_cast<int64_t>(--BigCub(i2)) == --i2);
cassert(static_cast<int64_t>(BigCub(i1) * BigCub(i2)) == static_cast<int64_t>(i1 * i2));
}
}
void cmpTests() {
// std::cout << "Starting range cmp tests" << std::endl;
// for (intmax_t i = 0; i < LLONG_MAX; ++i) {
// BigCub tmp1(i);
// BigCub tmp2(-i);
//
// cassert((tmp1 == tmp2) == (i == -i));
// cassert((tmp1 != tmp2) == (i != -i));
// cassert((tmp1 < tmp2) == (i < -i));
// cassert((tmp1 > tmp2) == (i > -i));
// cassert((tmp1 <= tmp2) == (i <= -i));
// cassert((tmp1 >= tmp2) == (i >= -i));
//
// cassert((tmp2 == i) == (i == -i));
// cassert((tmp2 != i) == (i != -i));
// cassert((tmp2 < i) == (-i < i));
// cassert((tmp2 > i) == (-i > i));
// cassert((tmp2 <= i) == (-i <= i));
// cassert((tmp2 >= i) == (-i >= i));
// }
std::cout << "Starting random cmp tests" << std::endl;
std::random_device rd;
for (auto i = 0; i < 100000; i++) {
intmax_t i1 = rd();
intmax_t i2 = -rd();
BigCub tmp1(i1);
BigCub tmp2(i2);
cassert((BigCub(1) == BigCub(2)) == (1 == 2));
cassert((BigCub(454545454545454) == BigCub(454545454545454)) == (454545454545454 == 454545454545454));
cassert((tmp1 == tmp2) == (i1 == i2));
cassert((tmp1 != tmp2) == (i1 != i2));
cassert((tmp1 < tmp2) == (i1 < i2));
cassert((tmp1 > tmp2) == (i1 > i2));
cassert((tmp1 <= tmp2) == (i1 <= i2));
cassert((tmp1 >= tmp2) == (i1 >= i2));
}
}
void bitwiseTests() {
std::random_device rd;
intmax_t it;
// std::cout << "Starting range bitwise tests" << std::endl;
// for (intmax_t i = 0; i < LLONG_MAX; ++i) {
// it = rd();
//
// cassert((BigCub(i) & BigCub(it)) == (i & it));
// cassert((BigCub(i) | BigCub(it)) == (i | it));
// cassert((BigCub(i) ^ BigCub(it)) == (i ^ it));
//
// //cassert(BigCub(it) << (static_cast<uintmax_t>(i) % 50) == static_cast<int32_t>(it) << (i % 50));
// //cassert(BigCub(it) >> (static_cast<uintmax_t>(i) % 50) == static_cast<int32_t>(it) >> (i % 50));
//
// cassert(~BigCub(i) == ~i); // Can only use this if i is signed
// }
std::cout << "Starting random bitwise tests" << std::endl;
intmax_t it2;
for (uintmax_t i = 0; i < 100000; i++) {
it = rd();
it2 = -rd();
cassert((BigCub(it2) & BigCub(it)) == (it2 & it));
cassert((BigCub(it2) | BigCub(it)) == (it2 | it));
cassert((BigCub(it2) ^ BigCub(it)) == (it2 ^ it));
//cassert(BigCub(it) << i % 50 == it << i % 50);
//cassert(BigCub(it) >> i % 50 == it >> i % 50);
cassert(~BigCub(it) == ~it);
}
}
void readTests() {
cassert(BigCub("45646545656456456") == 45646545656456456);
cassert(BigCub("0") == 0);
cassert(BigCub("") == 0);
cassert(BigCub("-0") == -0);
cassert(BigCub("-1") == -1);
cassert(BigCub("1") == 1);
}
//#define printBits(x) std::cout << "Printing " << #x << ": "; printBits2(x);
//template<typename T>
//void printBits2(T n) {
// for (size_t i = 0; i < sizeof(T) * CHAR_BIT; ++i) {
// std::cout << ((n >> i) & 1);
// }
//
// std::cout << std::endl;
//}
int main() {
//initTests();
arithTests();
cmpTests();
bitwiseTests();
//readTests();
// std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
//
// BigCub a;
//(*a).resize(INT_MAX+54554554);
//a[a.size() - 1] = false;
// std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
// std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
//
// std::cout << "BigCub took " << time_span.count() << " seconds." << std::endl;
//
// std::cout << "Value: " << a << std::endl;
// for (auto i = (*a).begin(), e = (*a).end(); i != e; ++i) {
// std::cout << *i;
// }
//td::cout << std::endl;
// std::chrono::high_resolution_clock::time_point t5 = std::chrono::high_resolution_clock::now();
// mpz_class a3;
// for (;mpz_sizeinbase(a3.get_mpz_t(), 2) < 25;) {
// a3 += 1;
// }
// std::chrono::high_resolution_clock::time_point t6 = std::chrono::high_resolution_clock::now();
// std::chrono::duration<double> time_span3 = std::chrono::duration_cast<std::chrono::duration<double>>(t6 - t5);
//
// std::cout << "libgmp took " << time_span3.count() << " seconds." << std::endl;
// BigCub sum(0);
//
// for (size_t i = 0; i < 5000010; ++i) {
// sum += ULLONG_MAX;
// }
//
// std::cout << sum.size() << std::endl;
// int a = 1;
// printBits(a);
// printBits(a >> 1);
// printBits((BigCub(a) >> 1));
return 0;
}
//unsigned int ifib (unsigned int n){
// if (n < 2)
// return n;
// else
// return ifib(n - 1) + ifib(n - 2);
//}
//
//BigCub bfib(unsigned int n) {
// if (n < 2) {
// return BigCub(n);
// } else {
// return bfib(n - 1) + bfib(n - 2);
// }
//}
//
//mpz_class gmpfib(unsigned int n) {
// if (n < 2) {
// return mpz_class(n);
// } else {
// return gmpfib(n - 1) + gmpfib(n - 2);
// }
//}
//int main(int argc, char **argv) {
// int howmuch = std::atoi(argv[1]);
//
// std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
//
// BigCub a1(bfib(howmuch));
//
// std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
// std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1);
//
// std::cout << "BigCub fib took " << time_span.count() << " seconds." << std::endl;
//
//
//
//
// std::chrono::high_resolution_clock::time_point t3 = std::chrono::high_resolution_clock::now();
//
// unsigned int a2 = ifib(howmuch);
// (void)a2;
//
// std::chrono::high_resolution_clock::time_point t4 = std::chrono::high_resolution_clock::now();
// std::chrono::duration<double> time_span2 = std::chrono::duration_cast<std::chrono::duration<double>>(t4 - t3);
//
// std::cout << "int fib took " << time_span2.count() << " seconds." << std::endl;
//
// std::chrono::high_resolution_clock::time_point t5 = std::chrono::high_resolution_clock::now();
//
// mpz_class a3 = gmpfib(howmuch);
// (void)a3;
//
// std::chrono::high_resolution_clock::time_point t6 = std::chrono::high_resolution_clock::now();
// std::chrono::duration<double> time_span3 = std::chrono::duration_cast<std::chrono::duration<double>>(t6 - t5);
//
// std::cout << "libgmp fib took " << time_span3.count() << " seconds." << std::endl;
//}
//
//int main2()
//{
// int64_t i1 = 0;
// int64_t i2 = 0;
// size_t ok = 0;
// size_t tried = 0;
//
// std::random_device rd;
//
// for (size_t i = 0; i < 100000; ++i) {
// i1 = rd();
// i2 = -rd();
//
// cassert(static_cast<int64_t>(BigCub(i1) + BigCub(i2)) == static_cast<int64_t>(i1 + i2));
// cassert(static_cast<int64_t>(BigCub(i1) - BigCub(i2)) == static_cast<int64_t>(i1 - i2));
// cassert(static_cast<int64_t>(-BigCub(i1) + BigCub(i2)) == static_cast<int64_t>(-i1 + i2));
// cassert(static_cast<int64_t>(BigCub(i1) + -BigCub(i2)) == static_cast<int64_t>(i1 + -i2));
//
// cassert(static_cast<int64_t>(BigCub(-i1) + BigCub(i2)) == static_cast<int64_t>(-i1 + i2));
// cassert(static_cast<int64_t>(BigCub(-i1) - BigCub(i2)) == static_cast<int64_t>(-i1 - i2));
// cassert(static_cast<int64_t>(-BigCub(-i1) + BigCub(i2)) == static_cast<int64_t>(i1 + i2));
// cassert(static_cast<int64_t>(BigCub(-i1) + -BigCub(i2)) == static_cast<int64_t>(-i1 + -i2));
//
// cassert(static_cast<int64_t>(BigCub(i1) + BigCub(-i2)) == static_cast<int64_t>(i1 + -i2));
// cassert(static_cast<int64_t>(BigCub(i1) - BigCub(-i2)) == static_cast<int64_t>(i1 - -i2));
// cassert(static_cast<int64_t>(-BigCub(i1) + BigCub(-i2)) == static_cast<int64_t>(-i1 + -i2));
// cassert(static_cast<int64_t>(BigCub(i1) + -BigCub(-i2)) == static_cast<int64_t>(i1 + i2));
//
// cassert(static_cast<int64_t>(BigCub(-i1) + BigCub(-i2)) == static_cast<int64_t>(-i1 + -i2));
// cassert(static_cast<int64_t>(BigCub(-i1) - BigCub(-i2)) == static_cast<int64_t>(-i1 - -i2));
// cassert(static_cast<int64_t>(-BigCub(-i1) + BigCub(-i2)) == static_cast<int64_t>(i1 + -i2));
// cassert(static_cast<int64_t>(BigCub(-i1) + -BigCub(-i2)) == static_cast<int64_t>(-i1 + i2));
//
// cassert(static_cast<int64_t>(++BigCub(i1)) == ++i1);
// cassert(static_cast<int64_t>(--BigCub(i1)) == --i1);
//
// cassert(static_cast<int64_t>(++BigCub(i2)) == ++i2);
// cassert(static_cast<int64_t>(--BigCub(i2)) == --i2);
//
// ++i1;
// --i2;
//
// }
//
// std::cout << "Success: " << ok << "/" << tried << " " << static_cast<double>((ok / static_cast<double>(tried)) * 100) << "%" << std::endl;
//
// return 0;
//}