Improve compatibility with older versions of Windows.
[llvm/msp430.git] / unittests / ADT / ImmutableSetTest.cpp
blob1be510d599e39758681ac7626d9aff4be42cb87f
1 //===----------- ImmutableSetTest.cpp - ImmutableSet 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 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/ImmutableSet.h"
13 using namespace llvm;
15 namespace {
16 class ImmutableSetTest : public testing::Test {
17 protected:
18 // for callback tests
19 static char buffer[10];
21 struct MyIter {
22 int counter;
23 char *ptr;
25 MyIter() : counter(0), ptr(buffer) {
26 for (unsigned i=0; i<sizeof(buffer);++i) buffer[i]='\0';
28 void operator()(char c) {
29 *ptr++ = c;
30 ++counter;
34 char ImmutableSetTest::buffer[10];
37 TEST_F(ImmutableSetTest, EmptyIntSetTest) {
38 ImmutableSet<int>::Factory f;
40 EXPECT_TRUE(f.GetEmptySet() == f.GetEmptySet());
41 EXPECT_FALSE(f.GetEmptySet() != f.GetEmptySet());
42 EXPECT_TRUE(f.GetEmptySet().isEmpty());
44 ImmutableSet<int> S = f.GetEmptySet();
45 EXPECT_EQ(0u, S.getHeight());
46 EXPECT_TRUE(S.begin() == S.end());
47 EXPECT_FALSE(S.begin() != S.end());
51 TEST_F(ImmutableSetTest, OneElemIntSetTest) {
52 ImmutableSet<int>::Factory f;
53 ImmutableSet<int> S = f.GetEmptySet();
55 ImmutableSet<int> S2 = f.Add(S, 3);
56 EXPECT_TRUE(S.isEmpty());
57 EXPECT_FALSE(S2.isEmpty());
58 EXPECT_FALSE(S == S2);
59 EXPECT_TRUE(S != S2);
60 EXPECT_FALSE(S.contains(3));
61 EXPECT_TRUE(S2.contains(3));
62 EXPECT_FALSE(S2.begin() == S2.end());
63 EXPECT_TRUE(S2.begin() != S2.end());
65 ImmutableSet<int> S3 = f.Add(S, 2);
66 EXPECT_TRUE(S.isEmpty());
67 EXPECT_FALSE(S3.isEmpty());
68 EXPECT_FALSE(S == S3);
69 EXPECT_TRUE(S != S3);
70 EXPECT_FALSE(S.contains(2));
71 EXPECT_TRUE(S3.contains(2));
73 EXPECT_FALSE(S2 == S3);
74 EXPECT_TRUE(S2 != S3);
75 EXPECT_FALSE(S2.contains(2));
76 EXPECT_FALSE(S3.contains(3));
79 TEST_F(ImmutableSetTest, MultiElemIntSetTest) {
80 ImmutableSet<int>::Factory f;
81 ImmutableSet<int> S = f.GetEmptySet();
83 ImmutableSet<int> S2 = f.Add(f.Add(f.Add(S, 3), 4), 5);
84 ImmutableSet<int> S3 = f.Add(f.Add(f.Add(S2, 9), 20), 43);
85 ImmutableSet<int> S4 = f.Add(S2, 9);
87 EXPECT_TRUE(S.isEmpty());
88 EXPECT_FALSE(S2.isEmpty());
89 EXPECT_FALSE(S3.isEmpty());
90 EXPECT_FALSE(S4.isEmpty());
92 EXPECT_FALSE(S.contains(3));
93 EXPECT_FALSE(S.contains(9));
95 EXPECT_TRUE(S2.contains(3));
96 EXPECT_TRUE(S2.contains(4));
97 EXPECT_TRUE(S2.contains(5));
98 EXPECT_FALSE(S2.contains(9));
99 EXPECT_FALSE(S2.contains(0));
101 EXPECT_TRUE(S3.contains(43));
102 EXPECT_TRUE(S3.contains(20));
103 EXPECT_TRUE(S3.contains(9));
104 EXPECT_TRUE(S3.contains(3));
105 EXPECT_TRUE(S3.contains(4));
106 EXPECT_TRUE(S3.contains(5));
107 EXPECT_FALSE(S3.contains(0));
109 EXPECT_TRUE(S4.contains(9));
110 EXPECT_TRUE(S4.contains(3));
111 EXPECT_TRUE(S4.contains(4));
112 EXPECT_TRUE(S4.contains(5));
113 EXPECT_FALSE(S4.contains(20));
114 EXPECT_FALSE(S4.contains(43));
117 TEST_F(ImmutableSetTest, RemoveIntSetTest) {
118 ImmutableSet<int>::Factory f;
119 ImmutableSet<int> S = f.GetEmptySet();
121 ImmutableSet<int> S2 = f.Add(f.Add(S, 4), 5);
122 ImmutableSet<int> S3 = f.Add(S2, 3);
123 ImmutableSet<int> S4 = f.Remove(S3, 3);
125 EXPECT_TRUE(S3.contains(3));
126 EXPECT_FALSE(S2.contains(3));
127 EXPECT_FALSE(S4.contains(3));
129 EXPECT_TRUE(S2 == S4);
130 EXPECT_TRUE(S3 != S2);
131 EXPECT_TRUE(S3 != S4);
133 EXPECT_TRUE(S3.contains(4));
134 EXPECT_TRUE(S3.contains(5));
136 EXPECT_TRUE(S4.contains(4));
137 EXPECT_TRUE(S4.contains(5));
140 TEST_F(ImmutableSetTest, CallbackCharSetTest) {
141 ImmutableSet<char>::Factory f;
142 ImmutableSet<char> S = f.GetEmptySet();
144 ImmutableSet<char> S2 = f.Add(f.Add(f.Add(S, 'a'), 'e'), 'i');
145 ImmutableSet<char> S3 = f.Add(f.Add(S2, 'o'), 'u');
147 S3.foreach<MyIter>();
149 ASSERT_STREQ("aeiou", buffer);
152 TEST_F(ImmutableSetTest, Callback2CharSetTest) {
153 ImmutableSet<char>::Factory f;
154 ImmutableSet<char> S = f.GetEmptySet();
156 ImmutableSet<char> S2 = f.Add(f.Add(f.Add(S, 'b'), 'c'), 'd');
157 ImmutableSet<char> S3 = f.Add(f.Add(f.Add(S2, 'f'), 'g'), 'h');
159 MyIter obj;
160 S3.foreach<MyIter>(obj);
161 ASSERT_STREQ("bcdfgh", buffer);
162 ASSERT_EQ(6, obj.counter);
164 MyIter obj2;
165 S2.foreach<MyIter>(obj2);
166 ASSERT_STREQ("bcd", buffer);
167 ASSERT_EQ(3, obj2.counter);
169 MyIter obj3;
170 S.foreach<MyIter>(obj);
171 ASSERT_STREQ("", buffer);
172 ASSERT_EQ(0, obj3.counter);
175 TEST_F(ImmutableSetTest, IterLongSetTest) {
176 ImmutableSet<long>::Factory f;
177 ImmutableSet<long> S = f.GetEmptySet();
179 ImmutableSet<long> S2 = f.Add(f.Add(f.Add(S, 0), 1), 2);
180 ImmutableSet<long> S3 = f.Add(f.Add(f.Add(S2, 3), 4), 5);
182 int i = 0;
183 for (ImmutableSet<long>::iterator I = S.begin(), E = S.end(); I != E; ++I) {
184 ASSERT_EQ(i++, *I);
186 ASSERT_EQ(0, i);
188 i = 0;
189 for (ImmutableSet<long>::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
190 ASSERT_EQ(i++, *I);
192 ASSERT_EQ(3, i);
194 i = 0;
195 for (ImmutableSet<long>::iterator I = S3.begin(), E = S3.end(); I != E; I++) {
196 ASSERT_EQ(i++, *I);
198 ASSERT_EQ(6, i);