blob: 134607c2fd43bb3614cac158920c4b47390f2b33 [file] [log] [blame]
Daniel Dunbar959ae592010-05-12 21:35:19 +00001//===- 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
Daniel Dunbar959ae592010-05-12 21:35:19 +000010#include "llvm/ADT/ilist.h"
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthb034cb72013-01-02 10:26:28 +000012#include "llvm/ADT/ilist_node.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000013#include "gtest/gtest.h"
14#include <ostream>
Daniel Dunbar959ae592010-05-12 21:35:19 +000015
16using namespace llvm;
17
18namespace {
19
20struct Node : ilist_node<Node> {
21 int Value;
22
23 Node() {}
24 Node(int _Value) : Value(_Value) {}
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +000025 ~Node() { Value = -1; }
Daniel Dunbar959ae592010-05-12 21:35:19 +000026};
27
28TEST(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);
Daniel Dunbar2842f252010-05-13 18:35:02 +000039
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);
Daniel Dunbar959ae592010-05-12 21:35:19 +000044}
45
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000046TEST(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);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000054 EXPECT_TRUE(std::next(List.begin()) == List.end());
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000055
56 // Altenative noop. Move the first element behind itself.
57 List.push_back(2);
58 List.push_back(3);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000059 List.splice(std::next(List.begin()), List, List.begin());
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000060 EXPECT_EQ(3u, List.size());
61 EXPECT_EQ(1, List.front().Value);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000062 EXPECT_EQ(2, std::next(List.begin())->Value);
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000063 EXPECT_EQ(3, List.back().Value);
64}
65
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +000066TEST(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
Daniel Dunbar959ae592010-05-12 21:35:19 +000098}