forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colony_test.cpp
1155 lines (874 loc) · 30.8 KB
/
colony_test.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 <algorithm> // std::find
#include <cstddef>
#include <functional> // std::greater
#include <type_traits>
#include <vector> // range-insert testing
#include "cata_catch.h"
#include "colony.h"
#include "colony_list_test_helpers.h"
TEST_CASE( "colony basics", "[colony]" )
{
cata::colony<int *> test_colony;
// Starts empty
CHECK( test_colony.empty() );
int ten = 10;
test_colony.insert( &ten );
// Not empty after insert
CHECK_FALSE( test_colony.empty() );
// Begin working
CHECK( **test_colony.begin() == 10 );
// End working
CHECK_FALSE( test_colony.begin() == test_colony.end() );
test_colony.clear();
// Begin = End after clear
CHECK( test_colony.begin() == test_colony.end() );
int twenty = 20;
for( int temp = 0; temp < 200; ++temp ) {
test_colony.insert( &ten );
test_colony.insert( &twenty );
}
int count = 0;
int sum = 0;
for( cata::colony<int *>::iterator it = test_colony.begin(); it < test_colony.end(); ++it ) {
++count;
sum += **it;
}
// Iterator count
CHECK( count == 400 );
// Iterator access
CHECK( sum == 6000 );
cata::colony<int *>::iterator plus_20 = test_colony.begin();
cata::colony<int *>::iterator plus_200 = test_colony.begin();
test_colony.advance( plus_20, 20 );
test_colony.advance( plus_200, 200 );
// Iterator + distance
CHECK( test_colony.distance( test_colony.begin(), plus_20 ) == 20 );
// Iterator - distance
CHECK( test_colony.distance( plus_200, test_colony.begin() ) == -200 );
cata::colony<int *>::iterator next_iterator = test_colony.next( test_colony.begin(), 5 );
cata::colony<int *>::const_iterator cprev_iterator = test_colony.prev( test_colony.cend(), 300 );
// Iterator next
CHECK( test_colony.distance( test_colony.begin(), next_iterator ) == 5 );
// Const iterator prev
CHECK( test_colony.distance( test_colony.cend(), cprev_iterator ) == -300 );
cata::colony<int *>::iterator prev_iterator = test_colony.prev( test_colony.end(), 300 );
// Iterator/const iterator equality operator
CHECK( cprev_iterator == prev_iterator );
cata::colony<int *> test_colony_2;
test_colony_2 = test_colony;
cata::colony<int *> test_colony_3( test_colony );
cata::colony<int *> test_colony_4( test_colony_2, test_colony_2.get_allocator() );
// Copy
CHECK( test_colony_2.size() == 400 );
// Copy construct
CHECK( test_colony_3.size() == 400 );
// Allocator-extended copy construct
CHECK( test_colony_4.size() == 400 );
// Equality operator
CHECK( test_colony == test_colony_2 );
CHECK( test_colony_2 == test_colony_3 );
CHECK( test_colony_3 == test_colony_4 );
test_colony_2.insert( &ten );
// Inequality operator
CHECK( test_colony_2 != test_colony_3 );
count = 0;
sum = 0;
for( cata::colony<int *>::reverse_iterator it = test_colony.rbegin(); it != test_colony.rend();
++it ) {
++count;
sum += **it;
}
// Reverse iterator count
CHECK( count == 400 );
// Reverse iterator access
CHECK( sum == 6000 );
cata::colony<int *>::reverse_iterator r_iterator = test_colony.rbegin();
test_colony.advance( r_iterator, 50 );
// Reverse iterator advance and distance test
CHECK( test_colony.distance( test_colony.rbegin(), r_iterator ) == 50 );
cata::colony<int *>::reverse_iterator r_iterator2 = test_colony.next( r_iterator, 2 );
// Reverse iterator next and distance test
CHECK( test_colony.distance( test_colony.rbegin(), r_iterator2 ) == 52 );
count = 0;
sum = 0;
for( cata::colony<int *>::iterator it = test_colony.begin(); it < test_colony.end();
test_colony.advance( it, 2 ) ) {
++count;
sum += **it;
}
// Multiple iteration count
CHECK( count == 200 );
// Multiple iteration access
CHECK( sum == 2000 );
count = 0;
sum = 0;
for( int *p : test_colony ) {
++count;
sum += *p;
}
// Constant iterator count
CHECK( count == 400 );
// Constant iterator access
CHECK( sum == 6000 );
count = 0;
sum = 0;
for( cata::colony<int *>::const_reverse_iterator it = --cata::colony<int *>::const_reverse_iterator(
test_colony.crend() ); it != cata::colony<int *>::const_reverse_iterator( test_colony.crbegin() );
--it ) {
++count;
sum += **it;
}
// Const_reverse_iterator -- count
CHECK( count == 399 );
//Const_reverse_iterator -- access
CHECK( sum == 5980 );
count = 0;
for( cata::colony<int *>::iterator it = ++cata::colony<int *>::iterator( test_colony.begin() );
it < test_colony.end(); ++it ) {
++count;
it = test_colony.erase( it );
}
// Partial erase iteration test
CHECK( count == 200 );
// Post-erase size test
CHECK( test_colony.size() == 200 );
const unsigned int temp_capacity = static_cast<unsigned int>( test_colony.capacity() );
test_colony.shrink_to_fit();
// Shrink_to_fit test
CHECK( test_colony.capacity() < temp_capacity );
CHECK( test_colony.capacity() == 200 );
count = 0;
for( cata::colony<int *>::reverse_iterator it = test_colony.rbegin(); it != test_colony.rend();
++it ) {
++count;
cata::colony<int *>::iterator temp = it.base();
it = test_colony.erase( --temp );
}
// Full erase reverse iteration test
CHECK( count == 200 );
// Post-erase size test
CHECK( test_colony.empty() );
// Restore previous state after erasure
for( int temp = 0; temp < 200; ++temp ) {
test_colony.insert( &ten );
test_colony.insert( &twenty );
}
count = 0;
for( cata::colony<int *>::iterator it = --cata::colony<int *>::iterator( test_colony.end() );
it != test_colony.begin(); --it ) {
++count;
}
// Negative iteration test
CHECK( count == 399 );
count = 0;
for( cata::colony<int *>::iterator it = --( cata::colony<int *>::iterator( test_colony.end() ) );
it != test_colony.begin(); test_colony.advance( it, -2 ) ) {
++count;
}
// Negative multiple iteration test
CHECK( count == 200 );
test_colony_2 = std::move( test_colony );
// Move test
CHECK( test_colony_2.size() == 400 );
// NOLINTNEXTLINE(bugprone-use-after-move)
test_colony.insert( &ten );
// Insert to post-moved-colony test
CHECK( test_colony.size() == 1 );
cata::colony<int *> test_colony_5( test_colony_2 );
cata::colony<int *> test_colony_6( std::move( test_colony_5 ), test_colony_2.get_allocator() );
// Allocator-extended move construct test
CHECK( test_colony_6.size() == 400 );
test_colony_3 = test_colony_2;
//Copy test 2
CHECK( test_colony_3.size() == 400 );
test_colony_2.insert( &ten );
test_colony_2.swap( test_colony_3 );
//Swap test
CHECK( test_colony_2.size() == test_colony_3.size() - 1 );
swap( test_colony_2, test_colony_3 );
//Swap test 2
CHECK( test_colony_3.size() == test_colony_2.size() - 1 );
//max_size()
CHECK( test_colony_2.max_size() > test_colony_2.size() );
}
TEST_CASE( "colony insert and erase", "[colony]" )
{
cata::colony<int> test_colony;
for( int i = 0; i < 500000; ++i ) {
test_colony.insert( i );
}
// Size after insert
CHECK( test_colony.size() == 500000 );
cata::colony<int>::iterator found = std::find( test_colony.begin(), test_colony.end(), 5000 );
cata::colony<int>::reverse_iterator found2 = std::find( test_colony.rbegin(), test_colony.rend(),
5000 );
// std::find reverse_iterator
CHECK( *found == 5000 );
// std::find iterator
CHECK( *found2 == 5000 );
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
it = test_colony.erase( it );
}
// Erase alternating
CHECK( test_colony.size() == 250000 );
do {
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ) {
if( ( xor_rand() & 7 ) == 0 ) {
it = test_colony.erase( it );
} else {
++it;
}
}
} while( !test_colony.empty() );
//Erase randomly till-empty
CHECK( test_colony.empty() );
test_colony.clear();
test_colony.change_minimum_group_size( 10000 );
for( int i = 0; i != 30000; ++i ) {
test_colony.insert( 1 );
}
// Size after reinitialize + insert
CHECK( test_colony.size() == 30000 );
int count = 0;
do {
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ) {
if( ( xor_rand() & 7 ) == 0 ) {
it = test_colony.erase( it );
++count;
} else {
++it;
}
}
} while( count < 15000 );
// Erase randomly till half-empty
CHECK( test_colony.size() == static_cast<size_t>( 30000 - count ) );
for( int i = 0; i < count; ++i ) {
test_colony.insert( 1 );
}
// Size after reinsert
CHECK( test_colony.size() == 30000 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ) {
if( ++count == 3 ) {
count = 0;
it = test_colony.erase( it );
} else {
test_colony.insert( 1 );
++it;
}
}
// Alternating insert/erase
CHECK( test_colony.size() == 45001 );
do {
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ) {
if( ( xor_rand() & 3 ) == 0 ) {
++it;
test_colony.insert( 1 );
} else {
it = test_colony.erase( it );
}
}
} while( !test_colony.empty() );
// Random insert/erase till empty
CHECK( test_colony.empty() );
for( int i = 0; i != 500000; ++i ) {
test_colony.insert( 10 );
}
// Insert post-erase
CHECK( test_colony.size() == 500000 );
cata::colony<int>::iterator it = test_colony.begin();
test_colony.advance( it, 250000 );
for( ; it != test_colony.end(); ) {
it = test_colony.erase( it );
}
// Large multi-increment iterator
CHECK( test_colony.size() == 250000 );
for( int i = 0; i < 250000; ++i ) {
test_colony.insert( 10 );
}
cata::colony<int>::iterator end_it = test_colony.end();
test_colony.advance( end_it, -250000 );
for( cata::colony<int>::iterator it = test_colony.begin(); it != end_it; ) {
it = test_colony.erase( it );
}
// Large multi-decrement iterator
CHECK( test_colony.size() == 250000 );
for( int i = 0; i < 250000; ++i ) {
test_colony.insert( 10 );
}
int sum = 0;
for( int it : test_colony ) {
sum += it;
}
// Re-insert post-heavy-erasure
CHECK( sum == 5000000 );
end_it = test_colony.end();
test_colony.advance( end_it, -50001 );
cata::colony<int>::iterator begin_it = test_colony.begin();
test_colony.advance( begin_it, 300000 );
for( cata::colony<int>::iterator it = begin_it; it != end_it; ) {
it = test_colony.erase( it );
}
// Non-end decrement + erase
CHECK( test_colony.size() == 350001 );
for( int i = 0; i < 100000; ++i ) {
test_colony.insert( 10 );
}
begin_it = test_colony.begin();
test_colony.advance( begin_it, 300001 );
for( cata::colony<int>::iterator it = begin_it; it != test_colony.end(); ) {
it = test_colony.erase( it );
}
// Non-beginning increment + erase
CHECK( test_colony.size() == 300001 );
it = test_colony.begin();
test_colony.advance( it, 2 ); // Advance test 1
unsigned int index = static_cast<unsigned int>( test_colony.get_index_from_iterator( it ) );
// Advance + iterator-to-index test
CHECK( index == 2 );
// Check edge-case with advance when erasures present in initial group
test_colony.erase( it );
it = test_colony.begin();
test_colony.advance( it, 500 );
index = static_cast<unsigned int>( test_colony.get_index_from_iterator( it ) );
// Advance + iterator-to-index test
CHECK( index == 500 );
cata::colony<int>::iterator it2 = test_colony.get_iterator_from_pointer( &( *it ) );
// Pointer-to-iterator test
CHECK( it2 != test_colony.end() );
it2 = test_colony.get_iterator_from_index( 500 );
// Index-to-iterator test
CHECK( it2 == it );
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ) {
it = test_colony.erase( it );
}
// Total erase
CHECK( test_colony.empty() );
test_colony.clear();
test_colony.change_minimum_group_size( 3 );
const unsigned int temp_capacity2 = static_cast<unsigned int>( test_colony.capacity() );
test_colony.reserve( 1000 );
// Colony reserve
CHECK( temp_capacity2 != test_colony.capacity() );
CHECK( test_colony.capacity() == 1000 );
count = 0;
for( int i = 0; i < 50000; ++i ) {
for( int j = 0; j < 10; ++j ) {
if( ( xor_rand() & 7 ) == 0 ) {
test_colony.insert( 1 );
++count;
}
}
int count2 = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ) {
if( ( xor_rand() & 7 ) == 0 ) {
it = test_colony.erase( it );
--count;
} else {
++it;
}
++count2;
}
}
// Multiple sequential small insert/erase commands
CHECK( test_colony.size() == static_cast<size_t>( count ) );
}
TEST_CASE( "colony range erase", "[colony]" )
{
cata::colony<int> test_colony;
int count = 0;
for( ; count != 1000; ++count ) {
test_colony.insert( count );
}
cata::colony<int>::iterator it1 = test_colony.begin();
cata::colony<int>::iterator it2 = test_colony.begin();
test_colony.advance( it1, 500 );
test_colony.advance( it2, 800 );
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
//Simple range erase 1
CHECK( count == 700 );
CHECK( test_colony.size() == 700 );
it1 = test_colony.begin();
it2 = test_colony.begin();
test_colony.advance( it1, 400 );
test_colony.advance( it2, 500 ); // This should put it2 past the point of previous erasures
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
//Simple range erase 2
CHECK( count == 600 );
CHECK( test_colony.size() == 600 );
it2 = it1 = test_colony.begin();
test_colony.advance( it1, 4 );
test_colony.advance( it2, 9 ); // This should put it2 past the point of previous erasures
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Simple range erase 3
CHECK( count == 595 );
CHECK( test_colony.size() == 595 );
it1 = test_colony.begin();
it2 = test_colony.begin();
test_colony.advance( it2, 50 );
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Range erase from begin()
CHECK( count == 545 );
CHECK( test_colony.size() == 545 );
it1 = test_colony.begin();
it2 = test_colony.end();
// Test erasing and validity when it removes the final group in colony
test_colony.advance( it1, 345 );
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Range erase to end()
CHECK( count == 345 );
CHECK( test_colony.size() == 345 );
test_colony.clear();
for( count = 0; count != 3000; ++count ) {
test_colony.insert( count );
}
for( cata::colony<int>::iterator it = test_colony.begin(); it < test_colony.end(); ++it ) {
it = test_colony.erase( it );
}
it2 = test_colony.begin();
it1 = test_colony.begin();
test_colony.advance( it1, 4 );
test_colony.advance( it2, 600 );
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Range erase with colony already half-erased, alternating erasures
CHECK( count == 904 );
CHECK( test_colony.size() == 904 );
test_colony.clear();
for( count = 0; count < 3000; ++count ) {
test_colony.insert( count );
}
for( cata::colony<int>::iterator it = test_colony.begin(); it < test_colony.end(); ++it ) {
if( ( xor_rand() & 1 ) == 0 ) {
it = test_colony.erase( it );
}
}
if( test_colony.size() < 400 ) {
for( count = 0; count != 400; ++count ) {
test_colony.insert( count );
}
}
it1 = test_colony.begin();
it2 = test_colony.end();
test_colony.advance( it1, 400 );
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Range-erase with colony already third-erased, randomized erasures
CHECK( count == 400 );
CHECK( test_colony.size() == 400 );
unsigned int size;
unsigned int range1;
unsigned int range2;
unsigned int loop_count;
unsigned int internal_loop_count;
for( loop_count = 0; loop_count < 50; ++loop_count ) {
test_colony.clear();
for( count = 0; count < 1000; ++count ) {
test_colony.insert( count );
}
internal_loop_count = 0;
while( !test_colony.empty() ) {
it1 = test_colony.begin();
it2 = test_colony.begin();
size = static_cast<unsigned int>( test_colony.size() );
range1 = xor_rand() % size;
range2 = range1 + 1 + ( xor_rand() % ( size - range1 ) );
test_colony.advance( it1, range1 );
test_colony.advance( it2, range2 );
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Fuzz-test range-erase randomly Fails
CAPTURE( loop_count, internal_loop_count );
CHECK( test_colony.size() == static_cast<unsigned int>( count ) );
if( test_colony.size() > 2 ) {
// Test to make sure our stored erased_locations are valid
test_colony.insert( 1 );
test_colony.insert( 10 );
}
++internal_loop_count;
}
}
// "Fuzz-test range-erase randomly until empty"
CHECK( test_colony.empty() );
for( loop_count = 0; loop_count < 50; ++loop_count ) {
test_colony.clear();
internal_loop_count = 0;
test_colony.insert( 10000, 10 ); // fill-insert
while( !test_colony.empty() ) {
it1 = test_colony.begin();
it2 = test_colony.begin();
size = static_cast<unsigned int>( test_colony.size() );
range1 = xor_rand() % size;
range2 = range1 + 1 + ( xor_rand() % ( size - range1 ) );
test_colony.advance( it1, range1 );
test_colony.advance( it2, range2 );
test_colony.erase( it1, it2 );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Fuzz-test range-erase + fill-insert randomly fails during erase
CAPTURE( loop_count, internal_loop_count );
CHECK( test_colony.size() == static_cast<unsigned int>( count ) );
if( test_colony.size() > 100 ) {
// Test to make sure our stored erased_locations are valid & fill-insert is functioning properly in these scenarios
const unsigned int extra_size = xor_rand() % 128;
test_colony.insert( extra_size, 5 );
// Fuzz-test range-erase + fill-insert randomly fails during insert
CAPTURE( loop_count, internal_loop_count );
CHECK( test_colony.size() == static_cast<unsigned int>( count ) + extra_size );
count = 0;
for( cata::colony<int>::iterator it = test_colony.begin(); it != test_colony.end(); ++it ) {
++count;
}
// Fuzz-test range-erase + fill-insert randomly fails during count-test fill-insert
CHECK( test_colony.size() == static_cast<unsigned int>( count ) );
}
++internal_loop_count;
}
}
// Fuzz-test range-erase + fill-insert randomly until empty
CHECK( test_colony.empty() );
test_colony.erase( test_colony.begin(), test_colony.end() );
// Range-erase when colony is empty test (crash test)
CHECK( test_colony.empty() );
test_colony.insert( 10, 1 );
test_colony.erase( test_colony.begin(), test_colony.begin() );
// Range-erase when range is empty test (crash test)
CHECK( test_colony.size() == 10 );
}
TEST_CASE( "colony sort", "[colony]" )
{
cata::colony<int> test_colony;
test_colony.reserve( 50000 );
for( int i = 0; i < 50000; ++i ) {
test_colony.insert( xor_rand() & 65535 );
}
test_colony.sort();
bool sorted = true;
int prev = 0;
for( int cur : test_colony ) {
if( prev > cur ) {
sorted = false;
break;
}
prev = cur;
}
// Less-than sort test
CHECK( sorted );
test_colony.sort( std::greater<>() );
prev = 65536;
for( int cur : test_colony ) {
if( prev < cur ) {
sorted = false;
break;
}
prev = cur;
}
// Greater-than sort test
CHECK( sorted );
}
TEST_CASE( "colony insertion methods", "[colony]" )
{
cata::colony<int> test_colony = {1, 2, 3};
// Initializer-list constructor
CHECK( test_colony.size() == 3 );
cata::colony<int> test_colony_2( test_colony.begin(), test_colony.end() );
// Range constructor
CHECK( test_colony_2.size() == 3 );
cata::colony<int> test_colony_3( 5000, 2, 100, 1000 );
// Fill construction
CHECK( test_colony_3.size() == 5000 );
test_colony_2.insert( 500000, 5 );
// Fill insertion
CHECK( test_colony_2.size() == 500003 );
std::vector<int> some_ints( 500, 2 );
test_colony_2.insert( some_ints.begin(), some_ints.end() );
// Range insertion
CHECK( test_colony_2.size() == 500503 );
test_colony_3.clear();
test_colony_2.clear();
test_colony_2.reserve( 50000 );
test_colony_2.insert( 60000, 1 );
int sum = 0;
for( int it : test_colony_2 ) {
sum += it;
}
// Reserve + fill insert
CHECK( test_colony_2.size() == 60000 );
CHECK( sum == 60000 );
test_colony_2.clear();
test_colony_2.reserve( 5000 );
test_colony_2.insert( 60, 1 );
sum = 0;
for( int it : test_colony_2 ) {
sum += it;
}
// Reserve + fill insert
CHECK( test_colony_2.size() == 60 );
CHECK( sum == 60 );
test_colony_2.insert( 6000, 1 );
sum = 0;
for( int it : test_colony_2 ) {
sum += it;
}
// Reserve + fill + fill
CHECK( test_colony_2.size() == 6060 );
CHECK( sum == 6060 );
test_colony_2.reserve( 18000 );
test_colony_2.insert( 6000, 1 );
sum = 0;
for( int it : test_colony_2 ) {
sum += it;
}
// Reserve + fill + fill + reserve + fill
CHECK( test_colony_2.size() == 12060 );
CHECK( sum == 12060 );
}
TEST_CASE( "colony perfect forwarding", "[colony]" )
{
cata::colony<perfect_forwarding_test> test_colony;
int lvalue = 0;
int &lvalueref = lvalue;
test_colony.emplace( 7, lvalueref );
CHECK( ( *test_colony.begin() ).success );
CHECK( lvalueref == 1 );
}
TEST_CASE( "colony emplace", "[colony]" )
{
cata::colony<small_struct> test_colony;
int sum1 = 0;
int sum2 = 0;
for( int i = 0; i < 100; ++i ) {
test_colony.emplace( i );
sum1 += i;
}
for( small_struct &s : test_colony ) {
sum2 += s.number;
}
//Basic emplace
CHECK( sum1 == sum2 );
//Basic emplace
CHECK( test_colony.size() == 100 );
}
TEST_CASE( "colony group size and capacity", "[colony]" )
{
cata::colony<int> test_colony;
test_colony.change_group_sizes( 50, 100 );
test_colony.insert( 27 );
// Change_group_sizes min-size
CHECK( test_colony.capacity() == 50 );
for( int i = 0; i != 100; ++i ) {
test_colony.insert( i );
}
// Change_group_sizes max-size
CHECK( test_colony.capacity() == 200 );
test_colony.reinitialize( 200, 2000 );
test_colony.insert( 27 );
// Reinitialize min-size
CHECK( test_colony.capacity() == 200 );
for( int i = 0; i != 3300; ++i ) {
test_colony.insert( i );
}
// Reinitialize max-size
CHECK( test_colony.capacity() == 5200 );
test_colony.change_group_sizes( 500, 500 );
// Change_group_sizes resize
CHECK( test_colony.capacity() == 3500 );
test_colony.change_minimum_group_size( 200 );
test_colony.change_maximum_group_size( 200 );
// Change_maximum_group_size resize
CHECK( test_colony.capacity() == 3400 );
}
TEST_CASE( "colony splice", "[colony]" )
{
cata::colony<int> test_colony_1;
cata::colony<int> test_colony_2;
SECTION( "small splice 1" ) {
int i = 0;
for( ; i < 20; ++i ) {
test_colony_1.insert( i );
test_colony_2.insert( i + 20 );
}
test_colony_1.splice( test_colony_2 );
i = 0;
for( cata::colony<int>::iterator it = test_colony_1.begin(); it < test_colony_1.end(); ++it ) {
CHECK( i++ == *it );
}
}
SECTION( "small splice 2" ) {
int i = 0;
for( ; i < 100; ++i ) {
test_colony_1.insert( i );
test_colony_2.insert( i + 100 );
}
test_colony_1.splice( test_colony_2 );
i = 0;
for( int it : test_colony_1 ) {
CHECK( i++ == it );
}
}
SECTION( "large splice" ) {
int i = 0;
for( ; i < 100000; ++i ) {
test_colony_1.insert( i );
test_colony_2.insert( i + 100000 );
}
test_colony_1.splice( test_colony_2 );
i = 0;
for( int it : test_colony_1 ) {