From 821756c32ee197556905a94910e631721113dbb3 Mon Sep 17 00:00:00 2001 From: Evan Brown Date: Thu, 21 Sep 2023 11:57:32 -0700 Subject: Replace BtreeAllocatorTest with individual test cases for copy/move/swap propagation (defined in test_allocator.h) and minimal alignment. Also remove some extraneous value_types from typed tests. The motivation is to reduce btree_test compile time. PiperOrigin-RevId: 567376572 Change-Id: I6ac6130b99faeadaedab8c2c7b05d5e23e77cc1e --- absl/container/btree_test.cc | 175 ++++--------------------------------------- 1 file changed, 13 insertions(+), 162 deletions(-) (limited to 'absl/container/btree_test.cc') diff --git a/absl/container/btree_test.cc b/absl/container/btree_test.cc index 1f4e368d..c52c3231 100644 --- a/absl/container/btree_test.cc +++ b/absl/container/btree_test.cc @@ -76,16 +76,6 @@ void CheckPairEquals(const std::pair &x, const std::pair &y) { CheckPairEquals(x.first, y.first); CheckPairEquals(x.second, y.second); } - -bool IsAssertEnabled() { - // Use an assert with side-effects to figure out if they are actually enabled. - bool assert_enabled = false; - assert([&]() { // NOLINT - assert_enabled = true; - return true; - }()); - return assert_enabled; -} } // namespace // The base class for a sorted associative container checker. TreeType is the @@ -667,96 +657,6 @@ void BtreeMultiTest() { DoTest("identical: ", &container, identical_values); } -// TODO(ezb): get rid of BtreeAllocatorTest and replace with test cases using -// specific propagating allocs (e.g. CopyAssignPropagatingCountingAlloc) and -// also a test for MinimumAlignmentAlloc. Motivation is better test coverage and -// faster compilation time. -template -void BtreeAllocatorTest() { - using value_type = typename T::value_type; - - int64_t bytes1 = 0, bytes2 = 0; - PropagatingCountingAlloc allocator1(&bytes1); - PropagatingCountingAlloc allocator2(&bytes2); - Generator 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 void BtreeMapTest() { using value_type = typename T::value_type; @@ -796,10 +696,7 @@ void SetTest() { sizeof(absl::btree_set), 2 * sizeof(void *) + sizeof(typename absl::btree_set::size_type)); using BtreeSet = absl::btree_set; - using CountingBtreeSet = - absl::btree_set, PropagatingCountingAlloc>; BtreeTest>(); - BtreeAllocatorTest(); } template @@ -808,24 +705,16 @@ void MapTest() { sizeof(absl::btree_map), 2 * sizeof(void *) + sizeof(typename absl::btree_map::size_type)); using BtreeMap = absl::btree_map; - using CountingBtreeMap = - absl::btree_map, - PropagatingCountingAlloc>>; BtreeTest>(); - BtreeAllocatorTest(); BtreeMapTest(); } TEST(Btree, set_int32) { SetTest(); } -TEST(Btree, set_int64) { SetTest(); } TEST(Btree, set_string) { SetTest(); } TEST(Btree, set_cord) { SetTest(); } -TEST(Btree, set_pair) { SetTest>(); } TEST(Btree, map_int32) { MapTest(); } -TEST(Btree, map_int64) { MapTest(); } TEST(Btree, map_string) { MapTest(); } TEST(Btree, map_cord) { MapTest(); } -TEST(Btree, map_pair) { MapTest>(); } template void MultiSetTest() { @@ -833,10 +722,7 @@ void MultiSetTest() { sizeof(absl::btree_multiset), 2 * sizeof(void *) + sizeof(typename absl::btree_multiset::size_type)); using BtreeMSet = absl::btree_multiset; - using CountingBtreeMSet = - absl::btree_multiset, PropagatingCountingAlloc>; BtreeMultiTest>(); - BtreeAllocatorTest(); } template @@ -845,24 +731,16 @@ void MultiMapTest() { 2 * sizeof(void *) + sizeof(typename absl::btree_multimap::size_type)); using BtreeMMap = absl::btree_multimap; - using CountingBtreeMMap = - absl::btree_multimap, - PropagatingCountingAlloc>>; BtreeMultiTest>(); BtreeMultiMapTest(); - BtreeAllocatorTest(); } TEST(Btree, multiset_int32) { MultiSetTest(); } -TEST(Btree, multiset_int64) { MultiSetTest(); } TEST(Btree, multiset_string) { MultiSetTest(); } TEST(Btree, multiset_cord) { MultiSetTest(); } -TEST(Btree, multiset_pair) { MultiSetTest>(); } TEST(Btree, multimap_int32) { MultiMapTest(); } -TEST(Btree, multimap_int64) { MultiMapTest(); } TEST(Btree, multimap_string) { MultiMapTest(); } TEST(Btree, multimap_cord) { MultiMapTest(); } -TEST(Btree, multimap_pair) { MultiMapTest>(); } struct CompareIntToString { bool operator()(const std::string &a, const std::string &b) const { @@ -2511,50 +2389,23 @@ TEST(Btree, TryEmplaceWithHintAndMultipleValueArgsWorks) { EXPECT_EQ(std::string(10, 'a'), m[1]); } -TEST(Btree, MoveAssignmentAllocatorPropagation) { - InstanceTracker tracker; - - int64_t bytes1 = 0, bytes2 = 0; - MoveAssignPropagatingCountingAlloc allocator1(&bytes1); - MoveAssignPropagatingCountingAlloc allocator2(&bytes2); - std::less cmp; - - // Test propagating allocator_type. - { - absl::btree_set, - MoveAssignPropagatingCountingAlloc> - set1(cmp, allocator1), set2(cmp, allocator2); - - for (int i = 0; i < 100; ++i) set1.insert(MovableOnlyInstance(i)); - - tracker.ResetCopiesMovesSwaps(); - set2 = std::move(set1); - EXPECT_EQ(tracker.moves(), 0); - } - // Test non-propagating allocator_type with equal allocators. - { - absl::btree_set, - CountingAllocator> - set1(cmp, allocator1), set2(cmp, allocator1); +template +using BtreeSetAlloc = absl::btree_set, Alloc>; - for (int i = 0; i < 100; ++i) set1.insert(MovableOnlyInstance(i)); +TEST(Btree, AllocatorPropagation) { + TestAllocPropagation(); +} - tracker.ResetCopiesMovesSwaps(); - set2 = std::move(set1); - EXPECT_EQ(tracker.moves(), 0); - } - // Test non-propagating allocator_type with different allocators. - { - absl::btree_set, - CountingAllocator> - set1(cmp, allocator1), set2(cmp, allocator2); +TEST(Btree, MinimumAlignmentAllocator) { + absl::btree_set, MinimumAlignmentAlloc> set; - for (int i = 0; i < 100; ++i) set1.insert(MovableOnlyInstance(i)); + // Do some basic operations. Test that everything is fine when allocator uses + // minimal alignment. + for (int8_t i = 0; i < 100; ++i) set.insert(i); + set.erase(set.find(50), set.end()); + for (int8_t i = 51; i < 101; ++i) set.insert(i); - tracker.ResetCopiesMovesSwaps(); - set2 = std::move(set1); - EXPECT_GE(tracker.moves(), 100); - } + EXPECT_EQ(set.size(), 100); } TEST(Btree, EmptyTree) { -- cgit v1.2.3