-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
1273 lines (1040 loc) · 32.5 KB
/
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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
#include "thh-handle-vector/handle-vector.hpp"
#include <numeric>
TEST_CASE("CanAllocContainer")
{
thh::handle_vector_t<char> handle_vector;
CHECK(true);
}
TEST_CASE("ContainerSizeZeroAfterInit")
{
thh::handle_vector_t<char> handle_vector;
const auto handle_vector_size = handle_vector.size();
CHECK(handle_vector_size == 0);
}
TEST_CASE("InitialHandleReturnedIsZero")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle = handle_vector.add();
CHECK(handle.id_ == 0);
}
TEST_CASE("ContainerSizeIsOneAfterSingleAdd")
{
thh::handle_vector_t<char> handle_vector;
[[maybe_unused]] thh::handle_t handle = handle_vector.add();
CHECK(handle_vector.size() == 1);
}
TEST_CASE("ContainerSizeGrowsWithConsecutiveAdds")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle1 = handle_vector.add();
thh::handle_t handle2 = handle_vector.add();
thh::handle_t handle3 = handle_vector.add();
CHECK(handle1.id_ == 0);
CHECK(handle2.id_ == 1);
CHECK(handle3.id_ == 2);
CHECK(handle_vector.size() == 3);
}
TEST_CASE("ContainerHasAddedHandle")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle = handle_vector.add();
CHECK(handle_vector.has(handle));
}
TEST_CASE("EmptyContainerDoesNotHaveHandle")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle(0, 0);
CHECK(!handle_vector.has(handle));
}
TEST_CASE("ContainerDoesNotHaveHandleId")
{
thh::handle_vector_t<char> handle_vector;
[[maybe_unused]] thh::handle_t handle = handle_vector.add();
thh::handle_t other_handle(1, 0);
CHECK(!handle_vector.has(other_handle));
}
TEST_CASE("ContainerDoesNotHaveHandleGen")
{
thh::handle_vector_t<char> handle_vector;
[[maybe_unused]] thh::handle_t handle = handle_vector.add();
thh::handle_t other_handle(0, 1);
CHECK(!handle_vector.has(other_handle));
}
TEST_CASE("RemoveDecreasesSize")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle1 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle2 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle3 = handle_vector.add();
CHECK(handle_vector.size() == 3);
const bool removed = handle_vector.remove(handle1);
CHECK(removed);
CHECK(handle_vector.size() == 2);
}
TEST_CASE("HandleReusedAfterRemoval")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t initial_handle = handle_vector.add();
handle_vector.remove(initial_handle);
thh::handle_t next_handle = handle_vector.add();
CHECK(next_handle.id_ == 0);
CHECK(next_handle.gen_ == 1);
}
TEST_CASE("CannotRemoveInvalidHandle")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t invalid_handle(-1, -1);
const bool removed = handle_vector.remove(invalid_handle);
CHECK(removed == false);
}
TEST_CASE("CanRemoveAddedHandle")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle = handle_vector.add();
bool removed = handle_vector.remove(handle);
bool has = handle_vector.has(handle);
CHECK(removed == true);
CHECK(has == false);
}
TEST_CASE("MultipleAddsAndRemovesReturnsExpectedSize")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle1 = handle_vector.add();
thh::handle_t handle2 = handle_vector.add();
thh::handle_t handle3 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle4 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle5 = handle_vector.add();
bool removed1 = handle_vector.remove(handle1);
bool removed2 = handle_vector.remove(handle2);
bool removed3 = handle_vector.remove(handle3);
CHECK(removed1);
CHECK(removed2);
CHECK(removed3);
[[maybe_unused]] thh::handle_t handle6 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle7 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle8 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle9 = handle_vector.add();
[[maybe_unused]] thh::handle_t handle10 = handle_vector.add();
CHECK(handle_vector.size() == 7);
}
TEST_CASE("AddAndRemoveHandlesReverseOrder")
{
thh::handle_vector_t<char> handle_vector;
constexpr const size_t element_count = 10;
thh::handle_t handles[element_count];
for (auto& handle : handles) {
handle = handle_vector.add();
}
CHECK(handle_vector.size() == element_count);
bool removed = true;
for (int32_t i = element_count - 1; i >= 0; i--) {
removed &= !!handle_vector.remove(handles[i]);
}
CHECK(removed);
CHECK(handle_vector.size() == 0);
for (auto& handle : handles) {
CHECK(!handle_vector.has(handle));
}
}
TEST_CASE("AddAndRemoveHandlesOrdered")
{
thh::handle_vector_t<char> handle_vector;
constexpr const size_t element_count = 10;
thh::handle_t handles[element_count];
for (auto& handle : handles) {
handle = handle_vector.add();
}
CHECK(handle_vector.size() == element_count);
bool removed = true;
for (auto& handle : handles) {
removed &= !!handle_vector.remove(handle);
}
CHECK(removed);
CHECK(handle_vector.size() == 0);
for (auto& handle : handles) {
CHECK(!handle_vector.has(handle));
}
}
TEST_CASE("CanGetElementViaHandle")
{
thh::handle_vector_t<char> handle_vector;
thh::handle_t handle = handle_vector.add();
const char* c = nullptr;
handle_vector.call(handle, [&c](const char& value) { c = &value; });
CHECK(c != nullptr);
}
TEST_CASE("AddTwoHandlesAndUpdateObjects")
{
struct test_t
{
int a_ = 0;
int b_ = 0;
};
thh::handle_vector_t<test_t> handle_vector;
thh::handle_t handle1 = handle_vector.add();
thh::handle_t handle2 = handle_vector.add();
handle_vector.call(handle1, [](auto& test1) {
test1.a_ = 6;
test1.b_ = 4;
});
handle_vector.call(handle2, [](auto& test2) {
test2.a_ = 4;
test2.b_ = 2;
});
handle_vector.call(handle1, [](const auto& test1) {
CHECK(test1.a_ == 6);
CHECK(test1.b_ == 4);
});
handle_vector.call(handle2, [](const auto& test2) {
CHECK(test2.a_ == 4);
CHECK(test2.b_ == 2);
});
}
TEST_CASE("OriginalHandleCannotAccessElementAfterRemoval")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handle = handle_vector.add();
handle_vector.remove(handle);
CHECK(!handle_vector.has(handle));
CHECK(handle_vector.call_return(handle, [](const auto&) {
return true;
}) == std::nullopt);
}
TEST_CASE("ElementsRemainPackedAfterRemoval")
{
thh::handle_vector_t<float> handle_vector;
thh::handle_t handles[5];
for (auto& handle : handles) {
handle = handle_vector.add();
}
handle_vector.remove(handles[2]);
const float* begin = nullptr;
handle_vector.call(
handles[0], [&begin](const auto& value) { begin = &value; });
const float* was_end = nullptr;
handle_vector.call(
handles[4], [&was_end](const auto& value) { was_end = &value; });
const float* new_end = nullptr;
handle_vector.call(
handles[3], [&new_end](const auto& value) { new_end = &value; });
CHECK(was_end - begin == 2);
CHECK(new_end - begin == 3);
}
TEST_CASE("ContainerDebugVisualization")
{
thh::handle_vector_t<float> handle_vector;
const size_t handle_count = 5;
thh::handle_t handles[handle_count];
handle_vector.reserve(handle_count);
for (auto& handle : handles) {
handle = handle_vector.add();
}
handle_vector.remove(handles[2]);
handle_vector.remove(handles[0]);
const std::string buffer = handle_vector.debug_handles();
const std::string expected_buffer = "[x][o][x][o][o]";
CHECK(expected_buffer == buffer);
}
TEST_CASE("EnsureHandlesReaddedInOrder")
{
thh::handle_vector_t<float> handle_vector;
const size_t handle_count = 5;
thh::handle_t handles[handle_count];
handle_vector.reserve(handle_count);
for (auto& handle : handles) {
handle = handle_vector.add();
}
std::string expected_buffer;
for (int32_t i = 0; i < handle_vector.capacity(); i++) {
expected_buffer.append("[x]");
}
for (auto& handle : handles) {
handle_vector.remove(handle);
}
std::string buffer = handle_vector.debug_handles();
CHECK(expected_buffer == buffer);
thh::handle_t first_new_handle = handle_vector.add();
buffer = handle_vector.debug_handles();
expected_buffer = "[x][x][x][x][o]";
CHECK(expected_buffer == buffer);
thh::handle_t second_new_handle = handle_vector.add();
buffer = handle_vector.debug_handles();
expected_buffer = "[x][x][x][o][o]";
CHECK(expected_buffer == buffer);
const float* begin = nullptr;
handle_vector.call(
first_new_handle, [&begin](const auto& value) { begin = &value; });
const float* end = nullptr;
handle_vector.call(
second_new_handle, [&end](const auto& value) { end = &value; });
// ensure objects are tightly packed
ptrdiff_t size = end - begin;
CHECK(size == 1);
}
TEST_CASE("EnsureResourceCleanedUpAfterRemoval")
{
struct resource_t
{
resource_t() = default;
resource_t(const resource_t&) = default;
resource_t& operator=(const resource_t&) = default;
~resource_t() { *resource_ = 42; }
int* resource_ = nullptr;
};
thh::handle_vector_t<resource_t> handle_vector;
const auto resource_handle = handle_vector.add();
int value = 100;
handle_vector.call(
resource_handle, [&value](auto& resource) { resource.resource_ = &value; });
handle_vector.remove(resource_handle);
CHECK(value == 42);
}
TEST_CASE("EnumerateMutableElements")
{
struct entity_t
{
int x_ = 0;
int y_ = 0;
};
const auto entity_handle_count = 10;
thh::handle_vector_t<entity_t> entities;
std::vector<thh::handle_t> entity_handles;
for (size_t i = 0; i < entity_handle_count; ++i) {
entity_handles.push_back(entities.add());
}
for (entity_t& entity : entities) {
entity.x_ += 1;
entity.y_ += 2;
}
CHECK(entity_handles.size() == entity_handle_count);
for (const auto& entity_handle : entity_handles) {
entities.call(entity_handle, [](const entity_t& entity) {
CHECK(entity.x_ == 1);
CHECK(entity.y_ == 2);
});
}
}
TEST_CASE("EnumerateImmutableElements")
{
struct entity_t
{
int w_ = 2;
int h_ = 1;
};
thh::handle_vector_t<entity_t> entities;
std::vector<thh::handle_t> entity_handles;
for (size_t i = 0; i < 10; ++i) {
entity_handles.push_back(entities.add());
}
int total_width = 0;
int total_height = 0;
for (const entity_t& entity : entities) {
total_width += entity.w_;
total_height += entity.h_;
}
CHECK(total_height == 10);
CHECK(total_width == 20);
}
TEST_CASE("HandleResolvesAfterInternalMove")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handles[5];
for (auto& handle : handles) {
handle = handle_vector.add();
}
for (size_t i = 0; i < std::size(handles); ++i) {
handle_vector.call(
handles[i], [i](int& value) { value = static_cast<int32_t>(i) + 1; });
}
void* address_before =
handle_vector.call_return(handles[0], [](int& value) { return &value; })
.value();
handle_vector.remove(handles[0]);
const auto* last =
handle_vector.call_return(handles[4], [](int& value) { return &value; })
.value();
CHECK(*last == 5);
CHECK(last == address_before);
}
TEST_CASE("ElementsCanBeReserved")
{
thh::handle_vector_t<int> handle_vector;
handle_vector.reserve(10);
CHECK(handle_vector.size() == 0);
CHECK(handle_vector.capacity() == 10);
}
TEST_CASE("ElementsCanBeReservedAfterFirstUse")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handles[5];
for (auto& handle : handles) {
handle = handle_vector.add();
}
handle_vector.reserve(10);
for (auto& handle : handles) {
CHECK(handle_vector.has(handle));
}
CHECK(handle_vector.size() == 5);
CHECK(handle_vector.capacity() == 10);
}
TEST_CASE("ContainerCanBeCleared")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handles[10];
for (auto& handle : handles) {
handle = handle_vector.add();
}
handle_vector.clear();
CHECK(handle_vector.size() == 0);
for (auto& handle : handles) {
CHECK(!handle_vector.has(handle));
}
}
TEST_CASE("FirstHandleReturnedAfterClear")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handles[10];
for (auto& handle : handles) {
handle = handle_vector.add();
}
handle_vector.clear();
thh::handle_t next_handle = handle_vector.add();
CHECK(next_handle.id_ == 0);
CHECK(next_handle.gen_ == 1);
}
TEST_CASE("FirstElementReturnedAfterClear")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handles[10];
for (auto& handle : handles) {
handle = handle_vector.add();
}
const void* begin = nullptr;
handle_vector.call(
handles[0], [&begin](const auto& value) { begin = &value; });
handle_vector.clear();
thh::handle_t next_handle = handle_vector.add();
const void* element = nullptr;
handle_vector.call(
next_handle, [&element](const auto& value) { element = &value; });
CHECK(begin == element);
}
TEST_CASE("ContainerGrowsCorrectlyAfterClear")
{
thh::handle_vector_t<int> handle_vector;
const size_t initial_handle_count = 10;
std::vector<thh::handle_t> handles;
for (size_t i = 0; i < initial_handle_count; ++i) {
handles.push_back(handle_vector.add());
}
const auto capacity_before_clear = handle_vector.capacity();
const auto difference_required_for_grow =
capacity_before_clear - handle_vector.size();
const auto grow_size = handles.size() + difference_required_for_grow;
handle_vector.clear();
for (auto& handle : handles) {
CHECK(!handle_vector.has(handle));
}
handles.clear();
for (size_t i = 0; i < grow_size + 1; ++i) {
handles.push_back(handle_vector.add());
}
const thh::handle_t another_handle = handle_vector.add();
const auto next_id = grow_size + 1;
CHECK(another_handle.id_ == next_id);
CHECK(another_handle.gen_ == 0);
handle_vector.remove(handles[5]);
const thh::handle_t next_handle = handle_vector.add();
CHECK(next_handle.id_ == 5);
CHECK(next_handle.gen_ == 2);
}
TEST_CASE("HoldMoveOnlyType")
{
struct resource_t
{
resource_t() {} // user provided constructor
resource_t(resource_t&&) = default;
resource_t& operator=(resource_t&&) = default;
};
thh::handle_vector_t<resource_t> handle_vector;
std::vector<thh::handle_t> handles;
const size_t initial_handle_count = 10;
for (size_t i = 0; i < initial_handle_count; ++i) {
handles.push_back(handle_vector.add());
}
for (auto& handle : handles) {
handle_vector.remove(handle);
};
CHECK(handle_vector.size() == 0);
}
TEST_CASE("TaggedHandle")
{
thh::handle_vector_t<float> float_handle_vector;
thh::handle_vector_t<float, struct height_tag_t> height_handle_vector;
thh::handle_vector_t<float, struct width_tag_t> width_handle_vector;
using width_handle_t = thh::typed_handle_t<struct width_tag_t>;
using height_handle_t = thh::typed_handle_t<struct height_tag_t>;
// thh::typed_handle_t<struct width_tag_t>
[[maybe_unused]] const width_handle_t width_handle =
width_handle_vector.add();
// thh::typed_handle_t<struct height_tag_t>
[[maybe_unused]] const height_handle_t height_handle =
height_handle_vector.add();
[[maybe_unused]] const thh::handle_t float_handle = float_handle_vector.add();
// note - lines do not compile (type mismatch error)
// width_handle_vector.call(height_handle, [](const auto&) {});
// width_handle_vector.call(float_handle, [](const auto&) {});
const float* width = nullptr;
width_handle_vector.call(
width_handle, [&width](const auto& value) { width = &value; });
CHECK(width != nullptr);
}
TEST_CASE("SupportNonDefaultConstructibleType")
{
struct no_default_constructor_t
{
explicit no_default_constructor_t(int i) : i_(i) {}
int i_ = 0;
};
thh::handle_vector_t<no_default_constructor_t> handle_vector;
const auto handle = handle_vector.add(4);
handle_vector.call(handle, [](const auto& value) { CHECK(value.i_ == 4); });
handle_vector.remove(handle);
CHECK(handle_vector.call_return(handle, [](const auto&) {
return true;
}) == std::nullopt);
}
TEST_CASE("ValuesAccessedThroughIterators")
{
thh::handle_vector_t<int> handle_vector;
constexpr const size_t element_count = 10;
thh::handle_t handles[element_count];
for (auto& handle : handles) {
handle = handle_vector.add();
}
std::for_each(
handle_vector.begin(), handle_vector.end(),
[i = 0](auto& elem) mutable { elem = i++; });
for (size_t i = 0; i < element_count; ++i) {
handle_vector.call(
handles[i], [i](const auto& value) { CHECK(value == i); });
}
}
TEST_CASE("AccumulateWithIterators")
{
thh::handle_vector_t<int> handle_vector;
constexpr const size_t element_count = 10;
thh::handle_t handles[element_count];
for (auto& handle : handles) {
const auto h = handle_vector.add();
handle_vector.call(h, [](auto& value) { value = 5; });
handle = h;
}
const auto total = std::accumulate(
handle_vector.begin(), handle_vector.end(), 0,
[](int acc, const auto& value) {
acc += value;
return acc;
});
CHECK(total == 50);
}
TEST_CASE("FindWithIterators")
{
thh::handle_vector_t<int> handle_vector;
constexpr const size_t element_count = 10;
thh::handle_t handles[element_count];
for (size_t i = 0; i < element_count; ++i) {
const auto h = handle_vector.add();
handle_vector.call(h, [i](auto& value) { value = static_cast<int>(i); });
handles[i] = h;
}
const auto found = std::find_if(
handle_vector.cbegin(), handle_vector.cend(),
[](const int value) { return value == 8; });
CHECK(found != handle_vector.cend());
CHECK(*found == 8);
}
TEST_CASE("HandleEqualityCheckPasses")
{
thh::handle_t handle_a{0, 1};
thh::handle_t handle_b{0, 1};
CHECK(handle_a == handle_b);
}
TEST_CASE("HandleEqualityCheckFails")
{
{
thh::handle_t handle_a{1, 0};
thh::handle_t handle_b{0, 0};
CHECK(handle_a != handle_b);
}
{
thh::handle_t handle_a{2, 1};
thh::handle_t handle_b{2, 0};
CHECK(handle_a != handle_b);
}
}
TEST_CASE("InvokeCall")
{
struct incrementer_t
{
int pre_increment() { return ++counter_; }
int counter() const { return counter_; }
private:
int counter_ = 0;
};
thh::handle_vector_t<incrementer_t> handle_vector;
const thh::handle_t handle = handle_vector.add();
int result = 0;
handle_vector.call(handle, [&result](incrementer_t& incrementer) {
result = incrementer.pre_increment();
});
CHECK(result == 1);
int next_result = 0;
const auto& handle_vector_ref = handle_vector;
handle_vector_ref.call(
handle, [&next_result](const incrementer_t& incrementer) {
next_result = incrementer.counter();
});
CHECK(next_result == 1);
}
TEST_CASE("InvokeCallReturn")
{
class Test
{
private:
Test() = default;
int v_ = 5;
public:
static Test make_test() { return Test(); }
int v() const { return v_; }
void inc() { v_++; }
};
thh::handle_vector_t<Test> handle_vector;
thh::handle_t handle = handle_vector.add(Test::make_test());
auto result =
handle_vector.call_return(handle, [](Test value) { return value; });
CHECK(result.value().v() == 5);
auto next_result = handle_vector.call_return(handle, [](Test& value) {
value.inc();
return value;
});
CHECK(next_result.value().v() == 6);
const auto& handle_vector_ref = handle_vector;
auto last_result = handle_vector_ref.call_return(
handle, [](const Test& value) { return value; });
CHECK(last_result.value().v() == 6);
}
TEST_CASE("InvokeCallReturnFails")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handle = handle_vector.add(10);
auto result =
handle_vector.call_return(handle, [](int value) { return value; });
CHECK(result.value() == 10);
// called with invalid handle
auto next_result =
handle_vector.call_return(thh::handle_t{}, [](int value) { return value; });
CHECK(!next_result.has_value());
const auto& handle_vector_ref = handle_vector;
auto last_result = handle_vector_ref.call_return(
thh::handle_t{}, [](const int& value) { return value; });
CHECK(!last_result.has_value());
}
TEST_CASE("DefaultContainerIsEmpty")
{
thh::handle_vector_t<int> handle_vector;
CHECK(handle_vector.empty());
}
TEST_CASE("ContainerIsNotEmptyAfterAdd")
{
thh::handle_vector_t<int> handle_vector;
[[maybe_unused]] thh::handle_t handle = handle_vector.add(10);
CHECK(!handle_vector.empty());
}
TEST_CASE("ContainerIsEmptyAfterRemove")
{
thh::handle_vector_t<int> handle_vector;
thh::handle_t handle = handle_vector.add(10);
handle_vector.remove(handle);
CHECK(handle_vector.empty());
}
TEST_CASE("ConstContainerEmptyCheck")
{
thh::handle_vector_t<int> handle_vector;
handle_vector.add();
handle_vector.clear();
const thh::handle_vector_t<int>& const_handle_vector = handle_vector;
CHECK(const_handle_vector.empty());
}
TEST_CASE("HandleCanBeReturnedFromIndex")
{
thh::handle_vector_t<int> handle_vector;
auto handle1 = handle_vector.add();
auto handle2 = handle_vector.add();
auto handle3 = handle_vector.add();
auto handle_from_index1 = handle_vector.handle_from_index(0);
auto handle_from_index2 = handle_vector.handle_from_index(1);
auto handle_from_index3 = handle_vector.handle_from_index(2);
CHECK(handle1 == handle_from_index1);
CHECK(handle2 == handle_from_index2);
CHECK(handle3 == handle_from_index3);
}
TEST_CASE("HandleCanBeReturnedFromIndexWithRemovals")
{
thh::handle_vector_t<int> handle_vector;
const int32_t initial_handle_count = 10;
std::vector<thh::handle_t> handles;
for (int32_t i = 0; i < initial_handle_count; ++i) {
handles.push_back(handle_vector.add(i));
}
for (int32_t i = 0; i < initial_handle_count; ++i) {
if (i % 2 == 0) {
handle_vector.remove(handles[i]);
}
}
for (int32_t i = 0; i < initial_handle_count / 2; ++i) {
const auto handle = handle_vector.handle_from_index(i);
const auto index = handle_vector.index_from_handle(handle);
CHECK(i == index);
}
}
TEST_CASE("InvalidHandleReturnedWithOutOfRangeIndex")
{
thh::handle_vector_t<int> handle_vector;
handle_vector.add();
handle_vector.add();
handle_vector.add();
const auto invalid_handle1 = handle_vector.handle_from_index(-1);
const auto invalid_handle2 = handle_vector.handle_from_index(4);
CHECK(invalid_handle1 == thh::handle_t{});
CHECK(invalid_handle2 == thh::handle_t{});
}
TEST_CASE("EmptyOptionalReturnedWithInvalidHandle")
{
thh::handle_vector_t<int> handle_vector;
handle_vector.add();
handle_vector.add();
const auto last_handle = handle_vector.add();
handle_vector.remove(last_handle);
const auto invalid_index1 = handle_vector.index_from_handle(thh::handle_t{});
const auto invalid_index2 = handle_vector.index_from_handle(last_handle);
CHECK(!invalid_index1.has_value());
CHECK(!invalid_index2.has_value());
}
TEST_CASE("AfterSortElementsAreIteratedInSortedOrder")
{
thh::handle_vector_t<int> handle_vector;
for (int i = 0; i < 10; ++i) {
handle_vector.add(10 - i);
}
for (int i = 0; i < 10; ++i) {
CHECK(handle_vector[i] == (10 - i));
}
handle_vector.sort([&handle_vector](const auto lhs, const auto rhs) {
return handle_vector[lhs] < handle_vector[rhs];
});
for (int i = 0; i < 10; ++i) {
CHECK(handle_vector[i] == (i + 1));
}
}
TEST_CASE("HandlesReferToSameElementsAfterSort")
{
thh::handle_vector_t<int> handle_vector;
std::vector<thh::handle_t> handles;
for (int i = 0; i < 10; ++i) {
handles.push_back(handle_vector.add(10 - i));
}
auto check_handles_fn = [&] {
for (int i = 0; i < 10; ++i) {
handle_vector.call(
handles[i], [i](const auto& element) { CHECK(element == (10 - i)); });
}
};
check_handles_fn();
handle_vector.sort([&handle_vector](const auto lhs, const auto rhs) {
return handle_vector[lhs] < handle_vector[rhs];
});
check_handles_fn();
}
TEST_CASE("AfterPartitionElementsPassingPredicateAreIteratedFirst")
{
thh::handle_vector_t<int> handle_vector;
for (int i = 0; i < 10; ++i) {
handle_vector.add(10 - i);
}
for (int i = 0; i < 10; ++i) {
CHECK(handle_vector[i] == (10 - i));
}
const auto second =
handle_vector.partition([&handle_vector](const auto index) {
return handle_vector[index] % 2 == 0;
});
// check first half
for (int i = 0; i < second; ++i) {
CHECK(handle_vector[i] % 2 == 0);
}
// check second half
for (int i = second; i < handle_vector.size(); ++i) {
CHECK(handle_vector[i] % 2 != 0);
}
}
TEST_CASE("HandlesReferToSameElementsAfterPartition")
{
thh::handle_vector_t<int> handle_vector;
std::vector<thh::handle_t> handles;
for (int i = 0; i < 10; ++i) {
handles.push_back(handle_vector.add(10 - i));
}
auto check_handles_fn = [&] {
for (int i = 0; i < 10; ++i) {
handle_vector.call(
handles[i], [i](const auto& element) { CHECK(element == (10 - i)); });
}
};
check_handles_fn();
handle_vector.partition(
[&handle_vector](const auto index) { return handle_vector[index] < 5; });
check_handles_fn();
}
TEST_CASE("MiddleSubsetOfContainerCanBeSorted")
{
thh::handle_vector_t<int> handle_vector;
std::vector<thh::handle_t> handles;
for (int i = 0; i < 10; ++i) {
handles.push_back(handle_vector.add(10 - i));
}
for (int i = 0; i < 10; ++i) {
CHECK(handle_vector[i] == (10 - i));
}
handle_vector.sort(3, 8, [&handle_vector](const auto lhs, const auto rhs) {
return handle_vector[lhs] < handle_vector[rhs];
});