-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathplf_colony_test_suite.cpp
2253 lines (1563 loc) · 55.9 KB
/
plf_colony_test_suite.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
// Basic feature testing for colony.
#define PLF_COLONY_TEST_DEBUG
#if defined(_MSC_VER) && !defined(__clang__) && !defined(__GNUC__)
#if _MSC_VER >= 1600
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#endif
#if _MSC_VER >= 1700
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#endif
#if _MSC_VER >= 1800
#define PLF_TEST_VARIADICS_SUPPORT // Variadics, in this context, means both variadic templates and variadic macros are supported
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if defined(_MSVC_LANG) && (_MSVC_LANG >= 202002L) && _MSC_VER >= 1929
#define PLF_TEST_CPP20_SUPPORT
#endif
#elif defined(__cplusplus) && __cplusplus >= 201103L // C++11 support, at least
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && !defined(__clang__) // If compiler is GCC/G++
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 // 4.2 and below do not support variadic templates
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#define PLF_TEST_VARIADICS_SUPPORT
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4 // 4.3 and below do not support initializer lists
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if __GNUC__ >= 5 // GCC v4.9 and below do not support std::is_trivially_copyable
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#endif
#elif defined(__clang__) && !defined(__GLIBCXX__) && !defined(_LIBCPP_CXX03_LANG)
#if __clang_major__ >= 3 // clang versions < 3 don't support __has_feature() or traits
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#if __has_feature(cxx_rvalue_references) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#endif
#if __has_feature(cxx_variadic_templates) && !defined(_LIBCPP_HAS_NO_VARIADICS)
#define PLF_TEST_VARIADICS_SUPPORT
#endif
#if (__clang_major__ == 3 && __clang_minor__ >= 1) || __clang_major__ > 3
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#endif
#elif defined(__GLIBCXX__)
#if __GLIBCXX__ >= 20080606
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#define PLF_TEST_VARIADICS_SUPPORT
#endif
#if __GLIBCXX__ >= 20090421
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if __GLIBCXX__ >= 20150422
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#endif
#elif !(defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) || defined(_LIBCPP_HAS_NO_VARIADICS))
// Assume full support for other compilers and standard libraries
#define PLF_TEST_VARIADICS_SUPPORT
#define PLF_TEST_TYPE_TRAITS_SUPPORT
#define PLF_TEST_MOVE_SEMANTICS_SUPPORT
#define PLF_TEST_INITIALIZER_LIST_SUPPORT
#endif
#if __cplusplus > 201704L && ((((defined(__clang__) && !defined(__APPLE_CC__) && __clang_major__ >= 14) || (defined(__GNUC__) && (__GNUC__ > 11 || (__GNUC__ == 11 && __GNUC_MINOR__ > 0)))) && ((defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 14) || (defined(__GLIBCXX__) && __GLIBCXX__ >= 201806L))) || (!defined(__clang__) && !defined(__GNUC__)))
#define PLF_TEST_CPP20_SUPPORT
#endif
#endif
#include <numeric> // std::accumulate
#include <functional> // std::greater, std::bind2nd
#include <vector> // range-insert testing
#include <algorithm> // std::find
#include <cstdio> // log redirection, printf
#include <cstdlib> // abort
#ifdef MOVE_SEMANTICS_SUPPORT
#include <utility> // std::move
#endif
#ifdef PLF_TEST_CPP20_SUPPORT
#include <ranges>
#endif
#include "plf_colony.h"
void message(const char *message_text)
{
printf("%s\n", message_text);
}
void title1(const char *title_text)
{
printf("\n\n\n*** %s ***\n", title_text);
printf("===========================================\n\n\n");
}
void title2(const char *title_text)
{
printf("\n\n--- %s ---\n\n", title_text);
}
void failpass(const char *test_type, bool condition)
{
printf("%s: ", test_type);
if (condition)
{
printf("Pass\n");
}
else
{
printf("Fail\n");
getchar();
abort();
}
}
#ifdef PLF_TEST_VARIADICS_SUPPORT
struct perfect_forwarding_test
{
const bool success;
perfect_forwarding_test(int&& /*perfect1*/, int& perfect2)
: success(true)
{
perfect2 = 1;
}
template <typename T, typename U>
perfect_forwarding_test(T&& /*imperfect1*/, U&& /*imperfect2*/)
: success(false)
{}
};
class non_copyable_type
{
private:
int i;
non_copyable_type(const non_copyable_type &); // non construction-copyable
non_copyable_type& operator=(const non_copyable_type &); // non copyable
public:
non_copyable_type(int a) : i(a) {}
};
#endif
struct small_struct
{
double *empty_field_1;
double unused_number;
unsigned int empty_field2;
double *empty_field_3;
int number;
unsigned int empty_field4;
int operator * () const { return number; };
bool operator == (const small_struct &source) const { return source.number == number; };
bool operator != (const small_struct &source) const { return source.number != number; };
bool operator > (const small_struct &source) const { return number > source.number; };
bool operator < (const small_struct &source) const { return number < source.number; };
bool operator >= (const small_struct &source) const { return number >= source.number; };
bool operator <= (const small_struct &source) const { return number <= source.number; };
small_struct(const unsigned int num): number(num) {};
};
int global_counter = 0;
struct small_struct_non_trivial
{
double *empty_field_1;
double unused_number;
unsigned int empty_field2;
double *empty_field_3;
int number;
unsigned int empty_field4;
small_struct_non_trivial(const int num) : number(num) {}
~small_struct_non_trivial() { ++global_counter; }
};
int main()
{
freopen("error.log","w", stderr); // For catching assertion failure info when run outside of a command line prompt
using namespace std;
using namespace plf;
for (unsigned int looper = 0; looper != 100; ++looper)
{
{
title1("Colony");
title2("Test Basics");
colony<int *> p_colony;
failpass("Colony empty", p_colony.empty());
int ten = 10;
p_colony.insert(&ten);
failpass("Colony not-empty", !p_colony.empty());
title2("Iterator tests");
failpass("Begin() working", **p_colony.begin() == 10);
failpass("End() working", p_colony.begin() != p_colony.end());
p_colony.clear();
failpass("Begin = End after clear", p_colony.begin() == p_colony.end());
int twenty = 20;
for (unsigned int temp = 0; temp != 200; ++temp)
{
p_colony.insert(&ten);
p_colony.insert(&twenty);
}
int total = 0, numtotal = 0;
for(colony<int *>::iterator the_iterator = p_colony.begin(); the_iterator != p_colony.end(); ++the_iterator)
{
++total;
numtotal += **the_iterator;
}
failpass("Iteration count test", total == 400);
failpass("Iterator access test", numtotal == 6000);
colony<int *>::iterator plus_twenty = p_colony.begin();
advance(plus_twenty, 20);
colony<int *>::iterator plus_two_hundred = p_colony.begin();
advance(plus_two_hundred, 200);
failpass("Iterator + distance test", distance(p_colony.begin(), plus_twenty) == 20);
failpass("Iterator - distance test", distance(plus_two_hundred, p_colony.begin()) == -200);
{
colony<int> d_colony(1000, 1, plf::limits(20, 20));
for (colony<int>::iterator current = d_colony.begin(), end = d_colony.end(); current != end;)
{
if ((rand() & 7) == 0)
{
current = d_colony.erase(current);
}
else
{
++current;
}
}
int d_size = static_cast<int>(d_colony.size());
for (int counter = 0; counter != 10000; ++counter)
{
const int dist1 = rand() % (d_size - 2), dist2 = rand() % ((d_size - 2) - dist1);
colony<int>::iterator first = d_colony.begin(), last;
advance(first, dist1);
last = first;
advance(last, dist2);
if (last > d_colony.end()) last = d_colony.end();
const int dist = static_cast<int>(distance(first, last));
if (dist != dist2)
{
printf("positive distance overload fuzz-test failed, real distance = %d, reported distance = %d, counter = %d, suite loop = %d", dist2, dist, counter, looper);
getchar();
abort();
}
}
failpass("Positive distance overload fuzz-test", true);
for (colony<int>::iterator current = d_colony.begin(), end = d_colony.end(); current!= end;)
{
if ((rand() & 3) == 0)
{
current = d_colony.erase(current);
}
else
{
++current;
}
}
d_size = static_cast<int>(d_colony.size());
for (int counter = 0; counter != 10000; ++counter)
{
const int dist1 = rand() % (d_size - 2), dist2 = rand() % (d_size - 2);
colony<int>::iterator first = d_colony.begin(), last = d_colony.begin();
advance(first, dist1);
advance(last, dist2);
if (last > d_colony.end()) last = d_colony.end();
const int dist = static_cast<int>(distance(first, last));
if (dist != dist2 - dist1)
{
printf("positive/negative distance overload fuzz-test failed, real distance = %d, reported distance = %d, counter = %d, suite loop = %d", dist2, dist, counter, looper);
getchar();
abort();
}
}
failpass("Positive/negative distance overload fuzz-test", true);
}
#ifdef PLF_TEST_CPP20_SUPPORT
colony<int *>::const_iterator plus_two_hundred_c = plus_two_hundred;
colony<int *> colony_copy(plus_twenty, plus_two_hundred_c);
total = 0;
for(colony<int *>::iterator the_iterator = colony_copy.begin(); the_iterator != colony_copy.end(); ++the_iterator)
{
++total;
}
failpass("Range-constructor with differing iterator types test", total == 180);
#endif
colony<int *>::iterator next_iterator = next(p_colony.begin(), 5);
colony<int *>::const_iterator prev_iterator = prev(p_colony.cend(), 300);
failpass("Iterator next test", distance(p_colony.begin(), next_iterator) == 5);
failpass("Const iterator prev test", distance(p_colony.cend(), prev_iterator) == -300);
#if defined(__cplusplus) && __cplusplus >= 201402L
colony<int *>::iterator prev_iterator2 = prev(p_colony.end(), 300);
failpass("Iterator/Const iterator equality operator test", prev_iterator == prev_iterator2);
#endif
prev_iterator = p_colony.begin();
advance(prev_iterator, 5);
failpass("Iterator/Const iterator equality operator test 2", prev_iterator == next_iterator);
colony<int *> p_colony2;
p_colony2 = p_colony;
colony<int *> p_colony3(p_colony);
colony<int *> p_colony4(p_colony2, p_colony2.get_allocator());
colony<int *>::iterator it1 = p_colony.begin();
colony<int *>::const_iterator cit(it1);
failpass("Copy test", p_colony2.size() == 400);
failpass("Copy construct test", p_colony3.size() == 400);
failpass("Allocator-extended copy construct test", p_colony4.size() == 400);
failpass("Equality operator test", p_colony == p_colony2);
failpass("Equality operator test 2", p_colony2 == p_colony3);
p_colony2.insert(&ten);
failpass("Inequality operator test", p_colony2 != p_colony3);
#ifdef PLF_TEST_CPP20_SUPPORT
failpass("Spaceship operator test", (p_colony2 <=> p_colony3) != 0);
#endif
numtotal = 0;
total = 0;
for (colony<int *>::reverse_iterator the_iterator = p_colony.rbegin(); the_iterator != p_colony.rend(); ++the_iterator)
{
++total;
numtotal += **the_iterator;
}
failpass("Reverse iteration count test", total == 400);
failpass("Reverse iterator access test", numtotal == 6000);
colony<int *>::reverse_iterator r_iterator = p_colony.rbegin();
advance(r_iterator, 50);
failpass("Reverse iterator advance and distance test", distance(p_colony.rbegin(), r_iterator) == 50);
colony<int *>::reverse_iterator r_iterator2 = next(r_iterator, 2);
failpass("Reverse iterator next and distance test", distance(p_colony.rbegin(), r_iterator2) == 52);
numtotal = 0;
total = 0;
for(colony<int *>::iterator the_iterator = p_colony.begin(); the_iterator < p_colony.end(); advance(the_iterator, 2))
{
++total;
numtotal += **the_iterator;
}
failpass("Multiple iteration test", total == 200);
failpass("Multiple iteration access test", numtotal == 2000);
numtotal = 0;
total = 0;
for(colony<int *>::const_iterator the_iterator = p_colony.cbegin(); the_iterator != p_colony.cend(); ++the_iterator)
{
++total;
numtotal += **the_iterator;
}
failpass("Const_iterator test", total == 400);
failpass("Const_iterator access test", numtotal == 6000);
numtotal = 0;
total = 0;
for(colony<int *>::const_reverse_iterator the_iterator = --colony<int *>::const_reverse_iterator(p_colony.crend()); the_iterator != colony<int *>::const_reverse_iterator(p_colony.crbegin()); --the_iterator)
{
++total;
numtotal += **the_iterator;
}
failpass("Const_reverse_iterator -- test", total == 399);
failpass("Const_reverse_iterator -- access test", numtotal == 5980);
total = 0;
for(colony<int *>::iterator the_iterator = ++colony<int *>::iterator(p_colony.begin()); the_iterator < p_colony.end(); ++the_iterator)
{
++total;
the_iterator = p_colony.erase(the_iterator);
}
failpass("Partial erase iteration test", total == 200);
failpass("Post-erase size test", p_colony.size() == 200);
#ifdef PLF_TEST_INITIALIZER_LIST_SUPPORT
{
colony<int> trim_colony(2000, 10, {200, 200});
trim_colony.reserve(4000);
trim_colony.trim_capacity(3000);
failpass("trim_capacity(n) test", trim_colony.capacity() == 3000);
}
#endif
const unsigned int temp_capacity = static_cast<unsigned int>(p_colony.capacity());
p_colony.shrink_to_fit();
failpass("Shrink_to_fit test", p_colony.capacity() < temp_capacity);
failpass("Shrink_to_fit test 2", p_colony.capacity() == 200);
total = 0;
for(colony<int *>::reverse_iterator the_iterator = p_colony.rbegin(); the_iterator != p_colony.rend(); ++the_iterator)
{
colony<int *>::iterator it = the_iterator.base();
p_colony.erase(--it);
++total;
}
failpass("Full erase reverse iteration test", total == 200);
failpass("Post-erase size test", p_colony.size() == 0);
for (unsigned int temp = 0; temp != 200; ++temp)
{
p_colony.insert(&ten);
p_colony.insert(&twenty);
}
total = 0;
for(colony<int *>::iterator the_iterator = --colony<int *>::iterator(p_colony.end()); the_iterator != p_colony.begin(); --the_iterator)
{
++total;
}
failpass("Negative iteration test", total == 399);
total = 0;
for(colony<int *>::iterator the_iterator = --(colony<int *>::iterator(p_colony.end())); the_iterator != p_colony.begin(); advance(the_iterator, -2))
{
++total;
}
failpass("Negative multiple iteration test", total == 200);
#ifdef PLF_TEST_MOVE_SEMANTICS_SUPPORT
p_colony2 = std::move(p_colony);
failpass("Move test", p_colony2.size() == 400);
p_colony.insert(&ten);
failpass("Insert to post-moved-colony test", p_colony.size() == 1);
colony<int *> p_colony5(p_colony2);
colony<int *> p_colony6(std::move(p_colony5), p_colony2.get_allocator());
failpass("Allocator-extended move construct test", p_colony6.size() == 400);
#else
p_colony2 = p_colony;
#endif
p_colony3 = p_colony2;
failpass("Copy test 2", p_colony3.size() == 400);
p_colony2.insert(&ten);
p_colony2.swap(p_colony3);
failpass("Swap test", p_colony2.size() == p_colony3.size() - 1);
std::swap(p_colony2, p_colony3);
failpass("Swap test 2", p_colony3.size() == p_colony2.size() - 1);
failpass("max_size() test", p_colony2.max_size() > p_colony2.size());
}
{
title2("Iterator comparison tests");
colony<int> i_colony;
for (int temp = 0; temp != 10; ++temp)
{
i_colony.insert(temp);
}
colony<int>::iterator it1 = i_colony.begin(), it2 = i_colony.begin();
++it2;
++it2;
++it2;
failpass("Iterator ++ test", *it2 == 3);
failpass("Iterator > test", it2 > it1);
failpass("Iterator >= test", it2 >= it1);
failpass("Iterator < test", it1 < it2);
failpass("Iterator <= test", it1 <= it2);
failpass("Iterator != test", it2 != it1);
#ifdef PLF_TEST_CPP20_SUPPORT
failpass("Iterator <=> test 1", (it2 <=> it1) == std::strong_ordering::greater);
failpass("Iterator <=> test 2", (it1 <=> it2) == std::strong_ordering::less);
it1 = it2;
failpass("Iterator <=> test 3", (it1 <=> it2) == std::strong_ordering::equal);
#endif
}
{
title2("Insert and Erase tests");
colony<int> i_colony;
for (int temp = 0; temp != 500000; ++temp)
{
i_colony.insert(temp);
}
failpass("Size after insert test", i_colony.size() == 500000);
colony<int>::iterator found_item = std::find(i_colony.begin(), i_colony.end(), 5000);;
failpass("std::find iterator test", *found_item == 5000);
colony<int>::reverse_iterator found_item2 = std::find(i_colony.rbegin(), i_colony.rend(), 5000);;
failpass("std::find reverse_iterator test", *found_item2 == 5000);
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end(); ++the_iterator)
{
the_iterator = i_colony.erase(the_iterator);
}
failpass("Erase alternating test", i_colony.size() == 250000);
do
{
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end();)
{
if ((rand() & 7) == 0)
{
the_iterator = i_colony.erase(the_iterator);
}
else
{
++the_iterator;
}
}
} while (!i_colony.empty());
failpass("Erase randomly till-empty test", i_colony.size() == 0);
i_colony.reset();
i_colony.reshape(plf::limits(100, i_colony.block_capacity_limits().max));
i_colony.insert(30000, 1); // fill-insert 30000 elements
failpass("Size after reinitialize + fill-insert test", i_colony.size() == 30000);
unsigned short count2 = 0;
do
{
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end();)
{
if ((rand() & 7) == 0)
{
the_iterator = i_colony.erase(the_iterator);
++count2;
}
else
{
++the_iterator;
}
}
} while (count2 < 15000);
failpass("Erase randomly till half-empty test", i_colony.size() == 30000u - count2);
i_colony.insert(count2, 1);
failpass("Size after reinsert test", i_colony.size() == 30000);
unsigned int sum = 0;
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end();)
{
if (++sum == 3)
{
sum = 0;
the_iterator = i_colony.erase(the_iterator);
}
else
{
i_colony.insert(1);
++the_iterator;
}
}
failpass("Alternating insert/erase test", i_colony.size() == 45001);
do
{
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end();)
{
if ((rand() & 3) == 0)
{
++the_iterator;
i_colony.insert(1);
}
else
{
the_iterator = i_colony.erase(the_iterator);
}
}
} while (!i_colony.empty());;
failpass("Random insert/erase till empty test", i_colony.size() == 0);
i_colony.insert(500000, 10);
failpass("Insert post-erase test", i_colony.size() == 500000);
colony<int>::iterator it2 = i_colony.begin();
advance(it2, 250000);
for (; it2 != i_colony.end();)
{
it2 = i_colony.erase(it2);
}
failpass("Large multi-increment iterator test", i_colony.size() == 250000);
i_colony.insert(250000, 10);
colony<int>::iterator end_iterator = i_colony.end();
colony<int>::iterator end_iterator2 = i_colony.end();
advance(end_iterator, -250000);
for (unsigned int count = 0; count != 250000; ++count, --end_iterator2){}
failpass("Large multi-decrement iterator test 1", end_iterator == end_iterator2);
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != end_iterator;)
{
the_iterator = i_colony.erase(the_iterator);
}
failpass("Large multi-decrement iterator test", i_colony.size() == 250000);
i_colony.insert(250000, 10);
int total = 0;
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end(); ++the_iterator)
{
total += *the_iterator;
}
failpass("Re-insert post-heavy-erasure test", total == 5000000);
end_iterator = i_colony.end();
advance(end_iterator, -50001);
colony<int>::iterator begin_iterator = i_colony.begin();
advance(begin_iterator, 300000);
for (colony<int>::iterator the_iterator = begin_iterator; the_iterator != end_iterator;)
{
the_iterator = i_colony.erase(the_iterator);
}
failpass("Non-end decrement + erase test", i_colony.size() == 350001);
i_colony.insert(100000, 10);
begin_iterator = i_colony.begin();
advance(begin_iterator, 300001);
for (colony<int>::iterator the_iterator = begin_iterator; the_iterator != i_colony.end();)
{
the_iterator = i_colony.erase(the_iterator);
}
failpass("Non-beginning increment + erase test", i_colony.size() == 300001);
colony<int>::iterator temp_iterator = i_colony.begin();
advance(temp_iterator, 20); // Advance test 1
unsigned int index = static_cast<unsigned int>(distance(i_colony.begin(), temp_iterator));
failpass("Advance + iterator-to-index test", index == 20);
i_colony.erase(temp_iterator);
failpass("is_active test 1", i_colony.is_active(temp_iterator) == false);
failpass("get_iterator test 1", i_colony.get_iterator(&(*temp_iterator)) == i_colony.end());
temp_iterator = i_colony.begin(); // Check edge-case with advance when erasures present in initial group
failpass("is_active test 2", i_colony.is_active(temp_iterator) == true);
failpass("get_iterator test 2", i_colony.get_iterator(&(*temp_iterator)) == temp_iterator);
advance(temp_iterator, 500);
index = static_cast<unsigned int>(distance(i_colony.begin(), temp_iterator));
failpass("Advance + iterator-to-index test", index == 500);
colony<int>::iterator temp2 = i_colony.get_iterator(&(*temp_iterator));
colony<int>::const_iterator temp3 = i_colony.get_iterator(const_cast<colony<int>::const_pointer>(&(*temp_iterator)));
failpass("Pointer-to-iterator test", temp2 != i_colony.end());
failpass("Const_pointer-to-const_iterator test", temp3 != i_colony.end());
temp2 = i_colony.begin();
advance(temp2, 500);
failpass("Index-to-iterator test", temp2 == temp_iterator);
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end();)
{
the_iterator = i_colony.erase(the_iterator);
}
failpass("Total erase test", i_colony.empty());
// Test get_iterator etc on an empty colony with empty memory blocks retained:
failpass("is_active test 3", i_colony.is_active(temp_iterator) == false);
failpass("get_iterator test 3", i_colony.get_iterator(&(*temp_iterator)) == i_colony.end());
i_colony.trim_capacity();
// Test get_iterator etc on a colony with no blocks:
failpass("is_active test 4", i_colony.is_active(temp_iterator) == false);
failpass("get_iterator test 4", i_colony.get_iterator(&(*temp_iterator)) == i_colony.end());
i_colony.reshape(plf::limits(3, i_colony.block_capacity_limits().max));
const unsigned int temp_capacity2 = static_cast<unsigned int>(i_colony.capacity());
i_colony.reserve(100000);
failpass("Post-reset reserve test", temp_capacity2 != i_colony.capacity());
i_colony.insert(110000, 1);
failpass("Post-reserve insert test", i_colony.size() == 110000);
unsigned int count = 110000;
for (unsigned int loop1 = 0; loop1 != 50000; ++loop1)
{
for (unsigned int loop = 0; loop != 10; ++loop)
{
if ((rand() & 7) == 0)
{
i_colony.insert(1);
++count;
}
}
for (colony<int>::iterator the_iterator = i_colony.begin(); the_iterator != i_colony.end();)
{
if ((rand() & 7) == 0)
{
the_iterator = i_colony.erase(the_iterator);
--count;
}
else
{
++the_iterator;
}
}
}
failpass("Multiple sequential small insert/erase commands test", count == i_colony.size());
}
{
title2("Range-erase tests");
colony<int> i_colony;
int counter = 0;
for (; counter != 1000; ++counter)
{
i_colony.insert(counter);
}
colony<int>::iterator it1 = i_colony.begin(), it2 = i_colony.begin();
advance(it1, 500);
advance(it2, 800);
i_colony.erase(it1, it2);
counter = 0;
for (colony<int>::iterator it = i_colony.begin(); it != i_colony.end(); ++it)
{
++counter;
}
failpass("Simple range-erase test 1", counter == 700 && i_colony.size() == 700);
it1 = it2 = i_colony.begin();
advance(it1, 400);
advance(it2, 500); // This should put it2 past the point of previous erasures
i_colony.erase(it1, it2);
counter = 0;
for (colony<int>::iterator it = i_colony.begin(); it != i_colony.end(); ++it)
{
++counter;
}
failpass("Simple range-erase test 2", counter == 600 && i_colony.size() == 600);
it2 = it1 = i_colony.begin();
advance(it1, 4);
advance(it2, 9); // This should put it2 past the point of previous erasures
i_colony.erase(it1, it2);
counter = 0;
for (colony<int>::iterator it = i_colony.begin(); it != i_colony.end(); ++it)
{
++counter;
}
failpass("Simple range-erase test 3", counter == 595 && i_colony.size() == 595);
it2 = it1 = i_colony.begin();
advance(it2, 50);
i_colony.erase(it1, it2);
counter = 0;
for (colony<int>::iterator it = i_colony.begin(); it != i_colony.end(); ++it)
{
++counter;
}
failpass("Range-erase from begin()", counter == 545 && i_colony.size() == 545);
it1 = i_colony.begin();
it2 = i_colony.end();
advance(it1, 345); // Test erasing and validity when it removes the final group in colony
i_colony.erase(it1, it2);
counter = 0;
for (colony<int>::iterator it = i_colony.begin(); it != i_colony.end(); ++it)
{
++counter;
}
failpass("Range-erase to end()", counter == 345 && i_colony.size() == 345);
i_colony.clear();
for (counter = 0; counter != 3000; ++counter)
{
i_colony.insert(counter);
}
for (colony<int>::iterator it = i_colony.begin(); it < i_colony.end(); ++it)
{
it = i_colony.erase(it);
}
it2 = it1 = i_colony.begin();
advance(it1, 4);
advance(it2, 600);
i_colony.erase(it1, it2);
counter = 0;
for (colony<int>::iterator it = i_colony.begin(); it != i_colony.end(); ++it)
{
++counter;
}