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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2020 Gothel Software e.K.
*
* 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.
*/
#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cstring>
#include <random>
#include <vector>
#include <type_traits>
#define CATCH_CONFIG_RUNNER
// #define CATCH_CONFIG_MAIN
#include <catch2/catch_amalgamated.hpp>
#include <jau/test/catch2_ext.hpp>
#include <jau/basic_algos.hpp>
#include <jau/basic_types.hpp>
#include <jau/darray.hpp>
#include <jau/cow_darray.hpp>
#include <jau/cow_vector.hpp>
#include <jau/counting_allocator.hpp>
#include <jau/callocator.hpp>
#include <jau/counting_callocator.hpp>
/**
* Test jau:cow_[ro|rw]_iterator special properties from jau::cow_darray and jau::cow_vector in detail.
* <p>
* Normal random-access iterator operations are also tested from std::vector, jau::darray, jau::cow_darray and jau::cow_vector in detail,
* which either use the std::iterator (1s two container) or jau:cow_[ro|rw]_iterator (latter two cow-container).
* </p>
*/
using namespace jau;
typedef std::vector<uint64_t, counting_allocator<uint64_t>> std_vector_uint64_t;
typedef jau::darray<uint64_t, counting_callocator<uint64_t>> jau_darray_uint64_t;
typedef jau::cow_vector<uint64_t, counting_allocator<uint64_t>> jau_cow_vector_uint64_t;
typedef jau::cow_darray<uint64_t, counting_callocator<uint64_t>> jau_cow_darray_uint64_t;
JAU_TYPENAME_CUE_ALL(std_vector_uint64_t)
JAU_TYPENAME_CUE_ALL(jau_darray_uint64_t)
JAU_TYPENAME_CUE_ALL(jau_cow_vector_uint64_t)
JAU_TYPENAME_CUE_ALL(jau_cow_darray_uint64_t)
template<class T>
static void print_list(T& data) {
printf("list: %d { ", (int)data.size());
jau::for_each_const(data, [](const uint64_t & e) {
printf("%s, ", uint64DecString(e, ',', 2).c_str());
} );
printf("}\n");
}
template<class T>
static void print_list(const std::string& pre, T& data) {
printf("%s: %d { ", pre.c_str(), (int)data.size());
jau::for_each_const(data, [](const uint64_t & e) {
printf("%s, ", uint64DecString(e, ',', 2).c_str());
} );
printf("}\n");
}
template<class T>
static void fill_list(T& data, const std::size_t size) {
std::size_t i=0;
for(; i<size; i++) {
data.emplace_back( static_cast<uint64_t>(i+1) );
}
REQUIRE(i == data.size());
}
/****************************************************************************************
****************************************************************************************/
template< class Iter >
static void print_iterator_info(const std::string& typedefname,
typename std::enable_if<
std::is_class<Iter>::value
>::type* = 0
) {
jau::type_cue<Iter>::print(typedefname);
jau::type_cue<typename Iter::iterator_category>::print(typedefname+"::iterator_category");
jau::type_cue<typename Iter::iterator_type>::print(typedefname+"::iterator_type");
jau::type_cue<typename Iter::value_type>::print(typedefname+"::value_type");
jau::type_cue<typename Iter::reference>::print(typedefname+"::reference");
jau::type_cue<typename Iter::pointer>::print(typedefname+"::pointer");
}
template<class Iter>
static void print_iterator_info(const std::string& typedefname,
typename std::enable_if<
!std::is_class<Iter>::value
>::type* = 0
) {
jau::type_cue<Iter>::print(typedefname);
}
template<class T>
static bool test_00_inspect_iterator_types(const std::string& type_id) {
typedef typename T::size_type size_type;
typedef typename T::iterator iter_type;
typedef typename T::difference_type diff_type;
typedef typename T::const_iterator citer_type;
printf("**** Type Info: %s\n", type_id.c_str());
jau::type_cue<T>::print("T");
jau::type_cue<typename T::value_type>::print("T::value_type");
jau::type_cue<size_type>::print("T::size_type");
jau::type_cue<diff_type>::print("T::difference_type");
jau::type_cue<typename T::reference>::print("T::reference");
jau::type_cue<typename T::pointer>::print("T::pointer");
print_iterator_info<iter_type>("T::iterator");
print_iterator_info<citer_type>("T::citer_type");
printf("\n\n");
return true;
}
/****************************************************************************************
****************************************************************************************/
template<class T, typename iterator_type1, typename iterator_type2>
static void test_iterator_equal(iterator_type1& citer1, iterator_type2& citer2)
{
// Adding redundant switched operands comparison
// to test all relational combination of the overloading. (Leave it as is)
REQUIRE( citer1 == citer2 ); // iter op==(iter1, iter2)
REQUIRE( citer2 == citer1 ); // iter op==(iter1, iter2)
REQUIRE( !( citer1 != citer2 ) ); // iter op!=(iter1, iter2)
REQUIRE( !( citer2 != citer1 ) ); // iter op!=(iter1, iter2)
REQUIRE( *citer1 == *citer2 ); // iter op*() and value_type ==
REQUIRE( *citer2 == *citer1 ); // iter op*() and value_type ==
REQUIRE( !( *citer1 != *citer2 ) ); // iter op*() and value_type !=
REQUIRE( !( *citer2 != *citer1 ) ); // iter op*() and value_type !=
}
template<class T, typename iterator_type1, typename iterator_type2>
static void test_iterator_notequal(iterator_type1& citer1, iterator_type2& citer2)
{
// Adding redundant switched operands comparison
// to test all relational combination of the overloading. (Leave it as is)
REQUIRE( citer1 != citer2 ); // iter op==(iter1, iter2)
REQUIRE( citer2 != citer1 ); // iter op==(iter1, iter2)
REQUIRE( !( citer1 == citer2 ) ); // iter op!=(iter1, iter2)
REQUIRE( !( citer2 == citer1 ) ); // iter op!=(iter1, iter2)
REQUIRE( *citer1 != *citer2 ); // iter op*() and value_type ==
REQUIRE( *citer2 != *citer1 ); // iter op*() and value_type ==
REQUIRE( !( *citer1 == *citer2 ) ); // iter op*() and value_type !=
REQUIRE( !( *citer2 == *citer1 ) ); // iter op*() and value_type !=
}
// iterator_type1 .. can be all the same, but leave it to the caller which is which
template<class T, typename iterator_type1, typename iterator_type2, typename iterator_type3, typename iterator_type4>
static void test_iterator_compare(const typename T::size_type size,
iterator_type1& begin,
iterator_type2& end,
iterator_type3& citer1, iterator_type4& citer2,
const typename T::difference_type citer1_idx,
const typename T::difference_type citer2_idx)
{
typedef typename T::difference_type diff_type;
diff_type d_size = static_cast<diff_type>(size);
diff_type distance = citer2_idx - citer1_idx;
// iter op-(iter1, iter2)
REQUIRE( ( end - begin ) == d_size);
REQUIRE( ( citer2 - begin ) == citer2_idx);
REQUIRE( ( citer1 - begin ) == citer1_idx);
REQUIRE( ( end - citer1 ) == d_size - citer1_idx);
REQUIRE( ( end - citer2 ) == d_size - citer2_idx);
REQUIRE( ( citer2 - citer1 ) == distance);
// iter op-(iter, difference_type) and iter op==(iter1, iter2)
REQUIRE( ( citer1 - citer1_idx ) == begin);
REQUIRE( ( citer2 - citer2_idx ) == begin);
REQUIRE( ( citer2 - distance ) == citer1);
// iter op+(iter, difference_type) and iter op==(iter1, iter2)
{
diff_type d_citer1_end = end - citer1;
diff_type d_citer2_end = end - citer2;
REQUIRE( ( citer1_idx + d_citer1_end ) == d_size); // validate op-(iter1, iter2)
REQUIRE( ( citer2_idx + d_citer2_end ) == d_size); // validate op-(iter1, iter2)
REQUIRE( ( citer1 + d_citer1_end ) == end);
REQUIRE( ( citer2 + d_citer2_end ) == end);
}
// Adding redundant switched operands comparison
// to test all relational combination of the overloading. (Leave it as is)
if( 0 == distance ) {
test_iterator_equal<T>(citer1, citer2);
REQUIRE( !( citer2 > citer1 ) ); // iter op>(iter1, iter2)
REQUIRE( citer2 >= citer1 ); // iter op>=(iter1, iter2)
REQUIRE( !( citer2 < citer1 ) ); // iter op<(iter1, iter2)
REQUIRE( citer2 <= citer1 ); // iter op<=(iter1, iter2)
REQUIRE( citer1 <= citer2 ); // iter op>=(iter1, iter2)
REQUIRE( citer1 >= citer2 ); // iter op>=(iter1, iter2)
} else if( distance > 0 ) { // citer2 > citer1
test_iterator_notequal<T>(citer1, citer2);
REQUIRE( citer2 > citer1 ); // iter op>(iter1, iter2)
REQUIRE( citer2 >= citer1 ); // iter op>=(iter1, iter2)
REQUIRE( !( citer2 < citer1 ) ); // iter op<(iter1, iter2)
REQUIRE( !( citer2 <= citer1 ) ); // iter op<=(iter1, iter2)
REQUIRE( citer1 <= citer2 ); // iter op>(iter1, iter2)
REQUIRE( citer1 < citer2 ); // iter op>=(iter1, iter2)
} else { // distance < 0: citer2 < citer1
test_iterator_notequal<T>(citer1, citer2);
REQUIRE( !( citer2 > citer1 ) ); // iter op>(iter1, iter2)
REQUIRE( !( citer2 >= citer1 ) ); // iter op>=(iter1, iter2)
REQUIRE( citer2 < citer1 ); // iter op<(iter1, iter2)
REQUIRE( citer2 <= citer1 ); // iter op<=(iter1, iter2)
REQUIRE( citer1 > citer2 ); // iter op<(iter1, iter2)
REQUIRE( citer1 >= citer2 ); // iter op<=(iter1, iter2)
}
}
template<class T, typename iterator_type1, typename iterator_type2>
static void test_iterator_dereference(const typename T::size_type size,
iterator_type1& begin, iterator_type2& end)
{
printf("**** test_iterator_dereference:\n");
print_iterator_info<iterator_type1>("iterator_type1");
print_iterator_info<iterator_type2>("iterator_type2");
{
T data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
T data_has( begin, end );
REQUIRE(data_has == data_exp);
}
// dereferencing, pointer, equality
iterator_type1 citer1 = begin;
iterator_type2 citer2 = begin;
REQUIRE( citer1 == begin ); // iter op==()
REQUIRE( citer2 == begin ); // iter op==()
REQUIRE( citer1 == citer1 ); // iter op==()
REQUIRE( citer2 == citer1 ); // iter op==()
REQUIRE( *citer1 == *begin ); // iter op*(), and value_type ==
REQUIRE( *citer2 == *begin ); // iter op*(), and value_type ==
REQUIRE( *citer1 == *citer1 ); // iter op*(), and value_type ==
REQUIRE( *citer2 == *citer1 ); // iter op*(), and value_type ==
REQUIRE( citer1[1] == *(begin+1) ); // iter op[](diff), op+(iter, diff), iter op*(), and value_type ==
REQUIRE( citer2[1] == *(begin+1) ); // iter op[](diff), op+(iter, diff), iter op*(), and value_type ==
REQUIRE( citer1[1] == *(citer2+1) ); // iter op[](diff), op+(iter, diff), iter op*(), and value_type ==
REQUIRE( citer1 != end-1 ); // iter op!=()
REQUIRE( citer2 != end-1 ); // iter op!=()
REQUIRE( *citer1 != *(end-1) ); // iter op*(), and value_type ==
REQUIRE( *citer2 != *(end-1) ); // iter op*(), and value_type ==
REQUIRE( citer1[1] != *(end-2) ); // iter op[](diff), op+(iter, diff), iter op*(), and value_type ==
REQUIRE( citer2[1] != *(end-2) ); // iter op[](diff), op+(iter, diff), iter op*(), and value_type ==
REQUIRE( citer2+size-1 == end -1 );
REQUIRE( *(citer2+size-1) == *(end -1) );
REQUIRE( citer2[size-1] == end[-1] );
REQUIRE( *(citer2+0) == begin[0] );
REQUIRE( *(citer2+1) == begin[1] );
REQUIRE( *(citer2+2) == begin[2] );
REQUIRE( *(citer2+3) == begin[3] );
REQUIRE( *(citer2+size-1) == end[-1] );
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 0);
}
template<class T, typename iterator_type1, typename iterator_type2>
static void test_iterator_arithmetic(const typename T::size_type size,
iterator_type1& begin, iterator_type2& end)
{
printf("**** test_iterator_arithmetic:\n");
print_iterator_info<iterator_type1>("iterator_type1");
print_iterator_info<iterator_type2>("iterator_type2");
// citer_type operations
// op++(), op--(), op++(int), op--(int),
// op+=(difference_type), op+(iter a, difference_type) ..
{
iterator_type1 citer1 = begin;
iterator_type1 citer2 = begin;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 0);
// iter op++(int)
citer2++;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 1);
// iter op++(int)
citer1++;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 1, 1);
// iter op--(int)
citer2--;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 1, 0);
// iter op--(int)
citer1--;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 0);
REQUIRE( *citer2 == begin[0] );
// iter op++(int)
citer2++;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 1);
REQUIRE( *citer2 == *(begin+1) ); // iter op*(), op+(iter, difference_type) and value_type ==
REQUIRE( *citer2 == begin[1] ); // iter op*(), op[](difference_type) and value_type ==
// iter op++(int)
citer2++;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 2);
REQUIRE( *citer2 == *(begin+2) ); // iter op*(), op+(iter, difference_type) and value_type ==
REQUIRE( *citer2 == begin[2] ); // iter op*(), op[](difference_type) and value_type ==
// iter op++(int)
citer2++;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 3);
REQUIRE( *citer2 == *(begin+3) ); // iter op*(), op+(iter, difference_type) and value_type ==
REQUIRE( *citer2 == begin[3] ); // iter op*(), op[](difference_type) and value_type ==
// iter op++()
--citer2;
--citer2;
--citer2;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 0);
REQUIRE( *citer2 == *(begin+0) ); // iter op*(), op+(iter, difference_type) and value_type ==
REQUIRE( *citer2 == begin[0] ); // iter op*(), op[](difference_type) and value_type ==
// iter +=(diff)
citer2 += 3;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 3);
// iter +=(diff)
citer2 += 6;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 9);
// iter -=(diff)
citer2 -= 9;
test_iterator_compare<T>(size, begin, end, citer1, citer2, 0, 0);
}
{
// Adding redundant switched operands comparison
// to test all relational combination of the overloading. (Leave it as is)
iterator_type1 citer1 = begin;
iterator_type1 citer2 = begin;
REQUIRE( citer1 == citer1 ); // iter op==()
REQUIRE( citer2 == citer1 ); // iter op==()
++citer2;
REQUIRE( citer2 != citer1 ); // iter op==()
REQUIRE( citer1 != citer2 ); // iter op==()
REQUIRE( citer2 > citer1 ); // iter op==()
REQUIRE( citer2 >= citer1 ); // iter op==()
REQUIRE( citer1 < citer2 ); // iter op==()
REQUIRE( citer1 <= citer2 ); // iter op==()
REQUIRE( ( citer2 - citer1 ) == 1); // iter op==()
REQUIRE( ( citer1 - citer2 ) == -1); // iter op==()
}
}
template<class T>
static bool test_citer_type_ops(const std::string& type_id,
std::enable_if_t< is_cow_type<T>::value, bool> = true )
{
typedef typename T::const_iterator citer_type;
typedef typename T::difference_type diff_type;
T data;
fill_list(data, 10);
printf("**** test_citer_type_ops(CoW): %s\n", type_id.c_str());
{
citer_type begin = data.cbegin(); // immutable new_store non-const iterator, gets held until destruction
citer_type end = begin.cend(); // no new store iterator, on same store as begin, obtained from begin
diff_type data_size = static_cast<diff_type>(data.size());
diff_type begin_size = static_cast<diff_type>(begin.size());
diff_type end_size = static_cast<diff_type>(end.size());
REQUIRE( begin_size == data_size );
REQUIRE( end_size == data_size );
REQUIRE( end - begin == data_size );
REQUIRE( end - end_size == begin );
REQUIRE( begin + begin_size == end );
REQUIRE( *( end - end_size ) == *begin );
REQUIRE( *( begin + begin_size ) == *end );
test_iterator_dereference<T, citer_type>(begin.size(), begin, end);
}
{
citer_type begin = data.cbegin(); // no new store citer_type
citer_type end = begin.cend(); // no new store citer_type, on same store as begin, obtained from begin
test_iterator_arithmetic<T, citer_type>(data.size(), begin, end);
}
{
T data2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
print_list("work", data);
print_list("expt", data2);
REQUIRE(data == data2);
}
return true;
}
template<class T>
static bool test_citer_type_ops(const std::string& type_id,
std::enable_if_t< !is_cow_type<T>::value, bool> = true )
{
typedef typename T::const_iterator citer_type;
typedef typename T::difference_type diff_type;
T data;
fill_list(data, 10);
printf("**** test_citer_type_ops: %s\n", type_id.c_str());
{
citer_type begin = data.cbegin(); // mutable new_store non-const iterator, gets held until destruction
citer_type end = data.cend(); // no new store iterator, on same store as begin and from begin
diff_type data_size = static_cast<diff_type>(data.size());
REQUIRE( end - begin == data_size );
REQUIRE( end - data_size == begin );
REQUIRE( begin + data_size == end );
REQUIRE( *( end - data_size ) == *begin );
REQUIRE( *( begin + data_size - 1 ) == *( end - 1 ) );
REQUIRE( end[-data_size] == begin[0] );
REQUIRE( begin[data_size - 1] == end[-1] );
test_iterator_dereference<T, citer_type>(data.size(), begin, end);
}
{
citer_type begin = data.cbegin(); // no new store citer_type
citer_type end = data.cend(); // no new store citer_type, on same store as begin
test_iterator_arithmetic<T, citer_type, citer_type>(data.size(), begin, end);
}
{
T data2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
REQUIRE(data == data2);
}
return true;
}
template<class T>
static bool test_mutable_iterator_ops(const std::string& type_id,
std::enable_if_t< is_cow_type<T>::value, bool> = true )
{
typedef typename T::size_type size_type;
typedef typename T::const_iterator citer_type;
typedef typename T::iterator iter_type;
typedef typename T::difference_type diff_type;
typedef typename T::value_type value_type;
typedef typename T::storage_t storage_t;
printf("**** test_mutable_iterator_ops(CoW): %s\n", type_id.c_str());
{
T data;
fill_list(data, 10);
iter_type begin = data.begin(); // mutable new_store non-const iterator, gets held until destruction
iter_type end = begin.end(); // no new store iterator, on same store as begin and from begin
diff_type data_size = static_cast<diff_type>(data.size());
diff_type begin_size = static_cast<diff_type>(begin.size());
diff_type end_size = static_cast<diff_type>(end.size());
REQUIRE( begin_size == data_size );
REQUIRE( end_size == data_size );
REQUIRE( end - begin == data_size );
REQUIRE( end - end_size == begin );
REQUIRE( begin + begin_size == end );
REQUIRE( *( end - end_size ) == *begin );
REQUIRE( *( begin + begin_size - 1 ) == *( end - 1 ) );
REQUIRE( end[-end_size] == begin[0] );
REQUIRE( begin[begin_size - 1] == end[-1] );
test_iterator_dereference<T, iter_type>(begin.size(), begin, end);
}
{
T data;
fill_list(data, 10);
// all 4 combinations of iter, citer:
iter_type begin = data.begin(); // mutable new_store non-const iterator, gets held until destruction
iter_type end = begin.end();
citer_type cbegin = begin.immutable();
citer_type cend = cbegin.cend();
test_iterator_arithmetic<T>(data.size(), begin, end);
test_iterator_arithmetic<T>(data.size(), cbegin, cend);
test_iterator_arithmetic<T>(data.size(), begin, cend);
test_iterator_arithmetic<T>(data.size(), cbegin, end);
}
// iterator-op: darray/vector-op
// -------------------------------------------
// 1 pop_back()
// 1 erase (): erase (citer_type pos)
// 3 erase (count): erase (iterator first, citer_type last)
// 1 insert(const value_type& x): iterator insert(citer_type pos, const value_type& x)
// 0 insert(value_type&& x): iterator insert(citer_type pos, value_type&& x)
// 1 emplace(Args&&... args): emplace(citer_type pos, Args&&... args)
// 2 insert(InputIt first, InputIt last ): insert( citer_type pos, InputIt first, InputIt last )
// 1 void push_back(value_type& x)
// 1 void push_back(value_type&& x)
// 1 reference emplace_back(Args&&... args)
// 0 [void push_back( InputIt first, InputIt last )]
// 1 to_begin()
{
T data;
fill_list(data, 10);
citer_type citer0 = data.cbegin(); // immutable orig store iterator
{
T data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
REQUIRE(data == data_exp);
}
REQUIRE( *data.snapshot() == citer0.storage() );
iter_type iter = data.begin(); // mutable new_store non-const iterator, gets held until destruction
size_type size_pre = iter.size();
value_type elem = iter.end()[-2];
REQUIRE( iter != citer0 );
REQUIRE( iter.storage() == citer0.storage() );
REQUIRE( iter.storage() == *data.snapshot() );
REQUIRE( iter.dist_begin() == 0 );
REQUIRE( iter.dist_end() == static_cast<diff_type>(size_pre));
int i;
(void)i;
// pop_back()
iter.pop_back();
REQUIRE( iter.size() == size_pre-1 );
REQUIRE( iter == iter.end() );
REQUIRE( iter == iter.begin()+size_pre-1 );
REQUIRE( iter.dist_begin() == static_cast<diff_type>(size_pre)-1 );
REQUIRE( iter.dist_end() == 0 );
REQUIRE( iter[-1] == elem );
{
storage_t data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
REQUIRE(iter.storage() != citer0.storage());
REQUIRE(iter.storage() != *data.snapshot());
// insert( citer_type pos, InputIt first, InputIt last )
REQUIRE( iter == iter.end() );
size_pre = iter.size();
REQUIRE( iter.dist_begin() == static_cast<diff_type>(size_pre));
REQUIRE( iter.dist_end() == 0);
{
T data2;
fill_list(data2, 10);
// iter.push_back(data2.cbegin(), data2.cend()); // FIXME: Only in jau::darray not stl::vector
iter.insert(data2.cbegin(), data2.cbegin()+data2.size()); // same as push_pack(..) since pointing to end() - but iter points here to first new elem
}
REQUIRE( iter.size() == size_pre+10 );
REQUIRE( iter == iter.end()-10 );
REQUIRE( iter.dist_begin() == static_cast<diff_type>(size_pre));
REQUIRE( iter.dist_end() == 10);
{
storage_t data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
REQUIRE(iter.storage() == data_exp);
}
// erase (count): erase (iterator first, citer_type last)
REQUIRE( iter == iter.end()-10 );
size_pre = iter.size();
// std::cout << "iter.5" << iter << (iter - iter.begin()) << "/" << iter.size() << ", size2 " << size2 << std::endl;
iter.erase(10);
// std::cout << "iter.6" << iter << (iter - iter.begin()) << "/" << iter.size() << std::endl;
REQUIRE( iter.size() == size_pre-10 );
REQUIRE( iter == iter.end() );
{
storage_t data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// erase ()
size_pre = iter.size();
iter.to_begin();
REQUIRE( iter == iter.begin() );
elem = iter.begin()[1];
iter.erase();
REQUIRE( iter.size() == size_pre-1 );
REQUIRE( iter == iter.begin() );
REQUIRE( *iter == elem );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// void push_back(value_type& x)
size_pre = iter.size();
REQUIRE( iter == iter.begin() );
elem = iter.end()[-1];
{
T data2;
fill_list(data2, 10);
citer_type data2_iter = data2.cbegin();
iter.push_back(data2_iter[0]);
iter.push_back(data2_iter[1]);
iter.push_back(data2_iter[2]);
REQUIRE( iter.size() == size_pre+3 );
REQUIRE( iter == iter.end() );
REQUIRE( iter[-3] == data2_iter[0] );
REQUIRE( iter[-2] == data2_iter[1] );
REQUIRE( iter[-1] == data2_iter[2] );
}
{
storage_t data_exp = { 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3 };
REQUIRE(iter.storage() == data_exp);
}
// erase (count): erase (iterator first, citer_type last)
size_pre = iter.size();
REQUIRE( iter == iter.end() );
iter -= 3;
iter.erase(3);
REQUIRE( iter.size() == size_pre-3 );
REQUIRE( iter == iter.end() );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// void push_back(value_type&& x)
size_pre = iter.size();
REQUIRE( iter == iter.end() );
{
value_type elem0 = iter.begin()[0];
iter.push_back( std::move(elem0));
}
{
value_type elem0 = iter.begin()[1];
iter.push_back( std::move(elem0));
}
{
value_type elem0 = iter.begin()[2];
iter.push_back( std::move(elem0));
}
REQUIRE( iter.size() == size_pre+3 );
REQUIRE( iter == iter.end() );
REQUIRE( iter[-3] == iter.begin()[0] );
REQUIRE( iter[-2] == iter.begin()[1] );
REQUIRE( iter[-1] == iter.begin()[2] );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4 };
REQUIRE(iter.storage() == data_exp);
}
// erase last three
REQUIRE( iter == iter.end() );
iter -= 3;
iter.erase();
iter.erase();
iter.erase();
REQUIRE( iter == iter.end() );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// iterator insert(citer_type pos, const value_type& x)
iter.to_begin();
iter += 5;
REQUIRE( iter == iter.begin()+5 );
REQUIRE( iter.dist_begin() == 5 );
size_pre = iter.size();
{
T data2;
fill_list(data2, 10);
citer_type data2_iter = data2.cbegin();
iter.insert(data2_iter[0]);
iter.insert(data2_iter[1]);
iter.insert(data2_iter[2]);
REQUIRE( iter.size() == size_pre+3 );
REQUIRE( iter == iter.begin()+5 );
iter.to_begin();
REQUIRE( iter[5] == data2_iter[2] );
REQUIRE( iter[6] == data2_iter[1] );
REQUIRE( iter[7] == data2_iter[0] );
}
{
storage_t data_exp = { 2, 3, 4, 5, 6, 3, 2, 1, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// insert( citer_type pos, InputIt first, InputIt last )
iter += 5;
REQUIRE( iter == iter.begin()+5 );
size_pre = iter.size();
{
T data2;
fill_list(data2, 10);
iter.insert(data2.cbegin(), data2.cbegin()+3);
}
REQUIRE( iter.size() == size_pre+3 );
REQUIRE( iter == iter.begin()+5 );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 1, 2, 3, 3, 2, 1, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// erase (count): erase (iterator first, citer_type last)
REQUIRE( iter == iter.begin()+5 );
size_pre = iter.size();
iter.erase(6);
REQUIRE( iter.size() == size_pre-6 );
REQUIRE( iter == iter.begin()+5 );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// 1 emplace(Args&&... args): emplace(citer_type pos, Args&&... args)
size_pre = iter.size();
REQUIRE( iter == iter.begin()+5 );
iter.emplace( static_cast<uint64_t>(2) );
iter.emplace( static_cast<uint64_t>(3) );
iter.emplace( static_cast<uint64_t>(4) );
REQUIRE( iter == iter.begin()+5 );
REQUIRE( iter[0] == 4 );
REQUIRE( iter[1] == 3 );
REQUIRE( iter[2] == 2 );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 4, 3, 2, 7, 8, 9 };
REQUIRE(iter.storage() == data_exp);
}
// 1 reference emplace_back(Args&&... args)
size_pre = iter.size();
REQUIRE( iter == iter.begin()+5 );
iter.emplace_back( static_cast<uint64_t>(2) );
iter.emplace_back( static_cast<uint64_t>(3) );
iter.emplace_back( static_cast<uint64_t>(4) );
REQUIRE( iter == iter.end() );
REQUIRE( iter[-1] == 4 );
REQUIRE( iter[-2] == 3 );
REQUIRE( iter[-3] == 2 );
{
storage_t data_exp = { 2, 3, 4, 5, 6, 4, 3, 2, 7, 8, 9, 2, 3, 4 };
REQUIRE(iter.storage() == data_exp);
}
// multiple erase()
size_pre = iter.size();
REQUIRE( iter == iter.end() );
iter -= 10;
REQUIRE( iter == iter.end()-10 );
{
int count = 0;
while( iter != iter.end() ) {
iter.erase();
count++;
}
REQUIRE( iter.size() == size_pre - 10 );
}
{
storage_t data_exp = { 2, 3, 4, 5 };
REQUIRE(iter.storage() == data_exp);
}
iter.to_begin(); // set to its begin
// write back ..
REQUIRE( iter != data.cbegin() ); // still not the same
REQUIRE( iter.storage() != *data.snapshot() ); // neither content
{
T data_exp = { 2, 3, 4, 5 };
REQUIRE(data != data_exp);
}
iter.write_back(); // invalidates iter and ...
{
T data_exp = { 2, 3, 4, 5 };
REQUIRE(data == data_exp); // now got the newt content
}
}
return true;
}
template<class T>
static bool test_mutable_iterator_ops(const std::string& type_id,
std::enable_if_t< !is_cow_type<T>::value, bool> = true )
{
typedef typename T::iterator iter_type;
typedef typename T::const_iterator citer_type;
typedef typename T::difference_type diff_type;
printf("**** test_mutable_iterator_ops(___): %s\n", type_id.c_str());
{
T data;
fill_list(data, 10);
iter_type begin = data.begin();
iter_type end = data.end();
diff_type data_size = static_cast<diff_type>(data.size());
REQUIRE( end - begin == data_size );
REQUIRE( end - data_size == begin );
REQUIRE( begin + data_size == end );
REQUIRE( *( end - data_size ) == *begin );
REQUIRE( *( begin + data_size ) == *end );
test_iterator_dereference<T, iter_type>(data.size(), begin, end);
}
{
T data;
fill_list(data, 10);
// all 4 combinations of iter, citer:
iter_type begin = data.begin();
iter_type end = data.end();
citer_type cend = data.cend();
citer_type cbegin = data.cbegin();
test_iterator_arithmetic<T>(data.size(), begin, end);
test_iterator_arithmetic<T>(data.size(), cbegin, cend);
test_iterator_arithmetic<T>(data.size(), begin, cend);
test_iterator_arithmetic<T>(data.size(), cbegin, end);
}
// iterator-op: darray/vector-op
// -------------------------------------------
// 1 pop_back()
// 1 erase (citer_type pos)
// 3 erase (iterator first, citer_type last)
// 1 iterator insert(citer_type pos, const value_type& x)
// 0 iterator insert(citer_type pos, value_type&& x)
// 1 emplace(citer_type pos, Args&&... args)
// 2 insert( citer_type pos, InputIt first, InputIt last )
// 1 void push_back(value_type& x)
// 1 void push_back(value_type&& x)
// 1 reference emplace_back(Args&&... args)
// 0 [void push_back( InputIt first, InputIt last )]
{
T data;
fill_list(data, 10);
{
T data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
REQUIRE(data == data_exp);
}
iter_type iter = data.end();
typename T::size_type size_pre = data.size();
typename T::value_type elem = iter[-2];
// pop_back()
int i;
(void)i;
data.pop_back();
iter--;
REQUIRE( data.size() == size_pre-1 );
REQUIRE( iter == data.end() );
REQUIRE( iter == data.begin()+size_pre-1 );
REQUIRE( iter[-1] == elem );
{
T data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// insert( citer_type pos, InputIt first, InputIt last )
REQUIRE( iter == data.end() );
size_pre = data.size();
{
T data2;
fill_list(data2, 10);
iter = data.insert(iter, data2.cbegin(), data2.cbegin()+data2.size()); // same as push_pack(..) since pointing to end() - but data points here to first new elem
}
REQUIRE( data.size() == size_pre+10 );
REQUIRE( iter == data.end()-10 );
{
T data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
REQUIRE(data == data_exp);
}
// erase (count): erase (iterator first, citer_type last)
REQUIRE( iter == data.end()-10 );
size_pre = data.size();
// std::cout << "data.5" << data << (data - data.begin()) << "/" << data.size() << ", size2 " << size2 << std::endl;
iter = data.erase(iter, iter+10);
// std::cout << "data.6" << data << (data - data.begin()) << "/" << data.size() << std::endl;
REQUIRE( data.size() == size_pre-10 );
REQUIRE( iter == data.end() );
{
T data_exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// erase ()
size_pre = data.size();
iter = data.begin();
REQUIRE( iter == data.begin() );
elem = iter[1];
iter = data.erase(iter);
REQUIRE( data.size() == size_pre-1 );
REQUIRE( iter == data.begin() );
REQUIRE( *iter == elem );
{
T data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// void push_back(value_type& x)
size_pre = data.size();
REQUIRE( iter == data.begin() );
elem = data.end()[-1];
{
T data2;
fill_list(data2, 10);
data.push_back(data2[0]);
data.push_back(data2[1]);
data.push_back(data2[2]);
iter = data.end();
REQUIRE( data.size() == size_pre+3 );
REQUIRE( iter == data.end() );
REQUIRE( iter[-3] == data2[0] );
REQUIRE( iter[-2] == data2[1] );
REQUIRE( iter[-1] == data2[2] );
}
{
T data_exp = { 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3 };
REQUIRE(data == data_exp);
}
// erase (count): erase (iterator first, citer_type last)
size_pre = data.size();
REQUIRE( iter == data.end() );
iter -= 3;
iter = data.erase(iter, iter+3);
REQUIRE( data.size() == size_pre-3 );
REQUIRE( iter == data.end() );
{
T data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// void push_back(value_type&& x)
size_pre = data.size();
REQUIRE( iter == data.end() );
{
typename T::value_type elem0 = data.begin()[0];
data.push_back( std::move(elem0));
}
{
typename T::value_type elem0 = data.begin()[1];
data.push_back( std::move(elem0));
}
{
typename T::value_type elem0 = data.begin()[2];
data.push_back( std::move(elem0));
}
iter = data.end();
REQUIRE( data.size() == size_pre+3 );
REQUIRE( iter == data.end() );
REQUIRE( iter[-3] == data.begin()[0] );
REQUIRE( iter[-2] == data.begin()[1] );
REQUIRE( iter[-1] == data.begin()[2] );
{
T data_exp = { 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4 };
REQUIRE(data == data_exp);
}
// erase last three
REQUIRE( iter == data.end() );
iter -= 3;
iter = data.erase(iter);
iter = data.erase(iter);
iter = data.erase(iter);
REQUIRE( iter == data.end() );
{
T data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// iterator insert(citer_type pos, const value_type& x)
iter = data.begin();
iter += 5;
REQUIRE( iter == data.begin()+5 );
size_pre = data.size();
{
T data2;
fill_list(data2, 10);
iter = data.insert(iter, data2[0]);
iter = data.insert(iter, data2[1]);
iter = data.insert(iter, data2[2]);
REQUIRE( data.size() == size_pre+3 );
REQUIRE( iter == data.begin()+5 );
iter = data.begin();
REQUIRE( iter[5] == data2[2] );
REQUIRE( iter[6] == data2[1] );
REQUIRE( iter[7] == data2[0] );
}
{
T data_exp = { 2, 3, 4, 5, 6, 3, 2, 1, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// insert( citer_type pos, InputIt first, InputIt last )
iter += 5;
REQUIRE( iter == data.begin()+5 );
size_pre = data.size();
{
T data2;
fill_list(data2, 10);
iter = data.insert(iter, data2.cbegin(), data2.cbegin()+3);
}
REQUIRE( data.size() == size_pre+3 );
REQUIRE( iter == data.begin()+5 );
{
T data_exp = { 2, 3, 4, 5, 6, 1, 2, 3, 3, 2, 1, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// erase (count): erase (iterator first, citer_type last)
REQUIRE( iter == data.begin()+5 );
size_pre = data.size();
iter = data.erase(iter, iter+6);
REQUIRE( data.size() == size_pre-6 );
REQUIRE( iter == data.begin()+5 );
{
T data_exp = { 2, 3, 4, 5, 6, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// 1 emplace(Args&&... args): emplace(citer_type pos, Args&&... args)
size_pre = data.size();
REQUIRE( iter == data.begin()+5 );
iter = data.emplace(iter, static_cast<uint64_t>(2) );
iter = data.emplace(iter, static_cast<uint64_t>(3) );
iter = data.emplace(iter, static_cast<uint64_t>(4) );
REQUIRE( iter == data.begin()+5 );
REQUIRE( iter[0] == 4 );
REQUIRE( iter[1] == 3 );
REQUIRE( iter[2] == 2 );
{
T data_exp = { 2, 3, 4, 5, 6, 4, 3, 2, 7, 8, 9 };
REQUIRE(data == data_exp);
}
// 1 reference emplace_back(Args&&... args)
size_pre = data.size();
REQUIRE( iter == data.begin()+5 );
data.emplace_back( static_cast<uint64_t>(2) );
data.emplace_back( static_cast<uint64_t>(3) );
data.emplace_back( static_cast<uint64_t>(4) );
iter = data.end();
REQUIRE( iter == data.end() );
REQUIRE( iter[-1] == 4 );
REQUIRE( iter[-2] == 3 );
REQUIRE( iter[-3] == 2 );
{
T data_exp = { 2, 3, 4, 5, 6, 4, 3, 2, 7, 8, 9, 2, 3, 4 };
REQUIRE(data == data_exp);
}
// multiple erase()
size_pre = data.size();
REQUIRE( iter == data.end() );
iter -= 10;
REQUIRE( iter == data.end()-10 );
{
int count = 0;
while( iter != data.end() ) {
iter = data.erase(iter);
count++;
}
REQUIRE( data.size() == size_pre - 10 );
REQUIRE( iter == data.end() );
}
{
T data_exp = { 2, 3, 4, 5 };
REQUIRE(data == data_exp);
}
}
{
T data;
fill_list(data, 10);
T data2 = data;
T data3(data);
print_list("orig", data2);
print_list("copy1", data2);
print_list("copy2", data3);
REQUIRE(data == data2);
REQUIRE(data == data3);
}
return true;
}
/****************************************************************************************
****************************************************************************************/
template<class T>
static bool test_01_validate_iterator_ops(const std::string& type_id) {
test_citer_type_ops<T>(type_id);
test_mutable_iterator_ops<T>(type_id);
return true;
}
template<class T>
static bool test_01_cow_iterator_properties(const std::string& type_id) {
typedef typename T::size_type size_type;
typedef typename T::const_iterator citer_type;
typedef typename T::iterator iter_type;
printf("**** test_cow_iterator_properties: %s\n", type_id.c_str());
print_iterator_info<citer_type>("citer_type");
print_iterator_info<iter_type>("iter_type");
const size_type size0 = 100;
T data;
REQUIRE(0 == data.get_allocator().memory_usage);
REQUIRE(data.size() == 0);
REQUIRE(data.capacity() == 0);
REQUIRE(data.empty() == true);
fill_list(data, size0);
REQUIRE(0 != data.get_allocator().memory_usage);
REQUIRE(data.size() == size0);
REQUIRE(data.size() <= data.capacity());
// test relationship and distance with mixed iterator and citer_type
// in both direction using the free overloaded operator of cow_ro_* and cow_rw_*
{
iter_type iter1 = data.begin();
citer_type citer2 = iter1.immutable();
citer_type citer3 = iter1.immutable().to_end();
REQUIRE( iter1.is_begin() );
REQUIRE( citer2.is_begin() );
REQUIRE( citer3.is_end() );
REQUIRE( iter1.dist_begin() == 0);
REQUIRE( iter1.dist_end() == size0);
REQUIRE( citer2.dist_begin() == 0);
REQUIRE( citer2.dist_end() == size0);
REQUIRE( citer3.dist_begin() == size0);
REQUIRE( citer3.dist_end() == 0);
REQUIRE( ( iter1 == citer2 ) == true); // iter op==()
REQUIRE( ( citer2 == iter1 ) == true); // iter op==()
++citer2;
REQUIRE( ( citer2 != iter1 ) == true); // iter op==()
REQUIRE( ( iter1 != citer2 ) == true); // iter op==()
REQUIRE( ( citer2 > iter1 ) == true); // iter op==()
REQUIRE( ( citer2 >= iter1 ) == true); // iter op==()
REQUIRE( ( iter1 < citer2 ) == true); // iter op==()
REQUIRE( ( iter1 <= citer2 ) == true); // iter op==()
REQUIRE( ( citer2 - iter1 ) == 1); // iter op==()
REQUIRE( ( iter1 - citer2 ) == -1); // iter op==()
REQUIRE( citer2.dist_begin() == 1);
REQUIRE( citer2.dist_end() == size0-1);
--citer2;
++iter1;
REQUIRE( ( iter1 != citer2 ) == true); // iter op==()
REQUIRE( ( citer2 != iter1 ) == true); // iter op==()
REQUIRE( ( iter1 > citer2 ) == true); // iter op==()
REQUIRE( ( iter1 >= citer2 ) == true); // iter op==()
REQUIRE( ( citer2 < iter1 ) == true); // iter op==()
REQUIRE( ( citer2 <= iter1 ) == true); // iter op==()
REQUIRE( ( iter1 - citer2 ) == 1); // iter op==()
REQUIRE( ( citer2 - iter1 ) == -1); // iter op==()
REQUIRE( iter1.dist_begin() == 1);
REQUIRE( iter1.dist_end() == size0-1);
REQUIRE( citer2.dist_begin() == 0);
REQUIRE( citer2.dist_end() == size0);
REQUIRE( ( iter1.end() == citer3 ) == true); // iter op==()
REQUIRE( ( iter1.to_end() == citer3 ) == true); // iter op==()
REQUIRE( iter1.is_end() );
REQUIRE( citer3.is_end() );
REQUIRE( iter1.dist_begin() == size0);
REQUIRE( iter1.dist_end() == 0);
}
// test mutable non-const 'new store' behavior
citer_type c_begin0 = data.cbegin(); // orig store
{
// iterator_type is mutable iterator.
// The cow_rw_iterator is being fetched via data.begin(), which creates a new store and stays.
// The cow_rw_iterator's new store is moved into the cow container via write_back() later.
printf("testing mutable non-const behavior.\n");
citer_type c_begin1;
{
iter_type m_begin1 = data.begin(); // mutable new_store non-const iterator, gets held until write_back() or destruction
c_begin1 = m_begin1.immutable(); // get immutable citer_type from newly created store
REQUIRE(*c_begin1 == *m_begin1);
REQUIRE( c_begin1 == m_begin1);
REQUIRE( ( c_begin1 - m_begin1 ) == 0);
printf(" 1st store: %s == %s, dist %u\n",
uint64DecString(*c_begin1, ',', 2).c_str(), uint64DecString(*m_begin1, ',', 2).c_str(), (unsigned int)(c_begin1 - m_begin1));
citer_type c_begin2;
{
iter_type m_begin2 = data.begin(); // mutable new_store non-const iterator, gets held until write_back() or destruction
c_begin2 = m_begin2.immutable(); // get immutable citer_type from newly created store
REQUIRE(*c_begin2 == *m_begin2);
REQUIRE( c_begin2 == m_begin2);
REQUIRE( ( c_begin2 - m_begin2 ) == 0);
printf(" 2nd store: %s == %s, dist %u\n",
uint64DecString(*c_begin2, ',', 2).c_str(), uint64DecString(*m_begin2, ',', 2).c_str(), (unsigned int)(c_begin2 - m_begin2));
REQUIRE(*c_begin2 == *c_begin1);
REQUIRE( c_begin2 != c_begin1);
REQUIRE( ( c_begin2 - c_begin1 ) != 0);
printf("2nd -> 1st store: %s == %s, dist %u\n",
uint64DecString(*c_begin2, ',', 2).c_str(), uint64DecString(*c_begin1, ',', 2).c_str(), (unsigned int)(c_begin2 - c_begin1));
m_begin2.write_back(); // write back storage of m_begin2 to parent CoW and invalidate m_begin2
}
// 2nd store -> cow_xxx
citer_type c_begin2b = data.cbegin();
REQUIRE(*c_begin2 == *c_begin2b);
REQUIRE( c_begin2 == c_begin2b);
REQUIRE( ( c_begin2 - c_begin2b ) == 0);
printf("2nd -> cow == cbegin: %s == %s, dist %u\n",
uint64DecString(*c_begin2, ',', 2).c_str(), uint64DecString(*c_begin2b, ',', 2).c_str(), (unsigned int)(c_begin2 - c_begin2b));
printf("2nd -> 1st : %s == %s, dist %u\n",
uint64DecString(*c_begin1, ',', 2).c_str(), uint64DecString(*c_begin2, ',', 2).c_str(), (unsigned int)(c_begin1 - c_begin2));
m_begin1.write_back(); // write back storage of m_begin1 to parent CoW and invalidate m_begin2
}
// 1st store -> cow_xxx
citer_type c_begin1b = data.cbegin();
printf("1st -> cow == cbegin: %s == %s, dist %u\n",
uint64DecString(*c_begin1, ',', 2).c_str(), uint64DecString(*c_begin1b, ',', 2).c_str(), (unsigned int)(c_begin1 - c_begin1b));
REQUIRE(*c_begin1 == *c_begin1b);
REQUIRE( c_begin1 == c_begin1b);
REQUIRE( ( c_begin1 - c_begin1b ) == 0);
}
return true;
}
/****************************************************************************************
****************************************************************************************/
TEST_CASE( "Iterator Test 00 - Inspect all Iterator Types", "[datatype][std][vector][darray][cow_vector][cow_darray]" ) {
test_00_inspect_iterator_types< std_vector_uint64_t >("std::vector<T>");
test_00_inspect_iterator_types< jau_darray_uint64_t >("jau::darray<T>");
test_00_inspect_iterator_types< jau_cow_vector_uint64_t >("jau::cow_vector<T>");
test_00_inspect_iterator_types< jau_cow_darray_uint64_t >("jau::cow_darray<T>");
}
TEST_CASE( "STD Vector Test 01 - Validate Iterator and Index Operations", "[datatype][std][vector]" ) {
test_01_validate_iterator_ops< std_vector_uint64_t >("std::vector<T>");
}
TEST_CASE( "JAU DArray Test 02 - Validate Iterator and Index Operations", "[datatype][jau][darray]" ) {
test_01_validate_iterator_ops< jau_darray_uint64_t >("jau::darray<T>");
}
TEST_CASE( "JAU COW_Vector Test 11 - Validate Iterator Operations", "[datatype][jau][cow_vector]" ) {
test_01_validate_iterator_ops< jau_cow_vector_uint64_t >("jau::cow_vector<T>");
test_01_cow_iterator_properties<jau_cow_vector_uint64_t>("jau::cow_vector<T>");
}
TEST_CASE( "JAU COW_DArray Test 21 - Validate Iterator Operations", "[datatype][jau][cow_darray]" ) {
test_01_validate_iterator_ops< jau_cow_darray_uint64_t >("jau::cow_darray<T>");
test_01_cow_iterator_properties<jau_cow_darray_uint64_t>("jau::cow_darray<T>");
}
|