-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbigint_tests.cpp
More file actions
1951 lines (1781 loc) · 84.6 KB
/
Copy pathbigint_tests.cpp
File metadata and controls
1951 lines (1781 loc) · 84.6 KB
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2024-2025 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <script/bigint.h>
#include <compat/endian.h>
#include <random.h>
#include <script/script.h>
#include <span.h>
#include <univalue.h>
#include <util/strencodings.h>
#include <test/data/bigint_exp_tests.json.h>
#include <test/data/bigint_mod_tests.json.h>
#include <test/data/bigint_mul_tests.json.h>
#include <test/data/bigint_shift_tests.json.h>
#include <test/data/bigint_sum_tests.json.h>
#include <test/data/bigint_test_vectors.json.h>
#include <test/jsonutil.h>
#include <test/scriptnum10.h>
#include <test/util.h>
#include <test/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <algorithm>
#include <cstring>
#include <ios>
#include <ostream>
#include <sstream>
#include <string_view>
#include <type_traits>
// to be removed once Boost 1.59+ is minimum version.
#ifndef BOOST_TEST_CONTEXT
#define BOOST_TEST_CONTEXT(x)
#endif
// Allows for literals e.g. "f00fb33f"_v to auto-parse to std::vector<uint8_t>
static std::vector<uint8_t> operator""_v(const char *hex, size_t) { return ParseHex(hex); }
// Stream support for byte vectors.. this is so that BOOST_CHECK_EQUAL on vectors works
static std::ostream &operator<< [[maybe_unused]] (std::ostream &os, const std::vector<uint8_t> &v) {
return os << HexStr(v);
}
#if HAVE_INT128
using int128_t = BigInt::int128_t;
using uint128_t = BigInt::uint128_t;
static std::ostream &operator<< [[maybe_unused]] (std::ostream &os, const int128_t i) {
Span<const uint8_t> sp{reinterpret_cast<const uint8_t*>(&i), sizeof(i)};
return os << "(int128_t hex) " << HexStr(sp);
}
static std::ostream &operator<< [[maybe_unused]] (std::ostream &os, const uint128_t i) {
Span<const uint8_t> sp{reinterpret_cast<const uint8_t*>(&i), sizeof(i)};
return os << "(uint128_t hex) " << HexStr(sp);
}
#endif
// Hack to make BOOST be able to print vectors and (u)int128_t
namespace boost {
namespace test_tools {
namespace tt_detail {
template<> struct print_log_value<std::vector<uint8_t>> {
std::ostream & operator()(std::ostream& os, std::vector<uint8_t> const& ts) { return ::operator<<(os,ts); }
};
template<> struct print_log_value<BigInt> {
std::ostream & operator()(std::ostream& os, BigInt const& ts) { return os << ts; }
};
#if HAVE_INT128
template<> struct print_log_value<int128_t> {
std::ostream & operator()(std::ostream& os, int128_t ts) { return ::operator<<(os, ts); }
};
template<> struct print_log_value<uint128_t> {
std::ostream & operator()(std::ostream& os, uint128_t ts) { return ::operator<<(os, ts); }
};
#endif
} // namespace tt_detail
} // namespace test_tools
} // namespace boost
BOOST_FIXTURE_TEST_SUITE(bigint_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(construction) {
// basic default c'tor
{
BigInt bi;
BOOST_REQUIRE(bi.getInt());
BOOST_CHECK_EQUAL(*bi.getInt(), 0);
BOOST_CHECK(bi.serialize() == ""_v);
BOOST_CHECK_EQUAL(bi.ToString(), "0");
}
// string-based c'tor
{
BigInt bi("-9223372036854775808"); // INT64_MIN (requires 9 bytes to serialize)
BOOST_REQUIRE(bi.getInt());
BOOST_REQUIRE(!bi.getUInt());
BOOST_CHECK_EQUAL(*bi.getInt(), std::numeric_limits<int64_t>::min());
// special-case when serializing in CScriptNum notation -- requires 9 bytes
BOOST_CHECK_EQUAL(bi.serialize(), "000000000000008080"_v);
// operator""_bi (uses string-based c'tor)
auto bi2 = "-9223372036854775808"_bi;
BOOST_CHECK(bi == bi2);
BOOST_CHECK_EQUAL(bi2.serialize(), "000000000000008080"_v);
}
{
// auto-detect base
BigInt bi("0xb3eff00d"); // base16
BOOST_REQUIRE(bi.getInt());
BOOST_REQUIRE(bi.getUInt());
BOOST_CHECK_EQUAL(*bi.getInt(), 0xb3eff00d);
BOOST_CHECK_EQUAL(*bi.getUInt(), 0xb3eff00d);
BOOST_CHECK_EQUAL(bi.serialize(), "0df0efb300"_v);
BOOST_CHECK_EQUAL(bi.serialize(), CScriptNum::fromIntUnchecked(*bi.getInt()).getvch());
bi = "0b10110011111011111111000000001101"_bi; // binary repr of 0xb3eff00d
BOOST_REQUIRE(bi.getInt());
BOOST_CHECK_EQUAL(*bi.getInt(), 0xb3eff00d);
BOOST_CHECK_EQUAL(bi.serialize(), "0df0efb300"_v);
bi = "026373770015"_bi; // octal repr of 0xb3eff00d
BOOST_REQUIRE(bi.getInt());
BOOST_CHECK_EQUAL(*bi.getInt(), 0xb3eff00d);
BOOST_CHECK_EQUAL(bi.serialize(), "0df0efb300"_v);
}
{
// failure to parse
BigInt bi("7bad");
BOOST_REQUIRE(bi.getInt());
BOOST_CHECK_EQUAL(*bi.getInt(), 0);
BOOST_CHECK( ! BigInt::FromString("7bad"));
// empty string should fail to parse
bi = ""_bi;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
BOOST_CHECK( ! BigInt::FromString(""));
// leading whitespace ignored
bi = " 7"_bi;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 7);
// trailing whitespace ignored
bi = "7 "_bi;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 7);
// quirk: in-between whitespace gets trimmed so "7 0" -> 70
bi = "7 0"_bi;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 70);
}
{
BigInt bi("9223372036854775808"); // INT64_MAX+1 (requires 9 bytes to serialize)
BOOST_REQUIRE(!bi.getInt());
BOOST_REQUIRE(bi.getUInt());
#if HAVE_INT128
BOOST_REQUIRE(bi.getInt128());
BOOST_CHECK_EQUAL(*bi.getInt128(), int128_t(std::numeric_limits<int64_t>::max()) + 1);
#endif
BOOST_CHECK_EQUAL(*bi.getUInt(), uint64_t(std::numeric_limits<int64_t>::max()) + 1u);
// special-case when serializing in CScriptNum notation -- requires 9 bytes
BOOST_CHECK_EQUAL(bi.serialize(), "000000000000008000"_v);
}
{
BigInt bi("-9223372036854775807"); // -INT64_MIN + 1
BOOST_REQUIRE(bi.getInt());
BOOST_CHECK_EQUAL(*bi.getInt(), std::numeric_limits<int64_t>::min() + 1);
BOOST_CHECK_EQUAL(bi.serialize(), "ffffffffffffffff"_v);
// operator""_bi (uses string-based c'tor)
auto bi2 = "-9223372036854775807"_bi;
BOOST_CHECK(bi == bi2);
BOOST_CHECK_EQUAL(bi2.serialize(), "ffffffffffffffff"_v);
}
{
BigInt bi("9223372036854775807"); // INT64_MAX
BOOST_REQUIRE(bi.getInt());
BOOST_CHECK_EQUAL(*bi.getInt(), std::numeric_limits<int64_t>::max());
BOOST_CHECK_EQUAL(bi.serialize(), "ffffffffffffff7f"_v);
}
{
BigInt bi("-9223372036854775809"); // INT64_MIN - 1 (requires 9 bytes)
BOOST_REQUIRE(!bi.getInt());
#if HAVE_INT128
BOOST_REQUIRE(bi.getInt128());
BOOST_CHECK_EQUAL(*bi.getInt128(), int128_t(std::numeric_limits<int64_t>::min()) - 1);
#endif
BOOST_CHECK_EQUAL(bi.serialize(), "010000000000008080"_v);
// operator""_bi (uses string-based c'tor)
auto bi2 = "-9223372036854775809"_bi;
BOOST_CHECK(bi == bi2);
BOOST_CHECK_EQUAL(bi2.serialize(), "010000000000008080"_v);
// alternate form of _bi literal (no quotes)
bi2 = 0; BOOST_CHECK_EQUAL(bi2, 0);
bi2 = -9223372036854775809_bi;
BOOST_CHECK_EQUAL(bi2, bi);
BOOST_CHECK_EQUAL(bi2.serialize(), "010000000000008080"_v);
}
// int64_t-based c'tor
{
BigInt bi(int64_t{1234567});
BOOST_REQUIRE(bi.getInt());
BOOST_REQUIRE(bi.getUInt());
BOOST_CHECK_EQUAL(*bi.getInt(), 1234567);
BOOST_CHECK_EQUAL(*bi.getUInt(), 1234567u);
BOOST_CHECK_EQUAL(bi.serialize(), "87d612"_v);
BigInt bi2(int64_t{-1234567});
BOOST_REQUIRE(bi2.getInt());
BOOST_REQUIRE( ! bi2.getUInt());
BOOST_CHECK_EQUAL(*bi2.getInt(), -1234567);
BOOST_CHECK_EQUAL(bi2.serialize(), "87d692"_v);
}
#if HAVE_INT128
// int128_t-based c'tor
{
constexpr auto val = int128_t(uint128_t{0xffffffffffffffffULL} * int128_t{0x7fffffffffffffffLL});
static_assert(val > 0);
BigInt bi(val);
BOOST_REQUIRE(!bi.getInt());
BOOST_REQUIRE(bi.getInt128());
BOOST_CHECK_EQUAL(*bi.getInt128(), val);
BOOST_CHECK_EQUAL(bi.serialize(), "0100000000000080feffffffffffff7f"_v);
BigInt bi2(int128_t{-1234567});
BOOST_REQUIRE(bi2.getInt());
BOOST_REQUIRE(!bi2.getUInt());
BOOST_REQUIRE(bi2.getInt128());
BOOST_CHECK_EQUAL(*bi2.getInt(), -1234567);
BOOST_CHECK_EQUAL(*bi2.getInt128(), -1234567);
BOOST_CHECK_EQUAL(bi2.serialize(), "87d692"_v);
}
// operator""_bi (uses string-based c'tor)
{
auto bi = "-170141183460469231731687303715884105728"_bi; // INT128_MIN edge case (17 bytes to serialize)
BOOST_REQUIRE(!bi.getInt());
BOOST_REQUIRE(bi.getInt128());
BOOST_CHECK_EQUAL(*bi.getInt128(), std::numeric_limits<int128_t>::min());
BOOST_CHECK_EQUAL(bi.serialize(), "0000000000000000000000000000008080"_v);
}
// test assignment (128-bit ints)
{
BigInt bi;
bi = int128_t(-123456);
BOOST_REQUIRE(bi.getInt());
BOOST_REQUIRE(bi.getInt128());
BOOST_CHECK_EQUAL(*bi.getInt128(), -123456);
BOOST_CHECK_EQUAL(bi.serialize(), CScriptNum::fromIntUnchecked(*bi.getInt()).getvch());
bi = std::numeric_limits<uint128_t>::max();
BOOST_REQUIRE(!bi.getInt());
BOOST_REQUIRE(!bi.getInt128());
BOOST_REQUIRE(bi.getUInt128());
BOOST_CHECK_EQUAL(*bi.getUInt128(), std::numeric_limits<uint128_t>::max());
BOOST_CHECK_EQUAL(bi.serialize(), "ffffffffffffffffffffffffffffffff00"_v);
}
{
auto bi = "170141183460469231731687303715884105727"_bi; // INT128_MAX edge case (16 bytes to serialize)
BOOST_REQUIRE(!bi.getInt());
BOOST_REQUIRE(bi.getInt128());
BOOST_CHECK_EQUAL(*bi.getInt128(), std::numeric_limits<int128_t>::max());
BOOST_CHECK_EQUAL(bi.serialize(), "ffffffffffffffffffffffffffffff7f"_v);
}
#endif
// copy c'tor and copy-assign
{
// copy c'tor
BigInt bi(42);
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 42);
BOOST_CHECK_EQUAL(bi.getUInt().value_or(-1), 42u);
BigInt bi2(bi);
BOOST_CHECK_EQUAL(bi2.getInt().value_or(-1), 42);
BOOST_CHECK(bi == bi2);
// copy-assign
BigInt bi3;
BOOST_CHECK_EQUAL(bi3.getInt().value_or(-1), 0);
bi3 = bi2;
BOOST_CHECK_EQUAL(bi3.getInt().value_or(-1), 42);
BOOST_CHECK(bi3 == bi2 && bi3 == bi);
}
// move c'tor and move-assign
{
// move c'tor
BigInt bi(42);
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 42);
BigInt bi2(std::move(bi));
BOOST_CHECK_EQUAL(bi2.getInt().value_or(-1), 42);
BOOST_CHECK(bi != bi2);
BOOST_CHECK(bi.getInt().value_or(-1) == 0); // moved-from value is 0
// move-assign
BigInt bi3;
BOOST_CHECK_EQUAL(bi3.getInt().value_or(-1), 0);
bi3 = std::move(bi2);
BOOST_CHECK_EQUAL(bi3.getInt().value_or(-1), 42);
BOOST_CHECK(bi2.getInt().value_or(-1) == 0); // moved-from value is 0
BOOST_CHECK(bi3 != bi2 && bi3 != bi);
}
// C++ int types should be unambiguously resolved; except: `unsigned long` which may be bigger than int64_t
{
BOOST_CHECK_EQUAL(BigInt(char{42}).getInt().value_or(-1), 42);
BOOST_CHECK_EQUAL(BigInt(int{42}).getInt().value_or(-1), 42);
BOOST_CHECK_EQUAL(BigInt(short{42}).getInt().value_or(-1), 42);
BOOST_CHECK_EQUAL(BigInt(long{42}).getInt().value_or(-1), 42);
BOOST_CHECK_EQUAL(BigInt(unsigned{42}).getInt().value_or(-1), 42);
BOOST_CHECK_EQUAL(BigInt(static_cast<unsigned char>(42)).getInt().value_or(-1), 42);
BOOST_CHECK_EQUAL(BigInt(static_cast<unsigned short>(42)).getInt().value_or(-1), 42);
BigInt bi;
BOOST_CHECK_EQUAL((bi = char{42}).getInt().value_or(-1), 42);
bi = 0;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL((bi = int{42}).getInt().value_or(-1), 42);
bi = 0;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL((bi = short{42}).getInt().value_or(-1), 42);
bi = 0;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL((bi = long{42}).getInt().value_or(-1), 42);
bi = 0;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL((bi = unsigned{42}).getInt().value_or(-1), 42);
bi = 0;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL((bi = static_cast<unsigned char>(42)).getInt().value_or(-1), 42);
bi = 0;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL((bi = static_cast<unsigned short>(42)).getInt().value_or(-1), 42);
bi = 0;
BOOST_CHECK_EQUAL(bi.getInt().value_or(-1), 0);
}
}
BOOST_AUTO_TEST_CASE(misc) {
{
// swap
BigInt a(123), b(345);
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 123);
BOOST_CHECK_EQUAL(b.getInt().value_or(-1), 345);
a.swap(b);
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 345);
BOOST_CHECK_EQUAL(b.getInt().value_or(-1), 123);
}
{
// Negate
auto a = "123456"_bi;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 123456);
a.negate();
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), -123456);
a.negate();
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 123456);
a = -a;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), -123456);
a = -a;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 123456);
}
{
// sign & isNegative
auto a = "123456"_bi;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 123456);
BOOST_CHECK_EQUAL(a.sign(), 1);
BOOST_CHECK(!a.isNegative());
a = 0;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL(a.sign(), 0);
BOOST_CHECK(!a.isNegative());
a = -42;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), -42);
BOOST_CHECK_EQUAL(a.sign(), -1);
BOOST_CHECK(a.isNegative());
}
{
// setInt* and get*Int* and pre/post-increment and pre/post-decrement and absValNumBits and abs
BigInt a;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 0);
BOOST_CHECK_EQUAL(a.absValNumBits(), 1);
BOOST_CHECK_EQUAL(a--.getInt().value_or(99), 0);
BOOST_CHECK_EQUAL(a.getInt().value_or(99), -1);
BOOST_CHECK_EQUAL(a.absValNumBits(), 1);
BOOST_CHECK_EQUAL(a.ToString(), "-1");
BOOST_CHECK_EQUAL(a++.getInt().value_or(99), -1);
BOOST_CHECK_EQUAL(a.getInt().value_or(99), 0);
BOOST_CHECK_EQUAL(a.ToString(), "0");
a.setInt(16);
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), 16);
BOOST_CHECK_EQUAL(a.getUInt().value_or(-1), 16);
BOOST_CHECK_EQUAL(a.absValNumBits(), 5);
BOOST_CHECK_EQUAL(a.abs().getUInt().value_or(1), 16);
--a;
BOOST_CHECK_EQUAL(a.absValNumBits(), 4);
a.setInt(std::numeric_limits<int64_t>::max());
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), std::numeric_limits<int64_t>::max());
BOOST_CHECK_EQUAL(a.getUInt().value_or(1), uint64_t(std::numeric_limits<int64_t>::max()));
BOOST_CHECK_EQUAL(a.absValNumBits(), 63);
++a;
BOOST_CHECK(!a.getInt());
BOOST_CHECK_EQUAL(a.getUInt().value_or(1), uint64_t(std::numeric_limits<int64_t>::max()) + 1u);
BOOST_CHECK_EQUAL(a.absValNumBits(), 64);
--a;
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), std::numeric_limits<int64_t>::max());
BOOST_CHECK_EQUAL(a.getUInt().value_or(1), uint64_t(std::numeric_limits<int64_t>::max()));
BOOST_CHECK_EQUAL(a.absValNumBits(), 63);
a.setInt(-42);
BOOST_CHECK_EQUAL(a.getInt().value_or(-1), -42);
BOOST_CHECK_EQUAL(a.abs().getInt().value_or(1), 42);
BOOST_CHECK(!a.getUInt());
BOOST_CHECK_EQUAL(a.absValNumBits(), 6);
BOOST_CHECK_EQUAL(BigInt{}.getInt().value(), 0); // corner case with null m_p
BOOST_CHECK_EQUAL((++BigInt(-1)).getInt().value(), 0); // corner case where m_p is not null but it stores 0
BOOST_CHECK_EQUAL((++BigInt(-1)).getUInt().value(), 0);
#if HAVE_INT128
a.setInt(std::numeric_limits<uint64_t>::max());
BOOST_CHECK(!a.getInt());
BOOST_CHECK_EQUAL(a.getUInt().value_or(1), std::numeric_limits<uint64_t>::max());
BOOST_CHECK_EQUAL(a.getInt128().value_or(1), int128_t{std::numeric_limits<uint64_t>::max()});
BOOST_CHECK_EQUAL(a.absValNumBits(), 64);
++a;
BOOST_CHECK_EQUAL(a.getInt128().value_or(1), int128_t{std::numeric_limits<uint64_t>::max()} + 1);
BOOST_CHECK_EQUAL(a.absValNumBits(), 65);
a.setInt(std::numeric_limits<int128_t>::max());
BOOST_CHECK(!a.getInt());
BOOST_CHECK(!a.getUInt());
BOOST_CHECK_EQUAL(a.getInt128().value_or(1), std::numeric_limits<int128_t>::max());
++a;
BOOST_CHECK(!a.getInt128());
BOOST_CHECK_EQUAL(a.ToString(), "170141183460469231731687303715884105728");
--a;
BOOST_CHECK_EQUAL(a.getInt128().value_or(1), std::numeric_limits<int128_t>::max());
a.setInt(std::numeric_limits<int128_t>::min());
BOOST_CHECK(!a.getInt());
BOOST_CHECK(!a.getUInt());
BOOST_CHECK_EQUAL(a.getInt128().value_or(1), std::numeric_limits<int128_t>::min());
BOOST_CHECK_EQUAL(a.ToString(), "-170141183460469231731687303715884105728");
BOOST_CHECK_EQUAL(a.abs().ToString(), "170141183460469231731687303715884105728");
--a;
BOOST_CHECK(!a.getInt());
BOOST_CHECK(!a.getUInt());
BOOST_CHECK(!a.getInt128());
BOOST_CHECK_EQUAL(a.ToString(), "-170141183460469231731687303715884105729");
++a;
BOOST_CHECK_EQUAL(a.getInt128().value_or(1), std::numeric_limits<int128_t>::min());
BOOST_CHECK_EQUAL(a.absValNumBits(), 128);
#endif
}
{
// sqrt and pow
BOOST_CHECK_EQUAL(("42"_bi.pow(2).sqrt()).getInt().value_or(1), 42);
BOOST_CHECK_EQUAL(("42"_bi * "42"_bi).sqrt().getInt().value_or(1), 42);
auto a = "42"_bi.pow(42);
BOOST_CHECK_EQUAL(a.ToString(), "150130937545296572356771972164254457814047970568738777235893533016064");
a = a.pow(2);
BOOST_CHECK_EQUAL(a.ToString(), "22539298408229739998969300776759130257080980854275852333895157904893661197944366624126022728932809301985081175336480346256876428482052096");
a = a.sqrt();
BOOST_CHECK_EQUAL(a.ToString(), "150130937545296572356771972164254457814047970568738777235893533016064");
// pow 0 & 1
BOOST_CHECK_EQUAL(a.pow(0), 1_bi);
BOOST_CHECK_EQUAL(BigInt().pow(0), 1_bi);
BOOST_CHECK_EQUAL(BigInt().pow(1), 0_bi);
BOOST_CHECK_EQUAL(a.powMod(0, 2), 1_bi);
BOOST_CHECK_EQUAL(a.powMod(--BigInt(1), 2), 1_bi);
BOOST_CHECK_EQUAL(BigInt().powMod(0, 2), 1_bi);
BOOST_CHECK_EQUAL(BigInt().powMod(0, -2), 1_bi);
BOOST_CHECK_EQUAL(a.powMod(0, 1), 0_bi);
BOOST_CHECK_EQUAL(a.powMod(--BigInt(1), 1), 0_bi);
BOOST_CHECK_EQUAL(BigInt().powMod(0, 1), 0_bi);
BOOST_CHECK_EQUAL(BigInt().powMod(1, 1), 0_bi);
BOOST_CHECK_EQUAL(BigInt().powMod(1, 2), 0_bi);
}
{
// various things below should throw
// div by zero, etc
BOOST_CHECK_THROW(BigInt(42) / 0, std::invalid_argument);
BOOST_CHECK_THROW(BigInt(42) % 0, std::invalid_argument);
BOOST_CHECK_THROW(BigInt(42).mathModulo(0), std::invalid_argument);
BOOST_CHECK_THROW(BigInt(42) / --BigInt(1), std::invalid_argument);
BOOST_CHECK_THROW(BigInt(42) % --BigInt(1), std::invalid_argument);
BOOST_CHECK_THROW(BigInt(42).mathModulo(--BigInt(1)), std::invalid_argument);
// powMod
BOOST_CHECK_NO_THROW(BigInt(42).powMod(1, 1));
BOOST_CHECK_THROW(BigInt(42).powMod(-1, 1), std::invalid_argument); // negative exponent
BOOST_CHECK_NO_THROW(BigInt(42).powMod(0, 1)); // 0 exponent ok
BOOST_CHECK_THROW(BigInt(42).powMod(1, 0), std::invalid_argument); // zero mod
BOOST_CHECK_NO_THROW(BigInt(42).powMod(1, -1)); // negative mod ok
BOOST_CHECK_NO_THROW(BigInt(42).powMod(0, -1)); // 0 exp and negative mod ok
// ToString with bad base, edge cases
BOOST_CHECK_THROW(BigInt().ToString(-1), std::invalid_argument);
BOOST_CHECK_THROW(BigInt().ToString(0), std::invalid_argument);
BOOST_CHECK_THROW(BigInt().ToString(1), std::invalid_argument);
BOOST_CHECK_THROW(BigInt().ToString(-37), std::invalid_argument);
BOOST_CHECK_THROW(BigInt().ToString(63), std::invalid_argument);
// below are just inside the acceptable bounds for `base`, so they do not throw
BOOST_CHECK_NO_THROW(BigInt().ToString(-2));
BOOST_CHECK_NO_THROW(BigInt().ToString(2));
BOOST_CHECK_NO_THROW(BigInt().ToString(-36));
BOOST_CHECK_NO_THROW(BigInt().ToString(62));
}
{
// misc. edge-case bit ops
BOOST_CHECK_EQUAL(BigInt() & BigInt(42), 0);
BOOST_CHECK_EQUAL(BigInt() | BigInt(42), 42);
BOOST_CHECK_EQUAL(BigInt() ^ BigInt(42), 42);
BOOST_CHECK_EQUAL(BigInt(42) & BigInt(), 0);
BOOST_CHECK_EQUAL(BigInt(42) | BigInt(), 42);
BOOST_CHECK_EQUAL(BigInt(42) ^ BigInt(), 42);
BOOST_CHECK_EQUAL("0xffffffffffffffffff"_bi & "0x42"_bi, "0x42"_bi);
BOOST_CHECK_EQUAL("0xffffffffffffffffbd"_bi | "0x42"_bi, "0xffffffffffffffffff"_bi);
BOOST_CHECK_EQUAL("0xffffffffffffffffff"_bi ^ "0x42"_bi, "0xffffffffffffffffbd"_bi);
BOOST_CHECK_EQUAL("0xffffffffffffffffff"_bi << 1, "0x1fffffffffffffffffe"_bi);
BOOST_CHECK_EQUAL("0x1fffffffffffffffffe"_bi >> 1, "0xffffffffffffffffff"_bi);
BOOST_CHECK_EQUAL(-42_bi << 1, -84);
BOOST_CHECK_EQUAL("-42"_bi << 2, -168);
BOOST_CHECK_EQUAL("-42"_bi >> 1, -21);
// Ensure right-shift behaves like C++
BOOST_CHECK_EQUAL("-42"_bi >> 4, -42 >> 4);
BOOST_CHECK_EQUAL("-42"_bi >> 5, -42 >> 5);
BOOST_CHECK_EQUAL("-42"_bi >> 6, -42 >> 6);
}
{
// serialize
BOOST_CHECK_EQUAL("0"_bi.serialize(), ""_v);
BOOST_CHECK_EQUAL("-0"_bi.serialize(), ""_v);
BOOST_CHECK_EQUAL("42"_bi.serialize(), "2a"_v);
BOOST_CHECK_EQUAL("-42"_bi.serialize(), "aa"_v);
BOOST_CHECK_EQUAL("127"_bi.serialize(), "7f"_v);
BOOST_CHECK_EQUAL("-127"_bi.serialize(), "ff"_v);
BOOST_CHECK_EQUAL("128"_bi.serialize(), "8000"_v); // edge case for CScriptNum to distinguish positive from negative
BOOST_CHECK_EQUAL("-128"_bi.serialize(), "8080"_v); // edge case for CScriptNum to distinguish positive from negative
// unserialize
BigInt a;
a.unserialize("2a"_v); BOOST_CHECK_EQUAL(a.getInt().value_or(9999), 42);
BOOST_CHECK_EQUAL(CScriptNum::fromIntUnchecked(a.getInt().value_or(9999)).getvch(), a.serialize());
a.unserialize("aa"_v); BOOST_CHECK_EQUAL(a.getInt().value_or(9999), -42);
BOOST_CHECK_EQUAL(CScriptNum::fromIntUnchecked(a.getInt().value_or(9999)).getvch(), a.serialize());
a.unserialize("7f"_v); BOOST_CHECK_EQUAL(a.getInt().value_or(9999), 127);
BOOST_CHECK_EQUAL(CScriptNum::fromIntUnchecked(a.getInt().value_or(9999)).getvch(), a.serialize());
a.unserialize("ff"_v); BOOST_CHECK_EQUAL(a.getInt().value_or(9999), -127);
BOOST_CHECK_EQUAL(CScriptNum::fromIntUnchecked(a.getInt().value_or(9999)).getvch(), a.serialize());
a.unserialize("8000"_v); BOOST_CHECK_EQUAL(a.getInt().value_or(9999), 128); // edge case for CScriptNum to distinguish positive from negative
BOOST_CHECK_EQUAL(CScriptNum::fromIntUnchecked(a.getInt().value_or(9999)).getvch(), a.serialize());
a.unserialize("8080"_v); BOOST_CHECK_EQUAL(a.getInt().value_or(9999), -128); // edge case for CScriptNum to distinguish positive from negative
BOOST_CHECK_EQUAL(CScriptNum::fromIntUnchecked(a.getInt().value_or(9999)).getvch(), a.serialize());
}
{
// other assorted edge cases
BOOST_CHECK_EQUAL(ScriptBigInt::bigIntConsensusMin() % -1, 0);
BOOST_CHECK_EQUAL(ScriptBigInt::bigIntConsensusMax() % -1, 0);
BOOST_CHECK_EQUAL(ScriptBigInt::bigIntConsensusMin() / -1, ScriptBigInt::bigIntConsensusMax());
BOOST_CHECK_EQUAL(ScriptBigInt::bigIntConsensusMax() / -1, ScriptBigInt::bigIntConsensusMin());
}
}
BOOST_AUTO_TEST_CASE(int_interop) {
long li = -42; int i = -42; short si = -42; long long lli = -42; char c = 42; signed char sc = -42;
unsigned long uli = 42; int ui = 42; unsigned short us = 42; unsigned long long ulli = 42; unsigned char uc = 42;
bool b = 42;
#if HAVE_INT128
int128_t i128 = -42; uint128_t u128 = 42;
#endif
BigInt bi;
// various C++ types should all be assignable ...
bi = li; BOOST_CHECK_EQUAL(bi, -42); bi = 0;
bi = i; BOOST_CHECK_EQUAL(bi, -42); bi = 0;
bi = si; BOOST_CHECK_EQUAL(bi, -42); bi = 0;
bi = lli; BOOST_CHECK_EQUAL(bi, -42); bi = 0;
bi = c; BOOST_CHECK_EQUAL(bi, 42); bi = 0; // it is platform-specific which overload is used for `char`
bi = sc; BOOST_CHECK_EQUAL(bi, -42); bi = 0;
bi = uli; BOOST_CHECK_EQUAL(bi, 42); bi = 0;
bi = ui; BOOST_CHECK_EQUAL(bi, 42); bi = 0;
bi = us; BOOST_CHECK_EQUAL(bi, 42); bi = 0;
bi = ulli; BOOST_CHECK_EQUAL(bi, 42); bi = 0;
bi = uc; BOOST_CHECK_EQUAL(bi, 42); bi = 0;
bi = b; BOOST_CHECK_EQUAL(bi, 1); bi = 0; // bool resolves to int
#if HAVE_INT128
bi = i128; BOOST_CHECK_EQUAL(bi, -42); bi = 0;
bi = u128; BOOST_CHECK_EQUAL(bi, 42); bi = 0;
#endif
// ... and constructible
BOOST_CHECK_EQUAL(BigInt(li), -42);
BOOST_CHECK_EQUAL(BigInt(i), -42);
BOOST_CHECK_EQUAL(BigInt(si), -42);
BOOST_CHECK_EQUAL(BigInt(lli), -42);
BOOST_CHECK_EQUAL(BigInt(c), 42); // it is platform-specific which overload is used for `char`
BOOST_CHECK_EQUAL(BigInt(sc), -42);
BOOST_CHECK_EQUAL(BigInt(uli), 42);
BOOST_CHECK_EQUAL(BigInt(ui), 42);
BOOST_CHECK_EQUAL(BigInt(us), 42);
BOOST_CHECK_EQUAL(BigInt(ulli), 42);
BOOST_CHECK_EQUAL(BigInt(uc), 42);
BOOST_CHECK_EQUAL(BigInt(b), 1); // bool resolves to int
#if HAVE_INT128
BOOST_CHECK_EQUAL(BigInt(i128), -42);
BOOST_CHECK_EQUAL(BigInt(u128), 42);
#endif
// ... and comparable (use various forms... default c'tor'd, explicit c'tor'd, etc)
for (const auto &bi2 : {BigInt(), BigInt(0), BigInt(-1), BigInt(1), BigInt(true), BigInt(false)}) {
BOOST_CHECK_GT(bi2, li);
BOOST_CHECK_GT(bi2, i);
BOOST_CHECK_GT(bi2, si);
BOOST_CHECK_GT(bi2, lli);
BOOST_CHECK_LT(bi2, c); // it is platform-specific which overload is used for `char`
BOOST_CHECK_GT(bi2, sc);
BOOST_CHECK_LT(bi2, uli);
BOOST_CHECK_LT(bi2, ui);
BOOST_CHECK_LT(bi2, us);
BOOST_CHECK_LT(bi2, ulli);
BOOST_CHECK_LT(bi2, uc);
// check bool comparison (behaves like C++ comparison of int vs bool)
BOOST_CHECK_EQUAL(bi2 < b, bi2.getInt().value() < b);
BOOST_CHECK_EQUAL(bi2 > b, bi2.getInt().value() > b);
BOOST_CHECK_EQUAL(bi2 == b, bi2.getInt().value() == b);
BOOST_CHECK_EQUAL(bi2 != b, bi2.getInt().value() != b);
BOOST_CHECK_EQUAL(static_cast<bool>(bi2), bool(bi2.getInt().value()));
#if HAVE_INT128
BOOST_CHECK_GT(bi2, i128);
BOOST_CHECK_LT(bi2, u128);
#endif
}
}
// Check std stream operator<< behaves as expected
static void CheckStreamOp(const BigInt &a) {
BigInt c;
std::ostringstream os;
os << BigInt();
BOOST_CHECK_EQUAL(os.str(), "0");
c = BigInt(os.str()); // test that we can parse what we wrote via operator<<
BOOST_CHECK_EQUAL(c, 0);
os.str({});
os << a;
BOOST_CHECK_EQUAL(os.str(), a.ToString());
c = BigInt(os.str()); // test that we can parse what we wrote via operator<<
BOOST_CHECK_EQUAL(c, a);
os.str({});
os << std::hex << a;
BOOST_CHECK_EQUAL(os.str(), a.ToString(16));
c = BigInt(os.str(), 16); // test that we can parse what we wrote via operator<<
BOOST_CHECK_EQUAL(c, a);
os.str({});
os << std::oct << a;
BOOST_CHECK_EQUAL(os.str(), a.ToString(8));
c = BigInt(os.str(), 8); // test that we can parse what we wrote via operator<<
BOOST_CHECK_EQUAL(c, a);
os.str({});
os << std::showbase << std::hex << a << std::noshowbase;
if (auto str = a.ToString(16); a.isNegative()) {
str.insert(1, "0x");
BOOST_CHECK_EQUAL(os.str(), str);
} else {
BOOST_CHECK_EQUAL(os.str(), "0x" + str);
}
c = BigInt(os.str()); // test that we can parse what we wrote via operator<<
BOOST_CHECK_EQUAL(c, a);
os.str({});
os << std::showbase << std::oct << a << std::noshowbase;
if (auto str = a.ToString(8); a.isNegative()) {
str.insert(1, "0");
BOOST_CHECK_EQUAL(os.str(), str);
} else {
BOOST_CHECK_EQUAL(os.str(), (a ? "0" : "") + str);
}
c = BigInt(os.str()); // test that we can parse what we wrote via operator<<
BOOST_CHECK_EQUAL(c, a);
}
template<typename Int, typename UInt>
static void CheckIntSerUnserRoundTrip(Int s, UInt u) {
static_assert(std::is_integral_v<Int> && std::is_integral_v<UInt>);
static_assert(std::is_same_v<std::make_unsigned_t<Int>, UInt>);
static_assert(std::is_same_v<std::make_signed_t<UInt>, Int>);
static_assert(std::is_signed_v<Int> && std::is_unsigned_v<UInt>);
BigInt a, b;
auto GetExpectedBytes = [](UInt le, bool neg) {
std::vector<uint8_t> expected(sizeof(le));
std::memcpy(expected.data(), &le, sizeof(le));
// pop MSB zeroes
while (!expected.empty() && !expected.back()) {
expected.pop_back();
}
// ensure no sign bit conflict
if (!expected.empty()) {
if (expected.back() & 0x80u) {
expected.push_back(neg ? 0x80 : 0x00);
} else if (neg) expected.back() |= 0x80;
}
return expected;
};
// do it for signed
a = s;
if constexpr (sizeof(s) <= 8u) {
BOOST_CHECK_EQUAL(a.getInt().value_or(s ^ 12345), s);
BOOST_CHECK_EQUAL(b.getInt().value_or(s ^ 12345), 0);
b.unserialize(a.serialize());
BOOST_CHECK_EQUAL(a.getInt().value_or(s ^ 12345), s);
BOOST_CHECK_EQUAL(b.getInt().value_or(s ^ 12345), s);
// Check that it matches CScriptNum serialization
if (auto optcsn = CScriptNum::fromInt(s)) {
BOOST_CHECK_EQUAL(optcsn->getvch(), a.serialize());
} else {
// Must be this forbidden value if this branch is taken
BOOST_CHECK_EQUAL(s, std::numeric_limits<int64_t>::min());
}
} else {
BOOST_CHECK_EQUAL(a.getInt128().value_or(s ^ 12345), s);
BOOST_CHECK_EQUAL(b.getInt128().value_or(s ^ 12345), 0);
b.unserialize(a.serialize());
BOOST_CHECK_EQUAL(a.getInt128().value_or(s ^ 12345), s);
BOOST_CHECK_EQUAL(b.getInt128().value_or(s ^ 12345), s);
}
BOOST_CHECK(a == b);
if (s >= std::numeric_limits<int64_t>::min() && s <= std::numeric_limits<int64_t>::max()) {
// strprintf only works with int64_t at most ...
BOOST_CHECK(a.ToString() == strprintf("%i", static_cast<int64_t>(s)));
BOOST_CHECK(b.ToString() == strprintf("%i", static_cast<int64_t>(s)));
}
auto ToLE = [](UInt x) {
if (const unsigned i = 1; *reinterpret_cast<const uint8_t *>(&i) != 1) {
// not already little endian, reverse x
auto *begin = reinterpret_cast<unsigned char *>(&x);
auto *end = begin + sizeof(x);
std::reverse(begin, end);
}
return x;
};
if (s < 0) {
BOOST_CHECK(! a.getUInt());
#if HAVE_INT128
BOOST_CHECK(! a.getUInt128());
#endif
if (s == std::numeric_limits<Int>::min()) {
// special case
std::vector<uint8_t> vec(sizeof(UInt) + 1u);
vec[vec.size()-1u] = 0x80u;
vec[vec.size()-2u] = 0x80u;
BOOST_CHECK_EQUAL(a.serialize(), vec);
} else {
const auto expected = GetExpectedBytes(ToLE(UInt(-s)), true);
BOOST_CHECK_EQUAL(a.serialize(), expected);
}
}
// Check operator<<
CheckStreamOp(a);
// do what we did above for unsigned
b = a = 0;
a = u;
if constexpr (sizeof(u) <= 8) {
BOOST_CHECK_EQUAL(a.getUInt().value_or(u ^ 12345u), u);
BOOST_CHECK_EQUAL(b.getUInt().value_or(u ^ 12345), 0);
b.unserialize(a.serialize());
BOOST_CHECK_EQUAL(a.getUInt().value_or(u ^ 12345), u);
BOOST_CHECK_EQUAL(b.getUInt().value_or(u ^ 12345), u);
} else {
BOOST_CHECK_EQUAL(a.getUInt128().value_or(u ^ 12345u), u);
BOOST_CHECK_EQUAL(b.getUInt128().value_or(u ^ 12345), 0);
b.unserialize(a.serialize());
BOOST_CHECK_EQUAL(a.getUInt128().value_or(u ^ 12345), u);
BOOST_CHECK_EQUAL(b.getUInt128().value_or(u ^ 12345), u);
}
BOOST_CHECK(a == b);
if (u >= std::numeric_limits<uint64_t>::min() && u <= std::numeric_limits<uint64_t>::max()) {
// strprintf only works with uint64_t at most ...
BOOST_CHECK(a.ToString() == strprintf("%u", static_cast<uint64_t>(u)));
BOOST_CHECK(b.ToString() == strprintf("%u", static_cast<uint64_t>(u)));
}
if (u > UInt(std::numeric_limits<int64_t>::max())) {
BOOST_CHECK(! a.getInt());
}
BigInt c;
#if HAVE_INT128
if (u > uint128_t(std::numeric_limits<int128_t>::max())) {
BOOST_CHECK(! a.getInt128());
}
// test setInt (u128)
BOOST_CHECK(c == 0);
c.setInt(static_cast<uint128_t>(u));
BOOST_CHECK(a == c && b == c);
BOOST_CHECK_EQUAL(c.ToString(), a.ToString());
c = 0;
#endif
// test setInt (unsigned)
BOOST_CHECK(c == 0);
c.setInt(u);
BOOST_CHECK(a == c && b == c);
BOOST_CHECK_EQUAL(c.ToString(), a.ToString());
c = 0;
// Test ser is what we expect
const auto expected = GetExpectedBytes(ToLE(u), false);
BOOST_CHECK_EQUAL(a.serialize(), expected);
// Check operator<<
CheckStreamOp(a);
}
BOOST_AUTO_TEST_CASE(ser_unser_round_trip) {
// Check serialize()/unserialize() rount-trip + ToString()
FastRandomContext ctx;
// check edge cases
CheckIntSerUnserRoundTrip(int64_t{0}, uint64_t{0u});
CheckIntSerUnserRoundTrip(std::numeric_limits<int64_t>::min(), std::numeric_limits<uint64_t>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<int64_t>::min(), std::numeric_limits<uint64_t>::max());
CheckIntSerUnserRoundTrip(std::numeric_limits<int64_t>::max(), std::numeric_limits<uint64_t>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<int64_t>::max(), std::numeric_limits<uint64_t>::max());
CheckIntSerUnserRoundTrip(std::numeric_limits<int>::min(), std::numeric_limits<unsigned>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<int>::min(), std::numeric_limits<unsigned>::max());
CheckIntSerUnserRoundTrip(std::numeric_limits<int>::max(), std::numeric_limits<unsigned>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<int>::max(), std::numeric_limits<unsigned>::max());
CheckIntSerUnserRoundTrip(std::numeric_limits<short>::min(), std::numeric_limits<unsigned short>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<short>::min(), std::numeric_limits<unsigned short>::max());
CheckIntSerUnserRoundTrip(std::numeric_limits<short>::max(), std::numeric_limits<unsigned short>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<short>::max(), std::numeric_limits<unsigned short>::max());
#if HAVE_INT128
CheckIntSerUnserRoundTrip(int128_t{0}, uint128_t{0u});
CheckIntSerUnserRoundTrip(std::numeric_limits<int128_t>::min(), std::numeric_limits<uint128_t>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<int128_t>::min(), std::numeric_limits<uint128_t>::max());
CheckIntSerUnserRoundTrip(std::numeric_limits<int128_t>::max(), std::numeric_limits<uint128_t>::min());
CheckIntSerUnserRoundTrip(std::numeric_limits<int128_t>::max(), std::numeric_limits<uint128_t>::max());
#endif
for (size_t i = 0; i < 25'000u; ++i) {
// Run through random tries of numbers in the uint64_t range, serializing then unserializing and ensuring
// same value is preserved.
{
const uint64_t u = ctx.rand64();
int64_t s;
std::memcpy(&s, &u, sizeof(s));
// Do test
CheckIntSerUnserRoundTrip(s, u);
}
#if HAVE_INT128
// Also do this for 128-bit numbers, if the compiler supports such numbers
{
uint128_t u;
int128_t s;
// build unsigned 128-bit int by reading 2 64-bit words from the random context
{
unsigned char *p = reinterpret_cast<unsigned char *>(&u);
const uint64_t w1 = ctx.rand64(), w2 = ctx.rand64();
static_assert(sizeof(u) == sizeof(w1) + sizeof(w2));
std::memcpy(p, &w1, sizeof(w1));
p += sizeof(w1);
std::memcpy(p, &w2, sizeof(w2));
}
std::memcpy(&s, &u, sizeof(s)); // copy bytes also to signed repr
// Do test
CheckIntSerUnserRoundTrip(s, u);
}
#endif
}
}
BOOST_AUTO_TEST_CASE(compare) {
FastRandomContext ctx;
int64_t sprev{};
uint64_t uprev{};
int32_t sprev32{};
uint32_t uprev32{};
int16_t sprev16{};
uint16_t uprev16{};
#if HAVE_INT128
int128_t sprev128{};
uint128_t uprev128{};
#endif
for (size_t i = 0; i < 25'000u; ++i) {
const uint64_t u = ctx.rand64();
int64_t s;
std::memcpy(&s, &u, sizeof(s));
const uint32_t u32 = ctx.rand32();
int32_t s32;
std::memcpy(&s32, &u32, sizeof(s32));
uint16_t u16;
int16_t s16;
std::memcpy(&u16, &u32, sizeof(u16));
std::memcpy(&s16, &s32, sizeof(s16));
auto DoTest = [](auto val1, auto val2) {
const auto as = std::min(val1, val2), bs = std::max(val1, val2);
// default c'tord BigInt vs as
BOOST_CHECK_EQUAL(BigInt().compare(as), as < 0 ? 1 : (as ? -1 : 0));
BOOST_CHECK_EQUAL(BigInt() < as, 0 < as);
BOOST_CHECK_EQUAL(BigInt() <= as, 0 <= as);
BOOST_CHECK_EQUAL(BigInt() == as, 0 == as);
BOOST_CHECK_EQUAL(BigInt() != as, 0 != as);
BOOST_CHECK_EQUAL(BigInt() >= as, 0 >= as);
BOOST_CHECK_EQUAL(BigInt() > as, 0 > as);
BOOST_CHECK_EQUAL(BigInt().compare(BigInt(as)), as < 0 ? 1 : (as ? -1 : 0));
// compare apples to apples (both BigInt)
BOOST_CHECK_EQUAL(BigInt() < BigInt(as), 0 < as);
BOOST_CHECK_EQUAL(BigInt() <= BigInt(as), 0 <= as);
BOOST_CHECK_EQUAL(BigInt() == BigInt(as), 0 == as);
BOOST_CHECK_EQUAL(BigInt() != BigInt(as), 0 != as);
BOOST_CHECK_EQUAL(BigInt() >= BigInt(as), 0 >= as);
BOOST_CHECK_EQUAL(BigInt() > BigInt(as), 0 > as);
// as vs default c'tord BigInt
BOOST_CHECK_EQUAL(BigInt(as).compare(BigInt()), as < 0 ? -1 : (as ? 1 : 0));
BOOST_CHECK_EQUAL(BigInt(as) < BigInt(), as < 0);
BOOST_CHECK_EQUAL(BigInt(as) <= BigInt(), as <= 0);
BOOST_CHECK_EQUAL(BigInt(as) == BigInt(), as == 0);
BOOST_CHECK_EQUAL(BigInt(as) != BigInt(), as != 0);
BOOST_CHECK_EQUAL(BigInt(as) >= BigInt(), as >= 0);
BOOST_CHECK_EQUAL(BigInt(as) > BigInt(), as > 0);
// default c'tord BigInt vs bs
BOOST_CHECK_EQUAL(BigInt().compare(bs), bs < 0 ? 1 : (bs ? -1 : 0));
BOOST_CHECK_EQUAL(BigInt() < bs, 0 < bs);
BOOST_CHECK_EQUAL(BigInt() <= bs, 0 <= bs);
BOOST_CHECK_EQUAL(BigInt() == bs, 0 == bs);
BOOST_CHECK_EQUAL(BigInt() != bs, 0 != bs);
BOOST_CHECK_EQUAL(BigInt() >= bs, 0 >= bs);
BOOST_CHECK_EQUAL(BigInt() > bs, 0 > bs);
// bs vs default c'tord BigInt
BOOST_CHECK_EQUAL(BigInt(bs).compare(BigInt()), bs < 0 ? -1 : (bs ? 1 : 0));
BOOST_CHECK_EQUAL(BigInt(bs) < BigInt(), bs < 0);
BOOST_CHECK_EQUAL(BigInt(bs) <= BigInt(), bs <= 0);
BOOST_CHECK_EQUAL(BigInt(bs) == BigInt(), bs == 0);
BOOST_CHECK_EQUAL(BigInt(bs) != BigInt(), bs != 0);
BOOST_CHECK_EQUAL(BigInt(bs) >= BigInt(), bs >= 0);
BOOST_CHECK_EQUAL(BigInt(bs) > BigInt(), bs > 0);
// compare apples to apples (both BigInt)
BOOST_CHECK_EQUAL(BigInt().compare(BigInt(bs)), bs < 0 ? 1 : (bs ? -1 : 0));
BOOST_CHECK_EQUAL(BigInt() < BigInt(bs), 0 < bs);
BOOST_CHECK_EQUAL(BigInt() <= BigInt(bs), 0 <= bs);
BOOST_CHECK_EQUAL(BigInt() == BigInt(bs), 0 == bs);
BOOST_CHECK_EQUAL(BigInt() != BigInt(bs), 0 != bs);
BOOST_CHECK_EQUAL(BigInt() >= BigInt(bs), 0 >= bs);
BOOST_CHECK_EQUAL(BigInt() > BigInt(bs), 0 > bs);
if (as < bs) {
BOOST_CHECK_EQUAL(BigInt(as).compare(BigInt(bs)), -1);
BOOST_CHECK_EQUAL(BigInt(bs).compare(BigInt(as)), 1);
BOOST_CHECK_EQUAL(BigInt(as).compare(bs), -1);
BOOST_CHECK_EQUAL(BigInt(bs).compare(as), 1);
BOOST_CHECK(BigInt(as) < BigInt(bs));
BOOST_CHECK(BigInt(as) <= BigInt(bs));
BOOST_CHECK(!(BigInt(as) == BigInt(bs)));
BOOST_CHECK(!(BigInt(as) > BigInt(bs)));
BOOST_CHECK(!(BigInt(as) >= BigInt(bs)));
BOOST_CHECK(BigInt(as) != BigInt(bs));
BOOST_CHECK(BigInt(as) < bs);
BOOST_CHECK(BigInt(as) <= bs);
BOOST_CHECK(!(BigInt(as) == bs));
BOOST_CHECK(!(BigInt(as) > bs));
BOOST_CHECK(!(BigInt(as) >= bs));
BOOST_CHECK(BigInt(as) != bs);
BOOST_CHECK(!(BigInt(bs) < BigInt(as)));
BOOST_CHECK(!(BigInt(bs) <= BigInt(as)));
BOOST_CHECK(!(BigInt(bs) == BigInt(as)));
BOOST_CHECK(BigInt(bs) > BigInt(as));
BOOST_CHECK(BigInt(bs) >= BigInt(as));
BOOST_CHECK(BigInt(bs) != BigInt(as));
BOOST_CHECK(!(BigInt(bs) < as));
BOOST_CHECK(!(BigInt(bs) <= as));
BOOST_CHECK(!(BigInt(bs) == as));
BOOST_CHECK(BigInt(bs) > as);
BOOST_CHECK(BigInt(bs) >= as);
BOOST_CHECK(BigInt(bs) != as);
} else if (as == bs) {
BOOST_CHECK(!(BigInt(as) < bs));
BOOST_CHECK(BigInt(as) <= bs);
BOOST_CHECK(BigInt(as) == bs);
BOOST_CHECK(!(BigInt(as) > bs));
BOOST_CHECK(BigInt(as) >= bs);
BOOST_CHECK(!(BigInt(as) != bs));
BOOST_CHECK_EQUAL(BigInt(as).compare(bs), 0);
BOOST_CHECK_EQUAL(BigInt(bs).compare(as), 0);
BOOST_CHECK_EQUAL(BigInt(as).compare(BigInt(bs)), 0);
BOOST_CHECK_EQUAL(BigInt(bs).compare(BigInt(as)), 0);
BOOST_CHECK(!(BigInt(as) < BigInt(bs)));
BOOST_CHECK(BigInt(as) <= BigInt(bs));
BOOST_CHECK(!(BigInt(as) > BigInt(bs)));
BOOST_CHECK(BigInt(as) >= BigInt(bs));
BOOST_CHECK(BigInt(as) == BigInt(bs));
BOOST_CHECK(!(BigInt(as) != BigInt(bs)));
BOOST_CHECK(!(BigInt(as) < bs));
BOOST_CHECK(BigInt(as) <= bs);
BOOST_CHECK(!(BigInt(as) > bs));
BOOST_CHECK(BigInt(as) >= bs);
BOOST_CHECK(BigInt(as) == bs);
BOOST_CHECK(!(BigInt(as) != bs));
BOOST_CHECK(!(BigInt(bs) < BigInt(as)));
BOOST_CHECK(BigInt(bs) <= BigInt(as));
BOOST_CHECK(!(BigInt(bs) > BigInt(as)));
BOOST_CHECK(BigInt(bs) >= BigInt(as));
BOOST_CHECK(BigInt(bs) == BigInt(as));