1 //===- llvm/unittest/ADT/SmallSetTest.cpp ------------------------------===//
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 // SmallSet unit tests.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/SmallSet.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 
19 TEST(SmallSetTest, Insert) {
20 
21   SmallSet<int, 4> s1;
22 
23   for (int i = 0; i < 4; i++)
24     s1.insert(i);
25 
26   for (int i = 0; i < 4; i++)
27     s1.insert(i);
28 
29   EXPECT_EQ(4u, s1.size());
30 
31   for (int i = 0; i < 4; i++)
32     EXPECT_EQ(1u, s1.count(i));
33 
34   EXPECT_EQ(0u, s1.count(4));
35 }
36 
37 TEST(SmallSetTest, Grow) {
38   SmallSet<int, 4> s1;
39 
40   for (int i = 0; i < 8; i++)
41     s1.insert(i);
42 
43   EXPECT_EQ(8u, s1.size());
44 
45   for (int i = 0; i < 8; i++)
46     EXPECT_EQ(1u, s1.count(i));
47 
48   EXPECT_EQ(0u, s1.count(8));
49 }
50 
51 TEST(SmallSetTest, Erase) {
52   SmallSet<int, 4> s1;
53 
54   for (int i = 0; i < 8; i++)
55     s1.insert(i);
56 
57   EXPECT_EQ(8u, s1.size());
58 
59   // Remove elements one by one and check if all other elements are still there.
60   for (int i = 0; i < 8; i++) {
61     EXPECT_EQ(1u, s1.count(i));
62     EXPECT_TRUE(s1.erase(i));
63     EXPECT_EQ(0u, s1.count(i));
64     EXPECT_EQ(8u - i - 1, s1.size());
65     for (int j = i + 1; j < 8; j++)
66       EXPECT_EQ(1u, s1.count(j));
67   }
68 
69   EXPECT_EQ(0u, s1.count(8));
70 }
71