12216c980SNuno Lopes //===----------- ImmutableSetTest.cpp - ImmutableSet unit tests ------------===//
2533eac82SNuno Lopes //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6533eac82SNuno Lopes //
7533eac82SNuno Lopes //===----------------------------------------------------------------------===//
8533eac82SNuno Lopes
9533eac82SNuno Lopes #include "llvm/ADT/ImmutableSet.h"
109a67b073SChandler Carruth #include "gtest/gtest.h"
11533eac82SNuno Lopes
12533eac82SNuno Lopes using namespace llvm;
13533eac82SNuno Lopes
14533eac82SNuno Lopes namespace {
15533eac82SNuno Lopes class ImmutableSetTest : public testing::Test {
162216c980SNuno Lopes protected:
172216c980SNuno Lopes // for callback tests
182216c980SNuno Lopes static char buffer[10];
192216c980SNuno Lopes
202216c980SNuno Lopes struct MyIter {
212216c980SNuno Lopes int counter;
222216c980SNuno Lopes char *ptr;
232216c980SNuno Lopes
MyIter__anon7356c8900111::ImmutableSetTest::MyIter242216c980SNuno Lopes MyIter() : counter(0), ptr(buffer) {
252216c980SNuno Lopes for (unsigned i=0; i<sizeof(buffer);++i) buffer[i]='\0';
262216c980SNuno Lopes }
operator ()__anon7356c8900111::ImmutableSetTest::MyIter272216c980SNuno Lopes void operator()(char c) {
282216c980SNuno Lopes *ptr++ = c;
292216c980SNuno Lopes ++counter;
302216c980SNuno Lopes }
31533eac82SNuno Lopes };
322216c980SNuno Lopes };
332216c980SNuno Lopes char ImmutableSetTest::buffer[10];
34533eac82SNuno Lopes
35533eac82SNuno Lopes
TEST_F(ImmutableSetTest,EmptyIntSetTest)36533eac82SNuno Lopes TEST_F(ImmutableSetTest, EmptyIntSetTest) {
37533eac82SNuno Lopes ImmutableSet<int>::Factory f;
38533eac82SNuno Lopes
391ef52416STed Kremenek EXPECT_TRUE(f.getEmptySet() == f.getEmptySet());
401ef52416STed Kremenek EXPECT_FALSE(f.getEmptySet() != f.getEmptySet());
411ef52416STed Kremenek EXPECT_TRUE(f.getEmptySet().isEmpty());
42533eac82SNuno Lopes
431ef52416STed Kremenek ImmutableSet<int> S = f.getEmptySet();
44533eac82SNuno Lopes EXPECT_EQ(0u, S.getHeight());
45533eac82SNuno Lopes EXPECT_TRUE(S.begin() == S.end());
46533eac82SNuno Lopes EXPECT_FALSE(S.begin() != S.end());
47533eac82SNuno Lopes }
48533eac82SNuno Lopes
49533eac82SNuno Lopes
TEST_F(ImmutableSetTest,OneElemIntSetTest)50533eac82SNuno Lopes TEST_F(ImmutableSetTest, OneElemIntSetTest) {
51533eac82SNuno Lopes ImmutableSet<int>::Factory f;
521ef52416STed Kremenek ImmutableSet<int> S = f.getEmptySet();
53533eac82SNuno Lopes
541ef52416STed Kremenek ImmutableSet<int> S2 = f.add(S, 3);
55533eac82SNuno Lopes EXPECT_TRUE(S.isEmpty());
56533eac82SNuno Lopes EXPECT_FALSE(S2.isEmpty());
57533eac82SNuno Lopes EXPECT_FALSE(S == S2);
58533eac82SNuno Lopes EXPECT_TRUE(S != S2);
59533eac82SNuno Lopes EXPECT_FALSE(S.contains(3));
60533eac82SNuno Lopes EXPECT_TRUE(S2.contains(3));
61533eac82SNuno Lopes EXPECT_FALSE(S2.begin() == S2.end());
62533eac82SNuno Lopes EXPECT_TRUE(S2.begin() != S2.end());
63533eac82SNuno Lopes
641ef52416STed Kremenek ImmutableSet<int> S3 = f.add(S, 2);
65533eac82SNuno Lopes EXPECT_TRUE(S.isEmpty());
66533eac82SNuno Lopes EXPECT_FALSE(S3.isEmpty());
67533eac82SNuno Lopes EXPECT_FALSE(S == S3);
68533eac82SNuno Lopes EXPECT_TRUE(S != S3);
69533eac82SNuno Lopes EXPECT_FALSE(S.contains(2));
70533eac82SNuno Lopes EXPECT_TRUE(S3.contains(2));
71533eac82SNuno Lopes
72533eac82SNuno Lopes EXPECT_FALSE(S2 == S3);
73533eac82SNuno Lopes EXPECT_TRUE(S2 != S3);
74533eac82SNuno Lopes EXPECT_FALSE(S2.contains(2));
75533eac82SNuno Lopes EXPECT_FALSE(S3.contains(3));
76533eac82SNuno Lopes }
77533eac82SNuno Lopes
TEST_F(ImmutableSetTest,MultiElemIntSetTest)78533eac82SNuno Lopes TEST_F(ImmutableSetTest, MultiElemIntSetTest) {
79533eac82SNuno Lopes ImmutableSet<int>::Factory f;
801ef52416STed Kremenek ImmutableSet<int> S = f.getEmptySet();
81533eac82SNuno Lopes
821ef52416STed Kremenek ImmutableSet<int> S2 = f.add(f.add(f.add(S, 3), 4), 5);
831ef52416STed Kremenek ImmutableSet<int> S3 = f.add(f.add(f.add(S2, 9), 20), 43);
841ef52416STed Kremenek ImmutableSet<int> S4 = f.add(S2, 9);
85533eac82SNuno Lopes
86533eac82SNuno Lopes EXPECT_TRUE(S.isEmpty());
87533eac82SNuno Lopes EXPECT_FALSE(S2.isEmpty());
88533eac82SNuno Lopes EXPECT_FALSE(S3.isEmpty());
89533eac82SNuno Lopes EXPECT_FALSE(S4.isEmpty());
90533eac82SNuno Lopes
91533eac82SNuno Lopes EXPECT_FALSE(S.contains(3));
92533eac82SNuno Lopes EXPECT_FALSE(S.contains(9));
93533eac82SNuno Lopes
94533eac82SNuno Lopes EXPECT_TRUE(S2.contains(3));
95533eac82SNuno Lopes EXPECT_TRUE(S2.contains(4));
96533eac82SNuno Lopes EXPECT_TRUE(S2.contains(5));
97533eac82SNuno Lopes EXPECT_FALSE(S2.contains(9));
98533eac82SNuno Lopes EXPECT_FALSE(S2.contains(0));
99533eac82SNuno Lopes
100533eac82SNuno Lopes EXPECT_TRUE(S3.contains(43));
101533eac82SNuno Lopes EXPECT_TRUE(S3.contains(20));
102533eac82SNuno Lopes EXPECT_TRUE(S3.contains(9));
103533eac82SNuno Lopes EXPECT_TRUE(S3.contains(3));
104533eac82SNuno Lopes EXPECT_TRUE(S3.contains(4));
105533eac82SNuno Lopes EXPECT_TRUE(S3.contains(5));
106533eac82SNuno Lopes EXPECT_FALSE(S3.contains(0));
107533eac82SNuno Lopes
108533eac82SNuno Lopes EXPECT_TRUE(S4.contains(9));
109533eac82SNuno Lopes EXPECT_TRUE(S4.contains(3));
110533eac82SNuno Lopes EXPECT_TRUE(S4.contains(4));
111533eac82SNuno Lopes EXPECT_TRUE(S4.contains(5));
112533eac82SNuno Lopes EXPECT_FALSE(S4.contains(20));
113533eac82SNuno Lopes EXPECT_FALSE(S4.contains(43));
114533eac82SNuno Lopes }
115533eac82SNuno Lopes
TEST_F(ImmutableSetTest,RemoveIntSetTest)116533eac82SNuno Lopes TEST_F(ImmutableSetTest, RemoveIntSetTest) {
117533eac82SNuno Lopes ImmutableSet<int>::Factory f;
1181ef52416STed Kremenek ImmutableSet<int> S = f.getEmptySet();
119533eac82SNuno Lopes
1201ef52416STed Kremenek ImmutableSet<int> S2 = f.add(f.add(S, 4), 5);
1211ef52416STed Kremenek ImmutableSet<int> S3 = f.add(S2, 3);
1221ef52416STed Kremenek ImmutableSet<int> S4 = f.remove(S3, 3);
123533eac82SNuno Lopes
124533eac82SNuno Lopes EXPECT_TRUE(S3.contains(3));
125533eac82SNuno Lopes EXPECT_FALSE(S2.contains(3));
126533eac82SNuno Lopes EXPECT_FALSE(S4.contains(3));
127533eac82SNuno Lopes
128533eac82SNuno Lopes EXPECT_TRUE(S2 == S4);
129533eac82SNuno Lopes EXPECT_TRUE(S3 != S2);
130533eac82SNuno Lopes EXPECT_TRUE(S3 != S4);
131533eac82SNuno Lopes
132533eac82SNuno Lopes EXPECT_TRUE(S3.contains(4));
133533eac82SNuno Lopes EXPECT_TRUE(S3.contains(5));
134533eac82SNuno Lopes
135533eac82SNuno Lopes EXPECT_TRUE(S4.contains(4));
136533eac82SNuno Lopes EXPECT_TRUE(S4.contains(5));
137533eac82SNuno Lopes }
138533eac82SNuno Lopes
TEST_F(ImmutableSetTest,IterLongSetTest)139533eac82SNuno Lopes TEST_F(ImmutableSetTest, IterLongSetTest) {
140533eac82SNuno Lopes ImmutableSet<long>::Factory f;
1411ef52416STed Kremenek ImmutableSet<long> S = f.getEmptySet();
142533eac82SNuno Lopes
1431ef52416STed Kremenek ImmutableSet<long> S2 = f.add(f.add(f.add(S, 0), 1), 2);
1441ef52416STed Kremenek ImmutableSet<long> S3 = f.add(f.add(f.add(S2, 3), 4), 5);
145533eac82SNuno Lopes
146533eac82SNuno Lopes int i = 0;
147533eac82SNuno Lopes for (ImmutableSet<long>::iterator I = S.begin(), E = S.end(); I != E; ++I) {
148b17269daSJustin Lebar i++;
149533eac82SNuno Lopes }
1502216c980SNuno Lopes ASSERT_EQ(0, i);
151533eac82SNuno Lopes
152533eac82SNuno Lopes i = 0;
153533eac82SNuno Lopes for (ImmutableSet<long>::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
154b17269daSJustin Lebar ASSERT_EQ(i, *I);
155b17269daSJustin Lebar i++;
156533eac82SNuno Lopes }
1572216c980SNuno Lopes ASSERT_EQ(3, i);
158533eac82SNuno Lopes
159533eac82SNuno Lopes i = 0;
160533eac82SNuno Lopes for (ImmutableSet<long>::iterator I = S3.begin(), E = S3.end(); I != E; I++) {
161b17269daSJustin Lebar ASSERT_EQ(i, *I);
162b17269daSJustin Lebar i++;
163533eac82SNuno Lopes }
1642216c980SNuno Lopes ASSERT_EQ(6, i);
165533eac82SNuno Lopes }
166533eac82SNuno Lopes
167533eac82SNuno Lopes }
168