blob: 4e5235ba03beb1320cc410429a6ec80388bdb855 [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() {}
David Blaikie0bc56da2015-03-04 17:01:18 +000024 Node(int Value) : Value(Value) {}
25 Node(const Node&) = default;
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +000026 ~Node() { Value = -1; }
Daniel Dunbar959ae592010-05-12 21:35:19 +000027};
28
29TEST(ilistTest, Basic) {
30 ilist<Node> List;
31 List.push_back(Node(1));
32 EXPECT_EQ(1, List.back().Value);
Craig Topper66f09ad2014-06-08 22:29:17 +000033 EXPECT_EQ(nullptr, List.back().getPrevNode());
34 EXPECT_EQ(nullptr, List.back().getNextNode());
Daniel Dunbar959ae592010-05-12 21:35:19 +000035
36 List.push_back(Node(2));
37 EXPECT_EQ(2, List.back().Value);
38 EXPECT_EQ(2, List.front().getNextNode()->Value);
39 EXPECT_EQ(1, List.back().getPrevNode()->Value);
Daniel Dunbar2842f252010-05-13 18:35:02 +000040
41 const ilist<Node> &ConstList = List;
42 EXPECT_EQ(2, ConstList.back().Value);
43 EXPECT_EQ(2, ConstList.front().getNextNode()->Value);
44 EXPECT_EQ(1, ConstList.back().getPrevNode()->Value);
Daniel Dunbar959ae592010-05-12 21:35:19 +000045}
46
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000047TEST(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);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000055 EXPECT_TRUE(std::next(List.begin()) == List.end());
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000056
57 // Altenative noop. Move the first element behind itself.
58 List.push_back(2);
59 List.push_back(3);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000060 List.splice(std::next(List.begin()), List, List.begin());
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000061 EXPECT_EQ(3u, List.size());
62 EXPECT_EQ(1, List.front().Value);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +000063 EXPECT_EQ(2, std::next(List.begin())->Value);
Jakob Stoklund Olesenb8d29bf2012-12-18 19:28:37 +000064 EXPECT_EQ(3, List.back().Value);
65}
66
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +000067TEST(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());
Duncan P. N. Exon Smithc8925b12015-10-20 18:30:20 +000084 Node *N = &*List.begin();
Jakob Stoklund Olesen4ccabc12013-01-04 22:35:42 +000085 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
Daniel Dunbar959ae592010-05-12 21:35:19 +000099}