llvm.org GIT mirror llvm / 7f1d6d6
Add an assertion for a likely ilist::splice() contract violation. The single-element ilist::splice() function supports a noop move: List.splice(I, List, I); The corresponding std::list function doesn't allow that, so add a unit test to document that behavior. This also means that List.splice(I, List, F); is somewhat surprisingly not equivalent to List.splice(I, List, F, next(F)); This patch adds an assertion to catch the illegal case I == F above. Alternatively, we could make I == F a legal noop, but that would make ilist differ even more from std::list. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170443 91177308-0d34-0410-b5e6-96231b3b80d8 Jakob Stoklund Olesen 6 years ago
2 changed file(s) with 25 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
471471 //
472472 void transfer(iterator position, iplist &L2, iterator first, iterator last) {
473473 assert(first != last && "Should be checked by callers");
474 // Position cannot be contained in the range to be transferred.
475 // Check for the most common mistake.
476 assert(position != first &&
477 "Insertion point can't be one of the transferred nodes");
474478
475479 if (position != last) {
476480 // Note: we have to be careful about the case when we move the first node
88
99 #include "llvm/ADT/ilist.h"
1010 #include "llvm/ADT/ilist_node.h"
11 #include "llvm/ADT/STLExtras.h"
1112 #include "gtest/gtest.h"
1213 #include
1314
4041 EXPECT_EQ(1, ConstList.back().getPrevNode()->Value);
4142 }
4243
44 TEST(ilistTest, SpliceOne) {
45 ilist List;
46 List.push_back(1);
47
48 // The single-element splice operation supports noops.
49 List.splice(List.begin(), List, List.begin());
50 EXPECT_EQ(1u, List.size());
51 EXPECT_EQ(1, List.front().Value);
52 EXPECT_TRUE(llvm::next(List.begin()) == List.end());
53
54 // Altenative noop. Move the first element behind itself.
55 List.push_back(2);
56 List.push_back(3);
57 List.splice(llvm::next(List.begin()), List, List.begin());
58 EXPECT_EQ(3u, List.size());
59 EXPECT_EQ(1, List.front().Value);
60 EXPECT_EQ(2, llvm::next(List.begin())->Value);
61 EXPECT_EQ(3, List.back().Value);
4362 }
63
64 }