[Alignment] Migrate Attribute::getWith(Stack)Alignment
[llvm-core.git] / unittests / ADT / ImmutableSetTest.cpp
blob9fe7a80d99001f6b0d9e11727dee35be6be5f074
1 //===----------- ImmutableSetTest.cpp - ImmutableSet unit tests ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/ImmutableSet.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
15 class ImmutableSetTest : public testing::Test {
16 protected:
17 // for callback tests
18 static char buffer[10];
20 struct MyIter {
21 int counter;
22 char *ptr;
24 MyIter() : counter(0), ptr(buffer) {
25 for (unsigned i=0; i<sizeof(buffer);++i) buffer[i]='\0';
27 void operator()(char c) {
28 *ptr++ = c;
29 ++counter;
33 char ImmutableSetTest::buffer[10];
36 TEST_F(ImmutableSetTest, EmptyIntSetTest) {
37 ImmutableSet<int>::Factory f;
39 EXPECT_TRUE(f.getEmptySet() == f.getEmptySet());
40 EXPECT_FALSE(f.getEmptySet() != f.getEmptySet());
41 EXPECT_TRUE(f.getEmptySet().isEmpty());
43 ImmutableSet<int> S = f.getEmptySet();
44 EXPECT_EQ(0u, S.getHeight());
45 EXPECT_TRUE(S.begin() == S.end());
46 EXPECT_FALSE(S.begin() != S.end());
50 TEST_F(ImmutableSetTest, OneElemIntSetTest) {
51 ImmutableSet<int>::Factory f;
52 ImmutableSet<int> S = f.getEmptySet();
54 ImmutableSet<int> S2 = f.add(S, 3);
55 EXPECT_TRUE(S.isEmpty());
56 EXPECT_FALSE(S2.isEmpty());
57 EXPECT_FALSE(S == S2);
58 EXPECT_TRUE(S != S2);
59 EXPECT_FALSE(S.contains(3));
60 EXPECT_TRUE(S2.contains(3));
61 EXPECT_FALSE(S2.begin() == S2.end());
62 EXPECT_TRUE(S2.begin() != S2.end());
64 ImmutableSet<int> S3 = f.add(S, 2);
65 EXPECT_TRUE(S.isEmpty());
66 EXPECT_FALSE(S3.isEmpty());
67 EXPECT_FALSE(S == S3);
68 EXPECT_TRUE(S != S3);
69 EXPECT_FALSE(S.contains(2));
70 EXPECT_TRUE(S3.contains(2));
72 EXPECT_FALSE(S2 == S3);
73 EXPECT_TRUE(S2 != S3);
74 EXPECT_FALSE(S2.contains(2));
75 EXPECT_FALSE(S3.contains(3));
78 TEST_F(ImmutableSetTest, MultiElemIntSetTest) {
79 ImmutableSet<int>::Factory f;
80 ImmutableSet<int> S = f.getEmptySet();
82 ImmutableSet<int> S2 = f.add(f.add(f.add(S, 3), 4), 5);
83 ImmutableSet<int> S3 = f.add(f.add(f.add(S2, 9), 20), 43);
84 ImmutableSet<int> S4 = f.add(S2, 9);
86 EXPECT_TRUE(S.isEmpty());
87 EXPECT_FALSE(S2.isEmpty());
88 EXPECT_FALSE(S3.isEmpty());
89 EXPECT_FALSE(S4.isEmpty());
91 EXPECT_FALSE(S.contains(3));
92 EXPECT_FALSE(S.contains(9));
94 EXPECT_TRUE(S2.contains(3));
95 EXPECT_TRUE(S2.contains(4));
96 EXPECT_TRUE(S2.contains(5));
97 EXPECT_FALSE(S2.contains(9));
98 EXPECT_FALSE(S2.contains(0));
100 EXPECT_TRUE(S3.contains(43));
101 EXPECT_TRUE(S3.contains(20));
102 EXPECT_TRUE(S3.contains(9));
103 EXPECT_TRUE(S3.contains(3));
104 EXPECT_TRUE(S3.contains(4));
105 EXPECT_TRUE(S3.contains(5));
106 EXPECT_FALSE(S3.contains(0));
108 EXPECT_TRUE(S4.contains(9));
109 EXPECT_TRUE(S4.contains(3));
110 EXPECT_TRUE(S4.contains(4));
111 EXPECT_TRUE(S4.contains(5));
112 EXPECT_FALSE(S4.contains(20));
113 EXPECT_FALSE(S4.contains(43));
116 TEST_F(ImmutableSetTest, RemoveIntSetTest) {
117 ImmutableSet<int>::Factory f;
118 ImmutableSet<int> S = f.getEmptySet();
120 ImmutableSet<int> S2 = f.add(f.add(S, 4), 5);
121 ImmutableSet<int> S3 = f.add(S2, 3);
122 ImmutableSet<int> S4 = f.remove(S3, 3);
124 EXPECT_TRUE(S3.contains(3));
125 EXPECT_FALSE(S2.contains(3));
126 EXPECT_FALSE(S4.contains(3));
128 EXPECT_TRUE(S2 == S4);
129 EXPECT_TRUE(S3 != S2);
130 EXPECT_TRUE(S3 != S4);
132 EXPECT_TRUE(S3.contains(4));
133 EXPECT_TRUE(S3.contains(5));
135 EXPECT_TRUE(S4.contains(4));
136 EXPECT_TRUE(S4.contains(5));
139 TEST_F(ImmutableSetTest, CallbackCharSetTest) {
140 ImmutableSet<char>::Factory f;
141 ImmutableSet<char> S = f.getEmptySet();
143 ImmutableSet<char> S2 = f.add(f.add(f.add(S, 'a'), 'e'), 'i');
144 ImmutableSet<char> S3 = f.add(f.add(S2, 'o'), 'u');
146 S3.foreach<MyIter>();
148 ASSERT_STREQ("aeiou", buffer);
151 TEST_F(ImmutableSetTest, Callback2CharSetTest) {
152 ImmutableSet<char>::Factory f;
153 ImmutableSet<char> S = f.getEmptySet();
155 ImmutableSet<char> S2 = f.add(f.add(f.add(S, 'b'), 'c'), 'd');
156 ImmutableSet<char> S3 = f.add(f.add(f.add(S2, 'f'), 'g'), 'h');
158 MyIter obj;
159 S3.foreach<MyIter>(obj);
160 ASSERT_STREQ("bcdfgh", buffer);
161 ASSERT_EQ(6, obj.counter);
163 MyIter obj2;
164 S2.foreach<MyIter>(obj2);
165 ASSERT_STREQ("bcd", buffer);
166 ASSERT_EQ(3, obj2.counter);
168 MyIter obj3;
169 S.foreach<MyIter>(obj);
170 ASSERT_STREQ("", buffer);
171 ASSERT_EQ(0, obj3.counter);
174 TEST_F(ImmutableSetTest, IterLongSetTest) {
175 ImmutableSet<long>::Factory f;
176 ImmutableSet<long> S = f.getEmptySet();
178 ImmutableSet<long> S2 = f.add(f.add(f.add(S, 0), 1), 2);
179 ImmutableSet<long> S3 = f.add(f.add(f.add(S2, 3), 4), 5);
181 int i = 0;
182 for (ImmutableSet<long>::iterator I = S.begin(), E = S.end(); I != E; ++I) {
183 ASSERT_EQ(i, *I);
184 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);
191 i++;
193 ASSERT_EQ(3, i);
195 i = 0;
196 for (ImmutableSet<long>::iterator I = S3.begin(), E = S3.end(); I != E; I++) {
197 ASSERT_EQ(i, *I);
198 i++;
200 ASSERT_EQ(6, i);