| 1 |
//===- unittests/Analysis/Scalable/EntityIdTableTest.cpp -----------------===// |
| 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 "clang/Analysis/Scalable/Model/EntityIdTable.h" |
| 10 |
#include "clang/Analysis/Scalable/Model/BuildNamespace.h" |
| 11 |
#include "clang/Analysis/Scalable/Model/EntityId.h" |
| 12 |
#include "clang/Analysis/Scalable/Model/EntityName.h" |
| 13 |
#include "gtest/gtest.h" |
| 14 |
|
| 15 |
namespace clang { |
| 16 |
namespace ssaf { |
| 17 |
namespace { |
| 18 |
|
| 19 |
TEST(EntityIdTableTest, CreateNewEntity) { |
| 20 |
EntityIdTable Table; |
| 21 |
|
| 22 |
EntityName Entity("c:@F@foo", "", {}); |
| 23 |
Table.getId(Entity); |
| 24 |
|
| 25 |
EXPECT_TRUE(Table.contains(Entity)); |
| 26 |
} |
| 27 |
|
| 28 |
TEST(EntityIdTableTest, Idempotency) { |
| 29 |
EntityIdTable Table; |
| 30 |
|
| 31 |
EntityName Entity("c:@F@foo", "", {}); |
| 32 |
EntityName EntityCopy = Entity; |
| 33 |
|
| 34 |
EntityId Id1 = Table.getId(Entity); |
| 35 |
EntityId Id2 = Table.getId(Entity); |
| 36 |
EntityId Id3 = Table.getId(EntityCopy); |
| 37 |
|
| 38 |
EXPECT_EQ(Id1, Id2); |
| 39 |
EXPECT_EQ(Id2, Id3); |
| 40 |
EXPECT_EQ(Id1, Id3); |
| 41 |
} |
| 42 |
|
| 43 |
TEST(EntityIdTableTest, ExistsTrue) { |
| 44 |
EntityIdTable Table; |
| 45 |
|
| 46 |
EntityName Entity1("c:@F@foo", "", {}); |
| 47 |
EntityName Entity2("c:@V@bar", "", {}); |
| 48 |
|
| 49 |
Table.getId(Entity1); |
| 50 |
Table.getId(Entity2); |
| 51 |
|
| 52 |
EXPECT_TRUE(Table.contains(Entity1)); |
| 53 |
EXPECT_TRUE(Table.contains(Entity2)); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(EntityIdTableTest, ExistsFalse) { |
| 57 |
EntityIdTable Table; |
| 58 |
|
| 59 |
EntityName Entity1("c:@F@foo", "", {}); |
| 60 |
EntityName Entity2("c:@F@bar", "", {}); |
| 61 |
|
| 62 |
Table.getId(Entity1); |
| 63 |
|
| 64 |
EXPECT_TRUE(Table.contains(Entity1)); |
| 65 |
EXPECT_FALSE(Table.contains(Entity2)); |
| 66 |
} |
| 67 |
|
| 68 |
TEST(EntityIdTableTest, MultipleEntities) { |
| 69 |
EntityIdTable Table; |
| 70 |
|
| 71 |
EntityName Entity1("c:@F@foo", "", {}); |
| 72 |
EntityName Entity2("c:@F@bar", "", {}); |
| 73 |
EntityName Entity3("c:@V@baz", "", {}); |
| 74 |
|
| 75 |
EntityId Id1 = Table.getId(Entity1); |
| 76 |
EntityId Id2 = Table.getId(Entity2); |
| 77 |
EntityId Id3 = Table.getId(Entity3); |
| 78 |
|
| 79 |
EXPECT_NE(Id1, Id2); |
| 80 |
EXPECT_NE(Id1, Id3); |
| 81 |
EXPECT_NE(Id2, Id3); |
| 82 |
} |
| 83 |
|
| 84 |
TEST(EntityIdTableTest, WithBuildNamespace) { |
| 85 |
EntityIdTable Table; |
| 86 |
|
| 87 |
NestedBuildNamespace NS = NestedBuildNamespace::makeCompilationUnit("test.o"); |
| 88 |
|
| 89 |
EntityName Entity1("c:@F@foo", "", NS); |
| 90 |
EntityName Entity2("c:@F@foo", "", |
| 91 |
NestedBuildNamespace::makeCompilationUnit("other.o")); |
| 92 |
|
| 93 |
EntityId Id1 = Table.getId(Entity1); |
| 94 |
EntityId Id2 = Table.getId(Entity2); |
| 95 |
|
| 96 |
EXPECT_NE(Id1, Id2); |
| 97 |
} |
| 98 |
|
| 99 |
TEST(EntityIdTableTest, ForEachEmptyTable) { |
| 100 |
EntityIdTable Table; |
| 101 |
|
| 102 |
int CallbackCount = 0; |
| 103 |
Table.forEach( |
| 104 |
[&CallbackCount](const EntityName &, EntityId) { CallbackCount++; }); |
| 105 |
|
| 106 |
EXPECT_EQ(CallbackCount, 0); |
| 107 |
} |
| 108 |
|
| 109 |
TEST(EntityIdTableTest, ForEachMultipleEntities) { |
| 110 |
EntityIdTable Table; |
| 111 |
|
| 112 |
EntityName Entity1("c:@F@foo", "", {}); |
| 113 |
EntityName Entity2("c:@F@bar", "", {}); |
| 114 |
EntityName Entity3("c:@V@baz", "", {}); |
| 115 |
|
| 116 |
EntityId Id1 = Table.getId(Entity1); |
| 117 |
EntityId Id2 = Table.getId(Entity2); |
| 118 |
EntityId Id3 = Table.getId(Entity3); |
| 119 |
|
| 120 |
std::set<EntityId> VisitedIds; |
| 121 |
std::set<EntityName> VisitedNames; |
| 122 |
|
| 123 |
Table.forEach([&](const EntityName &Name, EntityId Id) { |
| 124 |
VisitedIds.insert(Id); |
| 125 |
VisitedNames.insert(Name); |
| 126 |
}); |
| 127 |
|
| 128 |
EXPECT_EQ(VisitedIds.size(), 3u); |
| 129 |
EXPECT_EQ(VisitedNames.size(), 3u); |
| 130 |
|
| 131 |
EXPECT_TRUE(VisitedIds.count(Id1)); |
| 132 |
EXPECT_TRUE(VisitedIds.count(Id2)); |
| 133 |
EXPECT_TRUE(VisitedIds.count(Id3)); |
| 134 |
|
| 135 |
EXPECT_TRUE(VisitedNames.count(Entity1)); |
| 136 |
EXPECT_TRUE(VisitedNames.count(Entity2)); |
| 137 |
EXPECT_TRUE(VisitedNames.count(Entity3)); |
| 138 |
} |
| 139 |
|
| 140 |
} // namespace |
| 141 |
} // namespace ssaf |
| 142 |
} // namespace clang |