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