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"
19
20 #include "common/bigint.h"
21
22 #include <cassert>
23 #include <cstdint>
24 #include <iostream>
25 #include <string>
26
27 #include "common/xalloc.h"
28
29 #include "common/setup_after.h"
30
31 using namespace pvpgn;
32
33 const unsigned char data1[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
34 const unsigned char data2[] = { 0x12, 0xff, 0x34, 0xff, 0x56, 0xff, 0x78, 0xff, 0x90, 0xff, 0xab, 0xff, 0xcd, 0xff, 0xef, 0xff };
35 const unsigned char data3[] = { 0x12, 0x34, 0x56, 0x78 };
36 const unsigned char data4[] = { 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
37
38 void constructorTests()
39 {
40 // std::cout << __FUNCTION__ << "\n";
41 BigInt* number;
42 number = new BigInt();
43 assert(number->toHexString() == "00");
44 delete number;
45
46 number = new BigInt((std::uint8_t)0xFF);
47 assert(number->toHexString() == "ff");
48 delete number;
49
50 number = new BigInt((std::uint16_t)0xFFFF);
51 assert(number->toHexString() == "ffff");
52 delete number;
53
54 number = new BigInt((std::uint32_t)0xFFFFFFFF);
55 assert(number->toHexString() == "ffffffff");
56 delete number;
57
58 number = new BigInt(data1, 8);
59 assert(number->toHexString() == "1234567890abcdef");
60 delete number;
61
62 number = new BigInt(data1, 7);
63 assert(number->toHexString() == "1234567890abcd");
64 delete number;
65
66 number = new BigInt(data2, 16);
67 assert(number->toHexString() == "12ff34ff56ff78ff90ffabffcdffefff");
68 delete number;
69 }
70
71 void getDataTests()
72 {
73 int i;
74 unsigned char* data;
75 data = BigInt(data4, 16).getData(16);
76 for (i = 0; i < 16; i++)
77 {
78 assert(data[i] = data4[i]);
79 }
80 xfree(data);
81 data = BigInt((std::uint32_t)0x12345678).getData(3);
82 assert(BigInt(data, 3) == BigInt((std::uint32_t)0x345678));
83 xfree(data);
84 data = BigInt((std::uint32_t)0x12345678).getData(2);
85 assert(BigInt(data, 2) == BigInt((std::uint16_t)0x5678));
86 xfree(data);
87 data = BigInt((std::uint32_t)0x12345678).getData(1);
88 assert(BigInt(data, 1) == BigInt((std::uint8_t)0x78));
89 xfree(data);
90 }
91
92 void compareOperatorsTests()
93 {
94 // std::cout << __FUNCTION__ << "\n";
95 assert(BigInt((std::uint32_t)0x12345678) == BigInt(data3, 4));
96 assert(BigInt((std::uint16_t)0x27a2) < BigInt((std::uint16_t)0x9876));
97 assert(BigInt() < BigInt((std::uint16_t)0x9876));
98 assert(BigInt((std::uint16_t)0x9876) > BigInt());
99 }
100
101 void addTests()
102 {
103 // std::cout << __FUNCTION__ << "\n";
104 assert(BigInt((std::uint32_t)0x12344321) + BigInt((std::uint32_t)0x43211234) == BigInt((std::uint32_t)0x55555555));
105 assert(BigInt((std::uint16_t)0xFFFF) + BigInt((std::uint8_t)0x01) == BigInt((std::uint32_t)0x00010000));
106 }
107
108 void subTests()
109 {
110 // std::cout << __FUNCTION__ << "\n";
111 assert(BigInt((std::uint32_t)0x00010000) - BigInt((std::uint8_t)0x01) == BigInt((std::uint16_t)0xFFFF));
112 assert(BigInt((std::uint32_t)0x12345678) - BigInt((std::uint16_t)0x9876) == BigInt((std::uint32_t)0x1233BE02));
113 assert(BigInt((std::uint8_t)0x10) - BigInt((std::uint8_t)0xFF) == BigInt((std::uint8_t)0x00));
114 }
115
116 void mulTests()
117 {
118 // std::cout << __FUNCTION__ << "\n";
119 assert(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 assert(BigInt(data3, 4) * BigInt(data3, 4) == BigInt(data4, 8));
122 }
123
124 void divTests()
125 {
126 // std::cout << __FUNCTION__ << "\n";
127 assert(BigInt((std::uint16_t)0x9876) / BigInt((std::uint32_t)0x98765432) == BigInt());
128 assert(BigInt((std::uint32_t)0x1e0f7fbc) / BigInt((std::uint16_t)0x1e2f) == BigInt((std::uint16_t)0xfef4));
129 assert(BigInt((std::uint32_t)0x01000000) / BigInt((std::uint32_t)0x00FFFFFF) == BigInt((std::uint8_t)0x01));
130 assert(BigInt(data4, 16) / BigInt((std::uint8_t)0x02) > BigInt());
131 }
132
133 void modTests()
134 {
135 // std::cout << __FUNCTION__ << "\n";
136 assert(BigInt((std::uint32_t)0x1e0f7fbc) % BigInt((std::uint16_t)0x1e2f) == BigInt((std::uint16_t)0x18f0));
137 assert(BigInt((std::uint32_t)0x01000000) % BigInt((std::uint32_t)0x00FFFFFF) == BigInt((std::uint8_t)0x01));
138 assert(BigInt((std::uint32_t)0x80000000) % BigInt((std::uint32_t)0xFFFFFFFF) == BigInt((std::uint32_t)0x80000000));
139
140 }
141
142 void randTests()
143 {
144 // std::cout << __FUNCTION__ << "\n";
145 assert(BigInt::random(8) > BigInt());
146 assert(BigInt::random(16) > BigInt());
147 assert(BigInt::random(32) > BigInt());
148 assert(BigInt::random(64) > BigInt());
149 assert(BigInt::random(128) > BigInt());
150 }
151
152 void powmTests()
153 {
154 // std::cout << __FUNCTION__ << "\n";
155 assert(BigInt((std::uint8_t)0x02).powm(BigInt((std::uint8_t)0x1F), BigInt((std::uint32_t)0xFFFFFFFF)) == BigInt((std::uint32_t)0x80000000));
156 BigInt mod = BigInt::random(32);
157 assert(BigInt((std::uint8_t)0x2f).powm(BigInt::random(32), mod) < mod);
158 }
159
160 int main()
161 {
162 constructorTests();
163 getDataTests();
164 compareOperatorsTests();
165 addTests();
166 subTests();
167 mulTests();
168 divTests();
169 modTests();
170 randTests();
171 powmTests();
172 return 0;
173 }