File tree 4 files changed +43
-3
lines changed 4 files changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -201,6 +201,14 @@ class DenseMapBase : public DebugEpochBase {
201
201
return ValueT ();
202
202
}
203
203
204
+ // / at - Return the entry for the specified key, or abort if no such
205
+ // / entry exists.
206
+ const ValueT &at (const_arg_type_t <KeyT> Val) const {
207
+ auto Iter = this ->find (std::move (Val));
208
+ assert (Iter != this ->end () && " DenseMap::at failed due to a missing key" );
209
+ return Iter->second ;
210
+ }
211
+
204
212
// Inserts key,value pair into the map if the key isn't already in the map.
205
213
// If the key is already in the map, it returns false and doesn't update the
206
214
// value.
Original file line number Diff line number Diff line change @@ -231,12 +231,20 @@ class StringMap : public StringMapImpl,
231
231
// / lookup - Return the entry for the specified key, or a default
232
232
// / constructed value if no such entry exists.
233
233
ValueTy lookup (StringRef Key) const {
234
- const_iterator it = find (Key);
235
- if (it != end ())
236
- return it ->second ;
234
+ const_iterator Iter = find (Key);
235
+ if (Iter != end ())
236
+ return Iter ->second ;
237
237
return ValueTy ();
238
238
}
239
239
240
+ // / at - Return the entry for the specified key, or abort if no such
241
+ // / entry exists.
242
+ const ValueTy &at (StringRef Val) const {
243
+ auto Iter = this ->find (std::move (Val));
244
+ assert (Iter != this ->end () && " StringMap::at failed due to a missing key" );
245
+ return Iter->second ;
246
+ }
247
+
240
248
// / Lookup the ValueTy for the \p Key, or create a default constructed value
241
249
// / if the key is not in the map.
242
250
ValueTy &operator [](StringRef Key) { return try_emplace (Key).first ->second ; }
Original file line number Diff line number Diff line change @@ -125,6 +125,10 @@ TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
125
125
EXPECT_TRUE (this ->Map .find (this ->getKey ()) == this ->Map .end ());
126
126
EXPECT_EQ (typename TypeParam::mapped_type (),
127
127
this ->Map .lookup (this ->getKey ()));
128
+
129
+ // LookupOrTrap tests
130
+ EXPECT_DEATH ({ this ->Map .at (this ->getKey ()); },
131
+ " DenseMap::at failed due to a missing key" );
128
132
}
129
133
130
134
// Constant map tests
@@ -156,6 +160,10 @@ TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
156
160
EXPECT_TRUE (this ->Map .find (this ->getKey ()) == this ->Map .begin ());
157
161
EXPECT_EQ (this ->getValue (), this ->Map .lookup (this ->getKey ()));
158
162
EXPECT_EQ (this ->getValue (), this ->Map [this ->getKey ()]);
163
+
164
+ // LookupOrTrap tests
165
+ EXPECT_DEATH ({ this ->Map .at (this ->getKey (1 )); },
166
+ " DenseMap::at failed due to a missing key" );
159
167
}
160
168
161
169
// Test clear() method
Original file line number Diff line number Diff line change @@ -205,6 +205,22 @@ TEST_F(StringMapTest, CopyCtorTest) {
205
205
EXPECT_EQ (5 , Map2.lookup (" funf" ));
206
206
}
207
207
208
+ TEST_F (StringMapTest, LookupOrTrapTest) {
209
+ llvm::StringMap<int > Map;
210
+
211
+ // key not found on empty map
212
+ EXPECT_DEATH ({ Map.at (" a" ); }, " StringMap::at failed due to a missing key" );
213
+
214
+ // keys both found and not found on non-empty map
215
+ Map[" a" ] = 1 ;
216
+ Map[" b" ] = 2 ;
217
+ Map[" c" ] = 3 ;
218
+ EXPECT_EQ (1 , Map.at (" a" ));
219
+ EXPECT_EQ (2 , Map.at (" b" ));
220
+ EXPECT_EQ (3 , Map.at (" c" ));
221
+ EXPECT_DEATH ({ Map.at (" d" ); }, " StringMap::at failed due to a missing key" );
222
+ }
223
+
208
224
// A more complex iteration test.
209
225
TEST_F (StringMapTest, IterationTest) {
210
226
bool visited[100 ];
You can’t perform that action at this time.
0 commit comments