1 //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ADT/ilist.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/ilist_node.h" 13 #include "gtest/gtest.h" 14 #include <ostream> 15 16 using namespace llvm; 17 18 namespace { 19 20 struct Node : ilist_node<Node> { 21 int Value; 22 23 Node() {} 24 Node(int _Value) : Value(_Value) {} 25 ~Node() { Value = -1; } 26 }; 27 28 TEST(ilistTest, Basic) { 29 ilist<Node> List; 30 List.push_back(Node(1)); 31 EXPECT_EQ(1, List.back().Value); 32 EXPECT_EQ(0, List.back().getPrevNode()); 33 EXPECT_EQ(0, List.back().getNextNode()); 34 35 List.push_back(Node(2)); 36 EXPECT_EQ(2, List.back().Value); 37 EXPECT_EQ(2, List.front().getNextNode()->Value); 38 EXPECT_EQ(1, List.back().getPrevNode()->Value); 39 40 const ilist<Node> &ConstList = List; 41 EXPECT_EQ(2, ConstList.back().Value); 42 EXPECT_EQ(2, ConstList.front().getNextNode()->Value); 43 EXPECT_EQ(1, ConstList.back().getPrevNode()->Value); 44 } 45 46 TEST(ilistTest, SpliceOne) { 47 ilist<Node> List; 48 List.push_back(1); 49 50 // The single-element splice operation supports noops. 51 List.splice(List.begin(), List, List.begin()); 52 EXPECT_EQ(1u, List.size()); 53 EXPECT_EQ(1, List.front().Value); 54 EXPECT_TRUE(llvm::next(List.begin()) == List.end()); 55 56 // Altenative noop. Move the first element behind itself. 57 List.push_back(2); 58 List.push_back(3); 59 List.splice(llvm::next(List.begin()), List, List.begin()); 60 EXPECT_EQ(3u, List.size()); 61 EXPECT_EQ(1, List.front().Value); 62 EXPECT_EQ(2, llvm::next(List.begin())->Value); 63 EXPECT_EQ(3, List.back().Value); 64 } 65 66 TEST(ilistTest, UnsafeClear) { 67 ilist<Node> List; 68 69 // Before even allocating a sentinel. 70 List.clearAndLeakNodesUnsafely(); 71 EXPECT_EQ(0u, List.size()); 72 73 // Empty list with sentinel. 74 ilist<Node>::iterator E = List.end(); 75 List.clearAndLeakNodesUnsafely(); 76 EXPECT_EQ(0u, List.size()); 77 // The sentinel shouldn't change. 78 EXPECT_TRUE(E == List.end()); 79 80 // List with contents. 81 List.push_back(1); 82 ASSERT_EQ(1u, List.size()); 83 Node *N = List.begin(); 84 EXPECT_EQ(1, N->Value); 85 List.clearAndLeakNodesUnsafely(); 86 EXPECT_EQ(0u, List.size()); 87 ASSERT_EQ(1, N->Value); 88 delete N; 89 90 // List is still functional. 91 List.push_back(5); 92 List.push_back(6); 93 ASSERT_EQ(2u, List.size()); 94 EXPECT_EQ(5, List.front().Value); 95 EXPECT_EQ(6, List.back().Value); 96 } 97 98 } 99