Skip to content

Commit

Permalink
Refactor tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
s1dharth-s committed Nov 11, 2024
1 parent c918f3b commit a73c84a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
17 changes: 15 additions & 2 deletions test/CustomHashSetWithMemoryLimitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using namespace ad_utility::memory_literals;

using Set = ad_utility::NodeHashSetWithMemoryLimit<int>;

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, sizeAndInsert) {
Set hashSet{makeAllocationMemoryLeftThreadsafeObject(100_B)};
ASSERT_EQ(hashSet.size(), 0);
Expand All @@ -18,6 +19,7 @@ TEST(CustomHashSetWithMemoryLimitTest, sizeAndInsert) {
ASSERT_EQ(hashSet.size(), 3);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, memoryLimit) {
Set hashSet{makeAllocationMemoryLeftThreadsafeObject(10_B)};
std::initializer_list<int> testNums = {
Expand All @@ -36,6 +38,7 @@ TEST(CustomHashSetWithMemoryLimitTest, memoryLimit) {
ad_utility::detail::AllocationExceedsLimitException);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, iteratorOperations) {
Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};
hashSet.insert(1);
Expand All @@ -59,6 +62,7 @@ TEST(CustomHashSetWithMemoryLimitTest, iteratorOperations) {
ASSERT_EQ(values, std::vector<int>({1, 2, 3}));
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, eraseOperations) {
Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};
hashSet.insert(1);
Expand All @@ -76,6 +80,7 @@ TEST(CustomHashSetWithMemoryLimitTest, eraseOperations) {
ASSERT_EQ(hashSet.size(), originalSize);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, clearOperation) {
Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};
auto initialMemory = hashSet.getCurrentMemoryUsage();
Expand All @@ -98,6 +103,7 @@ TEST(CustomHashSetWithMemoryLimitTest, clearOperation) {
ASSERT_LE(hashSet.getCurrentMemoryUsage(), usedMemory);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, memoryTrackingAccuracy) {
Set hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};
auto initialMemory = hashSet.getCurrentMemoryUsage();
Expand All @@ -117,20 +123,22 @@ TEST(CustomHashSetWithMemoryLimitTest, memoryTrackingAccuracy) {
ASSERT_EQ(hashSet.getCurrentMemoryUsage(), initialMemory);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, edgeCases) {
// Test with zero memory limit
ASSERT_THROW(Set zeroHashSet{makeAllocationMemoryLeftThreadsafeObject(0_B)},
ad_utility::detail::AllocationExceedsLimitException);

// 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
Expand All @@ -144,6 +152,7 @@ struct StringSizeGetter {
using StringSet =
ad_utility::NodeHashSetWithMemoryLimit<std::string, StringSizeGetter>;

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, stringInsertAndMemoryTracking) {
StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};
auto initialMemory = hashSet.getCurrentMemoryUsage();
Expand All @@ -163,6 +172,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringInsertAndMemoryTracking) {
afterFirstInsert - initialMemory);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, stringMemoryLimit) {
// Set a very small memory limit
StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(100_B)};
Expand All @@ -177,6 +187,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringMemoryLimit) {
ad_utility::detail::AllocationExceedsLimitException);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, stringEraseAndClear) {
StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};

Expand All @@ -197,6 +208,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringEraseAndClear) {
ASSERT_LT(memoryAfterClear, memoryAfterErase);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, stringDuplicates) {
StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};

Expand All @@ -211,6 +223,7 @@ TEST(CustomHashSetWithMemoryLimitTest, stringDuplicates) {
ASSERT_EQ(hashSet.size(), 1);
}

// _____________________________________________________________________________
TEST(CustomHashSetWithMemoryLimitTest, stringCapacityVsSize) {
StringSet hashSet{makeAllocationMemoryLeftThreadsafeObject(1000_B)};

Expand Down
16 changes: 8 additions & 8 deletions test/LocalVocabTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit a73c84a

Please sign in to comment.