aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-01-14 21:32:09 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-01-14 21:32:09 +0100
commit38efc7d2df030d37e3f20efbce71fadad1294cef (patch)
treebbe8a7bbb6acbeb2b7ed54c189d1e719a7459427 /test
parent8ca27427af98bd3a82e8c5f4a199513295747930 (diff)
downloadzmodn-38efc7d2df030d37e3f20efbce71fadad1294cef.tar.gz
zmodn-38efc7d2df030d37e3f20efbce71fadad1294cef.zip
Added BigInt
Diffstat (limited to 'test')
-rwxr-xr-xtest349
1 files changed, 348 insertions, 1 deletions
diff --git a/test b/test
index 4b426c2..46ed7c3 100755
--- a/test
+++ b/test
@@ -2,13 +2,16 @@
2 2
3cc=${CC:-g++} 3cc=${CC:-g++}
4bin="$(mktemp)" 4bin="$(mktemp)"
5${cc} -x c++ -std=c++20 -o "$bin" "$(realpath $0)" 5${cc} -x c++ -std=c++20 -o "$bin" -g -O0 "$(realpath $0)"
6echo "Running $bin"
6"$bin" 7"$bin"
7 8
8exit 0 9exit 0
9#endif 10#endif
10 11
11#include "zmodn.h" 12#include "zmodn.h"
13#include "bigint.h"
14
12#include <concepts> 15#include <concepts>
13#include <functional> 16#include <functional>
14#include <iostream> 17#include <iostream>
@@ -141,6 +144,350 @@ public:
141 assert_equal(p, 92); 144 assert_equal(p, 92);
142 } 145 }
143}, 146},
147{
148 .name = "BigInt constructor zero",
149 .f = []() {
150 BigInt x;
151 BigInt y(0);
152
153 assert_equal(x, y);
154 }
155},
156{
157 .name = "BigInt constructor one digit",
158 .f = []() {
159 BigInt x(12345);
160 BigInt y("12345");
161
162 assert_equal(x, y);
163 }
164},
165{
166 .name = "BigInt constructor many small digits",
167 .f = []() {
168 BigInt<20, 2> x(123456789);
169 BigInt<20, 2> y("123456789");
170
171 assert_equal(x, y);
172 }
173},
174{
175 .name = "BigInt constructor negative many small digits",
176 .f = []() {
177 BigInt<20, 2> x(-123456789);
178 BigInt<20, 2> y("-123456789");
179
180 assert_equal(x, y);
181 }
182},
183{
184 .name = "BigInt operator==",
185 .f = []() {
186 BigInt<20, 2> x(123456789);
187 BigInt<20, 2> y("123456789");
188 BigInt<20, 2> z("12456789");
189
190 bool eq = (x == y);
191 bool diff = (x == z);
192
193 assert_equal(eq, true);
194 assert_equal(diff, false);
195 },
196},
197{
198 .name = "BigInt operator== negative",
199 .f = []() {
200 BigInt<20, 2> x("-123456789");
201 BigInt<20, 2> z("123456789");
202
203 bool diff = (x == z);
204
205 assert_equal(diff, false);
206 },
207},
208{
209 .name = "BigInt operator!= true",
210 .f = []() {
211 BigInt<20, 2> x(12345678);
212 BigInt<20, 2> y("123456789");
213 BigInt<20, 2> z("123456789");
214
215 bool diff = (x != y);
216 bool eq = (y != z);
217
218 assert_equal(diff, true);
219 assert_equal(eq, false);
220 },
221},
222{
223 .name = "BigInt operator< and operator>",
224 .f = []() {
225 BigInt<20, 2> x(7891);
226 BigInt<20, 2> y(7881);
227
228 bool t = (y < x);
229 bool f = (x < y);
230
231 assert_equal(t, true);
232 assert_equal(f, false);
233 }
234},
235{
236 .name = "BigInt operator< both negative",
237 .f = []() {
238 BigInt<20, 2> x(-7891);
239 BigInt<20, 2> y(-7881);
240
241 bool cmp = (x < y);
242
243 assert_equal(cmp, true);
244 }
245},
246{
247 .name = "BigInt operator< different sign",
248 .f = []() {
249 BigInt<20, 2> x(-7);
250 BigInt<20, 2> y(7);
251
252 bool cmp = (x < y);
253
254 assert_equal(cmp, true);
255 }
256},
257{
258 .name = "BigInt abs",
259 .f = []() {
260 BigInt<20, 2> x(-1234567);
261 BigInt<20, 2> y(7654321);
262
263 assert_equal(x.abs(), BigInt<20, 2>(1234567));
264 assert_equal(y.abs(), y);
265 }
266},
267{
268 .name = "BigInt opposite",
269 .f = []() {
270 BigInt<20, 2> x(-1234567);
271 BigInt<20, 2> y(7654321);
272
273 assert_equal(-x, BigInt<20, 2>(1234567));
274 assert_equal(-y, BigInt<20, 2>(-7654321));
275 }
276},
277{
278 .name = "BigInt -0 == 0",
279 .f = []() {
280 BigInt z(0);
281
282 assert_equal(-z, z);
283 }
284},
285{
286 .name = "BigInt sum",
287 .f = []() {
288 BigInt<20, 2> x("987608548588589");
289 BigInt<20, 2> y("6793564545455289");
290 BigInt<20, 2> z("7781173094043878");
291
292 assert_equal(x+y, z);
293 }
294},
295{
296 .name = "BigInt sum both negative",
297 .f = []() {
298 BigInt<20, 2> x("-987608548588589");
299 BigInt<20, 2> y("-6793564545455289");
300 BigInt<20, 2> z("-7781173094043878");
301
302 assert_equal(x+y, z);
303 }
304},
305{
306 .name = "BigInt sum negative + positive, result positive",
307 .f = []() {
308 BigInt<20, 2> x("-987608548588589");
309 BigInt<20, 2> y("6793564545455289");
310 BigInt<20, 2> z("5805955996866700");
311
312 assert_equal(x+y, z);
313 }
314},
315{
316 .name = "BigInt sum positive + negative, result negative",
317 .f = []() {
318 BigInt<20, 2> x("987608548588589");
319 BigInt<20, 2> y("-6793564545455289");
320 BigInt<20, 2> z("-5805955996866700");
321
322 assert_equal(x+y, z);
323 }
324},
325{
326 .name = "BigInt difference",
327 .f = []() {
328 BigInt<20, 2> x("2342442323434134");
329 BigInt<20, 2> y("2524342523342342");
330 BigInt<20, 2> z("-181900199908208");
331
332 assert_equal(x-y, z);
333 }
334},
335{
336 .name = "BigInt product",
337 .f = []() {
338 BigInt<100, 3> x("134142345244134");
339 BigInt<100, 3> y("-56543047058245");
340 BigInt<100, 3> z("-7584816939642416135042584830");
341
342 assert_equal(x*y, z);
343 }
344},
345{
346 .name = "BigInt operator+=",
347 .f = []() {
348 BigInt<20, 2> x("987608548588589");
349 BigInt<20, 2> y("6793564545455289");
350 BigInt<20, 2> z("7781173094043878");
351
352 x += y;
353
354 assert_equal(x, z);
355 }
356},
357{
358 .name = "BigInt 14 / 3",
359 .f = []() {
360 BigInt x(14);
361 BigInt y(3);
362
363 assert_equal(x / y, 4);
364 }
365},
366{
367 .name = "BigInt 14 % 3",
368 .f = []() {
369 BigInt x(14);
370 BigInt y(3);
371
372 assert_equal(x % y, 2);
373 }
374},
375{
376 .name = "BigInt 14 / -3",
377 .f = []() {
378 BigInt x(14);
379 BigInt y(-3);
380
381 assert_equal(x / y, -5);
382 }
383},
384{
385 .name = "BigInt 14 % -3",
386 .f = []() {
387 BigInt x(14);
388 BigInt y(-3);
389
390 assert_equal(x % y, -1);
391 }
392},
393{
394 .name = "BigInt -14 / 3",
395 .f = []() {
396 BigInt x(-14);
397 BigInt y(3);
398
399 assert_equal(x / y, -5);
400 }
401},
402{
403 .name = "BigInt -14 % 3",
404 .f = []() {
405 BigInt x(-14);
406 BigInt y(3);
407
408 assert_equal(x % y, 1);
409 }
410},
411{
412 .name = "BigInt -14 / -3",
413 .f = []() {
414 BigInt x(-14);
415 BigInt y(-3);
416
417 assert_equal(x / y, 4);
418 }
419},
420{
421 .name = "BigInt -14 % -3",
422 .f = []() {
423 BigInt x(-14);
424 BigInt y(-3);
425
426 assert_equal(x % y, -2);
427 }
428},
429{
430 .name = "BigInt division large numbers, quotient = 0",
431 .f = []() {
432 BigInt<50, 3> x("4534435234134244242");
433 BigInt<50, 3> y("7832478748237487343");
434
435 assert_equal(x / y, 0);
436 }
437},
438{
439 .name = "BigInt division large numbers",
440 .f = []() {
441 BigInt<50, 3> x("12344534435234134244242");
442 BigInt<50, 3> y("7832478748237487343");
443 BigInt<50, 3> z(1576);
444
445 assert_equal(x / y, z);
446 }
447},
448{
449 .name = "BigInt modulo large numbers",
450 .f = []() {
451 BigInt<50, 3> x("12344534435234134244242");
452 BigInt<50, 3> y("7832478748237487343");
453 BigInt<50, 3> z("547928011854191674");
454
455 assert_equal(x % y, z);
456 }
457},
458{
459 .name = "Zmod with BigInt constructor",
460 .f = []() {
461 constexpr BigInt<50, 3> N("78923471");
462 constexpr BigInt<50, 3> x("145452451");
463 Zmod<N> xmodN(x);
464
465 assert_equal(xmodN.toint(), x % N);
466 }
467},
468{
469 .name = "Zmod with BigInt big inverse",
470 .f = []() {
471 constexpr BigInt<50, 3> N("7520824651249795349285");
472 constexpr BigInt<50, 3> x("234589234599896924596");
473 constexpr BigInt<50, 3> expected("5901181270843786267351");
474 Zmod<N> xmodN(x);
475
476 auto inv = xmodN.inverse();
477
478 assert_equal(inv.has_value(), true);
479 assert_equal(inv.value().toint(), expected);
480 }
481},
482/*
483{
484 .name = "This does not compile",
485 .f = []() {
486 constexpr double N = 1.2;
487 Zmod<N> x;
488 }
489}
490*/
144}; 491};
145 492
146int main() { 493int main() {