llvm.org GIT mirror llvm / 71a6baa
Add != to YAMLParser's basic_collection_iterator. ...and mark it as merely an input_iterator rather than a forward_iterator, since it is destructive. And then rewrite == to take advantage of that. Patch by Alex Denisov! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256913 91177308-0d34-0410-b5e6-96231b3b80d8 Jordan Rose 3 years ago
2 changed file(s) with 90 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
304304 /// increment() which must set CurrentEntry to 0 to create an end iterator.
305305 template
306306 class basic_collection_iterator
307 : public std::iteratorforward_iterator_tag, ValueT> {
307 : public std::iteratorinput_iterator_tag, ValueT> {
308308 public:
309309 basic_collection_iterator() : Base(nullptr) {}
310310 basic_collection_iterator(BaseT *B) : Base(B) {}
325325 return Base->CurrentEntry;
326326 }
327327
328 /// Note on EqualityComparable:
329 ///
330 /// The iterator is not re-entrant,
331 /// it is meant to be used for parsing YAML on-demand
332 /// Once iteration started - it can point only to one entry at a time
333 /// hence Base.CurrentEntry and Other.Base.CurrentEntry are equal
334 /// iff Base and Other.Base are equal.
335 bool operator==(const basic_collection_iterator &Other) const {
336 if (Base && (Base == Other.Base)) {
337 assert((Base->CurrentEntry == Other.Base->CurrentEntry)
338 && "Equal Bases expected to point to equal Entries");
339 }
340
341 return Base == Other.Base;
342 }
343
328344 bool operator!=(const basic_collection_iterator &Other) const {
329 if (Base != Other.Base)
330 return true;
331 return (Base && Other.Base) &&
332 Base->CurrentEntry != Other.Base->CurrentEntry;
345 return !(Base == Other.Base);
333346 }
334347
335348 basic_collection_iterator &operator++() {
259259 EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
260260 }
261261
262 TEST(YAMLParser, SameNodeIteratorOperatorNotEquals) {
263 SourceMgr SM;
264 yaml::Stream Stream("[\"1\", \"2\"]", SM);
265
266 yaml::SequenceNode *Node = dyn_cast(
267 Stream.begin()->getRoot());
268
269 auto Begin = Node->begin();
270 auto End = Node->end();
271
272 EXPECT_TRUE(Begin != End);
273 EXPECT_FALSE(Begin != Begin);
274 EXPECT_FALSE(End != End);
275 }
276
277 TEST(YAMLParser, SameNodeIteratorOperatorEquals) {
278 SourceMgr SM;
279 yaml::Stream Stream("[\"1\", \"2\"]", SM);
280
281 yaml::SequenceNode *Node = dyn_cast(
282 Stream.begin()->getRoot());
283
284 auto Begin = Node->begin();
285 auto End = Node->end();
286
287 EXPECT_FALSE(Begin == End);
288 EXPECT_TRUE(Begin == Begin);
289 EXPECT_TRUE(End == End);
290 }
291
292 TEST(YAMLParser, DifferentNodesIteratorOperatorNotEquals) {
293 SourceMgr SM;
294 yaml::Stream Stream("[\"1\", \"2\"]", SM);
295 yaml::Stream AnotherStream("[\"1\", \"2\"]", SM);
296
297 yaml::SequenceNode *Node = dyn_cast(
298 Stream.begin()->getRoot());
299 yaml::SequenceNode *AnotherNode = dyn_cast(
300 AnotherStream.begin()->getRoot());
301
302 auto Begin = Node->begin();
303 auto End = Node->end();
304
305 auto AnotherBegin = AnotherNode->begin();
306 auto AnotherEnd = AnotherNode->end();
307
308 EXPECT_TRUE(Begin != AnotherBegin);
309 EXPECT_TRUE(Begin != AnotherEnd);
310 EXPECT_FALSE(End != AnotherEnd);
311 }
312
313 TEST(YAMLParser, DifferentNodesIteratorOperatorEquals) {
314 SourceMgr SM;
315 yaml::Stream Stream("[\"1\", \"2\"]", SM);
316 yaml::Stream AnotherStream("[\"1\", \"2\"]", SM);
317
318 yaml::SequenceNode *Node = dyn_cast(
319 Stream.begin()->getRoot());
320 yaml::SequenceNode *AnotherNode = dyn_cast(
321 AnotherStream.begin()->getRoot());
322
323 auto Begin = Node->begin();
324 auto End = Node->end();
325
326 auto AnotherBegin = AnotherNode->begin();
327 auto AnotherEnd = AnotherNode->end();
328
329 EXPECT_FALSE(Begin == AnotherBegin);
330 EXPECT_FALSE(Begin == AnotherEnd);
331 EXPECT_TRUE(End == AnotherEnd);
332 }
333
262334 } // end namespace llvm