1 //===- unittests/ADT/IListNodeBaseTest.cpp - ilist_node_base 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_node_base.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
15 namespace {
16
17 typedef ilist_node_base<false> RawNode;
18 typedef ilist_node_base<true> TrackingNode;
19
TEST(IListNodeBaseTest,DefaultConstructor)20 TEST(IListNodeBaseTest, DefaultConstructor) {
21 RawNode A;
22 EXPECT_EQ(nullptr, A.getPrev());
23 EXPECT_EQ(nullptr, A.getNext());
24 EXPECT_FALSE(A.isKnownSentinel());
25
26 TrackingNode TA;
27 EXPECT_EQ(nullptr, TA.getPrev());
28 EXPECT_EQ(nullptr, TA.getNext());
29 EXPECT_FALSE(TA.isKnownSentinel());
30 EXPECT_FALSE(TA.isSentinel());
31 }
32
TEST(IListNodeBaseTest,setPrevAndNext)33 TEST(IListNodeBaseTest, setPrevAndNext) {
34 RawNode A, B, C;
35 A.setPrev(&B);
36 EXPECT_EQ(&B, A.getPrev());
37 EXPECT_EQ(nullptr, A.getNext());
38 EXPECT_EQ(nullptr, B.getPrev());
39 EXPECT_EQ(nullptr, B.getNext());
40 EXPECT_EQ(nullptr, C.getPrev());
41 EXPECT_EQ(nullptr, C.getNext());
42
43 A.setNext(&C);
44 EXPECT_EQ(&B, A.getPrev());
45 EXPECT_EQ(&C, A.getNext());
46 EXPECT_EQ(nullptr, B.getPrev());
47 EXPECT_EQ(nullptr, B.getNext());
48 EXPECT_EQ(nullptr, C.getPrev());
49 EXPECT_EQ(nullptr, C.getNext());
50
51 TrackingNode TA, TB, TC;
52 TA.setPrev(&TB);
53 EXPECT_EQ(&TB, TA.getPrev());
54 EXPECT_EQ(nullptr, TA.getNext());
55 EXPECT_EQ(nullptr, TB.getPrev());
56 EXPECT_EQ(nullptr, TB.getNext());
57 EXPECT_EQ(nullptr, TC.getPrev());
58 EXPECT_EQ(nullptr, TC.getNext());
59
60 TA.setNext(&TC);
61 EXPECT_EQ(&TB, TA.getPrev());
62 EXPECT_EQ(&TC, TA.getNext());
63 EXPECT_EQ(nullptr, TB.getPrev());
64 EXPECT_EQ(nullptr, TB.getNext());
65 EXPECT_EQ(nullptr, TC.getPrev());
66 EXPECT_EQ(nullptr, TC.getNext());
67 }
68
TEST(IListNodeBaseTest,isKnownSentinel)69 TEST(IListNodeBaseTest, isKnownSentinel) {
70 // Without sentinel tracking.
71 RawNode A, B;
72 EXPECT_FALSE(A.isKnownSentinel());
73 A.setPrev(&B);
74 A.setNext(&B);
75 EXPECT_EQ(&B, A.getPrev());
76 EXPECT_EQ(&B, A.getNext());
77 EXPECT_FALSE(A.isKnownSentinel());
78 A.initializeSentinel();
79 EXPECT_FALSE(A.isKnownSentinel());
80 EXPECT_EQ(&B, A.getPrev());
81 EXPECT_EQ(&B, A.getNext());
82
83 // With sentinel tracking.
84 TrackingNode TA, TB;
85 EXPECT_FALSE(TA.isKnownSentinel());
86 EXPECT_FALSE(TA.isSentinel());
87 TA.setPrev(&TB);
88 TA.setNext(&TB);
89 EXPECT_EQ(&TB, TA.getPrev());
90 EXPECT_EQ(&TB, TA.getNext());
91 EXPECT_FALSE(TA.isKnownSentinel());
92 EXPECT_FALSE(TA.isSentinel());
93 TA.initializeSentinel();
94 EXPECT_TRUE(TA.isKnownSentinel());
95 EXPECT_TRUE(TA.isSentinel());
96 EXPECT_EQ(&TB, TA.getPrev());
97 EXPECT_EQ(&TB, TA.getNext());
98 }
99
100 } // end namespace
101