-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
463 lines (373 loc) · 17.6 KB
/
test.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "StringBigdata.h"
#include "BigInteger.h"
#include "iostream"
#include "sstream"
#include "time.h"
#include "cassert"
#include "FFTFunctor.h"
#include "FFTMultiplier.h"
#define COMPARE 1
#define COMPUTE(x) do{clock_t start_time = clock(); \
x; \
clock_t end_time = clock(); \
cout << "Running time is: " << static_cast<double>\
(end_time - start_time) / CLOCKS_PER_SEC * 1000 << "ms" << endl; }while (0) //输出运行时间
#define EXPECT_INT(result,correct) do{assert(result == correct);}while(0)
#define EXPECT_STR(x) do {BigInteger y = x;assert(y.str() == string(x));}while(0)
#define EXPECT_BIN_STR(x,c) do {BigInteger y = x;assert(y.str()==c);}while(0)
void Load_Test(){
#if 1
/*****测试10进制***********************************/
EXPECT_STR("123456789");
EXPECT_STR("12345678");
EXPECT_STR("12345678912345678910");
EXPECT_STR("100000000000000000000");
EXPECT_STR("-100000000000000000000");
EXPECT_STR(string(100,'3')+"123456789");
EXPECT_STR("100000000000000000000000000000000000000000000000000000000");
EXPECT_STR(string(6, '1'));
EXPECT_STR(string(11, '1'));
//EXPECT_STR(string(1023, '3'));
EXPECT_STR("-123456789987654320");
EXPECT_STR((string(6888, '1') + "523452345"));
EXPECT_STR((string(11888, '5') + "523537289452345"));
/*****测试二进制*******************************************/
//cout << BigInteger("0b1111100001111111000011111111111111111111");
EXPECT_BIN_STR("0b1111100001111111000011111111111111111111","1067283644415");
EXPECT_BIN_STR("0b11110000110101010101001010101001010101010","2068742230698");
EXPECT_BIN_STR("0b1111000011010101010100","3945812");
/*****测试16进制**********************/
EXPECT_BIN_STR("0xABCDE2203163623452454", "12981160722937192563156052");
EXPECT_BIN_STR("0xABCDEF758329057582203163623452454", "3653876239062442701971792070265375368276");
EXPECT_BIN_STR("0x00000", "0");
EXPECT_BIN_STR("0xF", "15");
#endif
if (!COMPARE)
return;
do {
cout << "test parsing decimal string\n";
/***********测试10进制效率**********************/
string a = "12434213431" + string(10000, '3');
string a2 = "12434213431" + string(50000, '3');
string a3 = "12434213431" + string(100000, '3');
string a4 = "12434213431" + string(500000, '3');
BigInteger b;
COMPUTE(b = BigInteger(a));
COMPUTE(b = BigInteger(a2));
COMPUTE(b = BigInteger(a3));
COMPUTE(b = BigInteger(a4););
cout << b.length() << endl;
} while (0);
}
void Big_Add_Test()
{
#define EXPECT_BIG_ADD(a,b,c) do {BigInteger x = a;BigInteger y = b;BigInteger z = c;\
EXPECT_INT(x + y, z); }while (0)
#define EXPECT_INT_ADD(a,b,c) do {EXPECT_INT(BigInteger(a)+b,BigInteger(c));}while(0)
/*测试与long运算*/
EXPECT_INT_ADD("123456789011111111", 1,"123456789011111112");
EXPECT_INT_ADD("123456789054234511111111", 1342424431, "123456789054235853535542");
EXPECT_INT_ADD("999999999999999999999999999", ((1 << 31) - 1), "1000000000000000002147483646");
/*测试Big+*/
EXPECT_BIG_ADD("123456", "123456", "246912");
EXPECT_BIG_ADD("123456123456", "123456", "123456246912");
EXPECT_BIG_ADD("123456123456123456", "123456", "123456123456246912");
EXPECT_BIG_ADD("54723895472398547230", "43528945723895723984", "98252841196294271214");
EXPECT_BIG_ADD("999999999999999999", "11111111111111", "1000011111111111110");
EXPECT_BIG_ADD("15681","7442833","7458514");
EXPECT_BIG_ADD("15681", "-7442833", "-7427152");
BigInteger t = "170870028598891192848284015688560738254488757978562964938989677583429754298574441955";
EXPECT_BIG_ADD("95437298723452938475389443298903485909254234524575382543245689999999999999999999122", \
"75432729875438254372894572389657252345234523453987582395743987583429754298574442833",\
"170870028598891192848284015688560738254488757978562964938989677583429754298574441955");
if (COMPARE){
do{
cout << "test BigInteger operator + :\n";
BigInteger a = "0x45239872398472309" + string(100758900, '5');
BigInteger b = "0x432972384022023949958" + string(60054340, '7') + "437298423097458942307592308";
BigInteger a1 = "45239872398472309" + string(100000, '5');
BigInteger b1 = "432972384022023949958" + string(100000, '7') + "437298423097458942307592308";
COMPUTE(a + b);
COMPUTE(for (int i = 0; i < 100000; ++i) auto c = a1 + b1;);
} while (0);
}
}
void sub_integer_rough_Test()
{
#define test_sub(x,s,e,res) do{BigInteger t = x; \
auto sub = t.sub_integer_rough(s, e); \
assert(sub.str() == res); }while (0)
test_sub("123456789100000000", 0, 1,"28744523");
test_sub("123456789100000000999999999", 0, 1, "6692605");
//test_sub("1234567891000000000000000000000000000000", 4, 1, "0");
//test_sub("123456789100000000", 0, 1, "12");
}
void Operator_Subtract_Test(){
#define EXPECT_INT_SUB(a,b,c) do {BigInteger x = a;BigInteger y = b;BigInteger z = c;\
assert(z == (x - y)); }while (0)
#if 0
EXPECT_INT_SUB("0x10000000000000", "0", "0x10000000000000");
EXPECT_INT_SUB("123456", "123456", "0");
EXPECT_INT_SUB("0x123456123456", "0x123456", "20015990439936");
EXPECT_INT_SUB("123456123456123456", "123456", "123456123456000000");
EXPECT_INT_SUB("54723895472398547230", "43528945723895723984", "11194949748502823246");
EXPECT_INT_SUB("999999999999999999", "11111111111111", "999988888888888888");
EXPECT_INT_SUB("1", "123456789987654321", "-123456789987654320");
EXPECT_INT_SUB("5422296287557037040", "71185184863703704", "5351111102693333336");
EXPECT_INT_SUB("71185184863703704", "70185184863745604", "999999999958100");
#endif
if (COMPARE)//比较性能
{
do{
cout << "test operator -\n";
BigInteger a = "0x4523" + string(100758900, '5');
BigInteger b = "0x5234532" + string(60054340, '7') + "437298423097458942307592308";
BigInteger a1 = "0x4523532452" + string(100000, '5');
BigInteger b1 = "0x5234532" + string(100000, '7') + "437298423097458942307592308";
BigInteger a2 = "0x45239872398472309" + string(100000, '5');
BigInteger b2 = "0x432972384022023949958" + string(100000, '7') + "437298423097458942307592308";
COMPUTE(auto c = a - b;);
COMPUTE(for (int i = 0; i < 100000;++i) auto c = b1 - a1;);
} while (0);
}
}
void Operator_Mod_Test(){
#define check_mod(x,y,z) do{assert((BigInteger(x)%BigInteger(y))==BigInteger(z));}while(0)
auto t = BigInteger("123456") % BigInteger("2") ;
check_mod("123456", "2", "0");
check_mod("172222222234566666666663456564365835634144443",\
"123455555555555255555555555552454232348897821",\
"48766666679011411111107901011911603285246622");
#if 1
BigInteger a1 = string(1000, '3') + "158610744283305641116339861354533472329775122";
BigInteger b1 = (string(200, '4') + "123455555555555255555555555552454232348897821");
BigInteger correct = "90691965790704901677330261963876131847549\
58818036214446122552271032357689245532028384930706999117863789625062896214097\
065882559894377367640182030209506368739503353301237508903141281380208333333333\
333158610744283305641116339861354533472329775122";
BigInteger a2 = a1 + string(20000, '12');
BigInteger b2 = a1 + string(10000, '23');
check_mod(a1, b1, correct);
BigInteger c;
if (COMPARE){
COMPUTE(c = a2 % b2);
cout << c.length() << endl;
}
#endif
check_mod(BigInteger("20000001122334455"), BigInteger("1000000000"), BigInteger("122334455"));
}
void Operator_Mul_Test(){
#define EXPECT_BIG_MUL(a,b,c) do {EXPECT_INT((BigInteger(a)*BigInteger(b)),BigInteger(c));} while(0)
#define EXPECT_INT_MUL(a,b,c) do {EXPECT_INT((BigInteger(a)* b ),BigInteger(c));}while(0)
/*BIG + INT */
EXPECT_INT_MUL("12345578329532984572398", 1, "12345578329532984572398");
EXPECT_INT_MUL("12345", 12345, "152399025");
EXPECT_INT_MUL("7458514", 7458514, "55629431088196");
EXPECT_INT_MUL("123456789123456789", 2147483647, \
"265121435753750918488629483");
/*BIG + BIG*/
EXPECT_BIG_MUL("1", "12345578329532984572398", "12345578329532984572398");
EXPECT_BIG_MUL("34252354231", "0", "0");
EXPECT_BIG_MUL("12345", "12345", "152399025");
EXPECT_BIG_MUL("7458514", "7458514", "55629431088196");
EXPECT_BIG_MUL("12345987654321", "12345543212345", "152417924085497789759792745");
EXPECT_BIG_MUL("44444444444", "2222222222", "98765432087901234568");
EXPECT_BIG_MUL("123456789123456789", "98765432123456789", \
"12193263126352689864349947750190521");
EXPECT_BIG_MUL("172222222234566666666663456564365835634144443", \
"123455555555555255555555555552454232348897821", \
"21261790124980728703703303694873314334566791251130343554428157608965752534229925361958703");
string a8 = "158610744283305641116339861354533472329775122";
EXPECT_BIG_MUL(a8, a8, \
"25157368202104173113981398220939162641587521514899015555207154711683506072518799090114884");
if (COMPARE){
do{
cout << "test Big operator mul:\n";
BigInteger a = "532"+string(50000, '3') + "172222222234566666666663456564365835634144443";// 1000 3
BigInteger b = "6263"+string(50000, '4') + "123455555555555255555555555552454232348897821";// 200 4
BigInteger a2 = "532"+string(9000, '3') + "12387766544329887776544345678899";
BigInteger b2 = "5354232"+string(9000, '2') + "54978723598347523985439875923085";
BigInteger a3 = "532"+string(90000, '9') + "52345235252";
BigInteger b3 = "532896572389755479872549875423985723987653298";
BigInteger a4 = "532" + string(500000, '9') + "52345235252";
BigInteger b4 = a4;
COMPUTE(a = a*b);// 50000 * 45000
COMPUTE(a2 = a2*b2);// 9000 * 9000
COMPUTE(a3 = a3*b3);// 90000 * const
COMPUTE(a4 = a4*b4);// 500000 * 500000
cout << "test tiny mul 50000!\n";
/*Test for Tiny mul*/
COMPUTE(BigInteger ans = "1"; for (int i = 1; i < 50000; ++i){ ans *= i; });
} while (0);
}
}
void Operator_Shif_Test(){
#define EXPECT_INT_SHIFT(B,num,C,shift) do{\
auto _y = BigInteger(B) shift num; EXPECT_INT(_y, BigInteger(C)); }while (0)
#define EXPECT_INT_LSHIFT(B,num,C) EXPECT_INT_SHIFT(B,num,C,<<)
#define EXPECT_INT_RSHIFT(B,num,C) EXPECT_INT_SHIFT(B,num,C,>>)
#define EXPECT_INT_RSHIFT_EQUAL(B,num,C) do{ \
BigInteger _B(B); \
_B >>= num; \
assert(_B == BigInteger(C));\
}while (0)
/********Test Shift Left*************************************/
EXPECT_INT_LSHIFT("1", 2, "4");
EXPECT_INT_LSHIFT("1111222212", 1, "2222444424");
EXPECT_INT_LSHIFT("0", 2, "0");
EXPECT_INT_LSHIFT("15342424532453254", 1, "30684849064906508");
EXPECT_INT_LSHIFT("15342424532453254", 10, "15710642721232132096");
EXPECT_INT_LSHIFT("15342424532453254", 12, "62842570884928528384");
EXPECT_INT_LSHIFT("15342424532453254", 50, "17274034351829107757228014698496");
EXPECT_INT_LSHIFT("-1", 1, "-2");
/********Test Shift Right*************************************/
EXPECT_INT_RSHIFT("1", 1, "0");
EXPECT_INT_RSHIFT("1111222212", 1, "555611106");
EXPECT_INT_RSHIFT("0", 2, "0");
EXPECT_INT_RSHIFT("15342424532453254", 1, "7671212266226627");
EXPECT_INT_RSHIFT("15342424532453254", 10, "14982836457473");
EXPECT_INT_RSHIFT("153424245324532541234565473829624523", 12, "37457091143684702449845086384185");
EXPECT_INT_RSHIFT("15342424532453254123456547382962452353424532523", 50, "13626810375602763979202822659930");
EXPECT_INT_RSHIFT("-1", 1, "0");
EXPECT_INT_RSHIFT("-100000000066666777777",42,"-22737367");
/********Test Performance****************************/
EXPECT_INT_RSHIFT_EQUAL("1", 1, "0");
EXPECT_INT_RSHIFT_EQUAL("1111222212", 1, "555611106");
EXPECT_INT_RSHIFT_EQUAL("0", 2, "0");
EXPECT_INT_RSHIFT_EQUAL("15342424532453254", 1, "7671212266226627");
EXPECT_INT_RSHIFT_EQUAL("15342424532453254", 10, "14982836457473");
EXPECT_INT_RSHIFT_EQUAL("153424245324532541234565473829624523", 12, "37457091143684702449845086384185");
EXPECT_INT_RSHIFT_EQUAL("15342424532453254123456547382962452353424532523", 50, "13626810375602763979202822659930");
EXPECT_INT_RSHIFT_EQUAL("-1", 1, "0");
EXPECT_INT_RSHIFT_EQUAL("-100000000066666777777", 42, "-22737367");
}
void Operator_addeq_Test(){
#define EXPECT_BIG_ADD(a,b,c) do {BigInteger x = a;BigInteger y = b;BigInteger z = c;\
EXPECT_INT(x + y, z); }while (0)
#define EXPECT_BIG_ADDEQ(a,b,c) do{BigInteger sum = a;sum += BigInteger(b);\
EXPECT_INT(sum, BigInteger(c));} while (0)
EXPECT_BIG_ADDEQ("123456", "123456", "246912");
EXPECT_BIG_ADDEQ("123456123456", "123456", "123456246912");
EXPECT_BIG_ADDEQ("123456123456123456", "123456", "123456123456246912");
EXPECT_BIG_ADDEQ("54723895472398547230", "43528945723895723984", "98252841196294271214");
EXPECT_BIG_ADDEQ("999999999999999999", "11111111111111", "1000011111111111110");
EXPECT_BIG_ADDEQ("15681", "7442833", "7458514");
EXPECT_BIG_ADDEQ("95437298723452938475389443298903485909254234524575382543245689999999999999999999122", \
"75432729875438254372894572389657252345234523453987582395743987583429754298574442833", \
"170870028598891192848284015688560738254488757978562964938989677583429754298574441955");
if (COMPARE){
do{
cout << "test BigInteger operator + :\n";
BigInteger _a = "0x45239872398472309" + string(100758900, '5');
BigInteger _b = "0x432972384022023949958" + string(60054340, '7') + "437298423097458942307592308";
BigInteger _a1 = "0x45239872398472309" + string(100000, '5');
BigInteger _b1 = "0x432972384022023949958" + string(100000, '7') + "437298423097458942307592308";
auto a = _a, b = _b, a1 = _a1, b1 = _b1;
COMPUTE(a = a + b);
COMPUTE(for (int i = 0; i < 100000; ++i) {a1 = a1 + b1;});
a = _a;b = _b;a1 = _a1; b1 = _b1;
cout << "compare with operator +=\n";
COMPUTE(a += b);
COMPUTE(for (int i = 0; i < 100000; ++i) { a1 += b1;});
} while (0);
}
}
void Test_Operator_Div()
{
#define EXPECT_BIG_DIV(a,b,c) do {BigInteger x = a;BigInteger y = b;BigInteger z = c;\
EXPECT_INT(x / y, z); }while (0)
#define EXPECT_LONG_DIV(a,b,c) do{EXPECT_INT((BigInteger(a)/b),BigInteger(c));}while(0)
EXPECT_LONG_DIV("123456789",1,"123456789");
EXPECT_LONG_DIV("123456789", 3, "41152263");
EXPECT_LONG_DIV("1234", 1235, "0");
EXPECT_LONG_DIV("1234", 1234, "1");
EXPECT_LONG_DIV("34252354231", 1, "34252354231");
EXPECT_LONG_DIV("55629431088196", 7458514, "7458514");
EXPECT_LONG_DIV("152417924085497789759792745", 1234554, "123459908667824809412");
EXPECT_LONG_DIV("12193263126352689864349947750190521", 999, \
"12205468594947637501851799549740");
EXPECT_BIG_DIV("1", "12345578329532984572398", "0");
EXPECT_BIG_DIV("34252354231", "1", "34252354231");
EXPECT_BIG_DIV("12345", "12345", "1");
EXPECT_BIG_DIV("55629431088196", "7458514", "7458514");
EXPECT_BIG_DIV("152417924085497789759792745", "12345543212345", "12345987654321");
EXPECT_BIG_DIV("44444444444", "22222222222", "2");
EXPECT_BIG_DIV("12193263126352689864349947750190521", "98765432123456789", \
"123456789123456789");
EXPECT_BIG_DIV("21261790124980728703703303694873314334566791251130343554428157608965752534229925361958703", \
"123455555555555255555555555552454232348897821", \
"172222222234566666666663456564365835634144443");
string a = "158610744283305641116339861354533472329775122";
EXPECT_BIG_DIV("25157368202104173113981398220939162641587521514899015555207154711683506072518799090114884", a, \
a);
BigInteger a0 = "11" + string(2000, '2');
auto c = a0 / BigInteger("1" + string(1000, '0'));
//BigInteger a1 = "0x54235234523452354324" + string(10000)
/*************测试性能***********************/
cout << "test for big div\n";
BigInteger a1 = "0x10" + string(330000, '5');
BigInteger a2 = "0x20" + string(180000, '6') + "72839647298034307439207";
COMPUTE(a1 / a2);
}
void Test_Truncate_mul(){
#define EXPECT_TRUNCATE_MUL(a,b,c) do{BigInteger(a)*BigInteger(b) }while(0)
cout << "test for truncate mul\n";
do{
BigInteger a = "0x"+string(492086,'1')+"123456789123456789123456789";
BigInteger b = "0x12345" + string(282345, '2');;
COMPUTE(a.truncate_mul(b));
COMPUTE(a*b);
} while (0);
}
void Test_toString(){
do {
#define EXPECT_BIN_STR(x,c) do {BigInteger y = x;assert(y.toString(Bin)==c);}while(0)
EXPECT_BIN_STR((1024 + 512), "11000000000");
EXPECT_BIN_STR("0b111111000000000000000", "111111000000000000000");
EXPECT_BIN_STR("0b111111", "111111");
EXPECT_BIN_STR("0b10000000000000000000000000000000000000000000000000", \
"10000000000000000000000000000000000000000000000000");
EXPECT_BIN_STR("0b111100011100011101010101010101010101001010101010100101010101010100101", \
"111100011100011101010101010101010101001010101010100101010101010100101");
} while (0);
do {
#define EXPECT_DEC_STR(x) do {BigInteger y = x;assert(y.toString(Dec) == string(x));}while(0)
EXPECT_DEC_STR("123456789123456789");
EXPECT_DEC_STR("1234567891234554237896789");
EXPECT_DEC_STR("123456789123456785734298299");
EXPECT_DEC_STR("0");
EXPECT_DEC_STR("-1");
EXPECT_DEC_STR("-17584230972390473");
if (COMPARE){
BigInteger a1 = "11" + string(50000, '2');
BigInteger a2 = "11" + string(30000, '2');
BigInteger a3 = "11" + string(100000, '2');
BigInteger a4 = "11" + string(213237, '2');
cout << "test for toString to DEC\n";
COMPUTE(a1.toString(Dec));
COMPUTE(a2.toString(Dec));
COMPUTE(a3.toString(Dec));
COMPUTE(a4.toString(Dec));
}
} while (0);
}
void Test_Pow(){
#define EXPECT_POWER(a,e,c) do{BigInteger(a).pow(e) == BigInteger(c);}while(0)
EXPECT_POWER("1234", 5, "2861381721051424");
EXPECT_POWER("12345", 5, "286718338524635465625");
EXPECT_POWER("0", 5, "0");
EXPECT_POWER("-2", 5, "-32");
EXPECT_POWER("-2", 6, "64");
}
int main()
{
Load_Test();
Big_Add_Test();
Operator_Subtract_Test();
Operator_Mul_Test();
Test_Operator_Div();
Operator_Mod_Test();
//Operator_Shif_Test();
Test_toString();
return 0;
}