[llvm] r278887 - ADT: Add some missing coverage for iplist::splice

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 19:08:08 PDT 2016


Author: dexonsmith
Date: Tue Aug 16 21:08:08 2016
New Revision: 278887

URL: http://llvm.org/viewvc/llvm-project?rev=278887&view=rev
Log:
ADT: Add some missing coverage for iplist::splice

These splices are interesting because they involve swapping two nodes in
the same list.  There are two ways to do this.  Assuming:

    A -> B -> [Sentinel]

You can either:
- splice B before A, with:        L.splice(A,       L, B) or
- splice A before Sentinel, with: L.splice(L.end(), L, A) to create:

    B -> A -> [Sentinel]

These two swapping-splices are somewhat interesting corner cases for
maintaining the list invariants.  The tests pass even with my new ilist
implementation, but I had some doubts about the latter when I was
looking at weird UB effects.  Since I can't find equivalent explicit
test coverage elsewhere it seems prudent to commit.

Modified:
    llvm/trunk/unittests/ADT/ilistTest.cpp

Modified: llvm/trunk/unittests/ADT/ilistTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ilistTest.cpp?rev=278887&r1=278886&r2=278887&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/ilistTest.cpp (original)
+++ llvm/trunk/unittests/ADT/ilistTest.cpp Tue Aug 16 21:08:08 2016
@@ -64,6 +64,38 @@ TEST(ilistTest, SpliceOne) {
   EXPECT_EQ(3, List.back().Value);
 }
 
+TEST(ilistTest, SpliceSwap) {
+  ilist<Node> L;
+  Node N0(0);
+  Node N1(1);
+  L.insert(L.end(), &N0);
+  L.insert(L.end(), &N1);
+  EXPECT_EQ(0, L.front().Value);
+  EXPECT_EQ(1, L.back().Value);
+
+  L.splice(L.begin(), L, ++L.begin());
+  EXPECT_EQ(1, L.front().Value);
+  EXPECT_EQ(0, L.back().Value);
+
+  L.clearAndLeakNodesUnsafely();
+}
+
+TEST(ilistTest, SpliceSwapOtherWay) {
+  ilist<Node> L;
+  Node N0(0);
+  Node N1(1);
+  L.insert(L.end(), &N0);
+  L.insert(L.end(), &N1);
+  EXPECT_EQ(0, L.front().Value);
+  EXPECT_EQ(1, L.back().Value);
+
+  L.splice(L.end(), L, L.begin());
+  EXPECT_EQ(1, L.front().Value);
+  EXPECT_EQ(0, L.back().Value);
+
+  L.clearAndLeakNodesUnsafely();
+}
+
 TEST(ilistTest, UnsafeClear) {
   ilist<Node> List;
 




More information about the llvm-commits mailing list