xref: /llvm-project/llvm/unittests/ADT/StringSetTest.cpp (revision 645bb8e2086c9d72a5c29d0b224c563f70555365)
1 //===- llvm/unittest/ADT/StringSetTest.cpp - StringSet 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 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/StringSet.h"
10 #include "gtest/gtest.h"
11 using namespace llvm;
12 
13 namespace {
14 
15 // Test fixture
16 class StringSetTest : public testing::Test {};
17 
18 TEST_F(StringSetTest, IterSetKeys) {
19   StringSet<> Set;
20   Set.insert("A");
21   Set.insert("B");
22   Set.insert("C");
23   Set.insert("D");
24 
25   auto Keys = to_vector<4>(Set.keys());
26   llvm::sort(Keys);
27 
28   SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
29   EXPECT_EQ(Expected, Keys);
30 }
31 
32 TEST_F(StringSetTest, InsertAndCountStringMapEntry) {
33   // Test insert(StringMapEntry) and count(StringMapEntry)
34   // which are required for set_difference(StringSet, StringSet).
35   StringSet<> Set;
36   StringMapEntry<StringRef> *Element =
37       StringMapEntry<StringRef>::Create("A", Set.getAllocator());
38   Set.insert(*Element);
39   size_t Count = Set.count(*Element);
40   size_t Expected = 1;
41   EXPECT_EQ(Expected, Count);
42   Element->Destroy(Set.getAllocator());
43 }
44 
45 TEST_F(StringSetTest, EmptyString) {
46   // Verify that the empty string can by successfully inserted
47   StringSet<> Set;
48   size_t Count = Set.count("");
49   EXPECT_EQ(Count, 0UL);
50 
51   Set.insert("");
52   Count = Set.count("");
53   EXPECT_EQ(Count, 1UL);
54 }
55 
56 TEST_F(StringSetTest, Contains) {
57   StringSet<> Set;
58   EXPECT_FALSE(Set.contains(""));
59   EXPECT_FALSE(Set.contains("test"));
60 
61   Set.insert("");
62   Set.insert("test");
63   EXPECT_TRUE(Set.contains(""));
64   EXPECT_TRUE(Set.contains("test"));
65 
66   Set.insert("test");
67   EXPECT_TRUE(Set.contains(""));
68   EXPECT_TRUE(Set.contains("test"));
69 
70   Set.erase("test");
71   EXPECT_TRUE(Set.contains(""));
72   EXPECT_FALSE(Set.contains("test"));
73 }
74 
75 } // end anonymous namespace
76