Skip to content

Commit 1cc926b

Browse files
authored
[ADT] Add a unittest for the ScopedHashTable class (#120183)
The ScopedHashTable class is particularly used to develop string tables for parsers and code convertors. For instance, the MLIRGen class from the toy example for MLIR actively uses this class to define scopes for declared variables. To demonstrate common use cases for the ScopedHashTable class as well as to check its behavior in different situations, the unittest has been added. Signed-off-by: Pavel Samolysov <[email protected]>
1 parent f0dcf32 commit 1cc926b

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

llvm/unittests/ADT/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ add_llvm_unittest(ADTTests
6767
SCCIteratorTest.cpp
6868
STLExtrasTest.cpp
6969
STLForwardCompatTest.cpp
70+
ScopedHashTableTest.cpp
7071
ScopeExitTest.cpp
7172
SequenceTest.cpp
7273
SetOperationsTest.cpp
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//===- ScopedHashTableTest.cpp - ScopedHashTable 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/ScopedHashTable.h"
10+
#include "llvm/ADT/StringRef.h"
11+
#include "gtest/gtest.h"
12+
#include <memory>
13+
#include <stack>
14+
15+
using ::llvm::ScopedHashTable;
16+
using ::llvm::ScopedHashTableScope;
17+
using ::llvm::StringLiteral;
18+
using ::llvm::StringRef;
19+
20+
using ::testing::Test;
21+
22+
class ScopedHashTableTest : public Test {
23+
protected:
24+
ScopedHashTableTest() { symbolTable.insert(kGlobalName, kGlobalValue); }
25+
26+
ScopedHashTable<StringRef, StringRef> symbolTable{};
27+
ScopedHashTableScope<StringRef, StringRef> globalScope{symbolTable};
28+
29+
static constexpr StringLiteral kGlobalName = "global";
30+
static constexpr StringLiteral kGlobalValue = "gvalue";
31+
static constexpr StringLiteral kLocalName = "local";
32+
static constexpr StringLiteral kLocalValue = "lvalue";
33+
static constexpr StringLiteral kLocalValue2 = "lvalue2";
34+
};
35+
36+
TEST_F(ScopedHashTableTest, AccessWithNoActiveScope) {
37+
EXPECT_EQ(symbolTable.count(kGlobalName), 1U);
38+
}
39+
40+
TEST_F(ScopedHashTableTest, AccessWithAScope) {
41+
[[maybe_unused]] ScopedHashTableScope varScope(symbolTable);
42+
EXPECT_EQ(symbolTable.count(kGlobalName), 1U);
43+
}
44+
45+
TEST_F(ScopedHashTableTest, InsertInScope) {
46+
[[maybe_unused]] ScopedHashTableScope varScope(symbolTable);
47+
symbolTable.insert(kLocalName, kLocalValue);
48+
EXPECT_EQ(symbolTable.count(kLocalName), 1U);
49+
}
50+
51+
TEST_F(ScopedHashTableTest, InsertInLinearSortedScope) {
52+
[[maybe_unused]] ScopedHashTableScope varScope(symbolTable);
53+
[[maybe_unused]] ScopedHashTableScope varScope2(symbolTable);
54+
[[maybe_unused]] ScopedHashTableScope varScope3(symbolTable);
55+
symbolTable.insert(kLocalName, kLocalValue);
56+
EXPECT_EQ(symbolTable.count(kLocalName), 1U);
57+
}
58+
59+
TEST_F(ScopedHashTableTest, InsertInOutedScope) {
60+
{
61+
[[maybe_unused]] ScopedHashTableScope varScope(symbolTable);
62+
symbolTable.insert(kLocalName, kLocalValue);
63+
}
64+
EXPECT_EQ(symbolTable.count(kLocalName), 0U);
65+
}
66+
67+
TEST_F(ScopedHashTableTest, OverrideInScope) {
68+
[[maybe_unused]] ScopedHashTableScope funScope(symbolTable);
69+
symbolTable.insert(kLocalName, kLocalValue);
70+
{
71+
[[maybe_unused]] ScopedHashTableScope varScope(symbolTable);
72+
symbolTable.insert(kLocalName, kLocalValue2);
73+
EXPECT_EQ(symbolTable.lookup(kLocalName), kLocalValue2);
74+
}
75+
EXPECT_EQ(symbolTable.lookup(kLocalName), kLocalValue);
76+
}
77+
78+
TEST_F(ScopedHashTableTest, GetCurScope) {
79+
EXPECT_EQ(symbolTable.getCurScope(), &globalScope);
80+
{
81+
ScopedHashTableScope funScope(symbolTable);
82+
ScopedHashTableScope funScope2(symbolTable);
83+
EXPECT_EQ(symbolTable.getCurScope(), &funScope2);
84+
{
85+
ScopedHashTableScope blockScope(symbolTable);
86+
EXPECT_EQ(symbolTable.getCurScope(), &blockScope);
87+
}
88+
EXPECT_EQ(symbolTable.getCurScope(), &funScope2);
89+
}
90+
EXPECT_EQ(symbolTable.getCurScope(), &globalScope);
91+
}
92+
93+
TEST_F(ScopedHashTableTest, PopScope) {
94+
using SymbolTableScopeTy = ScopedHashTable<StringRef, StringRef>::ScopeTy;
95+
96+
std::stack<StringRef> ExpectedValues;
97+
std::stack<std::unique_ptr<SymbolTableScopeTy>> Scopes;
98+
99+
Scopes.emplace(std::make_unique<SymbolTableScopeTy>(symbolTable));
100+
ExpectedValues.emplace(kLocalValue);
101+
symbolTable.insert(kGlobalName, kLocalValue);
102+
103+
Scopes.emplace(std::make_unique<SymbolTableScopeTy>(symbolTable));
104+
ExpectedValues.emplace(kLocalValue2);
105+
symbolTable.insert(kGlobalName, kLocalValue2);
106+
107+
while (symbolTable.getCurScope() != &globalScope) {
108+
EXPECT_EQ(symbolTable.getCurScope(), Scopes.top().get());
109+
EXPECT_EQ(symbolTable.lookup(kGlobalName), ExpectedValues.top());
110+
ExpectedValues.pop();
111+
Scopes.pop(); // destructs the SymbolTableScopeTy instance implicitly
112+
// calling Scopes.top()->~SymbolTableScopeTy();
113+
EXPECT_NE(symbolTable.getCurScope(), nullptr);
114+
}
115+
ASSERT_TRUE(ExpectedValues.empty());
116+
ASSERT_TRUE(Scopes.empty());
117+
EXPECT_EQ(symbolTable.lookup(kGlobalName), kGlobalValue);
118+
}
119+
120+
TEST_F(ScopedHashTableTest, DISABLED_PopScopeOnStack) {
121+
using SymbolTableScopeTy = ScopedHashTable<StringRef, StringRef>::ScopeTy;
122+
SymbolTableScopeTy funScope(symbolTable);
123+
symbolTable.insert(kGlobalName, kLocalValue);
124+
SymbolTableScopeTy funScope2(symbolTable);
125+
symbolTable.insert(kGlobalName, kLocalValue2);
126+
127+
std::stack<StringRef> expectedValues{{kLocalValue, kLocalValue2}};
128+
std::stack<SymbolTableScopeTy *> expectedScopes{{&funScope, &funScope2}};
129+
130+
while (symbolTable.getCurScope() != &globalScope) {
131+
EXPECT_EQ(symbolTable.getCurScope(), expectedScopes.top());
132+
expectedScopes.pop();
133+
EXPECT_EQ(symbolTable.lookup(kGlobalName), expectedValues.top());
134+
expectedValues.pop();
135+
symbolTable.getCurScope()->~SymbolTableScopeTy();
136+
EXPECT_NE(symbolTable.getCurScope(), nullptr);
137+
}
138+
139+
// We have imbalanced scopes here:
140+
// Assertion `HT.CurScope == this && "Scope imbalance!"' failed
141+
// HT.CurScope is a pointer to the `globalScope` while
142+
// `SymbolTableScopeTy.this` is still a pointer to `funScope2`.
143+
// There is no way to write an assert on an assert in googletest so that we
144+
// mark the test case as DISABLED.
145+
}

0 commit comments

Comments
 (0)