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