diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-14 11:47:38 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-14 11:47:38 +0200 |
commit | fdcff7d15e89297e94a0bfa33b51eebf6e55e96a (patch) | |
tree | 1e0a25fc25888d875f3685881b91acaa64eeb5a8 /AK/Tests | |
parent | f75b1127b2f0ae22af04ae590ba22652f5092f5f (diff) | |
download | serenity-fdcff7d15e89297e94a0bfa33b51eebf6e55e96a.zip |
AK: Make it possible to use HashMap<K, NonnullOwnPtr>::get()
Add the concept of a PeekType to Traits<T>. This is the type we'll
return (wrapped in an Optional) from HashMap::get().
The PeekType for OwnPtr<T> and NonnullOwnPtr<T> is const T*,
which means that HashMap::get() will return an Optional<const T*> for
maps-of-those.
Diffstat (limited to 'AK/Tests')
-rw-r--r-- | AK/Tests/TestHashMap.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/AK/Tests/TestHashMap.cpp b/AK/Tests/TestHashMap.cpp index 9daa7e494c..774e8e5a31 100644 --- a/AK/Tests/TestHashMap.cpp +++ b/AK/Tests/TestHashMap.cpp @@ -76,4 +76,33 @@ TEST_CASE(assert_on_iteration_during_clear) map.clear(); } +TEST_CASE(hashmap_of_nonnullownptr_get) +{ + struct Object { + Object(const String& s) : string(s) {} + String string; + }; + + HashMap<int, NonnullOwnPtr<Object>> objects; + objects.set(1, make<Object>("One")); + objects.set(2, make<Object>("Two")); + objects.set(3, make<Object>("Three")); + + { + auto x = objects.get(2); + EXPECT_EQ(x.has_value(), true); + EXPECT_EQ(x.value()->string, "Two"); + } + + { + // Do it again to make sure that peeking into the map above didn't + // remove the value from the map. + auto x = objects.get(2); + EXPECT_EQ(x.has_value(), true); + EXPECT_EQ(x.value()->string, "Two"); + } + + EXPECT_EQ(objects.size(), 3); +} + TEST_MAIN(HashMap) |