From 1d2ad781398d2a8743899eb54153998ca03ac090 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 2 Apr 2025 05:25:26 -0500 Subject: More work on hash maps. --- test/test_hash_map.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'test') diff --git a/test/test_hash_map.cpp b/test/test_hash_map.cpp index 065ae81..c55eda3 100644 --- a/test/test_hash_map.cpp +++ b/test/test_hash_map.cpp @@ -133,4 +133,65 @@ TEST(FudHash, InsertCopyKeyCopyValue) } } +TEST(FudHash, RemoveKeyConstRef) +{ + auto stringList{testStrings()}; + HashMap mapString2Int{}; + for (int index = 0; index < static_cast(stringList.size()); ++index) { + auto insertStatus = mapString2Int.insert(String::from(stringList[index]).takeOkay(), index * 1); + EXPECT_EQ(insertStatus, FudStatus::Success); + } + + EXPECT_EQ(mapString2Int.size(), stringList.size()); + EXPECT_GT(mapString2Int.capacity(), mapString2Int.size()); + + for (int index = 0; index < static_cast(stringList.size()); ++index) { + const int invalid = -1; + EXPECT_EQ(mapString2Int.get(stringList[index]).valueOr(invalid), index); + } + + for (const auto& entry : stringList) { + EXPECT_EQ(mapString2Int.remove(entry), FudStatus::Success); + } + + for (const auto& entry : stringList) { + EXPECT_EQ(mapString2Int.remove(entry), FudStatus::NotFound); + } + + for (const auto& entry : stringList) { + const int invalid = -1; + EXPECT_EQ(mapString2Int.get(entry).valueOr(invalid), invalid); + } +} + +TEST(FudHash, RemoveKeyMoveRef) +{ + auto stringList{testStrings()}; + HashMap mapInt2String{}; + for (int index = 0; index < static_cast(stringList.size()); ++index) { + auto insertStatus = mapInt2String.insert(index, String::from(stringList[index]).takeOkay()); + EXPECT_EQ(insertStatus, FudStatus::Success); + } + + EXPECT_EQ(mapInt2String.size(), stringList.size()); + EXPECT_GT(mapInt2String.capacity(), mapInt2String.size()); + + const String invalid{String::makeFromCString("Invalid").takeOkay()}; + for (int index = 0; index < static_cast(stringList.size()); ++index) { + EXPECT_EQ(mapInt2String.getConstRef(index).valueOr(invalid), stringList[index]); + } + + for (int index = 0; index < stringList.size(); ++index) { + EXPECT_EQ(mapInt2String.remove(index * 1), FudStatus::Success); + } + + for (int index = 0; index < stringList.size(); ++index) { + EXPECT_EQ(mapInt2String.remove(index * 1), FudStatus::NotFound); + } + + for (int index = 0; index < static_cast(stringList.size()); ++index) { + EXPECT_EQ(mapInt2String.getConstRef(index).valueOr(invalid), invalid); + } +} + } // namespace fud -- cgit v1.2.3