-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathint128_tests.hpp
2335 lines (2080 loc) · 78.1 KB
/
int128_tests.hpp
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 © 2020-2021 CJM Screws, LLC
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// CJM Screws, LLC is a Maryland Limited Liability Company.
// No copyright claimed to unmodified original work of others.
// The original, unmodified work of others, to the extent included in this library,
// is licensed to you under the same terms under which it was licensed to CJM Screws, LLC.
// For information about copyright and licensing of the original work of others,
// see Notices file in cjm/ folder.
#ifndef INT128_TESTS_HPP_
#define INT128_TESTS_HPP_
#include <iostream>
#include <cjm/numerics/numerics.hpp>
#include <cjm/numerics/uint128.hpp>
#include <cjm/numerics/cjm_numeric_concepts.hpp>
#include <cjm/string/cjm_string.hpp>
#include "testing.hpp"
#include <iostream>
#include <iomanip>
#include <string_view>
#include <array>
#include <string>
#include <sstream>
#include <limits>
#include <type_traits>
#include <cstdint>
#include <boost/io/ios_state.hpp>
#include <exception>
#include <stdexcept>
#include <utility>
#include <boost/multiprecision/cpp_int.hpp>
#include <concepts>
#include <cstdint>
#include <random>
#include <algorithm>
#include <vector>
#include<functional>
#include <memory>
#include <optional>
#include <chrono>
#include <date/date.h>
#include <filesystem>
#include <tuple>
#include <span>
#include <unordered_set>
#include <cjm/string/istream_utils.hpp>
#include "int128_test_switches.hpp"
#include <absl/numeric/int128.h>
#include <cjm/numerics/fixed_uint_container_math.hpp>
#include "umult.hpp"
namespace cjm::uint128_tests::generator
{
class rgen;
}
namespace cjm::uint128_tests
{
using ctrl_uint128_t = boost::multiprecision::uint128_t;
using alt_ctrl_uint128_t = absl::uint128;
using uint128_t = numerics::uint128;
using namespace numerics::uint128_literals;
using namespace numerics::literals;
using namespace std::string_literals;
using namespace std::string_view_literals;
using testing::cjm_assert;
using testing::cjm_deny;
using std::cout;
constexpr auto newl = '\n';
using cout_saver = boost::io::ios_flags_saver;
using switches::test_switch;
using switches::test_mode;
enum class unary_op : unsigned int;
enum class binary_op : unsigned int;
constexpr size_t binary_op_count = 11;
constexpr size_t unary_op_count = 9;
class bad_binary_op;
class bad_unary_op;
class divmod_fail_match;
template<typename Invocable>
concept invocable = requires (Invocable i)
{
{i()} -> std::convertible_to<void>;
};
template<typename TestTypeUi, typename ControlTypeUi>
concept test_uint_and_control_set =
cjm::numerics::concepts::cjm_unsigned_integer<TestTypeUi> &&
cjm::numerics::concepts::unsigned_integer<ControlTypeUi> &&
std::numeric_limits<TestTypeUi>::digits ==
std::numeric_limits<ControlTypeUi>::digits;
template<typename TestType = uint128_t , typename ControlType = ctrl_uint128_t>
requires (test_uint_and_control_set<TestType, ControlType>)
struct binary_operation;
template<typename TestType, typename ControlType>
requires (test_uint_and_control_set<TestType, ControlType>)
struct unary_operation;
using binary_op_u128_t = binary_operation<uint128_t, ctrl_uint128_t>;
using binary_op_u128_vect_t = std::vector<binary_op_u128_t>;
using unary_op_u128_t = unary_operation<uint128_t, ctrl_uint128_t>;
using unary_op_u128_vect_t = std::vector<unary_op_u128_t>;
/// <summary>
/// hascorrectresult() must be true or throws
/// must be divide or modulus or throws
/// </summary>
/// <param name="op">the result</param>
void validate_divmod_op(const binary_op_u128_t& op);
template<invocable Invocable>
void execute_test(Invocable test, std::string_view test_name);
alt_ctrl_uint128_t to_alt_ctrl(uint128_t convert) noexcept;
uint128_t to_test(alt_ctrl_uint128_t convert) noexcept;
ctrl_uint128_t to_ctrl(uint128_t convert);
uint128_t to_test(const ctrl_uint128_t& convert);
void print_environ_data();
void print_sizes();
void print_alignments();
void print_floating_point_info();
void execute_uint128_tests();
constexpr size_t pow_2_arr_size = 63;
constexpr std::array<std::uint64_t, pow_2_arr_size> get_pow2_arr();
constexpr std::array<int, pow_2_arr_size> get_pow2_res_arr();
[[maybe_unused]] void save_random_unary_ops_to_file(std::filesystem::path target);
[[maybe_unused]] void save_random_binary_ops_to_file(std::filesystem::path target);
void execute_test_switch(const test_switch& test_switch);
void execute_unary_test_file(const std::filesystem::path& file);
void execute_binary_test_file(const std::filesystem::path& file);
void run_test_application(std::span<test_switch> switches);
void execute_binary_operation_rt_ser_tests();
void execute_trim_tests();
void execute_ascii_char_interconversions();
void execute_div_mod_zero_dividend_nonzero_divisor_tests();
void execute_div_mod_by_zero_tests();
void execute_basic_test_one();
void execute_string_parse_test();
void execute_basic_multiplication_test();
void test_fls();
void print_uint128_eval_mode();
void print_constexpr_bitcast_available();
void print_cpp20_bitops_available();
void print_builtin_uint128_data_if_present();
void print_whether_has_consteval();
void test_interconversion(const ctrl_uint128_t& control, uint128_t test);
void execute_builtin_u128fls_test_if_avail();
void execute_first_bin_op_test();
void execute_gen_comp_ops_test();
void execute_stream_insert_bin_op_test();
void execute_print_generated_filename_test();
void execute_generate_addition_ops_rt_ser_deser_test();
void execute_addition_tests();
void execute_subtraction_tests();
void execute_shift_tests();
void execute_bw_tests();
void execute_comparison_tests();
void execute_multiplication_tests();
void execute_failing_division_test_1();
void execute_failing_division_test_2();
void execute_failing_modulus_test_1();
void execute_division_modulus_tests();
void execute_unary_op_code_rt_serialization_tests();
void execute_unary_operation_rt_serialization_tests();
void execute_unary_operation_vec_rt_serialization_tests();
void execute_unary_op_basic_test();
void execute_parse_file_test(std::string_view path, size_t expected_ops);
void execute_unary_op_post_stat_assert_test();
void execute_test_convert_to_float();
void execute_test_convert_to_double();
void execute_test_convert_to_long_double();
void execute_throwing_float_conversion_test();
void execute_safe_float_conversions_test();
void execute_controlled_from_float_conversion_test();
void execute_controlled_float_rt_conversion_test();
void execute_hash_dx();
void execute_issue_10_strm_insrt_test();
void execute_issue_10_showbase_test();
void execute_unary_op_pre_inc_test();
void execute_unary_op_post_inc_test();
void execute_unary_op_pre_dec_test();
void execute_unary_op_post_dec_test();
void execute_unary_op_unary_plus_test();
void execute_unary_op_unary_minus_test();
void execute_unary_op_bitwise_not_test();
void execute_unary_op_bool_cast_test();
void execute_unary_op_logical_negation_test();
void execute_builtin_add_with_carry_test();
void execute_basic_u128_adc_test();
void execute_basic_u128_sbb_test();
void execute_builtin_sub_with_borrow_test();
void execute_umult_spec_tests();
void execute_uintcontainer_adc_tests();
void execute_issue27_bug_test();
void execute_literal_test();
std::basic_ostream<char>& operator<<(std::basic_ostream<char>&, lit_type v);
std::pair<ctrl_uint128_t, std::string> create_random_dec_n_digits_long(size_t decimal_digits, generator::rgen& gen);
std::pair<ctrl_uint128_t, std::string> create_random_hex_n_digits_long(size_t hex_digits, generator::rgen& gen);
std::string generate_literal_test(lit_type literal_type, size_t num_digits, generator::rgen& gen);
std::vector<std::string> generate_literal_tests();
void generate_then_print_literal_tests();
[[maybe_unused]] void print_n_static_assertions(const binary_op_u128_vect_t& op_vec, size_t n);
[[maybe_unused]] void print_n_static_assertions(const unary_op_u128_vect_t& op_vec, size_t n);
constexpr auto base_un_op_filename = "unary_ops"sv;
constexpr auto base_bin_op_filename = "binary_ops"sv;
constexpr auto op_failed_test_tag = "failed_test"sv;
constexpr auto op_generated_tag = "generated"sv;
constexpr auto op_extension = "txt"sv;
std::filesystem::path create_generated_op_filename(binary_op op);
std::filesystem::path create_failing_op_pathname(binary_op op);
std::filesystem::path create_generated_op_filename(unary_op op);
std::filesystem::path create_failing_op_pathname(unary_op op);
template<numerics::concepts::character Char>
std::basic_ostream<Char, std::char_traits<Char>>& operator<<(std::basic_ostream<Char,
std::char_traits<Char>>&os, const binary_op_u128_t& op);
template<numerics::concepts::character Char>
std::basic_istream<Char, std::char_traits<Char>>& operator>>(std::basic_istream<Char,
std::char_traits<Char>>&is, binary_op_u128_t& op);
template<numerics::concepts::character Char>
std::basic_ostream<Char, std::char_traits<Char>>& operator<<(std::basic_ostream<Char,
std::char_traits<Char>>&os, const binary_op_u128_vect_t& op);
template<numerics::concepts::character Char>
std::basic_istream<Char, std::char_traits<Char>>& operator>>(std::basic_istream<Char,
std::char_traits<Char>>&is, binary_op_u128_vect_t& op);
template<numerics::concepts::character Char>
std::basic_ostream<Char, std::char_traits<Char>>& operator<<(std::basic_ostream<Char,
std::char_traits<Char>>&os, const unary_op_u128_t& op);
template<numerics::concepts::character Char>
std::basic_istream<Char, std::char_traits<Char>>& operator>>(std::basic_istream<Char,
std::char_traits<Char>>&is, unary_op_u128_t& op);
template<numerics::concepts::character Char>
std::basic_ostream<Char, std::char_traits<Char>>& operator<<(std::basic_ostream<Char,
std::char_traits<Char>>&os, const unary_op_u128_vect_t& op);
template<numerics::concepts::character Char>
std::basic_istream<Char, std::char_traits<Char>>& operator>>(std::basic_istream<Char,
std::char_traits<Char>>&is, unary_op_u128_vect_t& op);
template<numerics::concepts::character Char>
binary_op_u128_t parse(std::basic_string_view<Char> sv);
template<numerics::concepts::character Char>
unary_op_u128_t parse_unary(std::basic_string_view<Char> sv);
unary_op_u128_vect_t generate_random_standard_test_ops();
unary_op_u128_vect_t generate_post_inc_dec_ops(size_t num_ops_each_type, bool include_standard_tests);
unary_op_u128_vect_t generate_specified_un_ops(unary_op op_code, size_t num_rnd_ops,
bool include_standard_tests);
binary_op_u128_vect_t generate_easy_ops(size_t num_ops, binary_op op, bool include_standard_tests);
binary_op_u128_vect_t generate_shift_ops(size_t num_ops, bool include_standard_tests);
binary_op_u128_vect_t generate_bw_ops(size_t num_ops, bool include_standard_tests);
binary_op_u128_vect_t generate_mult_ops(size_t num_each_range, bool include_standard_tests);
binary_op_u128_vect_t generate_divmod_ops(size_t num_each_range, bool include_standard_tests);
void insert_standard_divmod_ops(binary_op_u128_vect_t& op_vec);
void test_binary_operation(binary_op_u128_t& op, std::string_view test_name);
void test_unary_operation(unary_op_u128_t& op, std::string_view test_name);
namespace u128_testing_constant_providers
{
namespace concepts
{
//Must be a cjm_unsigned_integer or a built-in, unsigned integer that is no larger than 64 bits.
template<typename T>
concept supports_testing_constant_provider =
//EITHER: (integer AND unsigned AND size <= 64)
(std::numeric_limits<T>::is_integer && !std::numeric_limits<T>::is_signed && std::is_fundamental_v<T> &&
sizeof(T) <= sizeof(std::uint64_t)) || cjm::numerics::concepts::cjm_unsigned_integer<T>;
}
template<concepts::supports_testing_constant_provider T>
struct testing_constant_provider;
template<>
struct testing_constant_provider<std::uint8_t>
{
using full_uint_t = std::uint8_t;
using half_uint_t = std::uint8_t;
static constexpr full_uint_t maximum = std::numeric_limits<full_uint_t>::max();
static constexpr full_uint_t max_less_one = maximum - full_uint_t{1};
static constexpr full_uint_t zero = std::numeric_limits<full_uint_t>::min();
static constexpr full_uint_t one = zero + full_uint_t{1};
static constexpr full_uint_t maximum_half = std::numeric_limits<half_uint_t>::max() >> (std::numeric_limits<half_uint_t>::digits / 2);
static constexpr full_uint_t maximum_half_less_one = maximum_half - 1;
static constexpr full_uint_t maximum_half_plus_one = maximum_half + 1;
static constexpr std::array<full_uint_t, 7> all_values =
{ maximum, max_less_one, zero,
one, maximum_half, max_less_one,
maximum_half_plus_one };
using half_provider_t = testing_constant_provider<half_uint_t>;
};
template<>
struct testing_constant_provider<std::uint16_t>
{
using full_uint_t = std::uint16_t;
using half_uint_t = std::uint8_t;
static constexpr full_uint_t maximum = std::numeric_limits<full_uint_t>::max();
static constexpr full_uint_t max_less_one = maximum - full_uint_t{1};
static constexpr full_uint_t zero = std::numeric_limits<full_uint_t>::min();
static constexpr full_uint_t one = zero + full_uint_t{1};
static constexpr full_uint_t maximum_half = std::numeric_limits<half_uint_t>::max();
static constexpr full_uint_t maximum_half_less_one = maximum_half - 1;
static constexpr full_uint_t maximum_half_plus_one = maximum_half + 1;
static constexpr std::array<full_uint_t, 7> all_values =
{ maximum, max_less_one, zero,
one, maximum_half, max_less_one,
maximum_half_plus_one };
using half_provider_t = testing_constant_provider<half_uint_t>;
};
template<>
struct testing_constant_provider<std::uint32_t>
{
using full_uint_t = std::uint32_t;
using half_uint_t = std::uint16_t;
static constexpr full_uint_t maximum = std::numeric_limits<full_uint_t>::max();
static constexpr full_uint_t max_less_one = maximum - full_uint_t{1};
static constexpr full_uint_t zero = std::numeric_limits<full_uint_t>::min();
static constexpr full_uint_t one = zero + full_uint_t{1};
static constexpr full_uint_t maximum_half = std::numeric_limits<half_uint_t>::max();
static constexpr full_uint_t maximum_half_less_one = maximum_half - 1;
static constexpr full_uint_t maximum_half_plus_one = maximum_half + 1;
static constexpr std::array<full_uint_t, 7> all_values =
{ maximum, max_less_one, zero,
one, maximum_half, max_less_one,
maximum_half_plus_one };
using half_provider_t = testing_constant_provider<half_uint_t>;
};
template<>
struct testing_constant_provider<std::uint64_t> final
{
using full_uint_t = std::uint64_t;
using half_uint_t = std::uint32_t;
static constexpr full_uint_t maximum = std::numeric_limits<full_uint_t>::max();
static constexpr full_uint_t max_less_one = maximum - full_uint_t{1};
static constexpr full_uint_t zero = std::numeric_limits<full_uint_t>::min();
static constexpr full_uint_t one = zero + full_uint_t{1};
static constexpr full_uint_t maximum_half = std::numeric_limits<half_uint_t>::max();
static constexpr full_uint_t maximum_half_less_one = maximum_half - 1;
static constexpr full_uint_t maximum_half_plus_one = maximum_half + 1;
static constexpr std::array<full_uint_t, 7> all_values =
{ maximum, max_less_one, zero,
one, maximum_half, max_less_one,
maximum_half_plus_one };
using half_provider_t = testing_constant_provider<half_uint_t>;
};
template<concepts::supports_testing_constant_provider T>
struct testing_constant_provider final
{
using full_uint_t = std::remove_const_t<T>;
using half_uint_t = typename T::int_part;
static constexpr size_t full_digits = std::numeric_limits<full_uint_t>::digits;
static constexpr size_t half_digits = std::numeric_limits<half_uint_t>::digits;
static constexpr full_uint_t maximum = std::numeric_limits<full_uint_t>::max();
static constexpr full_uint_t max_less_one = maximum - full_uint_t{1};
static constexpr full_uint_t zero = std::numeric_limits<full_uint_t>::min();
static constexpr full_uint_t one = zero + full_uint_t{1};
static constexpr full_uint_t signed_version_min_bit_pattern_full = 1_u128 << (full_digits - 1);
static constexpr full_uint_t signed_version_min_bit_pattern_half = half_uint_t{ 1 } << (half_digits - 1);
static constexpr full_uint_t maximum_half = std::numeric_limits<half_uint_t>::max();
static constexpr full_uint_t maximum_half_less_one = maximum_half -1;
static constexpr full_uint_t maximum_half_plus_one = maximum_half + 1;
static constexpr std::array<full_uint_t, 9> all_values =
{
maximum, max_less_one, zero,
one, signed_version_min_bit_pattern_full,
signed_version_min_bit_pattern_half, maximum_half,
max_less_one, maximum_half_plus_one
};
using half_provider_t = testing_constant_provider<half_uint_t>;
};
}
template<numerics::concepts::unsigned_integer UnsignedInt>
constexpr std::pair<UnsignedInt, UnsignedInt> test_post_increment(UnsignedInt inc_me) noexcept
{
auto first_res = inc_me++;
return std::make_pair(first_res, inc_me);
}
template<numerics::concepts::unsigned_integer UnsignedInt>
constexpr std::pair<UnsignedInt, UnsignedInt> test_post_decrement(UnsignedInt dec_me) noexcept
{
auto first_res = dec_me--;
return std::make_pair(first_res, dec_me);
}
template<invocable Invocable>
void execute_test(Invocable test, std::string_view test_name)
{
cout_saver o_saver{ cout };
cout_saver e_saver{ std::cerr };
cout << "Beginning test: [" << test_name << "]:" << newl;
try
{
test();
cout << "Test [" << test_name << "] PASSED" << newl << newl;
}
catch (const std::exception& ex)
{
std::stringstream ss;
ss << "Test [" << test_name << "] failed with exception message: [" << ex.what() << "]." << newl;
std::cerr << ss.str();
throw;
}
}
[[maybe_unused]] void compile_time_addition_test() noexcept;
[[maybe_unused]] void compile_time_shift_test() noexcept;
[[maybe_unused]] void compile_time_bw_test() noexcept;
[[maybe_unused]] void compile_time_subtraction_test() noexcept;
[[maybe_unused]] void compile_time_comparison_test() noexcept;
[[maybe_unused]] void compile_time_multiplication_test() noexcept;
[[maybe_unused]] void compile_time_divmod_test() noexcept;
[[maybe_unused]] void compile_time_postfix_un_op_test() noexcept;
[[maybe_unused]] void compile_time_unary_op_pre_inc_test() noexcept;
[[maybe_unused]] void compile_time_unary_op_pre_dec_test() noexcept;
[[maybe_unused]] void compile_time_unary_op_unary_plus_test() noexcept;
[[maybe_unused]] void compile_time_unary_op_unary_minus_test() noexcept;
[[maybe_unused]] void compile_time_unary_op_bitwise_not_test() noexcept;
[[maybe_unused]] void compile_time_unary_op_bool_cast_test() noexcept;
[[maybe_unused]] void compile_time_unary_op_logical_negation_test() noexcept;
}
namespace std
{
template<typename TestType, typename ControlType>
requires (cjm::uint128_tests::test_uint_and_control_set<TestType, ControlType>)
struct hash<cjm::uint128_tests::binary_operation<TestType, ControlType>>
{
std::size_t operator()(const cjm::uint128_tests::binary_operation<TestType, ControlType>& hash_me) noexcept;
};
template<typename TestType, typename ControlType>
requires (cjm::uint128_tests::test_uint_and_control_set<TestType, ControlType>)
struct hash<cjm::uint128_tests::unary_operation<TestType, ControlType>>
{
std::size_t operator()(const cjm::uint128_tests::unary_operation<TestType, ControlType>& hash_me) noexcept;
};
}
namespace cjm::uint128_tests
{
enum class binary_op : unsigned int
{ //IF YOU EDIT THIS, MAKE SURE YOU EDIT THE CONSTANTS BELOW AND KEEP RELATED GROUPS CONSECUTIVE!
left_shift = 0,
right_shift = 1,
bw_and = 2,
bw_or = 3,
bw_xor = 4,
divide = 5,
modulus = 6,
add = 7,
subtract = 8,
multiply = 9,
compare = 10
};
enum class unary_op : unsigned int
{
pre_increment = 0,
pre_decrement = 1,
post_increment = 2,
post_decrement = 3,
unary_plus = 4,
unary_minus=5,
bitwise_not = 6,
bool_cast = 7,
logical_negation = 8,
};
static_assert(!cjm::numerics::is_windows || std::is_unsigned_v<wchar_t>);
constexpr binary_op first_op = binary_op::left_shift;
constexpr binary_op last_op = binary_op::compare;
constexpr binary_op first_shift_op = binary_op::left_shift;
constexpr binary_op last_shift_op = binary_op::right_shift;
constexpr binary_op first_bw_op = binary_op::bw_and;
constexpr binary_op last_bw_op = binary_op::bw_xor;
constexpr binary_op first_divmod_op = binary_op::divide;
constexpr binary_op last_divmod_op = binary_op::modulus;
constexpr binary_op first_add_sub_mul_op = binary_op::add;
constexpr binary_op last_add_sub_mul_op = binary_op::multiply;
constexpr unary_op first_unary_op = unary_op::pre_increment;
constexpr unary_op last_unary_op = unary_op::logical_negation;
constexpr unary_op first_inc_dec_op = unary_op::pre_increment;
constexpr unary_op last_inc_dec_op = unary_op::post_decrement;
constexpr unary_op first_plus_minus_op = unary_op::unary_plus;
constexpr unary_op last_plus_minus_op = unary_op::unary_minus;
constexpr unary_op first_boolean_op = unary_op::bool_cast;
constexpr unary_op last_boolean_op = unary_op::logical_negation;
constexpr auto un_op_plus_plus = "++"sv;
constexpr auto un_op_minus_minus = "--"sv;
constexpr auto un_op_plus = "+"sv;
constexpr auto un_op_minus = "-"sv;
constexpr auto un_op_bw_not = "~"sv;
constexpr auto un_op_static_cast_bool = "static_cast<bool>"sv;
constexpr auto un_op_open_parens = "("sv;
constexpr auto un_op_close_parens = ")"sv;
constexpr auto un_op_logical_negate = "!"sv;
constexpr auto un_op_semicolon = ";"sv;
constexpr auto un_op_equals = "=="sv;
constexpr auto un_on_not_equal = "!="sv;
constexpr auto un_op_u128_literal_suffix = "_u128"sv;
constexpr auto un_op_name_lookup = std::array<sv_t, unary_op_count>
{
"PreIncrement"sv, "PreDecrement"sv, "PostIncrement"sv, "PostDecrement"sv,
"UnaryPlus"sv, "UnaryMinus"sv, "BitwiseNot"sv, "BoolCast"sv, "LogicalNegation"sv
};
constexpr auto un_op_name_lookup_wide = std::array<wsv_t, unary_op_count>
{
L"PreIncrement"sv, L"PreDecrement"sv, L"PostIncrement"sv, L"PostDecrement"sv,
L"UnaryPlus"sv, L"UnaryMinus"sv, L"BitwiseNot"sv, L"BoolCast"sv, L"LogicalNegation"sv
};
constexpr auto un_op_name_lookup_u8 = std::array<u8sv_t, unary_op_count>
{
u8"PreIncrement"sv, u8"PreDecrement"sv, u8"PostIncrement"sv, u8"PostDecrement"sv,
u8"UnaryPlus"sv, u8"UnaryMinus"sv, u8"BitwiseNot"sv, u8"BoolCast"sv, u8"LogicalNegation"sv
};
constexpr auto un_op_name_lookup_u16 = std::array<u16sv_t, unary_op_count>
{
u"PreIncrement"sv, u"PreDecrement"sv, u"PostIncrement"sv, u"PostDecrement"sv,
u"UnaryPlus"sv, u"UnaryMinus"sv, u"BitwiseNot"sv, u"BoolCast"sv, u"LogicalNegation"sv
};
constexpr auto un_op_name_lookup_u32 = std::array<u32sv_t, unary_op_count>
{
U"PreIncrement"sv, U"PreDecrement"sv, U"PostIncrement"sv, U"PostDecrement"sv,
U"UnaryPlus"sv, U"UnaryMinus"sv, U"BitwiseNot"sv, U"BoolCast"sv, U"LogicalNegation"sv
};
constexpr auto un_op_symbol_lookup = std::array<sv_t, unary_op_count>
{
"++x"sv, "--x"sv, "x++"sv, "x--"sv,
"+x"sv, "-x"sv, "~x"sv, "static_cast<bool>(x)"sv, "!x"sv
};
constexpr auto un_op_symbol_lookup_wide = std::array<wsv_t, unary_op_count>
{
L"++x"sv, L"--x"sv, L"x++"sv, L"x--"sv,
L"+x"sv, L"-x"sv, L"~x"sv, L"static_cast<bool>(x)"sv, L"!x"sv
};
constexpr auto un_op_symbol_lookup_u8 = std::array<u8sv_t, unary_op_count>
{
u8"++x"sv, u8"--x"sv, u8"x++"sv, u8"x--"sv,
u8"+x"sv, u8"-x"sv, u8"~x"sv, u8"static_cast<bool>(x)"sv, u8"!x"sv
};
constexpr auto un_op_symbol_lookup_u16 = std::array<u16sv_t, unary_op_count>
{
u"++x"sv, u"--x"sv, u"x++"sv, u"x--"sv,
u"+x"sv, u"-x"sv, u"~x"sv, u"static_cast<bool>(x)"sv, u"!x"sv
};
constexpr auto un_op_symbol_lookup_u32 = std::array<u32sv_t, unary_op_count>
{
U"++x"sv, U"--x"sv, U"x++"sv, U"x--"sv,
U"+x"sv, U"-x"sv, U"~x"sv, U"static_cast<bool>(x)"sv, U"!x"sv
};
constexpr auto op_name_lookup = std::array<sv_t, binary_op_count>{
"LeftShift"sv, "RightShift"sv, "And"sv, "Or"sv,
"Xor"sv, "Divide"sv, "Modulus"sv, "Add"sv,
"Subtract"sv, "Multiply"sv, "Compare"sv};
constexpr auto op_symbol_lookup = std::array<sv_t, binary_op_count>{
"<<"sv, ">>"sv, "&"sv, "|"sv,
"^"sv, "/"sv, "%"sv, "+"sv,
"-"sv, "*"sv, "<=>"sv};
constexpr auto op_name_lookup_wide = std::array<wsv_t, binary_op_count>{
L"LeftShift"sv, L"RightShift"sv, L"And"sv, L"Or"sv,
L"Xor"sv, L"Divide"sv, L"Modulus"sv, L"Add"sv,
L"Subtract"sv, L"Multiply"sv, L"Compare"sv};
constexpr auto op_symbol_lookup_wide = std::array<wsv_t, binary_op_count>{
L"<<"sv, L">>"sv, L"&"sv, L"|"sv,
L"^"sv, L"/"sv, L"%"sv, L"+"sv,
L"-"sv, L"*"sv, L"<=>"sv};
constexpr auto op_name_lookup_u8 = std::array<u8sv_t, binary_op_count>{
u8"LeftShift"sv, u8"RightShift"sv, u8"And"sv, u8"Or"sv,
u8"Xor"sv, u8"Divide"sv, u8"Modulus"sv, u8"Add"sv,
u8"Subtract"sv, u8"Multiply"sv, u8"Compare"sv};
constexpr auto op_symbol_lookup_u8 = std::array<u8sv_t, binary_op_count>{
u8"<<"sv, u8">>"sv, u8"&"sv, u8"|"sv,
u8"^"sv, u8"/"sv, u8"%"sv, u8"+"sv,
u8"-"sv, u8"*"sv, u8"<=>"sv};
constexpr auto op_name_lookup_u16 = std::array<u16sv_t, binary_op_count>{
u"LeftShift"sv, u"RightShift"sv, u"And"sv, u"Or"sv,
u"Xor"sv, u"Divide"sv, u"Modulus"sv, u"Add"sv,
u"Subtract"sv, u"Multiply"sv, u"Compare"sv};
constexpr auto op_symbol_lookup_u16 = std::array<u16sv_t, binary_op_count>{
u"<<"sv, u">>"sv, u"&"sv, u"|"sv,
u"^"sv, u"/"sv, u"%"sv, u"+"sv,
u"-"sv, u"*"sv, u"<=>"sv};
constexpr auto op_name_lookup_u32 = std::array<u32sv_t, binary_op_count>{
U"LeftShift"sv, U"RightShift"sv, U"And"sv, U"Or"sv,
U"Xor"sv, U"Divide"sv, U"Modulus"sv, U"Add"sv,
U"Subtract"sv, U"Multiply"sv, U"Compare"sv};
constexpr auto op_symbol_lookup_u32 = std::array<u32sv_t, binary_op_count>{
U"<<"sv, U">>"sv, U"&"sv, U"|"sv,
U"^"sv, U"/"sv, U"%"sv, U"+"sv,
U"-"sv, U"*"sv, U"<=>"sv};
constexpr sv_t get_op_text(binary_op op);
constexpr wsv_t get_op_wtext(binary_op op);
constexpr u8sv_t get_op_u8text(binary_op op);
constexpr u16sv_t get_op_u16text(binary_op op);
constexpr u32sv_t get_op_u32text(binary_op op);
constexpr sv_t get_un_op_text(unary_op op);
constexpr wsv_t get_un_op_wtext(unary_op op);
constexpr u8sv_t get_un_op_u8text(unary_op op);
constexpr u16sv_t get_un_op_u16text(unary_op op);
constexpr u32sv_t get_un_op_u32text(unary_op op);
constexpr sv_t get_un_op_symbol_n(unary_op op);
constexpr wsv_t get_un_op_symbol_w(unary_op op);
constexpr u8sv_t get_un_op_symbol_u8(unary_op op);
constexpr u16sv_t get_un_op_symbol_u16(unary_op op);
constexpr u32sv_t get_un_op_symbol_u32(unary_op op);
constexpr sv_t get_op_symbol_n(binary_op op);
constexpr wsv_t get_op_symbol_w(binary_op op);
constexpr u8sv_t get_op_symbol_u8(binary_op op);
constexpr u16sv_t get_op_symbol_u16(binary_op op);
constexpr u32sv_t get_op_symbol_u32(binary_op op);
template<numerics::concepts::character Char>
constexpr auto get_op_symbol(binary_op op) -> std::basic_string_view<Char>;
template<>
constexpr auto get_op_symbol<char>(binary_op op) -> std::basic_string_view<char>
{
return get_op_symbol_n(op);
}
template<>
constexpr auto get_op_symbol<wchar_t>(binary_op op) -> std::basic_string_view<wchar_t>
{
return get_op_symbol_w(op);
}
template<>
constexpr auto get_op_symbol<char8_t>(binary_op op) -> std::basic_string_view<char8_t>
{
return get_op_symbol_u8(op);
}
template<>
constexpr auto get_op_symbol<char16_t>(binary_op op) -> std::basic_string_view<char16_t>
{
return get_op_symbol_u16(op);
}
template<>
constexpr auto get_op_symbol<char32_t>(binary_op op) -> std::basic_string_view<char32_t>
{
return get_op_symbol_u32(op);
}
template<numerics::concepts::character Char>
constexpr auto get_un_op_symbol(unary_op op) -> std::basic_string_view<Char>;
template<>
constexpr auto get_un_op_symbol<char>(unary_op op) -> std::basic_string_view<char>
{
return get_un_op_symbol_n(op);
}
template<>
constexpr auto get_un_op_symbol<wchar_t>(unary_op op) -> std::basic_string_view<wchar_t>
{
return get_un_op_symbol_w(op);
}
template<>
constexpr auto get_un_op_symbol<char8_t>(unary_op op) -> std::basic_string_view<char8_t>
{
return get_un_op_symbol_u8(op);
}
template<>
constexpr auto get_un_op_symbol<char16_t>(unary_op op) -> std::basic_string_view<char16_t>
{
return get_un_op_symbol_u16(op);
}
template<>
constexpr auto get_un_op_symbol<char32_t>(unary_op op) -> std::basic_string_view<char32_t>
{
return get_un_op_symbol_u32(op);
}
unary_op parse_unary_op_symbol(sv_t text);
unary_op parse_unary_op_symbol(wsv_t text);
unary_op parse_unary_op_symbol(u8sv_t text);
unary_op parse_unary_op_symbol(u16sv_t text);
unary_op parse_unary_op_symbol(u32sv_t text);
binary_op parse_binary_op_symbol(sv_t text);
binary_op parse_binary_op_symbol(wsv_t text);
binary_op parse_binary_op_symbol(u8sv_t text);
binary_op parse_binary_op_symbol(u16sv_t text);
binary_op parse_binary_op_symbol(u32sv_t text);
template<numerics::concepts::character Char>
std::basic_ostream<Char, std::char_traits<Char>>& operator<<(std::basic_ostream<Char, std::char_traits<Char>>& os, binary_op op)
{
std::basic_string_view<Char> sv = get_op_symbol<std::remove_cvref_t<std::remove_const_t<Char>>>(op);
os << sv;
return os;
}
template<numerics::concepts::character Char>
std::basic_istream<Char, std::char_traits<Char>>& operator>>(std::basic_istream<Char,
std::char_traits<Char>>& is, binary_op& op)
{
using string_t = std::basic_string<Char>;
using lsv_t = std::basic_string_view<Char>;
string_t temp;
if constexpr (cjm::numerics::is_windows || !cjm::numerics::concepts::utf_character<Char>)
{
is >> temp;
}
else
{
Char c{};
do
{
if (is.good() && !is.eof() && is.peek() != std::char_traits<Char>::eof())
{
is.get(c);
if (!is.bad() && !is.fail())
{
if (!std::isspace<char>(convert_char<Char, char>(c), std::locale("")))
temp.push_back(c);
else
break;
}
}
else
break;
} while (!is.bad() && !is.fail() && !is.eof());
if (is.bad() || is.fail())
{
return is;
}
if (temp.empty())
{
is.setstate(std::ios_base::failbit);
return is;
}
}
op = parse_binary_op_symbol(lsv_t{temp});
return is;
}
template<numerics::concepts::character Char>
std::basic_ostream<Char, std::char_traits<Char>>& operator<<(std::basic_ostream<Char, std::char_traits<Char>>& os, unary_op op)
{
std::basic_string_view<Char> sv = get_un_op_symbol<std::remove_cvref_t<std::remove_const_t<Char>>>(op);
os << sv;
return os;
}
template<numerics::concepts::character Char>
std::basic_istream<Char, std::char_traits<Char>>& operator>>(std::basic_istream<Char,
std::char_traits<Char>>& is, unary_op& op)
{
using string_t = std::basic_string<Char>;
using lsv_t = std::basic_string_view<Char>;
string_t temp;
if constexpr (cjm::numerics::is_windows || !cjm::numerics::concepts::utf_character<Char>)
{
is >> temp;
}
else
{
Char c{};
do
{
if (is.good() && !is.eof() && is.peek() != std::char_traits<Char>::eof())
{
is.get(c);
if (!is.bad() && !is.fail())
{
if (!std::isspace<char>(convert_char<Char, char>(c), std::locale("")))
temp.push_back(c);
else
break;
}
}
else
break;
} while (!is.bad() && !is.fail() && !is.eof());
if (is.bad() || is.fail())
{
return is;
}
if (temp.empty())
{
is.setstate(std::ios_base::failbit);
return is;
}
}
op = parse_unary_op_symbol(lsv_t{temp});
return is;
}
class bad_binary_op final : public std::invalid_argument
{
public:
using stream_t = std::stringstream;
bad_binary_op(binary_op op)
: invalid_argument(create_message(op)) {}
using enum_type_t = std::underlying_type_t<binary_op>;
private:
static std::string create_message(binary_op op)
{
stream_t ss;
ss << "The value underlying the binary_op provided ("
<< static_cast<enum_type_t>(op)
<< "), is not a defined member of the binary_op enum class.";
return ss.str();
}
};
class bad_unary_op final : public std::invalid_argument
{
public:
using stream_t = std::stringstream;
bad_unary_op(unary_op op)
: invalid_argument(create_message(op)) {}
using enum_type_t = std::underlying_type_t<unary_op>;
private:
static std::string create_message(unary_op op)
{
stream_t ss;
ss << "The value underlying the unary_op provided ("
<< static_cast<enum_type_t>(op)
<< "), is not a defined member of the unary_op enum class.";
return ss.str();
}
};
template<typename TestType, typename ControlType>
requires (test_uint_and_control_set<TestType, ControlType>)
std::basic_ostream<char>& append_static_assertion(std::basic_ostream<char>& strm,
const binary_operation<TestType, ControlType>& bin_op);
template<typename TestType, typename ControlType>
requires (test_uint_and_control_set<TestType, ControlType>)
struct binary_operation final
{
using uint_test_t = std::remove_const_t<TestType>;
using uint_ctrl_t = std::remove_const_t<ControlType>;
using result_t = std::optional<std::pair<uint_test_t, uint_test_t>>;
using compound_result_t = std::optional<uint_test_t>;
[[nodiscard]] std::size_t hash_value() const noexcept
{
std::size_t seed = 0x1FBB0493;
boost::hash_combine(seed, static_cast<size_t>(m_op));
boost::hash_combine(seed, std::hash<uint_test_t>{}(m_lhs));
boost::hash_combine(seed, std::hash<uint_test_t>{}(m_rhs));
return seed;
}
friend bool operator<(const binary_operation& lhs, const binary_operation& rhs)
{
if (lhs.m_op < rhs.m_op)
return true;
if (rhs.m_op < lhs.m_op)
return false;
if (lhs.m_rhs < rhs.m_rhs)
return true;
if (rhs.m_rhs < lhs.m_rhs)
return false;
return lhs.m_lhs < rhs.m_lhs;
}
friend bool operator<=(const binary_operation& lhs, const binary_operation& rhs) { return !(rhs < lhs); }
friend bool operator>(const binary_operation& lhs, const binary_operation& rhs) { return rhs < lhs; }
friend bool operator>=(const binary_operation& lhs, const binary_operation& rhs) { return !(lhs < rhs); }
friend bool operator==(const binary_operation& lhs, const binary_operation& rhs)
{
return lhs.m_op == rhs.m_op
&& lhs.m_rhs == rhs.m_rhs
&& lhs.m_lhs == rhs.m_lhs;
}
friend bool operator!=(const binary_operation& lhs, const binary_operation& rhs) { return !(lhs == rhs); }
friend std::weak_ordering operator <=>(const binary_operation& lhs, const binary_operation& rhs) = default;
[[nodiscard]] const compound_result_t& compound_result() const noexcept {return m_compound_result;}
[[nodiscard]] binary_op op_code() const noexcept { return m_op; }
[[nodiscard]] const uint_test_t& left_operand() const noexcept { return m_lhs; }
[[nodiscard]] const uint_test_t& right_operand() const noexcept { return m_rhs; }
[[nodiscard]] const result_t& result() const noexcept { return m_result; }
[[nodiscard]] bool has_result() const noexcept { return m_result.has_value(); }
[[nodiscard]] bool has_correct_result() const
{
if (m_result.has_value())
{
if (m_result->first == m_result->second)
{
return m_op == binary_op::compare ||
(m_compound_result.has_value() &&
*m_compound_result == m_result->first);
}
return false;
}
return false;
}
binary_operation() noexcept : m_op(), m_lhs{ 0 }, m_rhs{ 0 }, m_result{}, m_compound_result{} {}
binary_operation(binary_op op, const uint_test_t& first_operand, const uint_test_t& second_operand, bool calculate_now)
: m_op(op), m_lhs(first_operand), m_rhs(second_operand), m_result{}, m_compound_result{}
{
validate(op, first_operand, second_operand);
if (calculate_now)
{
auto [result, comp_res] = perform_calculate_result(first_operand, second_operand, m_op);
m_result = result;
m_compound_result = comp_res;
}
}
binary_operation(binary_op op, const uint_test_t& first_operand,
const uint_test_t& second_operand) : m_op{ op }, m_lhs{ first_operand },
m_rhs{ second_operand }, m_result{}, m_compound_result{}
{
validate(op, first_operand, second_operand);
}
binary_operation(binary_op op, const uint_test_t& first_operand,
const uint_test_t& second_operand, const uint_test_t& test_result,
const uint_test_t& ctrl_result, std::optional<uint_test_t> compound_res)