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/DenseMapTest.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/DenseMapTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/DenseMapTest.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp index 2d01316..ba4e764 100644 --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -125,6 +125,10 @@ TYPED_TEST(DenseMapTest, EmptyIntMapTest) { EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end()); EXPECT_EQ(typename TypeParam::mapped_type(), this->Map.lookup(this->getKey())); + + // LookupOrTrap tests + EXPECT_DEATH({ this->Map.at(this->getKey()); }, + "DenseMap::at failed due to a missing key"); } // Constant map tests @@ -156,6 +160,10 @@ TYPED_TEST(DenseMapTest, SingleEntryMapTest) { EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin()); EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey())); EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); + + // LookupOrTrap tests + EXPECT_DEATH({ this->Map.at(this->getKey(1)); }, + "DenseMap::at failed due to a missing key"); } // Test clear() method |