forked from zhy888ai/ticketmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ticket_map.cpp
More file actions
734 lines (576 loc) · 17.8 KB
/
test_ticket_map.cpp
File metadata and controls
734 lines (576 loc) · 17.8 KB
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
#include "ticket_map.hpp"
#include <assert.h>
#include <type_traits>
#include <string>
#include <iostream>
void test_initially_empty() {
jss::ticket_map<int, int> map;
assert(map.empty());
assert(map.size() == 0);
}
void test_inserting_a_value_gives_ticket_for_new_element() {
jss::ticket_map<int, int> map;
auto ticket= map.insert(42);
assert(ticket == 0);
assert(map[ticket] == 42);
}
void test_inserting_a_second_value_gives_new_ticket() {
jss::ticket_map<int, int> map;
map.insert(99);
auto ticket= map.insert(42);
assert(ticket == 1);
}
void test_inserting_a_bunch_of_elements_gives_iterators_and_updates_size() {
jss::ticket_map<int, int> map;
auto const count= 100;
for(unsigned i= 0; i < count; ++i) {
auto ticket= map.insert(i);
assert(ticket == i);
assert(map[ticket] == i);
assert(map.size() == i + 1);
assert(!map.empty());
}
}
void test_value_can_be_retrieved_if_inserted() {
jss::ticket_map<int, int> map;
map.insert(42);
auto iter= map.find(0);
assert(iter->ticket == 0);
assert(iter->value == 42);
}
void test_can_iterate_over_values() {
jss::ticket_map<int, int> map;
std::vector<int> const values= {2, 3, 56, 12, 99, -12,
42, 1213, -1283137618, 0, 12, 12};
for(auto &e : values) {
map.insert(e);
}
std::size_t index= 0;
for(auto &entry : map) {
static_assert(
std::is_same<decltype(entry.ticket), const int &>::value,
"Ticket must be int");
static_assert(
std::is_same<decltype(entry.value), int &>::value,
"Value must be ref");
assert(entry.ticket == index);
assert(entry.value == values[index]);
++index;
}
}
void test_empty_map_has_begin_equal_end() {
jss::ticket_map<int, std::string> map;
assert(map.begin() == map.end());
}
void test_begin_and_end_types() {
jss::ticket_map<int, std::string> map;
static_assert(
std::is_same<
typename decltype(map)::iterator, decltype(map.begin())>::value,
"begin must return an iterator");
static_assert(
std::is_same<decltype((*map.begin()).ticket), int const &>::value,
"ticket is specified type");
static_assert(
std::is_same<decltype((*map.begin()).value), std::string &>::value,
"value is ref");
}
void test_after_erasing_value_not_there() {
jss::ticket_map<unsigned short, std::string> map;
auto ticket= map.insert("hello");
assert(ticket == map.begin()->ticket);
assert(map.find(ticket) == map.begin());
auto iter2= map.erase(ticket);
assert(map.size() == 0);
assert(map.empty());
assert(map.begin() == map.end());
assert(map.find(ticket) == map.end());
assert(iter2 == map.end());
}
void test_after_erasing_middle_element_iteration_skips_it() {
jss::ticket_map<unsigned short, std::string> map;
map.insert("first");
auto ticket= map.insert("second");
map.insert("third");
auto after_erased= map.erase(ticket);
assert(after_erased != map.end());
assert(after_erased->ticket == 2);
assert(after_erased->value == "third");
assert(map.size() == 2);
unsigned index= 0;
auto it= map.begin();
assert(it != after_erased);
assert(it != map.end());
assert(it->ticket == 0);
assert(it->value == "first");
assert(it != after_erased);
++it;
assert(it == after_erased);
assert(it != map.end());
assert(it->ticket == 2);
assert(it->value == "third");
assert((it++)->ticket == 2);
assert(it != after_erased);
assert(it == map.end());
}
void test_after_erasing_last_element_iteration_skips_it() {
jss::ticket_map<unsigned short, std::string> map;
map.insert("first");
map.insert("second");
auto ticket= map.insert("third");
auto after_erased= map.erase(ticket);
assert(after_erased == map.end());
assert(map.size() == 2);
unsigned index= 0;
auto it= map.begin();
assert(it != after_erased);
assert(it != map.end());
assert(it->ticket == 0);
assert(it->value == "first");
assert(it != after_erased);
++it;
assert(it != after_erased);
assert(it != map.end());
assert(it->ticket == 1);
assert(it->value == "second");
assert((it++)->ticket == 1);
assert(it == after_erased);
assert(it == map.end());
}
void test_after_erasing_new_elements_added_correctly() {
jss::ticket_map<unsigned short, int> map;
unsigned const count= 11;
for(unsigned i= 0; i < count; ++i) {
map.insert(i);
}
for(unsigned i= 0; i < count; i+= 2) {
map.erase(i);
}
assert(map.size() == count / 2);
auto ticket= map.insert(99);
assert(ticket == count);
assert(map[ticket] == 99);
assert(map.size() == count / 2 + 1);
std::vector<int> expected;
for(unsigned i= 1; i < count; i+= 2) {
expected.push_back(i);
}
expected.push_back(99);
assert(map.size() == expected.size());
unsigned i= 0;
for(auto &e : map) {
assert(e.value == expected[i]);
++i;
}
}
void test_after_erasing_lots_of_elements_still_ok() {
jss::ticket_map<unsigned short, int> map;
unsigned const count= 1000;
for(unsigned i= 0; i < count; ++i) {
map.insert(i);
}
auto const cutoff= (count * 9 / 10);
for(unsigned i= 0; i < cutoff; ++i) {
map.erase(map.begin()->ticket);
}
unsigned val= cutoff;
for(auto &e : map) {
assert(e.value == val);
++val;
}
}
void test_can_erase_with_iterator() {
jss::ticket_map<long, std::string> map;
map.insert("first");
auto ticket= map.insert("second");
map.insert("third");
auto iter= map.find(ticket);
auto after_erased= map.erase(iter);
assert(after_erased != map.end());
assert(after_erased->ticket == 2);
assert(after_erased->value == "third");
assert(map.size() == 2);
}
void test_erase_lots_of_element_use_returned_iterator() {
jss::ticket_map<unsigned short, int> map;
unsigned const count= 1000;
for(unsigned i= 0; i < count; ++i) {
map.insert(i);
}
auto const cutoff= (count * 9 / 10);
auto iter= map.begin();
for(unsigned i= 0; i < cutoff; ++i) {
iter= map.erase(iter);
}
assert(iter->value == cutoff);
assert(iter == map.begin());
unsigned val= cutoff;
for(; iter != map.end(); ++iter, ++val) {
assert(iter->value == val);
}
val= cutoff;
for(auto &e : map) {
assert(e.value == val);
++val;
}
}
void test_swapping_containers_swaps_everything_including_next_ticket() {
jss::ticket_map<int, int> a, b;
unsigned const count1= 100;
for(unsigned i= 0; i < count1; ++i) {
a.insert(i);
}
unsigned const count2= 200;
for(unsigned i= 0; i < count2; ++i) {
b.insert(i + 1000);
}
assert(a.size() == count1);
assert(b.size() == count2);
a.swap(b);
for(unsigned i= 0; i < count2; ++i) {
assert(a.find(i)->value == i + 1000);
}
for(unsigned i= 0; i < count1; ++i) {
assert(b.find(i)->value == i);
}
assert(b.size() == count1);
assert(a.size() == count2);
auto ticket1= a.insert(99);
auto ticket2= b.insert(99);
assert(ticket1 == count2);
assert(ticket2 == count1);
}
void test_can_swap_with_std_swap() {
jss::ticket_map<int, int> a, b;
unsigned const count1= 100;
for(unsigned i= 0; i < count1; ++i) {
a.insert(i);
}
unsigned const count2= 200;
for(unsigned i= 0; i < count2; ++i) {
b.insert(i + 1000);
}
assert(a.size() == count1);
assert(b.size() == count2);
std::swap(a, b);
for(unsigned i= 0; i < count2; ++i) {
assert(a.find(i)->value == i + 1000);
}
for(unsigned i= 0; i < count1; ++i) {
assert(b.find(i)->value == i);
}
assert(b.size() == count1);
assert(a.size() == count2);
auto ticket1= a.insert(99);
auto ticket2= b.insert(99);
assert(ticket1 == count2);
assert(ticket2 == count1);
}
void test_clear_removes_elements_but_does_not_reset_ticket() {
jss::ticket_map<unsigned short, int> map;
unsigned const count= 100;
for(unsigned i= 0; i < count; ++i) {
map.insert(i);
}
map.clear();
assert(map.size() == 0);
assert(map.empty());
auto ticket= map.insert(42);
assert(ticket == count);
}
void test_copies_preserve_elements() {
jss::ticket_map<unsigned short, int> map;
unsigned const count= 100;
for(unsigned i= 0; i < count; ++i) {
map.insert(i + 1000);
}
jss::ticket_map<unsigned short, int> map2(map);
assert(map2.size() == map.size());
for(auto &e : map) {
auto found= map2.find(e.ticket);
assert(found != map2.end());
assert(found->ticket == e.ticket);
assert(found->value == e.value);
assert(found != map.find(e.ticket));
}
assert(map2.insert(-1) == count);
jss::ticket_map<unsigned short, int> map3;
map3= map;
assert(map3.size() == map.size());
for(auto &e : map) {
auto found= map3.find(e.ticket);
assert(found != map3.end());
assert(found->ticket == e.ticket);
assert(found->value == e.value);
assert(found != map.find(e.ticket));
}
assert(map3.insert(-1) == count);
}
void test_move_transfers_elements() {
jss::ticket_map<unsigned short, int> map;
unsigned const count= 100;
for(unsigned i= 0; i < count; ++i) {
map.insert(i + 1000);
}
static_assert(
std::is_nothrow_move_constructible<jss::ticket_map<int, int>>::value);
static_assert(
std::is_nothrow_move_assignable<jss::ticket_map<int, int>>::value);
jss::ticket_map<unsigned short, int> map2(std::move(map));
assert(map2.size() == count);
assert(map.size() == 0);
assert(map.empty());
for(auto &e : map2) {
assert(e.value == e.ticket + 1000);
}
assert(map2.insert(-1) == count);
jss::ticket_map<unsigned short, int> map3;
map3= std::move(map2);
assert(map3.size() == count + 1);
assert(map2.size() == 0);
assert(map2.empty());
assert(map3.insert(-1) == count + 1);
assert(map2.insert(-1) == count + 1);
}
void test_bulk_insert() {
jss::ticket_map<unsigned short, int> map;
unsigned const count= 100;
for(unsigned i= 0; i < count; ++i) {
map.insert(i + 1000);
}
std::vector<int> const extras= {1, 2, 42, 59, 66, 78, 99};
auto iter= map.insert(extras.begin(), extras.end());
assert(iter != map.end());
assert(iter->ticket == count);
assert(map.size() == count + extras.size());
for(auto it= extras.begin(); it != extras.end(); ++it, ++iter) {
assert(iter != map.end());
assert(iter->value == *it);
}
assert(iter == map.end());
}
void test_construct_from_range() {
std::vector<int> const entries= {1, 2, 42, 59, 66, 78, 99};
jss::ticket_map<unsigned short, int> map(entries.begin(), entries.end());
assert(map.size() == entries.size());
assert(!map.empty());
auto iter= map.begin();
for(auto it= entries.begin(); it != entries.end(); ++it, ++iter) {
assert(iter != map.end());
assert(iter->value == *it);
}
assert(iter == map.end());
assert(map.size() == entries.size());
assert(map.insert(99) == entries.size());
}
void test_emplace() {
struct X {
int val;
std::string str;
X(int i, std::string const &s) : val(i + 100), str(s + " world") {}
};
jss::ticket_map<int, X> map;
auto ticket= map.emplace(42, "hello");
assert(ticket == 0);
assert(map[ticket].val == 142);
assert(map[ticket].str == "hello world");
assert(map.size() == 1);
}
void test_ticket_must_be_incrementable() {
// jss::ticket_map<std::string, int> map; // should fail to compile
}
void test_direct_lookup() {
jss::ticket_map<int, std::string> map;
auto ticket= map.insert("hello");
std::string &s= map[ticket];
assert(&s == &map.begin()->value);
assert(s == "hello");
try {
map[ticket + 1];
assert(!"Should throw");
} catch(std::out_of_range &) {
assert(true);
} catch(...) {
assert(!"Should throw out-of-range");
}
auto const &cmap= map;
static_assert(std::is_same_v<decltype(cmap[ticket]), const std::string &>);
assert(&map[ticket] == &s);
}
void test_iteration_over_const() {
std::vector<int> const entries= {1, 2, 42, 59, 66, 78, 99};
jss::ticket_map<int, int> map(entries.begin(), entries.end());
jss::ticket_map<int, int> const &cmap= map;
jss::ticket_map<int, int>::const_iterator iter= cmap.begin();
for(auto it= entries.begin(); it != entries.end(); ++it, ++iter) {
assert(iter != cmap.end());
assert(iter->value == *it);
}
static_assert(
std::is_same_v<
decltype(map.cbegin()), jss::ticket_map<int, int>::const_iterator>);
iter= map.cbegin();
for(auto it= entries.begin(); it != entries.end(); ++it, ++iter) {
assert(iter != map.cend());
assert(iter->value == *it);
}
}
void test_can_reserve() {
struct MoveCounter {
unsigned count;
MoveCounter() : count(0) {}
MoveCounter(MoveCounter &&other) : count(other.count + 1) {}
MoveCounter &operator=(MoveCounter &&other) {
count= other.count + 1;
return *this;
}
};
jss::ticket_map<int, MoveCounter> map;
unsigned const count= 45;
map.reserve(count);
assert(map.insert_capacity() >= count);
auto const initial_capacity= map.insert_capacity();
for(unsigned i= 0; i < count; ++i) {
map.emplace();
}
assert(map.size() == count);
assert(map[0].count == 0);
assert(map.insert_capacity() == initial_capacity - count);
}
struct MyTicket {
int i;
MyTicket() : i(100) {}
MyTicket operator++(int) {
MyTicket res(*this);
i+= 10;
return res;
}
friend bool operator==(MyTicket const &lhs, MyTicket const &rhs) noexcept {
return lhs.i == rhs.i;
}
friend bool operator!=(MyTicket const &lhs, MyTicket const &rhs) noexcept {
return lhs.i != rhs.i;
}
friend bool operator<(MyTicket const &lhs, MyTicket const &rhs) noexcept {
return lhs.i < rhs.i;
}
};
void test_custom_ticket_type() {
jss::ticket_map<MyTicket, int> map;
auto ticket= map.insert(42);
static_assert(std::is_same_v<decltype(ticket), MyTicket>);
assert(ticket.i == 100);
auto ticket2= map.insert(99);
assert(ticket2.i == 110);
assert(map[ticket] == 42);
assert(map[ticket2] == 99);
}
void test_cannot_overflow() {
jss::ticket_map<unsigned char, int> map;
for(unsigned i= 0; i < 256; ++i) {
map.insert(i);
}
assert(map.size() == 256);
try {
map.insert(-1);
assert(!"Should not be able to insert if ticket overflows");
} catch(std::overflow_error &) {
assert(true);
} catch(...) {
assert(!"Wrong type of exception thrown");
}
}
void test_cannot_overflow_signed() {
jss::ticket_map<signed char, int> map;
for(unsigned i= 0; i < 128; ++i) {
map.insert(i);
}
assert(map.size() == 128);
try {
map.insert(-1);
assert(!"Should not be able to insert if ticket overflows");
} catch(std::overflow_error &) {
assert(true);
} catch(...) {
assert(!"Wrong type of exception thrown");
}
}
struct SmallTicket {
unsigned char i;
SmallTicket() : i(0) {}
SmallTicket operator++(int) {
SmallTicket res(*this);
i+= 1;
return res;
}
friend bool
operator==(SmallTicket const &lhs, SmallTicket const &rhs) noexcept {
return lhs.i == rhs.i;
}
friend bool
operator!=(SmallTicket const &lhs, SmallTicket const &rhs) noexcept {
return lhs.i != rhs.i;
}
friend bool
operator<(SmallTicket const &lhs, SmallTicket const &rhs) noexcept {
return lhs.i < rhs.i;
}
};
void test_cannot_overflow_custom() {
jss::ticket_map<SmallTicket, int> map;
for(unsigned i= 0; i < 256; ++i) {
map.insert(i);
}
assert(map.size() == 256);
try {
map.insert(-1);
assert(!"Should not be able to insert if ticket overflows");
} catch(std::overflow_error &) {
assert(true);
} catch(...) {
assert(!"Wrong type of exception thrown");
}
}
void test_count() {
jss::ticket_map<int, int> map;
assert(!map.count(0));
assert(!map.count(1));
map.insert(42);
assert(map.count(0) == 1);
assert(!map.count(1));
map.erase(0);
assert(!map.count(0));
assert(!map.count(1));
}
int main() {
test_initially_empty();
test_inserting_a_value_gives_ticket_for_new_element();
test_inserting_a_second_value_gives_new_ticket();
test_inserting_a_bunch_of_elements_gives_iterators_and_updates_size();
test_can_iterate_over_values();
test_empty_map_has_begin_equal_end();
test_begin_and_end_types();
test_after_erasing_value_not_there();
test_after_erasing_middle_element_iteration_skips_it();
test_after_erasing_last_element_iteration_skips_it();
test_after_erasing_new_elements_added_correctly();
test_after_erasing_lots_of_elements_still_ok();
test_can_erase_with_iterator();
test_erase_lots_of_element_use_returned_iterator();
test_swapping_containers_swaps_everything_including_next_ticket();
test_can_swap_with_std_swap();
test_clear_removes_elements_but_does_not_reset_ticket();
test_copies_preserve_elements();
test_move_transfers_elements();
test_bulk_insert();
test_construct_from_range();
test_emplace();
test_direct_lookup();
test_iteration_over_const();
test_can_reserve();
test_custom_ticket_type();
test_cannot_overflow();
test_count();
test_cannot_overflow_signed();
test_cannot_overflow_custom();
}