1 //===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/ilist.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/ilist_node.h"
13 #include "gtest/gtest.h"
20 struct Node : ilist_node<Node> {
24 Node(int _Value) : Value(_Value) {}
25 ~Node() { Value = -1; }
28 TEST(ilistTest, Basic) {
30 List.push_back(Node(1));
31 EXPECT_EQ(1, List.back().Value);
32 EXPECT_EQ(nullptr, List.back().getPrevNode());
33 EXPECT_EQ(nullptr, List.back().getNextNode());
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);
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);
46 TEST(ilistTest, SpliceOne) {
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(std::next(List.begin()) == List.end());
56 // Altenative noop. Move the first element behind itself.
59 List.splice(std::next(List.begin()), List, List.begin());
60 EXPECT_EQ(3u, List.size());
61 EXPECT_EQ(1, List.front().Value);
62 EXPECT_EQ(2, std::next(List.begin())->Value);
63 EXPECT_EQ(3, List.back().Value);
66 TEST(ilistTest, UnsafeClear) {
69 // Before even allocating a sentinel.
70 List.clearAndLeakNodesUnsafely();
71 EXPECT_EQ(0u, List.size());
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());
80 // List with contents.
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);
90 // List is still functional.
93 ASSERT_EQ(2u, List.size());
94 EXPECT_EQ(5, List.front().Value);
95 EXPECT_EQ(6, List.back().Value);