diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2025-04-02 05:25:26 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2025-04-02 05:25:26 -0500 |
commit | 1d2ad781398d2a8743899eb54153998ca03ac090 (patch) | |
tree | c935ffe2880b7101d2d9162a76c8bed5be931ebb /test | |
parent | 8b0bc70db73b48d833a3b5791e55921768cf6932 (diff) |
More work on hash maps.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_hash_map.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
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<String, int> mapString2Int{}; + for (int index = 0; index < static_cast<int>(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<int>(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<int, String> mapInt2String{}; + for (int index = 0; index < static_cast<int>(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<int>(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<int>(stringList.size()); ++index) { + EXPECT_EQ(mapInt2String.getConstRef(index).valueOr(invalid), invalid); + } +} + } // namespace fud |