From a73c84a978c8dd27b980d881d845b87b9a0ffdea Mon Sep 17 00:00:00 2001 From: Sidharth Suresh Date: Sat, 9 Nov 2024 18:03:49 +0100 Subject: [PATCH] Refactor tests. --- test/CustomHashSetWithMemoryLimitTest.cpp | 17 +++++++++++++++-- test/LocalVocabTest.cpp | 16 ++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/test/CustomHashSetWithMemoryLimitTest.cpp b/test/CustomHashSetWithMemoryLimitTest.cpp index b2d5b42e41..3dcb299291 100644 --- a/test/CustomHashSetWithMemoryLimitTest.cpp +++ b/test/CustomHashSetWithMemoryLimitTest.cpp @@ -9,6 +9,7 @@ using namespace ad_utility::memory_literals; using Set = ad_utility::NodeHashSetWithMemoryLimit; +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, sizeAndInsert) { Set hashSet{makeAllocationMemoryLeftThreadsafeObject(100_B)}; ASSERT_EQ(hashSet.size(), 0); @@ -18,6 +19,7 @@ TEST(CustomHashSetWithMemoryLimitTest, sizeAndInsert) { ASSERT_EQ(hashSet.size(), 3); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, memoryLimit) { Set hashSet{makeAllocationMemoryLeftThreadsafeObject(10_B)}; std::initializer_list testNums = { @@ -36,6 +38,7 @@ TEST(CustomHashSetWithMemoryLimitTest, memoryLimit) { ad_utility::detail::AllocationExceedsLimitException); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, iteratorOperations) { Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; hashSet.insert(1); @@ -59,6 +62,7 @@ TEST(CustomHashSetWithMemoryLimitTest, iteratorOperations) { ASSERT_EQ(values, std::vector({1, 2, 3})); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, eraseOperations) { Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; hashSet.insert(1); @@ -76,6 +80,7 @@ TEST(CustomHashSetWithMemoryLimitTest, eraseOperations) { ASSERT_EQ(hashSet.size(), originalSize); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, clearOperation) { Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; auto initialMemory = hashSet.getCurrentMemoryUsage(); @@ -98,6 +103,7 @@ TEST(CustomHashSetWithMemoryLimitTest, clearOperation) { ASSERT_LE(hashSet.getCurrentMemoryUsage(), usedMemory); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, memoryTrackingAccuracy) { Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; auto initialMemory = hashSet.getCurrentMemoryUsage(); @@ -117,6 +123,7 @@ TEST(CustomHashSetWithMemoryLimitTest, memoryTrackingAccuracy) { ASSERT_EQ(hashSet.getCurrentMemoryUsage(), initialMemory); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, edgeCases) { // Test with zero memory limit ASSERT_THROW(Set zeroHashSet{makeAllocationMemoryLeftThreadsafeObject(0_B)}, @@ -124,13 +131,14 @@ TEST(CustomHashSetWithMemoryLimitTest, edgeCases) { // Test multiple insert/erase cycles Set cycleHashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; + auto memoryBeforeCycle = cycleHashSet.getCurrentMemoryUsage(); for (int i = 0; i < 10; ++i) { cycleHashSet.insert(i); cycleHashSet.erase(i); } + auto memoryAfterCycle = cycleHashSet.getCurrentMemoryUsage(); ASSERT_TRUE(cycleHashSet.empty()); - ASSERT_EQ(cycleHashSet.getCurrentMemoryUsage(), - cycleHashSet.getCurrentMemoryUsage()); + ASSERT_EQ(memoryBeforeCycle, memoryAfterCycle); } // Define a custom size getter for strings that includes the actual string @@ -144,6 +152,7 @@ struct StringSizeGetter { using StringSet = ad_utility::NodeHashSetWithMemoryLimit; +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, stringInsertAndMemoryTracking) { StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; auto initialMemory = hashSet.getCurrentMemoryUsage(); @@ -163,6 +172,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringInsertAndMemoryTracking) { afterFirstInsert - initialMemory); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, stringMemoryLimit) { // Set a very small memory limit StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(100_B)}; @@ -177,6 +187,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringMemoryLimit) { ad_utility::detail::AllocationExceedsLimitException); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, stringEraseAndClear) { StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; @@ -197,6 +208,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringEraseAndClear) { ASSERT_LT(memoryAfterClear, memoryAfterErase); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, stringDuplicates) { StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; @@ -211,6 +223,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringDuplicates) { ASSERT_EQ(hashSet.size(), 1); } +// _____________________________________________________________________________ TEST(CustomHashSetWithMemoryLimitTest, stringCapacityVsSize) { StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)}; diff --git a/test/LocalVocabTest.cpp b/test/LocalVocabTest.cpp index 977a99019c..ac0af9f51b 100644 --- a/test/LocalVocabTest.cpp +++ b/test/LocalVocabTest.cpp @@ -34,6 +34,7 @@ #include "global/Id.h" #include "gmock/gmock.h" #include "util/AllocatorWithLimit.h" +#include "util/GTestHelpers.h" #include "util/IndexTestHelpers.h" #include "util/MemorySize/MemorySize.h" @@ -393,14 +394,13 @@ TEST(LocalVocab, memoryLimit) { LocalVocab localVocab(smallLimit); TestWords testWords = getTestCollectionOfWords(1000); - try { - for (const auto& word : testWords) { - localVocab.getIndexAndAddIfNotContained(word); - } - } catch (const std::exception& e) { - std::string errorMessage = e.what(); - EXPECT_THAT(errorMessage, ::testing::StartsWith("Tried to allocate")); - } + EXPECT_THROW( + { + for (const auto& word : testWords) { + localVocab.getIndexAndAddIfNotContained(word); + } + }, + ad_utility::detail::AllocationExceedsLimitException); auto extraWord = ad_utility::triple_component::LiteralOrIri::literalWithoutQuotes(