Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_scope_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <gtest/gtest.h>
38
39# include <tpl_scope.H>
40
41# include <string>
42
43using namespace Aleph;
44
45
46TEST(CompilerScope, EnterLookupAndShadowAcrossNestedScopes)
47{
49
50 EXPECT_TRUE(scope.empty());
51 EXPECT_EQ(scope.enter_scope(), 1u);
52 EXPECT_FALSE(scope.empty());
53
54 EXPECT_TRUE(scope.insert("x", 1));
55 EXPECT_FALSE(scope.insert("x", 2));
56 ASSERT_NE(scope.lookup("x"), nullptr);
57 EXPECT_EQ(*scope.lookup("x"), 1);
58 ASSERT_NE(scope.lookup_local("x"), nullptr);
59 EXPECT_EQ(*scope.lookup_local("x"), 1);
60
61 EXPECT_EQ(scope.enter_scope(), 2u);
62 ASSERT_NE(scope.lookup("x"), nullptr);
63 EXPECT_EQ(*scope.lookup("x"), 1);
64 EXPECT_EQ(scope.lookup_local("x"), nullptr);
65
66 EXPECT_TRUE(scope.insert("x", 2));
67 ASSERT_NE(scope.lookup("x"), nullptr);
68 EXPECT_EQ(*scope.lookup("x"), 2);
69 ASSERT_NE(scope.lookup_local("x"), nullptr);
70 EXPECT_EQ(*scope.lookup_local("x"), 2);
71
72 scope.leave_scope();
73 ASSERT_NE(scope.lookup("x"), nullptr);
74 EXPECT_EQ(*scope.lookup("x"), 1);
75
76 scope.leave_scope();
77 EXPECT_TRUE(scope.empty());
78 EXPECT_EQ(scope.lookup("x"), nullptr);
79}
80
81
82TEST(CompilerScope, ClearDropsAllFramesAndBindings)
83{
85 scope.enter_scope();
86 scope.insert("a", 10);
87 scope.enter_scope();
88 scope.insert("b", 20);
89
90 ASSERT_NE(scope.lookup("a"), nullptr);
91 ASSERT_NE(scope.lookup("b"), nullptr);
92
93 scope.clear();
94
95 EXPECT_TRUE(scope.empty());
96 EXPECT_EQ(scope.lookup("a"), nullptr);
97 EXPECT_EQ(scope.lookup("b"), nullptr);
98}
99
100
101TEST(CompilerScope, ReenteringScopeAfterLeaveKeepsBindingsUsable)
102{
104 scope.enter_scope();
105 ASSERT_TRUE(scope.insert("root", 1));
106
107 scope.enter_scope();
108 ASSERT_TRUE(scope.insert("first", 2));
109 scope.leave_scope();
110
111 ASSERT_NE(scope.lookup("root"), nullptr);
112 EXPECT_EQ(*scope.lookup("root"), 1);
113
114 scope.enter_scope();
115 EXPECT_TRUE(scope.insert("second", 3));
116 ASSERT_NE(scope.lookup("second"), nullptr);
117 EXPECT_EQ(*scope.lookup("second"), 3);
118 ASSERT_NE(scope.lookup("root"), nullptr);
119 EXPECT_EQ(*scope.lookup("root"), 1);
120}
121
122
123TEST(CompilerScope, LeaveOnEmptyScopeThrows)
124{
126 EXPECT_THROW(scope.leave_scope(), std::runtime_error);
127}
Lexical scope stack for Key to Value associations.
Definition tpl_scope.H:88
void clear() noexcept
Resets the structure by removing all frames and bindings.
Definition tpl_scope.H:167
size_t enter_scope()
Creates a new visibility level.
Definition tpl_scope.H:179
Value * lookup(const Key &key) noexcept
Searches for a key traversing all active scopes (from inside out).
Definition tpl_scope.H:271
bool empty() const noexcept
Indicates if the structure is empty of frames.
Definition tpl_scope.H:158
bool insert(const Key &key, const Value &value)
Registers a new association in the current scope.
Definition tpl_scope.H:215
Value * lookup_local(const Key &key) noexcept
Searches for a key only in the innermost (current) scope.
Definition tpl_scope.H:246
void leave_scope()
Destroys the current visibility level and all its associations.
Definition tpl_scope.H:195
#define TEST(name)
Main namespace for Aleph-w library functions.
Lexical scope management using frame stacks.