Aleph-w
3.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
43
using namespace
Aleph
;
44
45
46
TEST
(
CompilerScope
,
EnterLookupAndShadowAcrossNestedScopes
)
47
{
48
Scope<std::string, int>
scope
;
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
82
TEST
(
CompilerScope
,
ClearDropsAllFramesAndBindings
)
83
{
84
Scope<std::string, int>
scope
;
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
101
TEST
(
CompilerScope
,
ReenteringScopeAfterLeaveKeepsBindingsUsable
)
102
{
103
Scope<std::string, int>
scope
;
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
123
TEST
(
CompilerScope
,
LeaveOnEmptyScopeThrows
)
124
{
125
Scope<std::string, int>
scope
;
126
EXPECT_THROW
(
scope
.leave_scope(), std::runtime_error);
127
}
Aleph::Scope
Lexical scope stack for Key to Value associations.
Definition
tpl_scope.H:88
Aleph::Scope::enter_scope
size_t enter_scope()
Creates a new visibility level.
Definition
tpl_scope.H:179
TEST
#define TEST(name)
Definition
disjoint_sparse_table_test.cc:95
Aleph::blossom_maximum_cardinality_matching
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition
Blossom.H:466
Aleph
Main namespace for Aleph-w library functions.
Definition
ah-arena.H:89
tpl_scope.H
Lexical scope management using frame stacks.
Tests
compiler_scope_test.cc
Generated by
1.9.8