forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concrete_syntax_tree_test.cc
395 lines (346 loc) · 12 KB
/
concrete_syntax_tree_test.cc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Sanity tests for tree node construction functions.
#include "common/text/concrete_syntax_tree.h"
#include <memory>
#include <utility>
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/symbol.h"
#include "common/text/tree_builder_test_util.h"
#include "common/text/tree_compare.h"
#include "common/util/casts.h"
#include "common/util/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace verible {
namespace {
using ::testing::IsEmpty;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::SizeIs;
static void SinkValue(SymbolPtr) {
// Do nothing, just consume and expire the pointer.
}
static SyntaxTreeNode *CheckTree(const SymbolPtr &ptr) {
CHECK(ptr->Kind() == SymbolKind::kNode);
return down_cast<SyntaxTreeNode *>(ptr.get());
}
// Test that MatchesTag matches correctly.
TEST(SyntaxTreeNodeMatchesTag, Matches) {
auto node = MakeTaggedNode(3);
EXPECT_TRUE(CheckTree(node)->MatchesTag(3));
}
// Test that MatchesTag does not match.
TEST(SyntaxTreeNodeMatchesTag, NotMatches) {
auto node = MakeTaggedNode(4);
EXPECT_FALSE(CheckTree(node)->MatchesTag(3));
}
// Test that MatchesTagAnyOf matches correctly.
TEST(SyntaxTreeNodeMatchesTagAnyOf, Matches) {
auto node = MakeTaggedNode(3);
EXPECT_TRUE(CheckTree(node)->MatchesTagAnyOf({2, 3}));
EXPECT_TRUE(CheckTree(node)->MatchesTagAnyOf({3, 4}));
EXPECT_FALSE(CheckTree(node)->MatchesTagAnyOf({2, 4}));
}
TEST(SyntaxTreeNodeAppend, AppendVoid) {
SyntaxTreeNode node;
node.Append();
EXPECT_THAT(node, IsEmpty());
}
// Test that std::move is automated.
TEST(SyntaxTreeNodeAppend, AppendChildReference) {
SyntaxTreeNode node;
SymbolPtr child;
node.Append(child);
EXPECT_THAT(node, SizeIs(1));
}
// Test that redundant move is accepted.
TEST(SyntaxTreeNodeAppend, AppendChildMoved) {
SyntaxTreeNode node;
SymbolPtr child;
node.Append(std::move(child));
EXPECT_THAT(node, SizeIs(1));
}
// Test that temporary value is properly forwarded.
TEST(SyntaxTreeNodeAppend, AppendChildTemporary) {
SyntaxTreeNode node;
node.Append(SymbolPtr());
EXPECT_THAT(node, SizeIs(1));
}
// Test that nullptrs can be appended.
TEST(SyntaxTreeNodeAppend, AppendChildNullPtr) {
SyntaxTreeNode node;
node.Append(nullptr);
EXPECT_THAT(node, SizeIs(1));
}
// Test that ownership is transferred to sink functions.
TEST(MakeNodeTest, EmptyConstructor) {
auto node = MakeNode();
EXPECT_THAT(node, NotNull());
SinkValue(std::move(node));
EXPECT_THAT(node, IsNull());
}
// Test that out-of-bounds is caught.
TEST(MakeNodeTest, ChildrenOutOfBounds) {
SyntaxTreeNode node;
EXPECT_DEATH(node[0], "");
}
// Test that out-of-bounds is caught (const).
TEST(MakeNodeTest, ChildrenOutOfBoundsConst) {
const SyntaxTreeNode node;
EXPECT_DEATH(node[0], "");
}
TEST(MakeNodeTest, SinkTemporary) { SinkValue(MakeNode()); }
// Test construction of tagged node.
TEST(MakeNodeTest, TaggedEmptyConstructor) {
const int tag = 10;
auto node = MakeTaggedNode(tag);
ASSERT_THAT(node, NotNull());
EXPECT_THAT(*CheckTree(node), IsEmpty());
}
// Test construction of tagged node.
TEST(MakeNodeTest, ImmediateTaggedEmptyConstructor) {
auto node = MakeTaggedNode(20);
ASSERT_THAT(node, NotNull());
EXPECT_THAT(*CheckTree(node), IsEmpty());
}
// Test construction of untagged node with one child.
TEST(MakeNodeTest, SingleChild) {
auto child = MakeNode();
EXPECT_THAT(child, NotNull());
auto parent = MakeNode(child);
EXPECT_THAT(parent, NotNull());
EXPECT_THAT(child, IsNull());
EXPECT_THAT(*CheckTree(parent), SizeIs(1));
}
// Test construction of untagged node with one (temporary) child.
TEST(MakeNodeTest, SingleChildTemporary) {
auto parent = MakeNode(MakeNode());
EXPECT_THAT(parent, NotNull());
EXPECT_THAT(*CheckTree(parent), SizeIs(1));
}
// Test construction of untagged node with multiple children.
TEST(MakeNodeTest, MultiChild) {
auto child1 = MakeNode();
auto child2 = MakeNode();
auto child3 = MakeNode();
EXPECT_THAT(child1, NotNull());
EXPECT_THAT(child2, NotNull());
EXPECT_THAT(child3, NotNull());
auto parent = MakeNode(child1, child2, child3);
ASSERT_THAT(parent, NotNull());
EXPECT_THAT(child1, IsNull());
EXPECT_THAT(child2, IsNull());
EXPECT_THAT(child3, IsNull());
EXPECT_THAT(*CheckTree(parent), SizeIs(3));
}
// Test ExtendNode with nothing to extend (base case).
TEST(ExtendNodeTest, ExtendNone) {
auto seq = MakeNode();
EXPECT_THAT(seq, NotNull());
auto seq2 = ExtendNode(seq);
EXPECT_THAT(seq2, NotNull());
EXPECT_THAT(seq, IsNull());
EXPECT_THAT(*CheckTree(seq2), IsEmpty());
}
// Test extending node with one child.
TEST(ExtendNodeTest, ExtendOne) {
auto seq = MakeNode();
auto item = MakeNode();
EXPECT_THAT(seq, NotNull());
EXPECT_THAT(item, NotNull());
auto seq2 = ExtendNode(seq, item);
EXPECT_THAT(seq2, NotNull());
EXPECT_THAT(seq, IsNull());
EXPECT_THAT(item, IsNull());
EXPECT_THAT(*CheckTree(seq2), SizeIs(1));
}
// Test extending node with multiple children.
TEST(ExtendNodeTest, ExtendMulti) {
auto seq = MakeNode();
auto item1 = MakeNode();
auto item2 = MakeNode();
auto item3 = MakeNode();
EXPECT_THAT(seq, NotNull());
EXPECT_THAT(item1, NotNull());
EXPECT_THAT(item2, NotNull());
EXPECT_THAT(item3, NotNull());
auto seq2 = ExtendNode(seq, item1, item2, item3);
EXPECT_THAT(seq2, NotNull());
EXPECT_THAT(seq, IsNull());
EXPECT_THAT(item1, IsNull());
EXPECT_THAT(item2, IsNull());
EXPECT_THAT(item3, IsNull());
EXPECT_THAT(*CheckTree(seq2), SizeIs(3));
}
// Test extending node with multiple (temporary) children.
TEST(ExtendNodeTest, ExtendMultiTemporary) {
auto seq = MakeNode();
EXPECT_THAT(seq, NotNull());
auto seq2 = ExtendNode(seq, MakeNode(), MakeNode());
ASSERT_THAT(seq2, NotNull());
EXPECT_THAT(seq, IsNull());
EXPECT_THAT(*CheckTree(seq2), SizeIs(2));
}
// Test extending node with temporary parent.
TEST(ExtendNodeTest, ExtendTemporaryNodeNoChildren) {
auto seq = ExtendNode(MakeNode());
ASSERT_THAT(seq, NotNull());
EXPECT_THAT(*CheckTree(seq), IsEmpty());
}
// Test extending node with temporary parent with temporary children.
TEST(ExtendNodeTest, ExtendTemporaryNode) {
auto seq = ExtendNode(MakeNode(), MakeNode(), MakeNode());
ASSERT_THAT(seq, NotNull());
EXPECT_THAT(*CheckTree(seq), SizeIs(2));
}
// Test forwarding empty set of children to new node.
TEST(SyntaxTreeNodeAppend, AdoptChildrenNone) {
auto seq = MakeNode();
auto parent = MakeNode(ForwardChildren(seq));
EXPECT_THAT(seq, IsNull());
EXPECT_THAT(*CheckTree(parent), IsEmpty());
}
// Test forwarding empty set of children to new node.
TEST(SyntaxTreeNodeAppend, AdoptLeaf) {
SymbolPtr leaf(new SyntaxTreeLeaf(0, "abc"));
auto parent = MakeNode(ForwardChildren(leaf));
EXPECT_THAT(leaf, IsNull());
EXPECT_THAT(*CheckTree(parent), SizeIs(1));
}
// Test forwarding set of children to new node, transferring ownership.
TEST(SyntaxTreeNodeAppend, AdoptChildren) {
auto seq = MakeNode(MakeNode(), MakeNode(), MakeNode());
auto parent = MakeNode(ForwardChildren(seq));
EXPECT_THAT(seq, IsNull());
auto parentnode = CheckTree(parent);
EXPECT_THAT(*parentnode, SizeIs(3));
for (const auto &child : parentnode->children()) {
EXPECT_THAT(child, NotNull());
}
}
// Test forwarding set of null children to new node.
TEST(SyntaxTreeNodeAppend, AdoptNullChildren) {
auto seq = MakeNode(nullptr, nullptr);
auto parent = MakeNode(ForwardChildren(seq));
EXPECT_THAT(seq, IsNull());
auto parentnode = CheckTree(parent);
EXPECT_THAT(*parentnode, SizeIs(2));
for (const auto &child : parentnode->children()) {
EXPECT_THAT(child, IsNull());
}
}
/*
// Test forwarding of children of null parent
TEST(SyntaxTreeNodeAppend, AdoptChildrenNullParent) {
auto seq = nullptr;
auto parent = MakeNode(ForwardChildren(seq));
auto parentnode = CheckTree(parent);
ASSERT_THAT(parentnode, NotNull());
EXPECT_THAT(parentnode->children(), IsEmpty());
}
// Test forwarding of children of null parent
TEST(SyntaxTreeNodeAppend, AdoptChildrenNullParentDirect) {
auto parent = MakeNode(ForwardChildren(nullptr));
auto parentnode = CheckTree(parent);
ASSERT_THAT(parentnode, NotNull());
EXPECT_THAT(parentnode->children(), IsEmpty());
}
*/
// Test mix of forwarded and non-forwarded children.
TEST(SyntaxTreeNodeAppend, AdoptChildrenMixed) {
auto seq = MakeNode(MakeNode(), MakeNode(), MakeNode());
auto parent = MakeNode(MakeNode(), ForwardChildren(seq), MakeNode());
EXPECT_THAT(seq, IsNull());
auto parentnode = CheckTree(parent);
ASSERT_THAT(parentnode, NotNull());
EXPECT_THAT(*parentnode, SizeIs(5));
for (const auto &child : parentnode->children()) {
EXPECT_THAT(child, NotNull());
}
}
// Test multiple of forwarded sets of children.
TEST(SyntaxTreeNodeAppend, AdoptChildrenMultiple) {
auto seq = MakeNode(MakeNode(), MakeNode(), MakeNode());
auto seq2 = MakeNode(MakeNode(), MakeNode(), MakeNode(), MakeNode());
auto parent = MakeNode(ForwardChildren(seq), ForwardChildren(seq2));
EXPECT_THAT(seq, IsNull());
EXPECT_THAT(seq2, IsNull());
auto parentnode = CheckTree(parent);
ASSERT_THAT(parentnode, NotNull());
EXPECT_THAT(*parentnode, SizeIs(7));
for (const auto &child : parentnode->children()) {
EXPECT_THAT(child, NotNull());
}
}
// Test extending one set with forwarded children.
TEST(ExtendNodeTest, AdoptChildren) {
auto seq = ExtendNode(MakeNode(), MakeNode(), MakeNode());
auto parent = MakeNode(ForwardChildren(seq));
EXPECT_THAT(seq, IsNull());
auto parentnode = CheckTree(parent);
EXPECT_THAT(*parentnode, SizeIs(2));
for (const auto &child : parentnode->children()) {
EXPECT_THAT(child, NotNull());
}
}
// Test extending temporary parent with forwarded children.
TEST(ExtendNodeTest, AdoptChildrenMixed) {
auto seq = MakeNode(MakeNode(), MakeNode(), MakeNode());
auto parent = ExtendNode(MakeNode(), ForwardChildren(seq), MakeNode());
EXPECT_THAT(seq, IsNull());
auto parentnode = CheckTree(parent);
EXPECT_THAT(*parentnode, SizeIs(4));
for (const auto &child : parentnode->children()) {
EXPECT_THAT(child, NotNull());
}
}
// Tests setting a placeholder to a single leaf
TEST(ExtendNodeTest, SetChild0Size1) {
auto expected = Node(Leaf(4, "z"));
auto node1 = Node(nullptr);
auto node2 = Leaf(4, "y");
SetChild(node1, 0, node2);
EXPECT_THAT(node1, NotNull());
EXPECT_THAT(node2, IsNull());
EXPECT_THAT(*CheckTree(node1), SizeIs(1));
EXPECT_TRUE(EqualTreesByEnum(expected.get(), node1.get()));
}
// Tests setting a placeholder to a more complex tree
TEST(ExtendNodeTest, SetChild1Size2) {
auto expected = Node(Leaf(1, "a"), Node(Leaf(4, "b")));
auto node1 = Node(Leaf(1, "a"), nullptr);
auto node2 = Node(Leaf(4, "b"));
SetChild(node1, 1, node2);
EXPECT_THAT(node1, NotNull());
EXPECT_THAT(node2, IsNull());
EXPECT_THAT(*CheckTree(node1), SizeIs(2));
EXPECT_TRUE(EqualTreesByEnum(expected.get(), node1.get()));
}
// Tests that subscript operator works (mutable).
TEST(NodeSubscriptTest, MutableReference) {
auto example = Node(Leaf(6, "6"), Leaf(7, "7"));
auto &example_node = down_cast<SyntaxTreeNode &>(*example);
EXPECT_EQ(example_node[0]->Tag().tag, 6);
EXPECT_EQ(example_node[1]->Tag().tag, 7);
}
// Tests that subscript operator works (const).
TEST(NodeSubscriptTest, ConstReference) {
auto example = Node(Leaf(8, "8"), Leaf(9, "9"));
const auto &example_node = down_cast<const SyntaxTreeNode &>(*example);
EXPECT_EQ(example_node[0]->Tag().tag, 8);
EXPECT_EQ(example_node[1]->Tag().tag, 9);
}
} // namespace
} // namespace verible