2009-01-03 22:55:26 +08:00
|
|
|
//===----------- ImmutableSetTest.cpp - ImmutableSet unit tests ------------===//
|
2009-01-02 21:49:50 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-01-02 21:49:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/ImmutableSet.h"
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "gtest/gtest.h"
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ImmutableSetTest : public testing::Test {
|
2009-01-03 22:55:26 +08:00
|
|
|
protected:
|
|
|
|
// for callback tests
|
|
|
|
static char buffer[10];
|
|
|
|
|
|
|
|
struct MyIter {
|
|
|
|
int counter;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
MyIter() : counter(0), ptr(buffer) {
|
|
|
|
for (unsigned i=0; i<sizeof(buffer);++i) buffer[i]='\0';
|
|
|
|
}
|
|
|
|
void operator()(char c) {
|
|
|
|
*ptr++ = c;
|
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
};
|
2009-01-02 21:49:50 +08:00
|
|
|
};
|
2009-01-03 22:55:26 +08:00
|
|
|
char ImmutableSetTest::buffer[10];
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
TEST_F(ImmutableSetTest, EmptyIntSetTest) {
|
|
|
|
ImmutableSet<int>::Factory f;
|
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
EXPECT_TRUE(f.getEmptySet() == f.getEmptySet());
|
|
|
|
EXPECT_FALSE(f.getEmptySet() != f.getEmptySet());
|
|
|
|
EXPECT_TRUE(f.getEmptySet().isEmpty());
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S = f.getEmptySet();
|
2009-01-02 21:49:50 +08:00
|
|
|
EXPECT_EQ(0u, S.getHeight());
|
|
|
|
EXPECT_TRUE(S.begin() == S.end());
|
|
|
|
EXPECT_FALSE(S.begin() != S.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(ImmutableSetTest, OneElemIntSetTest) {
|
|
|
|
ImmutableSet<int>::Factory f;
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S = f.getEmptySet();
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S2 = f.add(S, 3);
|
2009-01-02 21:49:50 +08:00
|
|
|
EXPECT_TRUE(S.isEmpty());
|
|
|
|
EXPECT_FALSE(S2.isEmpty());
|
|
|
|
EXPECT_FALSE(S == S2);
|
|
|
|
EXPECT_TRUE(S != S2);
|
|
|
|
EXPECT_FALSE(S.contains(3));
|
|
|
|
EXPECT_TRUE(S2.contains(3));
|
|
|
|
EXPECT_FALSE(S2.begin() == S2.end());
|
|
|
|
EXPECT_TRUE(S2.begin() != S2.end());
|
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S3 = f.add(S, 2);
|
2009-01-02 21:49:50 +08:00
|
|
|
EXPECT_TRUE(S.isEmpty());
|
|
|
|
EXPECT_FALSE(S3.isEmpty());
|
|
|
|
EXPECT_FALSE(S == S3);
|
|
|
|
EXPECT_TRUE(S != S3);
|
|
|
|
EXPECT_FALSE(S.contains(2));
|
|
|
|
EXPECT_TRUE(S3.contains(2));
|
|
|
|
|
|
|
|
EXPECT_FALSE(S2 == S3);
|
|
|
|
EXPECT_TRUE(S2 != S3);
|
|
|
|
EXPECT_FALSE(S2.contains(2));
|
|
|
|
EXPECT_FALSE(S3.contains(3));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImmutableSetTest, MultiElemIntSetTest) {
|
|
|
|
ImmutableSet<int>::Factory f;
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S = f.getEmptySet();
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S2 = f.add(f.add(f.add(S, 3), 4), 5);
|
|
|
|
ImmutableSet<int> S3 = f.add(f.add(f.add(S2, 9), 20), 43);
|
|
|
|
ImmutableSet<int> S4 = f.add(S2, 9);
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
EXPECT_TRUE(S.isEmpty());
|
|
|
|
EXPECT_FALSE(S2.isEmpty());
|
|
|
|
EXPECT_FALSE(S3.isEmpty());
|
|
|
|
EXPECT_FALSE(S4.isEmpty());
|
|
|
|
|
|
|
|
EXPECT_FALSE(S.contains(3));
|
|
|
|
EXPECT_FALSE(S.contains(9));
|
|
|
|
|
|
|
|
EXPECT_TRUE(S2.contains(3));
|
|
|
|
EXPECT_TRUE(S2.contains(4));
|
|
|
|
EXPECT_TRUE(S2.contains(5));
|
|
|
|
EXPECT_FALSE(S2.contains(9));
|
|
|
|
EXPECT_FALSE(S2.contains(0));
|
|
|
|
|
|
|
|
EXPECT_TRUE(S3.contains(43));
|
|
|
|
EXPECT_TRUE(S3.contains(20));
|
|
|
|
EXPECT_TRUE(S3.contains(9));
|
|
|
|
EXPECT_TRUE(S3.contains(3));
|
|
|
|
EXPECT_TRUE(S3.contains(4));
|
|
|
|
EXPECT_TRUE(S3.contains(5));
|
|
|
|
EXPECT_FALSE(S3.contains(0));
|
|
|
|
|
|
|
|
EXPECT_TRUE(S4.contains(9));
|
|
|
|
EXPECT_TRUE(S4.contains(3));
|
|
|
|
EXPECT_TRUE(S4.contains(4));
|
|
|
|
EXPECT_TRUE(S4.contains(5));
|
|
|
|
EXPECT_FALSE(S4.contains(20));
|
|
|
|
EXPECT_FALSE(S4.contains(43));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImmutableSetTest, RemoveIntSetTest) {
|
|
|
|
ImmutableSet<int>::Factory f;
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S = f.getEmptySet();
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<int> S2 = f.add(f.add(S, 4), 5);
|
|
|
|
ImmutableSet<int> S3 = f.add(S2, 3);
|
|
|
|
ImmutableSet<int> S4 = f.remove(S3, 3);
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
EXPECT_TRUE(S3.contains(3));
|
|
|
|
EXPECT_FALSE(S2.contains(3));
|
|
|
|
EXPECT_FALSE(S4.contains(3));
|
|
|
|
|
|
|
|
EXPECT_TRUE(S2 == S4);
|
|
|
|
EXPECT_TRUE(S3 != S2);
|
|
|
|
EXPECT_TRUE(S3 != S4);
|
|
|
|
|
|
|
|
EXPECT_TRUE(S3.contains(4));
|
|
|
|
EXPECT_TRUE(S3.contains(5));
|
|
|
|
|
|
|
|
EXPECT_TRUE(S4.contains(4));
|
|
|
|
EXPECT_TRUE(S4.contains(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImmutableSetTest, CallbackCharSetTest) {
|
|
|
|
ImmutableSet<char>::Factory f;
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<char> S = f.getEmptySet();
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<char> S2 = f.add(f.add(f.add(S, 'a'), 'e'), 'i');
|
|
|
|
ImmutableSet<char> S3 = f.add(f.add(S2, 'o'), 'u');
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
S3.foreach<MyIter>();
|
|
|
|
|
2009-01-03 22:55:26 +08:00
|
|
|
ASSERT_STREQ("aeiou", buffer);
|
2009-01-02 21:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImmutableSetTest, Callback2CharSetTest) {
|
|
|
|
ImmutableSet<char>::Factory f;
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<char> S = f.getEmptySet();
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<char> S2 = f.add(f.add(f.add(S, 'b'), 'c'), 'd');
|
|
|
|
ImmutableSet<char> S3 = f.add(f.add(f.add(S2, 'f'), 'g'), 'h');
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
MyIter obj;
|
|
|
|
S3.foreach<MyIter>(obj);
|
2009-01-03 22:55:26 +08:00
|
|
|
ASSERT_STREQ("bcdfgh", buffer);
|
|
|
|
ASSERT_EQ(6, obj.counter);
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2009-01-03 22:55:26 +08:00
|
|
|
MyIter obj2;
|
|
|
|
S2.foreach<MyIter>(obj2);
|
|
|
|
ASSERT_STREQ("bcd", buffer);
|
|
|
|
ASSERT_EQ(3, obj2.counter);
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2009-01-03 22:55:26 +08:00
|
|
|
MyIter obj3;
|
2009-01-02 21:49:50 +08:00
|
|
|
S.foreach<MyIter>(obj);
|
2009-01-03 22:55:26 +08:00
|
|
|
ASSERT_STREQ("", buffer);
|
|
|
|
ASSERT_EQ(0, obj3.counter);
|
2009-01-02 21:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ImmutableSetTest, IterLongSetTest) {
|
|
|
|
ImmutableSet<long>::Factory f;
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<long> S = f.getEmptySet();
|
2009-01-02 21:49:50 +08:00
|
|
|
|
2010-11-24 08:54:28 +08:00
|
|
|
ImmutableSet<long> S2 = f.add(f.add(f.add(S, 0), 1), 2);
|
|
|
|
ImmutableSet<long> S3 = f.add(f.add(f.add(S2, 3), 4), 5);
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (ImmutableSet<long>::iterator I = S.begin(), E = S.end(); I != E; ++I) {
|
2016-07-18 02:10:30 +08:00
|
|
|
ASSERT_EQ(i, *I);
|
|
|
|
i++;
|
2009-01-02 21:49:50 +08:00
|
|
|
}
|
2009-01-03 22:55:26 +08:00
|
|
|
ASSERT_EQ(0, i);
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (ImmutableSet<long>::iterator I = S2.begin(), E = S2.end(); I != E; ++I) {
|
2016-07-18 02:10:30 +08:00
|
|
|
ASSERT_EQ(i, *I);
|
|
|
|
i++;
|
2009-01-02 21:49:50 +08:00
|
|
|
}
|
2009-01-03 22:55:26 +08:00
|
|
|
ASSERT_EQ(3, i);
|
2009-01-02 21:49:50 +08:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (ImmutableSet<long>::iterator I = S3.begin(), E = S3.end(); I != E; I++) {
|
2016-07-18 02:10:30 +08:00
|
|
|
ASSERT_EQ(i, *I);
|
|
|
|
i++;
|
2009-01-02 21:49:50 +08:00
|
|
|
}
|
2009-01-03 22:55:26 +08:00
|
|
|
ASSERT_EQ(6, i);
|
2009-01-02 21:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|