xref: /llvm-project/llvm/unittests/ADT/StringSetTest.cpp (revision de77d2312751514ce9fbf9b0bf11cbe93cd486f9)
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 = StringMapEntry<StringRef>::Create("A");
37   Set.insert(*Element);
38   size_t Count = Set.count(*Element);
39   size_t Expected = 1;
40   EXPECT_EQ(Expected, Count);
41   Element->Destroy();
42 }
43 
44 TEST_F(StringSetTest, EmptyString) {
45   // Verify that the empty string can by successfully inserted
46   StringSet<> Set;
47   size_t Count = Set.count("");
48   EXPECT_EQ(Count, 0UL);
49 
50   Set.insert("");
51   Count = Set.count("");
52   EXPECT_EQ(Count, 1UL);
53 }
54 
55 } // end anonymous namespace
56