aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/ScopedHashTableTest.cpp
blob: 8ce5c7cecf998e0df060c25b08e0ceb2bfd06030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//===- ScopedHashTableTest.cpp - ScopedHashTable unit tests ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/ScopedHashTable.h"
#include "llvm/ADT/StringRef.h"
#include "gtest/gtest.h"
#include <memory>
#include <stack>

using ::llvm::ScopedHashTable;
using ::llvm::ScopedHashTableScope;
using ::llvm::StringLiteral;
using ::llvm::StringRef;

using ::testing::Test;

class ScopedHashTableTest : public Test {
protected:
  ScopedHashTableTest() { symbolTable.insert(kGlobalName, kGlobalValue); }

  ScopedHashTable<StringRef, StringRef> symbolTable{};
  ScopedHashTableScope<StringRef, StringRef> globalScope{symbolTable};

  static constexpr StringLiteral kGlobalName = "global";
  static constexpr StringLiteral kGlobalValue = "gvalue";
  static constexpr StringLiteral kLocalName = "local";
  static constexpr StringLiteral kLocalValue = "lvalue";
  static constexpr StringLiteral kLocalValue2 = "lvalue2";
};

TEST_F(ScopedHashTableTest, AccessWithNoActiveScope) {
  EXPECT_EQ(symbolTable.count(kGlobalName), 1U);
}

TEST_F(ScopedHashTableTest, AccessWithAScope) {
  [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> varScope(
      symbolTable);
  EXPECT_EQ(symbolTable.count(kGlobalName), 1U);
}

TEST_F(ScopedHashTableTest, InsertInScope) {
  [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> varScope(
      symbolTable);
  symbolTable.insert(kLocalName, kLocalValue);
  EXPECT_EQ(symbolTable.count(kLocalName), 1U);
}

TEST_F(ScopedHashTableTest, InsertInLinearSortedScope) {
  [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> varScope(
      symbolTable);
  [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> varScope2(
      symbolTable);
  [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> varScope3(
      symbolTable);
  symbolTable.insert(kLocalName, kLocalValue);
  EXPECT_EQ(symbolTable.count(kLocalName), 1U);
}

TEST_F(ScopedHashTableTest, InsertInOutedScope) {
  {
    [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> varScope(
        symbolTable);
    symbolTable.insert(kLocalName, kLocalValue);
  }
  EXPECT_EQ(symbolTable.count(kLocalName), 0U);
}

TEST_F(ScopedHashTableTest, OverrideInScope) {
  [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> funScope(
      symbolTable);
  symbolTable.insert(kLocalName, kLocalValue);
  {
    [[maybe_unused]] ScopedHashTableScope<StringRef, StringRef> varScope(
        symbolTable);
    symbolTable.insert(kLocalName, kLocalValue2);
    EXPECT_EQ(symbolTable.lookup(kLocalName), kLocalValue2);
  }
  EXPECT_EQ(symbolTable.lookup(kLocalName), kLocalValue);
}

TEST_F(ScopedHashTableTest, GetCurScope) {
  EXPECT_EQ(symbolTable.getCurScope(), &globalScope);
  {
    ScopedHashTableScope<StringRef, StringRef> funScope(symbolTable);
    ScopedHashTableScope<StringRef, StringRef> funScope2(symbolTable);
    EXPECT_EQ(symbolTable.getCurScope(), &funScope2);
    {
      ScopedHashTableScope<StringRef, StringRef> blockScope(symbolTable);
      EXPECT_EQ(symbolTable.getCurScope(), &blockScope);
    }
    EXPECT_EQ(symbolTable.getCurScope(), &funScope2);
  }
  EXPECT_EQ(symbolTable.getCurScope(), &globalScope);
}

TEST_F(ScopedHashTableTest, PopScope) {
  using SymbolTableScopeTy = ScopedHashTable<StringRef, StringRef>::ScopeTy;

  std::stack<StringRef> ExpectedValues;
  std::stack<std::unique_ptr<SymbolTableScopeTy>> Scopes;

  Scopes.emplace(std::make_unique<SymbolTableScopeTy>(symbolTable));
  ExpectedValues.emplace(kLocalValue);
  symbolTable.insert(kGlobalName, kLocalValue);

  Scopes.emplace(std::make_unique<SymbolTableScopeTy>(symbolTable));
  ExpectedValues.emplace(kLocalValue2);
  symbolTable.insert(kGlobalName, kLocalValue2);

  while (symbolTable.getCurScope() != &globalScope) {
    EXPECT_EQ(symbolTable.getCurScope(), Scopes.top().get());
    EXPECT_EQ(symbolTable.lookup(kGlobalName), ExpectedValues.top());
    ExpectedValues.pop();
    Scopes.pop(); // destructs the SymbolTableScopeTy instance implicitly
                  // calling Scopes.top()->~SymbolTableScopeTy();
    EXPECT_NE(symbolTable.getCurScope(), nullptr);
  }
  ASSERT_TRUE(ExpectedValues.empty());
  ASSERT_TRUE(Scopes.empty());
  EXPECT_EQ(symbolTable.lookup(kGlobalName), kGlobalValue);
}

TEST_F(ScopedHashTableTest, DISABLED_PopScopeOnStack) {
  using SymbolTableScopeTy = ScopedHashTable<StringRef, StringRef>::ScopeTy;
  SymbolTableScopeTy funScope(symbolTable);
  symbolTable.insert(kGlobalName, kLocalValue);
  SymbolTableScopeTy funScope2(symbolTable);
  symbolTable.insert(kGlobalName, kLocalValue2);

  std::stack<StringRef> expectedValues{{kLocalValue, kLocalValue2}};
  std::stack<SymbolTableScopeTy *> expectedScopes{{&funScope, &funScope2}};

  while (symbolTable.getCurScope() != &globalScope) {
    EXPECT_EQ(symbolTable.getCurScope(), expectedScopes.top());
    expectedScopes.pop();
    EXPECT_EQ(symbolTable.lookup(kGlobalName), expectedValues.top());
    expectedValues.pop();
    symbolTable.getCurScope()->~SymbolTableScopeTy();
    EXPECT_NE(symbolTable.getCurScope(), nullptr);
  }

  // We have imbalanced scopes here:
  // Assertion `HT.CurScope == this && "Scope imbalance!"' failed
  // HT.CurScope is a pointer to the `globalScope` while
  // `SymbolTableScopeTy.this` is still a pointer to `funScope2`.
  // There is no way to write an assert on an assert in googletest so that we
  // mark the test case as DISABLED.
}