104 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/ADT/StringSet.h"10#include "llvm/ADT/STLExtras.h"11#include "gtest/gtest.h"12using namespace llvm;13 14namespace {15 16// Test fixture17class StringSetTest : public testing::Test {};18 19TEST_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 33TEST_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 46TEST_F(StringSetTest, EmptyString) {47 // Verify that the empty string can by successfully inserted48 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 57TEST_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 76TEST_F(StringSetTest, Equal) {77 StringSet<> A = {"A"};78 StringSet<> B = {"B"};79 ASSERT_TRUE(A != B);80 ASSERT_FALSE(A == B);81 ASSERT_TRUE(A == A);82}83 84TEST_F(StringSetTest, CtorRange) {85 const char *Args[] = {"chair", "desk", "bed"};86 StringSet<> Set(llvm::from_range, Args);87 EXPECT_EQ(Set.size(), 3U);88 EXPECT_TRUE(Set.contains("bed"));89 EXPECT_TRUE(Set.contains("chair"));90 EXPECT_TRUE(Set.contains("desk"));91}92 93TEST_F(StringSetTest, InsertRange) {94 StringSet<> Set;95 const char *Args[] = {"chair", "desk", "bed"};96 Set.insert_range(Args);97 EXPECT_EQ(Set.size(), 3U);98 EXPECT_TRUE(Set.contains("bed"));99 EXPECT_TRUE(Set.contains("chair"));100 EXPECT_TRUE(Set.contains("desk"));101}102 103} // end anonymous namespace104