forked from abseil/abseil-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree_test.cc
2954 lines (2546 loc) · 94.1 KB
/
btree_test.cc
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
// Copyright 2018 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/btree_test.h"
#include <cstdint>
#include <limits>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/macros.h"
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "absl/container/internal/counting_allocator.h"
#include "absl/container/internal/test_instance_tracker.h"
#include "absl/flags/flag.h"
#include "absl/hash/hash_testing.h"
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/types/compare.h"
ABSL_FLAG(int, test_values, 10000, "The number of values to use for tests");
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {
using ::absl::test_internal::CopyableMovableInstance;
using ::absl::test_internal::InstanceTracker;
using ::absl::test_internal::MovableOnlyInstance;
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::IsEmpty;
using ::testing::IsNull;
using ::testing::Pair;
using ::testing::SizeIs;
template <typename T, typename U>
void CheckPairEquals(const T &x, const U &y) {
ABSL_INTERNAL_CHECK(x == y, "Values are unequal.");
}
template <typename T, typename U, typename V, typename W>
void CheckPairEquals(const std::pair<T, U> &x, const std::pair<V, W> &y) {
CheckPairEquals(x.first, y.first);
CheckPairEquals(x.second, y.second);
}
} // namespace
// The base class for a sorted associative container checker. TreeType is the
// container type to check and CheckerType is the container type to check
// against. TreeType is expected to be btree_{set,map,multiset,multimap} and
// CheckerType is expected to be {set,map,multiset,multimap}.
template <typename TreeType, typename CheckerType>
class base_checker {
public:
using key_type = typename TreeType::key_type;
using value_type = typename TreeType::value_type;
using key_compare = typename TreeType::key_compare;
using pointer = typename TreeType::pointer;
using const_pointer = typename TreeType::const_pointer;
using reference = typename TreeType::reference;
using const_reference = typename TreeType::const_reference;
using size_type = typename TreeType::size_type;
using difference_type = typename TreeType::difference_type;
using iterator = typename TreeType::iterator;
using const_iterator = typename TreeType::const_iterator;
using reverse_iterator = typename TreeType::reverse_iterator;
using const_reverse_iterator = typename TreeType::const_reverse_iterator;
public:
base_checker() : const_tree_(tree_) {}
base_checker(const base_checker &other)
: tree_(other.tree_), const_tree_(tree_), checker_(other.checker_) {}
template <typename InputIterator>
base_checker(InputIterator b, InputIterator e)
: tree_(b, e), const_tree_(tree_), checker_(b, e) {}
iterator begin() { return tree_.begin(); }
const_iterator begin() const { return tree_.begin(); }
iterator end() { return tree_.end(); }
const_iterator end() const { return tree_.end(); }
reverse_iterator rbegin() { return tree_.rbegin(); }
const_reverse_iterator rbegin() const { return tree_.rbegin(); }
reverse_iterator rend() { return tree_.rend(); }
const_reverse_iterator rend() const { return tree_.rend(); }
template <typename IterType, typename CheckerIterType>
IterType iter_check(IterType tree_iter, CheckerIterType checker_iter) const {
if (tree_iter == tree_.end()) {
ABSL_INTERNAL_CHECK(checker_iter == checker_.end(),
"Checker iterator not at end.");
} else {
CheckPairEquals(*tree_iter, *checker_iter);
}
return tree_iter;
}
template <typename IterType, typename CheckerIterType>
IterType riter_check(IterType tree_iter, CheckerIterType checker_iter) const {
if (tree_iter == tree_.rend()) {
ABSL_INTERNAL_CHECK(checker_iter == checker_.rend(),
"Checker iterator not at rend.");
} else {
CheckPairEquals(*tree_iter, *checker_iter);
}
return tree_iter;
}
void value_check(const value_type &v) {
typename KeyOfValue<typename TreeType::key_type,
typename TreeType::value_type>::type key_of_value;
const key_type &key = key_of_value(v);
CheckPairEquals(*find(key), v);
lower_bound(key);
upper_bound(key);
equal_range(key);
contains(key);
count(key);
}
void erase_check(const key_type &key) {
EXPECT_FALSE(tree_.contains(key));
EXPECT_EQ(tree_.find(key), const_tree_.end());
EXPECT_FALSE(const_tree_.contains(key));
EXPECT_EQ(const_tree_.find(key), tree_.end());
EXPECT_EQ(tree_.equal_range(key).first,
const_tree_.equal_range(key).second);
}
iterator lower_bound(const key_type &key) {
return iter_check(tree_.lower_bound(key), checker_.lower_bound(key));
}
const_iterator lower_bound(const key_type &key) const {
return iter_check(tree_.lower_bound(key), checker_.lower_bound(key));
}
iterator upper_bound(const key_type &key) {
return iter_check(tree_.upper_bound(key), checker_.upper_bound(key));
}
const_iterator upper_bound(const key_type &key) const {
return iter_check(tree_.upper_bound(key), checker_.upper_bound(key));
}
std::pair<iterator, iterator> equal_range(const key_type &key) {
std::pair<typename CheckerType::iterator, typename CheckerType::iterator>
checker_res = checker_.equal_range(key);
std::pair<iterator, iterator> tree_res = tree_.equal_range(key);
iter_check(tree_res.first, checker_res.first);
iter_check(tree_res.second, checker_res.second);
return tree_res;
}
std::pair<const_iterator, const_iterator> equal_range(
const key_type &key) const {
std::pair<typename CheckerType::const_iterator,
typename CheckerType::const_iterator>
checker_res = checker_.equal_range(key);
std::pair<const_iterator, const_iterator> tree_res = tree_.equal_range(key);
iter_check(tree_res.first, checker_res.first);
iter_check(tree_res.second, checker_res.second);
return tree_res;
}
iterator find(const key_type &key) {
return iter_check(tree_.find(key), checker_.find(key));
}
const_iterator find(const key_type &key) const {
return iter_check(tree_.find(key), checker_.find(key));
}
bool contains(const key_type &key) const { return find(key) != end(); }
size_type count(const key_type &key) const {
size_type res = checker_.count(key);
EXPECT_EQ(res, tree_.count(key));
return res;
}
base_checker &operator=(const base_checker &other) {
tree_ = other.tree_;
checker_ = other.checker_;
return *this;
}
int erase(const key_type &key) {
int size = tree_.size();
int res = checker_.erase(key);
EXPECT_EQ(res, tree_.count(key));
EXPECT_EQ(res, tree_.erase(key));
EXPECT_EQ(tree_.count(key), 0);
EXPECT_EQ(tree_.size(), size - res);
erase_check(key);
return res;
}
iterator erase(iterator iter) {
key_type key = iter.key();
int size = tree_.size();
int count = tree_.count(key);
auto checker_iter = checker_.lower_bound(key);
for (iterator tmp(tree_.lower_bound(key)); tmp != iter; ++tmp) {
++checker_iter;
}
auto checker_next = checker_iter;
++checker_next;
checker_.erase(checker_iter);
iter = tree_.erase(iter);
EXPECT_EQ(tree_.size(), checker_.size());
EXPECT_EQ(tree_.size(), size - 1);
EXPECT_EQ(tree_.count(key), count - 1);
if (count == 1) {
erase_check(key);
}
return iter_check(iter, checker_next);
}
void erase(iterator begin, iterator end) {
int size = tree_.size();
int count = std::distance(begin, end);
auto checker_begin = checker_.lower_bound(begin.key());
for (iterator tmp(tree_.lower_bound(begin.key())); tmp != begin; ++tmp) {
++checker_begin;
}
auto checker_end =
end == tree_.end() ? checker_.end() : checker_.lower_bound(end.key());
if (end != tree_.end()) {
for (iterator tmp(tree_.lower_bound(end.key())); tmp != end; ++tmp) {
++checker_end;
}
}
const auto checker_ret = checker_.erase(checker_begin, checker_end);
const auto tree_ret = tree_.erase(begin, end);
EXPECT_EQ(std::distance(checker_.begin(), checker_ret),
std::distance(tree_.begin(), tree_ret));
EXPECT_EQ(tree_.size(), checker_.size());
EXPECT_EQ(tree_.size(), size - count);
}
void clear() {
tree_.clear();
checker_.clear();
}
void swap(base_checker &other) {
tree_.swap(other.tree_);
checker_.swap(other.checker_);
}
void verify() const {
tree_.verify();
EXPECT_EQ(tree_.size(), checker_.size());
// Move through the forward iterators using increment.
auto checker_iter = checker_.begin();
const_iterator tree_iter(tree_.begin());
for (; tree_iter != tree_.end(); ++tree_iter, ++checker_iter) {
CheckPairEquals(*tree_iter, *checker_iter);
}
// Move through the forward iterators using decrement.
for (int n = tree_.size() - 1; n >= 0; --n) {
iter_check(tree_iter, checker_iter);
--tree_iter;
--checker_iter;
}
EXPECT_EQ(tree_iter, tree_.begin());
EXPECT_EQ(checker_iter, checker_.begin());
// Move through the reverse iterators using increment.
auto checker_riter = checker_.rbegin();
const_reverse_iterator tree_riter(tree_.rbegin());
for (; tree_riter != tree_.rend(); ++tree_riter, ++checker_riter) {
CheckPairEquals(*tree_riter, *checker_riter);
}
// Move through the reverse iterators using decrement.
for (int n = tree_.size() - 1; n >= 0; --n) {
riter_check(tree_riter, checker_riter);
--tree_riter;
--checker_riter;
}
EXPECT_EQ(tree_riter, tree_.rbegin());
EXPECT_EQ(checker_riter, checker_.rbegin());
}
const TreeType &tree() const { return tree_; }
size_type size() const {
EXPECT_EQ(tree_.size(), checker_.size());
return tree_.size();
}
size_type max_size() const { return tree_.max_size(); }
bool empty() const {
EXPECT_EQ(tree_.empty(), checker_.empty());
return tree_.empty();
}
protected:
TreeType tree_;
const TreeType &const_tree_;
CheckerType checker_;
};
namespace {
// A checker for unique sorted associative containers. TreeType is expected to
// be btree_{set,map} and CheckerType is expected to be {set,map}.
template <typename TreeType, typename CheckerType>
class unique_checker : public base_checker<TreeType, CheckerType> {
using super_type = base_checker<TreeType, CheckerType>;
public:
using iterator = typename super_type::iterator;
using value_type = typename super_type::value_type;
public:
unique_checker() : super_type() {}
unique_checker(const unique_checker &other) : super_type(other) {}
template <class InputIterator>
unique_checker(InputIterator b, InputIterator e) : super_type(b, e) {}
unique_checker &operator=(const unique_checker &) = default;
// Insertion routines.
std::pair<iterator, bool> insert(const value_type &v) {
int size = this->tree_.size();
std::pair<typename CheckerType::iterator, bool> checker_res =
this->checker_.insert(v);
std::pair<iterator, bool> tree_res = this->tree_.insert(v);
CheckPairEquals(*tree_res.first, *checker_res.first);
EXPECT_EQ(tree_res.second, checker_res.second);
EXPECT_EQ(this->tree_.size(), this->checker_.size());
EXPECT_EQ(this->tree_.size(), size + tree_res.second);
return tree_res;
}
iterator insert(iterator position, const value_type &v) {
int size = this->tree_.size();
std::pair<typename CheckerType::iterator, bool> checker_res =
this->checker_.insert(v);
iterator tree_res = this->tree_.insert(position, v);
CheckPairEquals(*tree_res, *checker_res.first);
EXPECT_EQ(this->tree_.size(), this->checker_.size());
EXPECT_EQ(this->tree_.size(), size + checker_res.second);
return tree_res;
}
template <typename InputIterator>
void insert(InputIterator b, InputIterator e) {
for (; b != e; ++b) {
insert(*b);
}
}
};
// A checker for multiple sorted associative containers. TreeType is expected
// to be btree_{multiset,multimap} and CheckerType is expected to be
// {multiset,multimap}.
template <typename TreeType, typename CheckerType>
class multi_checker : public base_checker<TreeType, CheckerType> {
using super_type = base_checker<TreeType, CheckerType>;
public:
using iterator = typename super_type::iterator;
using value_type = typename super_type::value_type;
public:
multi_checker() : super_type() {}
multi_checker(const multi_checker &other) : super_type(other) {}
template <class InputIterator>
multi_checker(InputIterator b, InputIterator e) : super_type(b, e) {}
multi_checker &operator=(const multi_checker &) = default;
// Insertion routines.
iterator insert(const value_type &v) {
int size = this->tree_.size();
auto checker_res = this->checker_.insert(v);
iterator tree_res = this->tree_.insert(v);
CheckPairEquals(*tree_res, *checker_res);
EXPECT_EQ(this->tree_.size(), this->checker_.size());
EXPECT_EQ(this->tree_.size(), size + 1);
return tree_res;
}
iterator insert(iterator position, const value_type &v) {
int size = this->tree_.size();
auto checker_res = this->checker_.insert(v);
iterator tree_res = this->tree_.insert(position, v);
CheckPairEquals(*tree_res, *checker_res);
EXPECT_EQ(this->tree_.size(), this->checker_.size());
EXPECT_EQ(this->tree_.size(), size + 1);
return tree_res;
}
template <typename InputIterator>
void insert(InputIterator b, InputIterator e) {
for (; b != e; ++b) {
insert(*b);
}
}
};
template <typename T, typename V>
void DoTest(const char *name, T *b, const std::vector<V> &values) {
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
T &mutable_b = *b;
const T &const_b = *b;
// Test insert.
for (int i = 0; i < values.size(); ++i) {
mutable_b.insert(values[i]);
mutable_b.value_check(values[i]);
}
ASSERT_EQ(mutable_b.size(), values.size());
const_b.verify();
// Test copy constructor.
T b_copy(const_b);
EXPECT_EQ(b_copy.size(), const_b.size());
for (int i = 0; i < values.size(); ++i) {
CheckPairEquals(*b_copy.find(key_of_value(values[i])), values[i]);
}
// Test range constructor.
T b_range(const_b.begin(), const_b.end());
EXPECT_EQ(b_range.size(), const_b.size());
for (int i = 0; i < values.size(); ++i) {
CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]);
}
// Test range insertion for values that already exist.
b_range.insert(b_copy.begin(), b_copy.end());
b_range.verify();
// Test range insertion for new values.
b_range.clear();
b_range.insert(b_copy.begin(), b_copy.end());
EXPECT_EQ(b_range.size(), b_copy.size());
for (int i = 0; i < values.size(); ++i) {
CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]);
}
// Test assignment to self. Nothing should change.
b_range.operator=(b_range);
EXPECT_EQ(b_range.size(), b_copy.size());
// Test assignment of new values.
b_range.clear();
b_range = b_copy;
EXPECT_EQ(b_range.size(), b_copy.size());
// Test swap.
b_range.clear();
b_range.swap(b_copy);
EXPECT_EQ(b_copy.size(), 0);
EXPECT_EQ(b_range.size(), const_b.size());
for (int i = 0; i < values.size(); ++i) {
CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]);
}
b_range.swap(b_copy);
// Test non-member function swap.
swap(b_range, b_copy);
EXPECT_EQ(b_copy.size(), 0);
EXPECT_EQ(b_range.size(), const_b.size());
for (int i = 0; i < values.size(); ++i) {
CheckPairEquals(*b_range.find(key_of_value(values[i])), values[i]);
}
swap(b_range, b_copy);
// Test erase via values.
for (int i = 0; i < values.size(); ++i) {
mutable_b.erase(key_of_value(values[i]));
// Erasing a non-existent key should have no effect.
ASSERT_EQ(mutable_b.erase(key_of_value(values[i])), 0);
}
const_b.verify();
EXPECT_EQ(const_b.size(), 0);
// Test erase via iterators.
mutable_b = b_copy;
for (int i = 0; i < values.size(); ++i) {
mutable_b.erase(mutable_b.find(key_of_value(values[i])));
}
const_b.verify();
EXPECT_EQ(const_b.size(), 0);
// Test insert with hint.
for (int i = 0; i < values.size(); i++) {
mutable_b.insert(mutable_b.upper_bound(key_of_value(values[i])), values[i]);
}
const_b.verify();
// Test range erase.
mutable_b.erase(mutable_b.begin(), mutable_b.end());
EXPECT_EQ(mutable_b.size(), 0);
const_b.verify();
// First half.
mutable_b = b_copy;
typename T::iterator mutable_iter_end = mutable_b.begin();
for (int i = 0; i < values.size() / 2; ++i) ++mutable_iter_end;
mutable_b.erase(mutable_b.begin(), mutable_iter_end);
EXPECT_EQ(mutable_b.size(), values.size() - values.size() / 2);
const_b.verify();
// Second half.
mutable_b = b_copy;
typename T::iterator mutable_iter_begin = mutable_b.begin();
for (int i = 0; i < values.size() / 2; ++i) ++mutable_iter_begin;
mutable_b.erase(mutable_iter_begin, mutable_b.end());
EXPECT_EQ(mutable_b.size(), values.size() / 2);
const_b.verify();
// Second quarter.
mutable_b = b_copy;
mutable_iter_begin = mutable_b.begin();
for (int i = 0; i < values.size() / 4; ++i) ++mutable_iter_begin;
mutable_iter_end = mutable_iter_begin;
for (int i = 0; i < values.size() / 4; ++i) ++mutable_iter_end;
mutable_b.erase(mutable_iter_begin, mutable_iter_end);
EXPECT_EQ(mutable_b.size(), values.size() - values.size() / 4);
const_b.verify();
mutable_b.clear();
}
template <typename T>
void ConstTest() {
using value_type = typename T::value_type;
typename KeyOfValue<typename T::key_type, value_type>::type key_of_value;
T mutable_b;
const T &const_b = mutable_b;
// Insert a single value into the container and test looking it up.
value_type value = Generator<value_type>(2)(2);
mutable_b.insert(value);
EXPECT_TRUE(mutable_b.contains(key_of_value(value)));
EXPECT_NE(mutable_b.find(key_of_value(value)), const_b.end());
EXPECT_TRUE(const_b.contains(key_of_value(value)));
EXPECT_NE(const_b.find(key_of_value(value)), mutable_b.end());
EXPECT_EQ(*const_b.lower_bound(key_of_value(value)), value);
EXPECT_EQ(const_b.upper_bound(key_of_value(value)), const_b.end());
EXPECT_EQ(*const_b.equal_range(key_of_value(value)).first, value);
// We can only create a non-const iterator from a non-const container.
typename T::iterator mutable_iter(mutable_b.begin());
EXPECT_EQ(mutable_iter, const_b.begin());
EXPECT_NE(mutable_iter, const_b.end());
EXPECT_EQ(const_b.begin(), mutable_iter);
EXPECT_NE(const_b.end(), mutable_iter);
typename T::reverse_iterator mutable_riter(mutable_b.rbegin());
EXPECT_EQ(mutable_riter, const_b.rbegin());
EXPECT_NE(mutable_riter, const_b.rend());
EXPECT_EQ(const_b.rbegin(), mutable_riter);
EXPECT_NE(const_b.rend(), mutable_riter);
// We can create a const iterator from a non-const iterator.
typename T::const_iterator const_iter(mutable_iter);
EXPECT_EQ(const_iter, mutable_b.begin());
EXPECT_NE(const_iter, mutable_b.end());
EXPECT_EQ(mutable_b.begin(), const_iter);
EXPECT_NE(mutable_b.end(), const_iter);
typename T::const_reverse_iterator const_riter(mutable_riter);
EXPECT_EQ(const_riter, mutable_b.rbegin());
EXPECT_NE(const_riter, mutable_b.rend());
EXPECT_EQ(mutable_b.rbegin(), const_riter);
EXPECT_NE(mutable_b.rend(), const_riter);
// Make sure various methods can be invoked on a const container.
const_b.verify();
ASSERT_TRUE(!const_b.empty());
EXPECT_EQ(const_b.size(), 1);
EXPECT_GT(const_b.max_size(), 0);
EXPECT_TRUE(const_b.contains(key_of_value(value)));
EXPECT_EQ(const_b.count(key_of_value(value)), 1);
}
template <typename T, typename C>
void BtreeTest() {
ConstTest<T>();
using V = typename remove_pair_const<typename T::value_type>::type;
const std::vector<V> random_values = GenerateValuesWithSeed<V>(
absl::GetFlag(FLAGS_test_values), 4 * absl::GetFlag(FLAGS_test_values),
GTEST_FLAG_GET(random_seed));
unique_checker<T, C> container;
// Test key insertion/deletion in sorted order.
std::vector<V> sorted_values(random_values);
std::sort(sorted_values.begin(), sorted_values.end());
DoTest("sorted: ", &container, sorted_values);
// Test key insertion/deletion in reverse sorted order.
std::reverse(sorted_values.begin(), sorted_values.end());
DoTest("rsorted: ", &container, sorted_values);
// Test key insertion/deletion in random order.
DoTest("random: ", &container, random_values);
}
template <typename T, typename C>
void BtreeMultiTest() {
ConstTest<T>();
using V = typename remove_pair_const<typename T::value_type>::type;
const std::vector<V> random_values = GenerateValuesWithSeed<V>(
absl::GetFlag(FLAGS_test_values), 4 * absl::GetFlag(FLAGS_test_values),
GTEST_FLAG_GET(random_seed));
multi_checker<T, C> container;
// Test keys in sorted order.
std::vector<V> sorted_values(random_values);
std::sort(sorted_values.begin(), sorted_values.end());
DoTest("sorted: ", &container, sorted_values);
// Test keys in reverse sorted order.
std::reverse(sorted_values.begin(), sorted_values.end());
DoTest("rsorted: ", &container, sorted_values);
// Test keys in random order.
DoTest("random: ", &container, random_values);
// Test keys in random order w/ duplicates.
std::vector<V> duplicate_values(random_values);
duplicate_values.insert(duplicate_values.end(), random_values.begin(),
random_values.end());
DoTest("duplicates:", &container, duplicate_values);
// Test all identical keys.
std::vector<V> identical_values(100);
std::fill(identical_values.begin(), identical_values.end(),
Generator<V>(2)(2));
DoTest("identical: ", &container, identical_values);
}
template <typename T>
struct PropagatingCountingAlloc : public CountingAllocator<T> {
using propagate_on_container_copy_assignment = std::true_type;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
using Base = CountingAllocator<T>;
using Base::Base;
template <typename U>
explicit PropagatingCountingAlloc(const PropagatingCountingAlloc<U> &other)
: Base(other.bytes_used_) {}
template <typename U>
struct rebind {
using other = PropagatingCountingAlloc<U>;
};
};
template <typename T>
void BtreeAllocatorTest() {
using value_type = typename T::value_type;
int64_t bytes1 = 0, bytes2 = 0;
PropagatingCountingAlloc<T> allocator1(&bytes1);
PropagatingCountingAlloc<T> allocator2(&bytes2);
Generator<value_type> generator(1000);
// Test that we allocate properly aligned memory. If we don't, then Layout
// will assert fail.
auto unused1 = allocator1.allocate(1);
auto unused2 = allocator2.allocate(1);
// Test copy assignment
{
T b1(typename T::key_compare(), allocator1);
T b2(typename T::key_compare(), allocator2);
int64_t original_bytes1 = bytes1;
b1.insert(generator(0));
EXPECT_GT(bytes1, original_bytes1);
// This should propagate the allocator.
b1 = b2;
EXPECT_EQ(b1.size(), 0);
EXPECT_EQ(b2.size(), 0);
EXPECT_EQ(bytes1, original_bytes1);
for (int i = 1; i < 1000; i++) {
b1.insert(generator(i));
}
// We should have allocated out of allocator2.
EXPECT_GT(bytes2, bytes1);
}
// Test move assignment
{
T b1(typename T::key_compare(), allocator1);
T b2(typename T::key_compare(), allocator2);
int64_t original_bytes1 = bytes1;
b1.insert(generator(0));
EXPECT_GT(bytes1, original_bytes1);
// This should propagate the allocator.
b1 = std::move(b2);
EXPECT_EQ(b1.size(), 0);
EXPECT_EQ(bytes1, original_bytes1);
for (int i = 1; i < 1000; i++) {
b1.insert(generator(i));
}
// We should have allocated out of allocator2.
EXPECT_GT(bytes2, bytes1);
}
// Test swap
{
T b1(typename T::key_compare(), allocator1);
T b2(typename T::key_compare(), allocator2);
int64_t original_bytes1 = bytes1;
b1.insert(generator(0));
EXPECT_GT(bytes1, original_bytes1);
// This should swap the allocators.
swap(b1, b2);
EXPECT_EQ(b1.size(), 0);
EXPECT_EQ(b2.size(), 1);
EXPECT_GT(bytes1, original_bytes1);
for (int i = 1; i < 1000; i++) {
b1.insert(generator(i));
}
// We should have allocated out of allocator2.
EXPECT_GT(bytes2, bytes1);
}
allocator1.deallocate(unused1, 1);
allocator2.deallocate(unused2, 1);
}
template <typename T>
void BtreeMapTest() {
using value_type = typename T::value_type;
using mapped_type = typename T::mapped_type;
mapped_type m = Generator<mapped_type>(0)(0);
(void)m;
T b;
// Verify we can insert using operator[].
for (int i = 0; i < 1000; i++) {
value_type v = Generator<value_type>(1000)(i);
b[v.first] = v.second;
}
EXPECT_EQ(b.size(), 1000);
// Test whether we can use the "->" operator on iterators and
// reverse_iterators. This stresses the btree_map_params::pair_pointer
// mechanism.
EXPECT_EQ(b.begin()->first, Generator<value_type>(1000)(0).first);
EXPECT_EQ(b.begin()->second, Generator<value_type>(1000)(0).second);
EXPECT_EQ(b.rbegin()->first, Generator<value_type>(1000)(999).first);
EXPECT_EQ(b.rbegin()->second, Generator<value_type>(1000)(999).second);
}
template <typename T>
void BtreeMultiMapTest() {
using mapped_type = typename T::mapped_type;
mapped_type m = Generator<mapped_type>(0)(0);
(void)m;
}
template <typename K, int N = 256>
void SetTest() {
EXPECT_EQ(
sizeof(absl::btree_set<K>),
2 * sizeof(void *) + sizeof(typename absl::btree_set<K>::size_type));
using BtreeSet = absl::btree_set<K>;
using CountingBtreeSet =
absl::btree_set<K, std::less<K>, PropagatingCountingAlloc<K>>;
BtreeTest<BtreeSet, std::set<K>>();
BtreeAllocatorTest<CountingBtreeSet>();
}
template <typename K, int N = 256>
void MapTest() {
EXPECT_EQ(
sizeof(absl::btree_map<K, K>),
2 * sizeof(void *) + sizeof(typename absl::btree_map<K, K>::size_type));
using BtreeMap = absl::btree_map<K, K>;
using CountingBtreeMap =
absl::btree_map<K, K, std::less<K>,
PropagatingCountingAlloc<std::pair<const K, K>>>;
BtreeTest<BtreeMap, std::map<K, K>>();
BtreeAllocatorTest<CountingBtreeMap>();
BtreeMapTest<BtreeMap>();
}
TEST(Btree, set_int32) { SetTest<int32_t>(); }
TEST(Btree, set_int64) { SetTest<int64_t>(); }
TEST(Btree, set_string) { SetTest<std::string>(); }
TEST(Btree, set_cord) { SetTest<absl::Cord>(); }
TEST(Btree, set_pair) { SetTest<std::pair<int, int>>(); }
TEST(Btree, map_int32) { MapTest<int32_t>(); }
TEST(Btree, map_int64) { MapTest<int64_t>(); }
TEST(Btree, map_string) { MapTest<std::string>(); }
TEST(Btree, map_cord) { MapTest<absl::Cord>(); }
TEST(Btree, map_pair) { MapTest<std::pair<int, int>>(); }
template <typename K, int N = 256>
void MultiSetTest() {
EXPECT_EQ(
sizeof(absl::btree_multiset<K>),
2 * sizeof(void *) + sizeof(typename absl::btree_multiset<K>::size_type));
using BtreeMSet = absl::btree_multiset<K>;
using CountingBtreeMSet =
absl::btree_multiset<K, std::less<K>, PropagatingCountingAlloc<K>>;
BtreeMultiTest<BtreeMSet, std::multiset<K>>();
BtreeAllocatorTest<CountingBtreeMSet>();
}
template <typename K, int N = 256>
void MultiMapTest() {
EXPECT_EQ(sizeof(absl::btree_multimap<K, K>),
2 * sizeof(void *) +
sizeof(typename absl::btree_multimap<K, K>::size_type));
using BtreeMMap = absl::btree_multimap<K, K>;
using CountingBtreeMMap =
absl::btree_multimap<K, K, std::less<K>,
PropagatingCountingAlloc<std::pair<const K, K>>>;
BtreeMultiTest<BtreeMMap, std::multimap<K, K>>();
BtreeMultiMapTest<BtreeMMap>();
BtreeAllocatorTest<CountingBtreeMMap>();
}
TEST(Btree, multiset_int32) { MultiSetTest<int32_t>(); }
TEST(Btree, multiset_int64) { MultiSetTest<int64_t>(); }
TEST(Btree, multiset_string) { MultiSetTest<std::string>(); }
TEST(Btree, multiset_cord) { MultiSetTest<absl::Cord>(); }
TEST(Btree, multiset_pair) { MultiSetTest<std::pair<int, int>>(); }
TEST(Btree, multimap_int32) { MultiMapTest<int32_t>(); }
TEST(Btree, multimap_int64) { MultiMapTest<int64_t>(); }
TEST(Btree, multimap_string) { MultiMapTest<std::string>(); }
TEST(Btree, multimap_cord) { MultiMapTest<absl::Cord>(); }
TEST(Btree, multimap_pair) { MultiMapTest<std::pair<int, int>>(); }
struct CompareIntToString {
bool operator()(const std::string &a, const std::string &b) const {
return a < b;
}
bool operator()(const std::string &a, int b) const {
return a < absl::StrCat(b);
}
bool operator()(int a, const std::string &b) const {
return absl::StrCat(a) < b;
}
using is_transparent = void;
};
struct NonTransparentCompare {
template <typename T, typename U>
bool operator()(const T &t, const U &u) const {
// Treating all comparators as transparent can cause inefficiencies (see
// N3657 C++ proposal). Test that for comparators without 'is_transparent'
// alias (like this one), we do not attempt heterogeneous lookup.
EXPECT_TRUE((std::is_same<T, U>()));
return t < u;
}
};
template <typename T>
bool CanEraseWithEmptyBrace(T t, decltype(t.erase({})) *) {
return true;
}
template <typename T>
bool CanEraseWithEmptyBrace(T, ...) {
return false;
}
template <typename T>
void TestHeterogeneous(T table) {
auto lb = table.lower_bound("3");
EXPECT_EQ(lb, table.lower_bound(3));
EXPECT_NE(lb, table.lower_bound(4));
EXPECT_EQ(lb, table.lower_bound({"3"}));
EXPECT_NE(lb, table.lower_bound({}));
auto ub = table.upper_bound("3");
EXPECT_EQ(ub, table.upper_bound(3));
EXPECT_NE(ub, table.upper_bound(5));
EXPECT_EQ(ub, table.upper_bound({"3"}));
EXPECT_NE(ub, table.upper_bound({}));
auto er = table.equal_range("3");
EXPECT_EQ(er, table.equal_range(3));
EXPECT_NE(er, table.equal_range(4));
EXPECT_EQ(er, table.equal_range({"3"}));
EXPECT_NE(er, table.equal_range({}));
auto it = table.find("3");
EXPECT_EQ(it, table.find(3));
EXPECT_NE(it, table.find(4));
EXPECT_EQ(it, table.find({"3"}));
EXPECT_NE(it, table.find({}));
EXPECT_TRUE(table.contains(3));
EXPECT_FALSE(table.contains(4));
EXPECT_TRUE(table.count({"3"}));
EXPECT_FALSE(table.contains({}));
EXPECT_EQ(1, table.count(3));
EXPECT_EQ(0, table.count(4));
EXPECT_EQ(1, table.count({"3"}));
EXPECT_EQ(0, table.count({}));
auto copy = table;
copy.erase(3);
EXPECT_EQ(table.size() - 1, copy.size());
copy.erase(4);
EXPECT_EQ(table.size() - 1, copy.size());
copy.erase({"5"});
EXPECT_EQ(table.size() - 2, copy.size());
EXPECT_FALSE(CanEraseWithEmptyBrace(table, nullptr));
// Also run it with const T&.
if (std::is_class<T>()) TestHeterogeneous<const T &>(table);
}
TEST(Btree, HeterogeneousLookup) {
TestHeterogeneous(btree_set<std::string, CompareIntToString>{"1", "3", "5"});
TestHeterogeneous(btree_map<std::string, int, CompareIntToString>{
{"1", 1}, {"3", 3}, {"5", 5}});
TestHeterogeneous(
btree_multiset<std::string, CompareIntToString>{"1", "3", "5"});
TestHeterogeneous(btree_multimap<std::string, int, CompareIntToString>{
{"1", 1}, {"3", 3}, {"5", 5}});
// Only maps have .at()
btree_map<std::string, int, CompareIntToString> map{
{"", -1}, {"1", 1}, {"3", 3}, {"5", 5}};
EXPECT_EQ(1, map.at(1));
EXPECT_EQ(3, map.at({"3"}));
EXPECT_EQ(-1, map.at({}));
const auto &cmap = map;
EXPECT_EQ(1, cmap.at(1));
EXPECT_EQ(3, cmap.at({"3"}));
EXPECT_EQ(-1, cmap.at({}));
}
TEST(Btree, NoHeterogeneousLookupWithoutAlias) {
using StringSet = absl::btree_set<std::string, NonTransparentCompare>;
StringSet s;
ASSERT_TRUE(s.insert("hello").second);
ASSERT_TRUE(s.insert("world").second);
EXPECT_TRUE(s.end() == s.find("blah"));
EXPECT_TRUE(s.begin() == s.lower_bound("hello"));
EXPECT_EQ(1, s.count("world"));
EXPECT_TRUE(s.contains("hello"));
EXPECT_TRUE(s.contains("world"));
EXPECT_FALSE(s.contains("blah"));
using StringMultiSet =
absl::btree_multiset<std::string, NonTransparentCompare>;
StringMultiSet ms;
ms.insert("hello");
ms.insert("world");
ms.insert("world");
EXPECT_TRUE(ms.end() == ms.find("blah"));
EXPECT_TRUE(ms.begin() == ms.lower_bound("hello"));
EXPECT_EQ(2, ms.count("world"));
EXPECT_TRUE(ms.contains("hello"));
EXPECT_TRUE(ms.contains("world"));
EXPECT_FALSE(ms.contains("blah"));
}
TEST(Btree, DefaultTransparent) {
{
// `int` does not have a default transparent comparator.
// The input value is converted to key_type.
btree_set<int> s = {1};
double d = 1.1;
EXPECT_EQ(s.begin(), s.find(d));
EXPECT_TRUE(s.contains(d));