-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
2336 lines (2038 loc) · 85.9 KB
/
main.cpp
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
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "list.hpp"
#include "map.hpp"
#include "queue.hpp"
#include "stack.hpp"
#include "utility.hpp"
#include "vector.hpp"
#ifndef NAMESPACE1
# define NAMESPACE1 ft
#endif
#ifndef NAMESPACE2
# define NAMESPACE2 std
#endif
static int g_errors;
template <class Container>
void print_container(const Container &c) {
typename Container::const_iterator it = c.begin();
typename Container::const_iterator ite = c.end();
std::cout << "Container content: ";
while (it != ite) {
std::cout << *it++ << ' ';
}
std::cout << '\n';
}
template <class T>
bool is_even(const T n) {
return n % 2;
}
template <class T>
bool both_numbers_are_even(const T a, const T b) {
return is_even(a) && is_even(b);
}
char generateRandomChar() {
return std::rand() % 128;
}
std::string generateRandomString() {
std::string str;
int length = std::rand() % 65;
str.reserve(length);
for (int i = 0; i < length; i++) {
str[i] = generateRandomChar();
}
return str;
}
template <class T, class Container1, class Container2>
void add_random_values_to_containers(
Container1 &c1, Container2 &c2, T (*generateRandomValue)(), int N) {
for (int i = 0; i < N; i++) {
T value = generateRandomValue();
c1.push_back(value);
c2.push_back(value);
}
}
template <class T, class Container1, class Container2>
void add_random_values_to_containers_adaptors(
Container1 &c1, Container2 &c2, T (*generateRandomValue)(), int N) {
for (int i = 0; i < N; i++) {
T value = generateRandomValue();
c1.push(value);
c2.push(value);
}
}
static void test_condition(const char *function_name, int line_number,
const char *message, bool condition) {
if (condition == false) {
std::cerr << "Error: " << function_name << ": line " << line_number
<< ": " << message << '\n';
g_errors++;
}
}
template <class T, class U>
static void test_values(const char *function_name, int line_number,
const char *message, const T &a, const U &b) {
if (a != b) {
std::cerr << "Error: " << function_name << ": line " << line_number
<< ": " << message << ": " << a << " != " << b << '\n';
g_errors++;
}
}
template <class T, class U>
static void test_values_message(const char *function_name, int line_number,
const char *message, const T &a, const U &b) {
if (a != b) {
std::cerr << "Error: " << function_name << ": line " << line_number
<< ": " << message << '\n';
g_errors++;
}
}
template <class Container1, class Container2>
static void test_equal_size(const Container1 &c1, const Container2 &c2,
const char *function_name, int line_number) {
test_values(function_name, line_number, "size", c1.size(), c2.size());
}
template <class Container1, class Container2>
static void test_equal_content(const Container1 &c1, const Container2 &c2,
const char *function_name, int line_number) {
typename Container1::const_iterator it1 = c1.begin();
typename Container2::const_iterator it2 = c2.begin();
while (it1 != c1.end() && it2 != c2.end()) {
test_values(function_name, line_number, "value", *it1++, *it2++);
}
test_values_message(
function_name, line_number, "it1 != c1.end()", it1, c1.end());
test_values_message(
function_name, line_number, "it2 != c2.end()", it2, c2.end());
}
template <class Container1, class Container2>
static void test_equal_container(const Container1 &c1, const Container2 &c2,
const char *function_name, int line_number) {
test_equal_size(c1, c2, function_name, line_number);
test_equal_content(c1, c2, function_name, line_number);
}
template <class Container1, class Container2>
static void test_container_default_constructor(const Container1 &,
const Container2 &, const char *function_name, int line_number) {
Container1 c1;
Container2 c2;
test_equal_container(c1, c2, function_name, line_number);
}
template <class Container1, class Container2>
static void test_container_adaptor_default_constructor(const Container1 &,
const Container2 &, const char *function_name, int line_number) {
Container1 c1;
Container2 c2;
test_equal_size(c1, c2, function_name, line_number);
}
template <class Value1, class Value2>
static void test_map_values(const char *message, const Value1 &value1,
const Value2 &value2, const char *function_name, int line_number) {
if (value1.first != value2.first) {
std::cerr << "Error: " << function_name << ": line " << line_number
<< ": " << message << ": " << value1.first
<< " != " << value2.first << '\n';
g_errors++;
}
if (value1.second != value2.second) {
std::cerr << "Error: " << function_name << ": line " << line_number
<< ": " << message << ": " << value1.second
<< " != " << value2.second << '\n';
g_errors++;
}
}
template <class Map1, class Map2>
static void test_equal_content_map(const Map1 &c1, const Map2 &c2,
const char *function_name, int line_number) {
typename Map1::const_iterator it1 = c1.begin();
typename Map2::const_iterator it2 = c2.begin();
while (it1 != c1.end() && it2 != c2.end()) {
test_map_values("value", *it1++, *it2++, function_name, line_number);
}
test_values_message(
function_name, line_number, "it1 != c1.end()", it1, c1.end());
test_values_message(
function_name, line_number, "it2 != c2.end()", it2, c2.end());
}
template <class Map1, class Map2>
static void test_equal_map_container(const Map1 &m1, const Map2 &m2,
const char *function_name, int line_number) {
test_equal_size(m1, m2, function_name, line_number);
test_equal_content_map(m1, m2, function_name, line_number);
}
template <class Map1, class Map2>
static void test_map_default_constructor(
const Map1 &, const Map2 &, const char *function_name, int line_number) {
Map1 m1;
Map2 m2;
test_equal_size(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_range_constructor(const Map1 &, const Value1 &,
const Map2 &, const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
const int size = 100;
std::vector<Value1> vec1;
std::vector<Value2> vec2;
for (int i = 0; i < size; ++i) {
Key key = generateRandomKey();
MappedType mapped_type = generateRandomMappedType();
Value1 value1 = Value1(key, mapped_type);
Value2 value2 = Value2(key, mapped_type);
vec1.push_back(value1);
vec2.push_back(value2);
}
Map1 m1(vec1.begin(), vec1.end());
Map2 m2(vec2.begin(), vec2.end());
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void add_random_map_values(Map1 &map1, const Value1 &, Map2 &map2,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), std::size_t n) {
while (map1.size() < n && map2.size() < n) {
Key key = generateRandomKey();
MappedType mapped_type = generateRandomMappedType();
Value1 value1 = Value1(key, mapped_type);
map1.insert(value1);
Value2 value2 = Value2(key, mapped_type);
map2.insert(value2);
}
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_copy_constructor(const Map1 &, const Value1 &,
const Map2 &, const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 30);
test_equal_map_container(m1, m2, function_name, line_number);
Map1 m1_copy(m1);
Map2 m2_copy(m2);
test_equal_map_container(m1_copy, m2_copy, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_assignment_operator(const Map1 &, const Value1 &,
const Map2 &, const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 30);
test_equal_map_container(m1, m2, function_name, line_number);
Map1 m1_copy;
Map2 m2_copy;
m1_copy = m1;
m2_copy = m2;
test_equal_map_container(m1_copy, m2_copy, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_begin(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 30);
test_equal_map_container(m1, m2, function_name, line_number);
typename Map1::iterator it1 = m1.begin();
typename Map2::iterator it2 = m2.begin();
test_map_values("iterator", *it1, *it2, function_name, line_number);
typename Map1::const_iterator it1c = m1.begin();
typename Map2::const_iterator it2c = m2.begin();
test_map_values("const_iterator", *it1c, *it2c, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_end(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 30);
test_equal_map_container(m1, m2, function_name, line_number);
typename Map1::iterator it1 = m1.end();
typename Map2::iterator it2 = m2.end();
test_map_values("iterator", *(--it1), *(--it2), function_name, line_number);
typename Map1::const_iterator it1c = m1.end();
typename Map2::const_iterator it2c = m2.end();
test_map_values(
"const_iterator", *(--it1c), *(--it2c), function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_rbegin(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 30);
test_equal_map_container(m1, m2, function_name, line_number);
typename Map1::reverse_iterator rit1 = m1.rbegin();
typename Map2::reverse_iterator rit2 = m2.rbegin();
test_map_values("iterator", *rit1, *rit2, function_name, line_number);
typename Map1::const_reverse_iterator rit1c = m1.rbegin();
typename Map2::const_reverse_iterator rit2c = m2.rbegin();
test_map_values(
"const_reverse_iterator", *rit1c, *rit2c, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_rend(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 30);
test_equal_map_container(m1, m2, function_name, line_number);
typename Map1::reverse_iterator rit1 = m1.rend();
typename Map2::reverse_iterator rit2 = m2.rend();
test_map_values(
"iterator", *(--rit1), *(--rit2), function_name, line_number);
typename Map1::const_reverse_iterator rit1c = m1.rend();
typename Map2::const_reverse_iterator rit2c = m2.rend();
test_map_values(
"const_iterator", *(--rit1c), *(--rit2c), function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_empty(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
test_equal_map_container(m1, m2, function_name, line_number);
test_values_message(function_name, line_number, "m1.empty() != m2.empty()",
m1.empty(), m2.empty());
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 30);
test_equal_map_container(m1, m2, function_name, line_number);
test_values_message(function_name, line_number, "m1.empty() != m2.empty()",
m1.empty(), m2.empty());
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_element_access(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
test_equal_map_container(m1, m2, function_name, line_number);
Key key = generateRandomKey();
test_values_message(
function_name, line_number, "operator[]", m1[key], m2[key]);
MappedType mapped_type = generateRandomMappedType();
m1[key] = mapped_type;
m2[key] = mapped_type;
test_values_message(
function_name, line_number, "operator[]", m1[key], m2[key]);
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map1Return, class Map2, class Value2,
class Map2Return, class Key, class MappedType>
static void test_map_insert_value(const Map1 &, const Value1 &,
const Map1Return &, const Map2 &, const Value2 &, const Map2Return &,
Key (*generateRandomKey)(), MappedType (*generateRandomMappedType)(),
const char *function_name, int line_number) {
Map1 m1;
Map2 m2;
test_equal_map_container(m1, m2, function_name, line_number);
Key key = generateRandomKey();
MappedType mapped_type = generateRandomMappedType();
Map1Return m1ret = m1.insert(Value1(key, mapped_type));
Map2Return m2ret = m2.insert(Value2(key, mapped_type));
test_map_values(
"insert", *(m1ret.first), *(m2ret.first), function_name, line_number);
test_values_message(
function_name, line_number, "insert", m1ret.second, m2ret.second);
test_equal_map_container(m1, m2, function_name, line_number);
m1ret = m1.insert(Value1(key, mapped_type));
m2ret = m2.insert(Value2(key, mapped_type));
test_map_values(
"insert", *(m1ret.first), *(m2ret.first), function_name, line_number);
test_values_message(
function_name, line_number, "insert", m1ret.second, m2ret.second);
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_insert_hint(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
test_equal_map_container(m1, m2, function_name, line_number);
Key key = generateRandomKey();
MappedType mapped_type = generateRandomMappedType();
typename Map1::iterator m1_it =
m1.insert(m1.begin(), Value1(key, mapped_type));
typename Map2::iterator m2_it =
m2.insert(m2.begin(), Value2(key, mapped_type));
test_map_values("insert", *(m1_it), *(m2_it), function_name, line_number);
test_equal_map_container(m1, m2, function_name, line_number);
m1_it = m1.insert(m1.begin(), Value1(key, mapped_type));
m2_it = m2.insert(m2.begin(), Value2(key, mapped_type));
test_map_values("insert", *(m1_it), *(m2_it), function_name, line_number);
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_insert_range(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
Map1 m1_rand;
Map2 m2_rand;
test_equal_map_container(m1, m2, function_name, line_number);
add_random_map_values(m1_rand, Value1(), m2_rand, Value2(),
generateRandomKey, generateRandomMappedType, 10);
m1.insert(m1_rand.begin(), m1_rand.end());
m2.insert(m2_rand.begin(), m2_rand.end());
test_equal_map_container(m1, m2, function_name, line_number);
m1.insert(m1_rand.begin(), m1_rand.end());
m2.insert(m2_rand.begin(), m2_rand.end());
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_erase_position(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
test_equal_map_container(m1, m2, function_name, line_number);
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 10);
m1.erase(m1.begin());
m2.erase(m2.begin());
test_equal_map_container(m1, m2, function_name, line_number);
m1.erase(ft::prev(m1.end()));
m2.erase(ft::prev(m2.end()));
test_equal_map_container(m1, m2, function_name, line_number);
m1.erase(ft::next(m1.begin()));
m2.erase(ft::next(m2.begin()));
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_erase_key(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
test_equal_map_container(m1, m2, function_name, line_number);
Key key = generateRandomKey();
MappedType mapped_type = generateRandomMappedType();
m1.insert(Value1(key, mapped_type));
m2.insert(Value2(key, mapped_type));
test_values_message(
function_name, line_number, "erase", m1.erase(key), m2.erase(key));
test_equal_map_container(m1, m2, function_name, line_number);
test_values_message(
function_name, line_number, "erase", m1.erase(key), m2.erase(key));
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_erase_range(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
test_equal_map_container(m1, m2, function_name, line_number);
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 10);
test_equal_map_container(m1, m2, function_name, line_number);
m1.erase(ft::next(m1.begin()), ft::prev(m1.end()));
m2.erase(ft::next(m2.begin()), ft::prev(m2.end()));
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_swap(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1a;
Map1 m1b;
Map2 m2a;
Map2 m2b;
add_random_map_values(m1a, Value1(), m2a, Value2(), generateRandomKey,
generateRandomMappedType, 10);
add_random_map_values(m1b, Value1(), m2b, Value2(), generateRandomKey,
generateRandomMappedType, 10);
m1a.swap(m1b);
m2a.swap(m2b);
test_equal_map_container(m1a, m2a, function_name, line_number);
test_equal_map_container(m2b, m2b, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_clear(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 10);
test_equal_map_container(m1, m2, function_name, line_number);
m1.clear();
m2.clear();
test_equal_map_container(m1, m2, function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_key_comp(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 10);
test_equal_map_container(m1, m2, function_name, line_number);
typename Map1::value_compare m1_value_comp = m1.value_comp();
typename Map2::value_compare m2_value_comp = m2.value_comp();
Key key1 = generateRandomKey();
Key key2 = generateRandomKey();
MappedType mapped_type1 = generateRandomMappedType();
MappedType mapped_type2 = generateRandomMappedType();
Value1 map1_value1(key1, mapped_type1);
Value1 map1_value2(key2, mapped_type2);
Value2 map2_value1(key1, mapped_type1);
Value2 map2_value2(key2, mapped_type2);
bool b1 = m1_value_comp(map1_value1, map1_value2);
bool b2 = m2_value_comp(map2_value1, map2_value2);
test_values_message(function_name, line_number, "value_comp", b1, b2);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_value_comp(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 10);
test_equal_map_container(m1, m2, function_name, line_number);
typename Map1::key_compare m1_key_comp = m1.key_comp();
typename Map2::key_compare m2_key_comp = m2.key_comp();
Key k1 = generateRandomKey();
Key k2 = generateRandomKey();
bool b1 = m1_key_comp(k1, k2);
bool b2 = m2_key_comp(k1, k2);
test_values_message(function_name, line_number, "key_comp", b1, b2);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_find(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
Key key = generateRandomKey();
typename Map1::iterator it1 = m1.find(key);
typename Map2::iterator it2 = m2.find(key);
typename Map1::const_iterator cit1 = m1.find(key);
typename Map2::const_iterator cit2 = m2.find(key);
test_values_message(
function_name, line_number, "it1 != m1.end()", it1, m1.end());
test_values_message(
function_name, line_number, "it2 != m2.end()", it2, m2.end());
test_values_message(
function_name, line_number, "cit1 != m1.end()", cit1, m1.end());
test_values_message(
function_name, line_number, "cit2 != m2.end()", cit2, m2.end());
MappedType mapped_type = generateRandomMappedType();
Value1 value1(key, mapped_type);
Value2 value2(key, mapped_type);
m1.insert(value1);
m2.insert(value2);
it1 = m1.find(key);
it2 = m2.find(key);
cit1 = m1.find(key);
cit2 = m2.find(key);
test_map_values("find", *(it1), *(it2), function_name, line_number);
test_map_values("find", *(cit1), *(cit2), function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_count(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
Key key = generateRandomKey();
MappedType mapped_type = generateRandomMappedType();
test_values_message(
function_name, line_number, "count", m1.count(key), m2.count(key));
m1.insert(Value1(key, mapped_type));
m2.insert(Value2(key, mapped_type));
test_equal_map_container(m1, m2, function_name, line_number);
test_values_message(
function_name, line_number, "count", m1.count(key), m2.count(key));
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_lower_bound(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
Key key = generateRandomKey();
MappedType mapped_type = generateRandomMappedType();
m1.insert(Value1(key, mapped_type));
m2.insert(Value2(key, mapped_type));
test_equal_map_container(m1, m2, function_name, line_number);
typename Map1::iterator it1 = m1.lower_bound(key);
typename Map2::iterator it2 = m2.lower_bound(key);
test_map_values(
"lower_bound iterator", *(it1), *(it2), function_name, line_number);
typename Map1::const_iterator cit1 = m1.lower_bound(key);
typename Map2::const_iterator cit2 = m2.lower_bound(key);
test_map_values("lower_bound const_iterator", *(cit1), *(cit2),
function_name, line_number);
}
template <class Map1, class Value1, class Map2, class Value2, class Key,
class MappedType>
static void test_map_upper_bound(const Map1 &, const Value1 &, const Map2 &,
const Value2 &, Key (*generateRandomKey)(),
MappedType (*generateRandomMappedType)(), const char *function_name,
int line_number) {
Map1 m1;
Map2 m2;
Key key1 = generateRandomKey();
MappedType mapped_type1 = generateRandomMappedType();
Value1 value1a(key1, mapped_type1);
Value2 value2a(key1, mapped_type1);
m1.insert(value1a);
m2.insert(value2a);
test_equal_map_container(m1, m2, function_name, line_number);
Key key2 = generateRandomKey();
while (key2 == key1) {
key2 = generateRandomKey();
}
MappedType mapped_type2 = generateRandomMappedType();
Value1 value1b(key2, mapped_type2);
Value2 value2b(key2, mapped_type2);
m1.insert(value1b);
m2.insert(value2b);
test_equal_map_container(m1, m2, function_name, line_number);
Key lesser_key = ft::min(key1, key2);
typename Map1::iterator it1 = m1.upper_bound(lesser_key);
typename Map2::iterator it2 = m2.upper_bound(lesser_key);
test_map_values(
"upper_bound iterator", *(it1), *(it2), function_name, line_number);
typename Map1::const_iterator cit1 = m1.upper_bound(lesser_key);
typename Map2::const_iterator cit2 = m2.upper_bound(lesser_key);
test_map_values("upper_bound const_iterator", *(cit1), *(cit2),
function_name, line_number);
}
template <class Map1, class Value1, class Map1Return, class Map2, class Value2,
class Map2Return, class Key, class MappedType>
static void test_map_equal_range(const Map1 &, const Value1 &,
const Map1Return &, const Map2 &, const Value2 &, const Map2Return &,
Key (*generateRandomKey)(), MappedType (*generateRandomMappedType)(),
const char *function_name, int line_number) {
Map1 m1;
Map2 m2;
add_random_map_values(m1, Value1(), m2, Value2(), generateRandomKey,
generateRandomMappedType, 10);
test_equal_map_container(m1, m2, function_name, line_number);
Map1Return map1_return = m1.equal_range(m1.begin()->first);
Map2Return map2_return = m2.equal_range(m2.begin()->first);
test_map_values("equal_range", *(map1_return.first), *(map2_return.first),
function_name, line_number);
test_map_values("equal_range", *(map1_return.second), *(map2_return.second),
function_name, line_number);
}
template <class Map1, class Map2>
static void test_map_comparison_operators(const Map1 &, const Map2 &,
typename ft::remove_const<typename Map1::value_type::first_type>::type (
*generateRandomKey)(),
typename ft::remove_const<typename Map1::value_type::second_type>::type (
*generateRandomMappedType)(),
const char *function_name, int line_number) {
Map1 m1a;
Map1 m1b;
Map2 m2a;
Map2 m2b;
add_random_map_values(m1a, typename Map1::value_type(), m2a,
typename Map2::value_type(), generateRandomKey,
generateRandomMappedType, 10);
test_equal_map_container(m1a, m2a, function_name, line_number);
add_random_map_values(m1b, typename Map1::value_type(), m2b,
typename Map2::value_type(), generateRandomKey,
generateRandomMappedType, 10);
test_equal_map_container(m1b, m2b, function_name, line_number);
test_condition(function_name, line_number, "equal operator",
(m1a == m1b) == (m2a == m2b));
test_condition(function_name, line_number, "not equal operator",
(m1a != m1b) == (m2a != m2b));
test_condition(function_name, line_number, "less than operator",
(m1a < m1b) == (m2a < m2b));
test_condition(function_name, line_number, "greater than operator",
(m1a > m1b) == (m2a > m2b));
test_condition(function_name, line_number, "less equal operator",
(m1a <= m1b) == (m2a <= m2b));
test_condition(function_name, line_number, "greater equal operator",
(m1a >= m1b) >= (m2a == m2b));
}
template <class Container1, class Container2>
static void test_container_count_constructor(const Container1 &,
const Container2 &, const char *function_name, int line_number) {
Container1 c1(10);
Container2 c2(10);
test_equal_container(c1, c2, function_name, line_number);
}
template <class T, class Container1, class Container2>
static void test_container_range_constructor(const Container1 &,
const Container2 &, T (*generateRandomValue)(), const char *function_name,
int line_number) {
std::vector<T> v;
for (int i = 0; i < 10; i++) {
T value = generateRandomValue();
v.push_back(value);
}
Container1 c1(v.begin(), v.end());
Container2 c2(v.begin(), v.end());
test_equal_container(c1, c2, function_name, line_number);
}
template <class Container>
static void test_container_copy_constructor(
const Container &c1, const char *function_name, int line_number) {
Container c2(c1);
test_equal_container(c1, c2, function_name, line_number);
}
template <class Container1>
static void test_container_assignment_operator(
const Container1 &c1, const char *function_name, int line_number) {
Container1 c2 = c1;
test_equal_container(c1, c2, function_name, line_number);
}
template <class Container1, class Container2, typename T>
static void test_container_assign_count(const Container1 &, const Container2 &,
const T &v, const char *function_name, int line_number) {
Container1 c1;
Container2 c2;
c1.assign(10, v);
c2.assign(10, v);
test_equal_container(c1, c2, function_name, line_number);
c1.assign(5, v);
c2.assign(5, v);
test_equal_container(c1, c2, function_name, line_number);
c1.assign(15, v);
c2.assign(15, v);
test_equal_container(c1, c2, function_name, line_number);
}
template <class Container1, class Container2>
static void test_container_assign_range(const Container1 &c1,
const Container2 &c2, const char *function_name, int line_number) {
Container1 c_a;
Container2 c_b;
c_a.assign(c1.begin(), c1.end());
c_b.assign(c2.begin(), c2.end());
test_equal_container(c_a, c_b, function_name, line_number);
c_a.assign(c1.begin(), c1.begin());
c_b.assign(c2.begin(), c2.begin());
test_equal_container(c_a, c_b, function_name, line_number);
}
template <class Container1, class Container2>
static void test_container_get_allocator(const Container1 &c1,
const Container2 &c2, const char *function_name, int line_number) {
test_values_message(function_name, line_number,
"c1.get_allocator() != c2.get_allocator()", c1.get_allocator(),
c2.get_allocator());
}
template <class Container1, class Container2>
static void test_container_begin(const Container1 &, const Container2 &,
const char *function_name, int line_number) {
Container1 c1(5);
Container2 c2(5);
test_equal_container(c1, c2, function_name, line_number);
typename Container1::iterator it1 = c1.begin();
typename Container2::iterator it2 = c2.begin();
test_values(function_name, line_number, "iterator", *it1, *it2);
typename Container1::const_iterator it1c = c1.begin();
typename Container2::const_iterator it2c = c2.begin();
test_values(function_name, line_number, "const_iterator", *it1c, *it2c);
}
template <class Container1, class Container2>
static void test_container_end(const Container1 &, const Container2 &,
const char *function_name, int line_number) {
Container1 c1(1);
Container2 c2(1);
test_equal_container(c1, c2, function_name, line_number);
typename Container1::iterator it1 = c1.end();
typename Container2::iterator it2 = c2.end();
// This works only for the containers that have bidirectional_iterator+
test_values(function_name, line_number, "iterator", *(--it1), *(--it2));
typename Container1::const_iterator it1c = c1.end();
typename Container2::const_iterator it2c = c2.end();
// This works only for the containers that have bidirectional_iterator+
test_values(
function_name, line_number, "const_iterator", *(--it1c), *(--it2c));
}
template <class Container1, class Container2>
static void test_container_rbegin(const Container1 &, const Container2 &,
const char *function_name, int line_number) {
Container1 c1(5);
Container2 c2(5);
test_equal_container(c1, c2, function_name, line_number);
typename Container1::reverse_iterator rit1 = c1.rbegin();
typename Container2::reverse_iterator rit2 = c2.rbegin();
test_values(function_name, line_number, "reverse_iterator", *rit1, *rit2);
typename Container1::const_reverse_iterator rit1c = c1.rbegin();
typename Container2::const_reverse_iterator rit2c = c2.rbegin();
test_values(
function_name, line_number, "const_reverse_iterator", *rit1c, *rit2c);
}
template <class Container1, class Container2>
static void test_container_rend(const Container1 &, const Container2 &,