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(key: "A" ); |
22 | Set.insert(key: "B" ); |
23 | Set.insert(key: "C" ); |
24 | Set.insert(key: "D" ); |
25 | |
26 | auto Keys = to_vector<4>(Range: Set.keys()); |
27 | llvm::sort(C&: 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(key: "A" , allocator&: Set.getAllocator()); |
39 | Set.insert(mapEntry: *Element); |
40 | size_t Count = Set.count(MapEntry: *Element); |
41 | size_t Expected = 1; |
42 | EXPECT_EQ(Expected, Count); |
43 | Element->Destroy(allocator&: 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(Key: "" ); |
50 | EXPECT_EQ(Count, 0UL); |
51 | |
52 | Set.insert(key: "" ); |
53 | Count = Set.count(Key: "" ); |
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(key: "" ); |
63 | Set.insert(key: "test" ); |
64 | EXPECT_TRUE(Set.contains("" )); |
65 | EXPECT_TRUE(Set.contains("test" )); |
66 | |
67 | Set.insert(key: "test" ); |
68 | EXPECT_TRUE(Set.contains("" )); |
69 | EXPECT_TRUE(Set.contains("test" )); |
70 | |
71 | Set.erase(Key: "test" ); |
72 | EXPECT_TRUE(Set.contains("" )); |
73 | EXPECT_FALSE(Set.contains("test" )); |
74 | } |
75 | |
76 | } // end anonymous namespace |
77 | |