Debian Codegraph @

src/test/bigint.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999,2001 Ross Combs ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/
19
20 #include "common/bigint.h"cxx . . $ `<file>/src/common/bigint.h`/
21
22 #include <cassert>
23 #include <cstdint>
24 #include <iostream>
25 #include <string>
26
27 #include "common/xalloc.h"cxx . . $ `<file>/src/common/xalloc.h`/
28
29 #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/
30
31 using namespace pvpgn;
32
33 const unsigned char data1cxx . . $ data1.[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
34 const unsigned char data2cxx . . $ data2.[] = { 0x12, 0xff, 0x34, 0xff, 0x56, 0xff, 0x78, 0xff, 0x90, 0xff, 0xab, 0xff, 0xcd, 0xff, 0xef, 0xff };
35 const unsigned char data3cxx . . $ data3.[] = { 0x12, 0x34, 0x56, 0x78 };
36 const unsigned char data4cxx . . $ data4.[] = { 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
37
38 void constructorTestscxx . . $ constructorTests(49f6e7a06ebc5aa8).()
39 {
40 // std::cout << __FUNCTION__ << "\n";
41 BigIntcxx . . $ pvpgn/BigInt#* number;
42 number = new BigIntcxx . . $ pvpgn/BigInt#();
43 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(number->toHexString() == "00");
44 delete number;
45
46 number = new BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint8_tcxx . . $ std/uint8_t#)0xFF);
47 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(number->toHexString() == "ff");
48 delete number;
49
50 number = new BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint16_tcxx . . $ std/uint16_t#)0xFFFF);
51 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(number->toHexString() == "ffff");
52 delete number;
53
54 number = new BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#)0xFFFFFFFF);
55 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(number->toHexString() == "ffffffff");
56 delete number;
57
58 number = new BigIntcxx . . $ pvpgn/BigInt#(data1cxx . . $ data1., 8);
59 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(number->toHexString() == "1234567890abcdef");
60 delete number;
61
62 number = new BigIntcxx . . $ pvpgn/BigInt#(data1cxx . . $ data1., 7);
63 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(number->toHexString() == "1234567890abcd");
64 delete number;
65
66 number = new BigIntcxx . . $ pvpgn/BigInt#(data2cxx . . $ data2., 16);
67 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(number->toHexString() == "12ff34ff56ff78ff90ffabffcdffefff");
68 delete number;
69 }
70
71 void getDataTestscxx . . $ getDataTests(49f6e7a06ebc5aa8).()
72 {
73 int i;
74 unsigned char* data;
75 data = BigIntcxx . . $ pvpgn/BigInt#BigInt(a7352a5524aaaab2).(data4cxx . . $ data4., 16).getDatacxx . . $ pvpgn/BigInt#getData(7fde0967c2cfd879).(16);
76 for (i = 0; i < 16; i++)
77 {
78 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(data[i] = data4[i]);
79 }
80 xfreecxx . . $ `src/common/xalloc.h:51:9`!(data);
81 data = BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#)0x12345678).getDatacxx . . $ pvpgn/BigInt#getData(7fde0967c2cfd879).(3);
82 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt(data, 3) == BigInt((std::uint32_t)0x345678));
83 xfreecxx . . $ `src/common/xalloc.h:51:9`!(data);
84 data = BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#)0x12345678).getDatacxx . . $ pvpgn/BigInt#getData(7fde0967c2cfd879).(2);
85 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt(data, 2) == BigInt((std::uint16_t)0x5678));
86 xfreecxx . . $ `src/common/xalloc.h:51:9`!(data);
87 data = BigIntcxx . . $ pvpgn/BigInt#((stdcxx . . $ std/::uint32_tcxx . . $ std/uint32_t#)0x12345678).getDatacxx . . $ pvpgn/BigInt#getData(7fde0967c2cfd879).(1);
88 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt(data, 1) == BigInt((std::uint8_t)0x78));
89 xfreecxx . . $ `src/common/xalloc.h:51:9`!(data);
90 }
91
92 void compareOperatorsTestscxx . . $ compareOperatorsTests(49f6e7a06ebc5aa8).()
93 {
94 // std::cout << __FUNCTION__ << "\n";
95 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x12345678) == BigInt(data3, 4));
96 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint16_t)0x27a2) < BigInt((std::uint16_t)0x9876));
97 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt() < BigInt((std::uint16_t)0x9876));
98 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint16_t)0x9876) > BigInt());
99 }
100
101 void addTestscxx . . $ addTests(49f6e7a06ebc5aa8).()
102 {
103 // std::cout << __FUNCTION__ << "\n";
104 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x12344321) + BigInt((std::uint32_t)0x43211234) == BigInt((std::uint32_t)0x55555555));
105 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint16_t)0xFFFF) + BigInt((std::uint8_t)0x01) == BigInt((std::uint32_t)0x00010000));
106 }
107
108 void subTestscxx . . $ subTests(49f6e7a06ebc5aa8).()
109 {
110 // std::cout << __FUNCTION__ << "\n";
111 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x00010000) - BigInt((std::uint8_t)0x01) == BigInt((std::uint16_t)0xFFFF));
112 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x12345678) - BigInt((std::uint16_t)0x9876) == BigInt((std::uint32_t)0x1233BE02));
113 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint8_t)0x10) - BigInt((std::uint8_t)0xFF) == BigInt((std::uint8_t)0x00));
114 }
115
116 void mulTestscxx . . $ mulTests(49f6e7a06ebc5aa8).()
117 {
118 // std::cout << __FUNCTION__ << "\n";
119 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint8_t)0x02) * BigInt((std::uint8_t)0xff) == BigInt((std::uint16_t)0x01fe));
120 const unsigned char data4[] = { 0x01, 0x4b, 0x66, 0xdc, 0x1d, 0xf4, 0xd8, 0x40 };
121 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt(data3, 4) * BigInt(data3, 4) == BigInt(data4, 8));
122 }
123
124 void divTestscxx . . $ divTests(49f6e7a06ebc5aa8).()
125 {
126 // std::cout << __FUNCTION__ << "\n";
127 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint16_t)0x9876) / BigInt((std::uint32_t)0x98765432) == BigInt());
128 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x1e0f7fbc) / BigInt((std::uint16_t)0x1e2f) == BigInt((std::uint16_t)0xfef4));
129 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x01000000) / BigInt((std::uint32_t)0x00FFFFFF) == BigInt((std::uint8_t)0x01));
130 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt(data4, 16) / BigInt((std::uint8_t)0x02) > BigInt());
131 }
132
133 void modTestscxx . . $ modTests(49f6e7a06ebc5aa8).()
134 {
135 // std::cout << __FUNCTION__ << "\n";
136 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x1e0f7fbc) % BigInt((std::uint16_t)0x1e2f) == BigInt((std::uint16_t)0x18f0));
137 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x01000000) % BigInt((std::uint32_t)0x00FFFFFF) == BigInt((std::uint8_t)0x01));
138 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint32_t)0x80000000) % BigInt((std::uint32_t)0xFFFFFFFF) == BigInt((std::uint32_t)0x80000000));
139
140 }
141
142 void randTestscxx . . $ randTests(49f6e7a06ebc5aa8).()
143 {
144 // std::cout << __FUNCTION__ << "\n";
145 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt::random(8) > BigInt());
146 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt::random(16) > BigInt());
147 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt::random(32) > BigInt());
148 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt::random(64) > BigInt());
149 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt::random(128) > BigInt());
150 }
151
152 void powmTestscxx . . $ powmTests(49f6e7a06ebc5aa8).()
153 {
154 // std::cout << __FUNCTION__ << "\n";
155 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint8_t)0x02).powm(BigInt((std::uint8_t)0x1F), BigInt((std::uint32_t)0xFFFFFFFF)) == BigInt((std::uint32_t)0x80000000));
156 BigIntcxx . . $ pvpgn/BigInt# mod = BigIntcxx . . $ pvpgn/BigInt#::randomcxx . . $ pvpgn/BigInt#random(1a9faf48955cd7cf).(32);
157 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(BigInt((std::uint8_t)0x2f).powm(BigInt::random(32), mod) < mod);
158 }
159
160 int maincxx . . $ main(b126dc7c1de90089).()
161 {
162 constructorTestscxx . . $ constructorTests(49f6e7a06ebc5aa8).();
163 getDataTestscxx . . $ getDataTests(49f6e7a06ebc5aa8).();
164 compareOperatorsTestscxx . . $ compareOperatorsTests(49f6e7a06ebc5aa8).();
165 addTestscxx . . $ addTests(49f6e7a06ebc5aa8).();
166 subTestscxx . . $ subTests(49f6e7a06ebc5aa8).();
167 mulTestscxx . . $ mulTests(49f6e7a06ebc5aa8).();
168 divTestscxx . . $ divTests(49f6e7a06ebc5aa8).();
169 modTestscxx . . $ modTests(49f6e7a06ebc5aa8).();
170 randTestscxx . . $ randTests(49f6e7a06ebc5aa8).();
171 powmTestscxx . . $ powmTests(49f6e7a06ebc5aa8).();
172 return 0;
173 }