forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractors_test.cc
107 lines (97 loc) · 3.52 KB
/
extractors_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
// 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.
#include "verilog/analysis/extractors.h"
#include <set>
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
#include "verilog/preprocessor/verilog_preprocess.h"
#undef EXPECT_OK
#define EXPECT_OK(value) EXPECT_TRUE((value).ok())
#undef ASSERT_OK
#define ASSERT_OK(value) ASSERT_TRUE((value).ok())
namespace verilog {
namespace analysis {
namespace {
static constexpr VerilogPreprocess::Config kDefaultPreprocess;
TEST(CollectInterfaceNamesTest, NonModuleTests) {
const std::pair<absl::string_view, std::set<std::string>> kTestCases[] = {
{"", {}},
{"class cls;\nendclass", {}},
{"function f;\nendfunction", {}},
{"package pkg;\nendpackage", {}},
};
for (const auto& test : kTestCases) {
std::set<std::string> preserved;
EXPECT_OK(
CollectInterfaceNames(test.first, &preserved, kDefaultPreprocess));
EXPECT_EQ(preserved, test.second);
}
}
TEST(CollectInterfaceNamesTest, MinimalistModuleTests) {
const std::pair<absl::string_view, std::set<std::string>> kTestCases[] = {
{"module mod;\nendmodule", {"mod"}},
{"module mod2(input foo);\nendmodule", {"mod2", "foo"}},
{"module top\nimport pkg::*;\n(input a);\nendmodule",
{"top", "pkg", "a"}},
{"module mod #(parameter N = 0);\nendmodule: mod", {"mod", "N"}},
{"module a;\nendmodule\nmodule b;\nendmodule", {"a", "b"}},
};
for (const auto& test : kTestCases) {
std::set<std::string> preserved;
EXPECT_OK(
CollectInterfaceNames(test.first, &preserved, kDefaultPreprocess));
EXPECT_EQ(preserved, test.second);
}
}
// Tests that serveral instances with many identifiers are working as expected
TEST(CollectInterfaceNamesTest, BiggerModuleTests) {
const std::pair<absl::string_view, std::set<std::string>> kTestCases[] = {
{"interface TheBus(input clk);\n"
" logic [7:0] addr, wdata, rdata;\n"
" logic write_en;\n"
"endinterface\n"
"module ram_model(clk, we, addr, rdata, wdata);\n"
" input clk, we;\n"
" input [7:0] addr, wdata;\n"
" output [7:0] rdata;\n"
" reg [7:0] mem [256];\n"
" assign rdata = mem[addr];\n"
" always @(posedge clk)\n"
" if (we)\n"
" mem[bus.addr] = wdata;\n"
"endmodule\n"
"module ram_wrapper(TheBus bus);\n"
" ram_model u_ram(\n"
" .clk(bus.clk),\n"
" .we(bus.write_en),\n"
" .addr(bus.addr),\n"
" .rdata(bus.rdata),\n"
" .wdata(bus.wdata)\n"
" );\n"
"endmodule\n",
{"ram_model", "clk", "we", "addr", "rdata", "wdata", "ram_wrapper",
"TheBus", "bus"}},
};
for (const auto& test : kTestCases) {
std::set<std::string> preserved;
EXPECT_OK(
CollectInterfaceNames(test.first, &preserved, kDefaultPreprocess));
EXPECT_EQ(preserved, test.second);
}
}
} // namespace
} // namespace analysis
} // namespace verilog