xref: /llvm-project/llvm/unittests/ADT/StringSetTest.cpp (revision b59461ac63aa1770a617f96bab31010442bd2090)
189c8ffd5SChris Lattner //===- llvm/unittest/ADT/StringSetTest.cpp - StringSet unit tests ---------===//
231650eaaSMichael Pozulp //
331650eaaSMichael Pozulp // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
431650eaaSMichael Pozulp // See https://llvm.org/LICENSE.txt for license information.
531650eaaSMichael Pozulp // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
631650eaaSMichael Pozulp //
731650eaaSMichael Pozulp //===----------------------------------------------------------------------===//
831650eaaSMichael Pozulp 
931650eaaSMichael Pozulp #include "llvm/ADT/StringSet.h"
105f290c09Sserge-sans-paille #include "llvm/ADT/STLExtras.h"
1131650eaaSMichael Pozulp #include "gtest/gtest.h"
1231650eaaSMichael Pozulp using namespace llvm;
1331650eaaSMichael Pozulp 
1431650eaaSMichael Pozulp namespace {
1531650eaaSMichael Pozulp 
1631650eaaSMichael Pozulp // Test fixture
1731650eaaSMichael Pozulp class StringSetTest : public testing::Test {};
1831650eaaSMichael Pozulp 
TEST_F(StringSetTest,IterSetKeys)1931650eaaSMichael Pozulp TEST_F(StringSetTest, IterSetKeys) {
2031650eaaSMichael Pozulp   StringSet<> Set;
2131650eaaSMichael Pozulp   Set.insert("A");
2231650eaaSMichael Pozulp   Set.insert("B");
2331650eaaSMichael Pozulp   Set.insert("C");
2431650eaaSMichael Pozulp   Set.insert("D");
2531650eaaSMichael Pozulp 
2631650eaaSMichael Pozulp   auto Keys = to_vector<4>(Set.keys());
2731650eaaSMichael Pozulp   llvm::sort(Keys);
2831650eaaSMichael Pozulp 
2931650eaaSMichael Pozulp   SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
3031650eaaSMichael Pozulp   EXPECT_EQ(Expected, Keys);
3131650eaaSMichael Pozulp }
3231650eaaSMichael Pozulp 
TEST_F(StringSetTest,InsertAndCountStringMapEntry)3331650eaaSMichael Pozulp TEST_F(StringSetTest, InsertAndCountStringMapEntry) {
3431650eaaSMichael Pozulp   // Test insert(StringMapEntry) and count(StringMapEntry)
3531650eaaSMichael Pozulp   // which are required for set_difference(StringSet, StringSet).
3631650eaaSMichael Pozulp   StringSet<> Set;
3789c8ffd5SChris Lattner   StringMapEntry<StringRef> *Element =
383284adf6SAlexey Lapshin       StringMapEntry<StringRef>::create("A", Set.getAllocator());
3931650eaaSMichael Pozulp   Set.insert(*Element);
4031650eaaSMichael Pozulp   size_t Count = Set.count(*Element);
4131650eaaSMichael Pozulp   size_t Expected = 1;
4231650eaaSMichael Pozulp   EXPECT_EQ(Expected, Count);
4389c8ffd5SChris Lattner   Element->Destroy(Set.getAllocator());
4431650eaaSMichael Pozulp }
4531650eaaSMichael Pozulp 
TEST_F(StringSetTest,EmptyString)46de77d231SSam Clegg TEST_F(StringSetTest, EmptyString) {
47de77d231SSam Clegg   // Verify that the empty string can by successfully inserted
48de77d231SSam Clegg   StringSet<> Set;
49de77d231SSam Clegg   size_t Count = Set.count("");
50de77d231SSam Clegg   EXPECT_EQ(Count, 0UL);
51de77d231SSam Clegg 
52de77d231SSam Clegg   Set.insert("");
53de77d231SSam Clegg   Count = Set.count("");
54de77d231SSam Clegg   EXPECT_EQ(Count, 1UL);
55de77d231SSam Clegg }
56de77d231SSam Clegg 
TEST_F(StringSetTest,Contains)57645bb8e2SDavid Blaikie TEST_F(StringSetTest, Contains) {
58645bb8e2SDavid Blaikie   StringSet<> Set;
59645bb8e2SDavid Blaikie   EXPECT_FALSE(Set.contains(""));
60645bb8e2SDavid Blaikie   EXPECT_FALSE(Set.contains("test"));
61645bb8e2SDavid Blaikie 
62645bb8e2SDavid Blaikie   Set.insert("");
63645bb8e2SDavid Blaikie   Set.insert("test");
64645bb8e2SDavid Blaikie   EXPECT_TRUE(Set.contains(""));
65645bb8e2SDavid Blaikie   EXPECT_TRUE(Set.contains("test"));
66645bb8e2SDavid Blaikie 
67645bb8e2SDavid Blaikie   Set.insert("test");
68645bb8e2SDavid Blaikie   EXPECT_TRUE(Set.contains(""));
69645bb8e2SDavid Blaikie   EXPECT_TRUE(Set.contains("test"));
70645bb8e2SDavid Blaikie 
71645bb8e2SDavid Blaikie   Set.erase("test");
72645bb8e2SDavid Blaikie   EXPECT_TRUE(Set.contains(""));
73645bb8e2SDavid Blaikie   EXPECT_FALSE(Set.contains("test"));
74645bb8e2SDavid Blaikie }
75645bb8e2SDavid Blaikie 
TEST_F(StringSetTest,Equal)76*b59461acSAleksandr Platonov TEST_F(StringSetTest, Equal) {
77*b59461acSAleksandr Platonov   StringSet<> A = {"A"};
78*b59461acSAleksandr Platonov   StringSet<> B = {"B"};
79*b59461acSAleksandr Platonov   ASSERT_TRUE(A != B);
80*b59461acSAleksandr Platonov   ASSERT_FALSE(A == B);
81*b59461acSAleksandr Platonov   ASSERT_TRUE(A == A);
82*b59461acSAleksandr Platonov }
83*b59461acSAleksandr Platonov 
8431650eaaSMichael Pozulp } // end anonymous namespace
85