diff options
author | Ryan Guo <ryanguo@modular.com> | 2023-02-13 20:06:44 -0800 |
---|---|---|
committer | Ryan Guo <ryanguo@modular.com> | 2023-02-17 08:32:29 -0800 |
commit | 132003603ae3453bc385ffd5ed53f5e8057ae1bc (patch) | |
tree | adf479054954e92863f6d53d0ff10077fe14d512 /llvm/unittests/ADT/StringMapTest.cpp | |
parent | 2b51c8cd2ac6b9a6a5b713700c8299954980242d (diff) | |
download | llvm-132003603ae3453bc385ffd5ed53f5e8057ae1bc.zip llvm-132003603ae3453bc385ffd5ed53f5e8057ae1bc.tar.gz llvm-132003603ae3453bc385ffd5ed53f5e8057ae1bc.tar.bz2 |
[ADT] Add `at` method (assertive lookup) to DenseMap and StringMap
This patch makes it easier for users when they want to use validated
lookup on DenseMap/StringMap as a composable C++ expression. For
instance:
```
// instead of
if (auto val = map.lookup(key))
return val;
assert("...");
// we can write
return map.at(key);
```
Differential Revision: https://reviews.llvm.org/D143976
Diffstat (limited to 'llvm/unittests/ADT/StringMapTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/StringMapTest.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index f40f22a..25562d3 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -205,6 +205,22 @@ TEST_F(StringMapTest, CopyCtorTest) { EXPECT_EQ(5, Map2.lookup("funf")); } +TEST_F(StringMapTest, LookupOrTrapTest) { + llvm::StringMap<int> Map; + + // key not found on empty map + EXPECT_DEATH({ Map.at("a"); }, "StringMap::at failed due to a missing key"); + + // keys both found and not found on non-empty map + Map["a"] = 1; + Map["b"] = 2; + Map["c"] = 3; + EXPECT_EQ(1, Map.at("a")); + EXPECT_EQ(2, Map.at("b")); + EXPECT_EQ(3, Map.at("c")); + EXPECT_DEATH({ Map.at("d"); }, "StringMap::at failed due to a missing key"); +} + // A more complex iteration test. TEST_F(StringMapTest, IterationTest) { bool visited[100]; |