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(const Node&) = default;
26 Node(Node &&RHS) : Value(RHS.Value) { RHS.Value = -1; }
27 ~Node() { Value = -1; }
30 TEST(ilistTest, Basic) {
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());
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);
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);
48 TEST(ilistTest, SpliceOne) {
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());
58 // Altenative noop. Move the first element behind itself.
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);
68 TEST(ilistTest, UnsafeClear) {
71 // Before even allocating a sentinel.
72 List.clearAndLeakNodesUnsafely();
73 EXPECT_EQ(0u, List.size());
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());
82 // List with contents.
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);
92 // List is still functional.
95 ASSERT_EQ(2u, List.size());
96 EXPECT_EQ(5, List.front().Value);
97 EXPECT_EQ(6, List.back().Value);