Remove the explicit SDNodeIterator::operator= in favor of the implicit default
[oota-llvm.git] / unittests / ADT / ilistTest.cpp
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(const Node&) = default;
26   Node(Node &&RHS) : Value(RHS.Value) { RHS.Value = -1; }
27   ~Node() { Value = -1; }
28 };
29
30 TEST(ilistTest, Basic) {
31   ilist<Node> List;
32   List.push_back(Node(1));
33   EXPECT_EQ(1, List.back().Value);
34   EXPECT_EQ(nullptr, List.back().getPrevNode());
35   EXPECT_EQ(nullptr, List.back().getNextNode());
36
37   List.push_back(Node(2));
38   EXPECT_EQ(2, List.back().Value);
39   EXPECT_EQ(2, List.front().getNextNode()->Value);
40   EXPECT_EQ(1, List.back().getPrevNode()->Value);
41
42   const ilist<Node> &ConstList = List;
43   EXPECT_EQ(2, ConstList.back().Value);
44   EXPECT_EQ(2, ConstList.front().getNextNode()->Value);
45   EXPECT_EQ(1, ConstList.back().getPrevNode()->Value);
46 }
47
48 TEST(ilistTest, SpliceOne) {
49   ilist<Node> List;
50   List.push_back(1);
51
52   // The single-element splice operation supports noops.
53   List.splice(List.begin(), List, List.begin());
54   EXPECT_EQ(1u, List.size());
55   EXPECT_EQ(1, List.front().Value);
56   EXPECT_TRUE(std::next(List.begin()) == List.end());
57
58   // Altenative noop. Move the first element behind itself.
59   List.push_back(2);
60   List.push_back(3);
61   List.splice(std::next(List.begin()), List, List.begin());
62   EXPECT_EQ(3u, List.size());
63   EXPECT_EQ(1, List.front().Value);
64   EXPECT_EQ(2, std::next(List.begin())->Value);
65   EXPECT_EQ(3, List.back().Value);
66 }
67
68 TEST(ilistTest, UnsafeClear) {
69   ilist<Node> List;
70
71   // Before even allocating a sentinel.
72   List.clearAndLeakNodesUnsafely();
73   EXPECT_EQ(0u, List.size());
74
75   // Empty list with sentinel.
76   ilist<Node>::iterator E = List.end();
77   List.clearAndLeakNodesUnsafely();
78   EXPECT_EQ(0u, List.size());
79   // The sentinel shouldn't change.
80   EXPECT_TRUE(E == List.end());
81
82   // List with contents.
83   List.push_back(1);
84   ASSERT_EQ(1u, List.size());
85   Node *N = List.begin();
86   EXPECT_EQ(1, N->Value);
87   List.clearAndLeakNodesUnsafely();
88   EXPECT_EQ(0u, List.size());
89   ASSERT_EQ(1, N->Value);
90   delete N;
91
92   // List is still functional.
93   List.push_back(5);
94   List.push_back(6);
95   ASSERT_EQ(2u, List.size());
96   EXPECT_EQ(5, List.front().Value);
97   EXPECT_EQ(6, List.back().Value);
98 }
99
100 }