@@ -49,7 +49,7 @@ class TrieRawHashMapTestHelper {
4949} // namespace llvm
5050
5151namespace {
52- template <typename DataType, size_t HashSize>
52+ template <typename DataType, size_t HashSize = sizeof ( uint64_t ) >
5353class SimpleTrieHashMapTest : public TrieRawHashMapTestHelper ,
5454 public ::testing::Test {
5555public:
@@ -64,11 +64,7 @@ class SimpleTrieHashMapTest : public TrieRawHashMapTestHelper,
6464 }
6565
6666 void destroyTrie () { Trie.reset (); }
67-
68- ~SimpleTrieHashMapTest () {
69- if (Trie)
70- Trie.reset ();
71- }
67+ ~SimpleTrieHashMapTest () { destroyTrie (); }
7268
7369 // Use the number itself as hash to test the pathological case.
7470 static HashType hash (uint64_t Num) {
@@ -83,7 +79,7 @@ class SimpleTrieHashMapTest : public TrieRawHashMapTestHelper,
8379 std::optional<TrieType> Trie;
8480};
8581
86- using SmallNodeTrieTest = SimpleTrieHashMapTest<uint64_t , sizeof ( uint64_t ) >;
82+ using SmallNodeTrieTest = SimpleTrieHashMapTest<uint64_t >;
8783
8884TEST_F (SmallNodeTrieTest, TrieAllocation) {
8985 NumType Numbers[] = {
@@ -209,9 +205,7 @@ TEST_F(SmallNodeTrieTest, TrieStructureSmallFinalSubtrie) {
209205 }
210206 for (NumType N : Numbers) {
211207 TrieType::pointer Lookup = Trie.find (hash (N));
212- EXPECT_TRUE (Lookup);
213- if (!Lookup)
214- continue ;
208+ ASSERT_TRUE (Lookup);
215209 EXPECT_EQ (hash (N), Lookup->Hash );
216210 EXPECT_EQ (N, Lookup->Data );
217211
@@ -273,11 +267,11 @@ TEST_F(SmallNodeTrieTest, TrieDestructionLoop) {
273267
274268struct NumWithDestructorT {
275269 uint64_t Num;
276- ~NumWithDestructorT () {}
270+ llvm::function_ref<void ()> DestructorCallback;
271+ ~NumWithDestructorT () { DestructorCallback (); }
277272};
278273
279- using NodeWithDestructorTrieTest =
280- SimpleTrieHashMapTest<NumWithDestructorT, sizeof (uint64_t )>;
274+ using NodeWithDestructorTrieTest = SimpleTrieHashMapTest<NumWithDestructorT>;
281275
282276TEST_F (NodeWithDestructorTrieTest, TrieDestructionLoop) {
283277 // Test destroying large Trie. Make sure there is no recursion that can
@@ -289,17 +283,26 @@ TEST_F(NodeWithDestructorTrieTest, TrieDestructionLoop) {
289283 // Fill them up. Pick a MaxN high enough to cause a stack overflow in debug
290284 // builds.
291285 static constexpr uint64_t MaxN = 100000 ;
286+
287+ uint64_t DestructorCalled = 0 ;
288+ auto DtorCallback = [&DestructorCalled]() { ++DestructorCalled; };
292289 for (uint64_t N = 0 ; N != MaxN; ++N) {
293290 HashType Hash = hash (N);
294- Trie.insert (TrieType::pointer (), TrieType::value_type (Hash, NumType{N}));
291+ Trie.insert (TrieType::pointer (),
292+ TrieType::value_type (Hash, NumType{N, DtorCallback}));
295293 }
294+ // Reset the count after all the temporaries get destroyed.
295+ DestructorCalled = 0 ;
296296
297297 // Destroy tries. If destruction is recursive and MaxN is high enough, these
298298 // will both fail.
299299 destroyTrie ();
300+
301+ // Count the number of destructor calls during `destroyTrie()`.
302+ ASSERT_EQ (DestructorCalled, MaxN);
300303}
301304
302- using NumStrNodeTrieTest = SimpleTrieHashMapTest<std::string, sizeof ( uint64_t ) >;
305+ using NumStrNodeTrieTest = SimpleTrieHashMapTest<std::string>;
303306
304307TEST_F (NumStrNodeTrieTest, TrieInsertLazy) {
305308 for (unsigned RootBits : {2 , 3 , 6 , 10 }) {
0 commit comments