forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers_test.cc
93 lines (83 loc) · 2.85 KB
/
numbers_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
// 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/CST/numbers.h"
#include <sstream>
#include <utility>
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
namespace verilog {
namespace analysis {
namespace {
struct BasedNumberTestCase {
absl::string_view base;
absl::string_view digits;
BasedNumber expected;
};
// Tests that BasedNumber literals are parsed correctly.
TEST(BasedNumberTest, ParseLiteral) {
const BasedNumberTestCase test_cases[] = {
{"\'b", "1", {'b', false, "1"}},
{"\'b", "1101", {'b', false, "1101"}},
{"\'b", "_1_1_0_1_", {'b', false, "1101"}},
{"\'sb", "1100_0011", {'b', true, "11000011"}},
{"\'b", "1101", {'b', false, "1101"}},
{"\'b", "xz01", {'b', false, "xz01"}},
{"\'B", "0", {'b', false, "0"}},
{"\'sB", "1", {'b', true, "1"}},
{"\'SB", "0_0", {'b', true, "00"}},
{"\'d", "12", {'d', false, "12"}},
{"\'D", "12", {'d', false, "12"}},
{"\'o", "66", {'o', false, "66"}},
{"\'O", "44", {'o', false, "44"}},
{"\'sO", "44", {'o', true, "44"}},
{"\'h", "F00D", {'h', false, "F00D"}},
{"\'H", "FEED_face", {'h', false, "FEEDface"}},
{"\'sh", "ADee", {'h', true, "ADee"}},
};
for (const auto& test : test_cases) {
const BasedNumber actual(test.base, test.digits);
EXPECT_TRUE(actual.ok);
EXPECT_EQ(test.expected, actual);
}
}
// Tests that invalid inputs are marked as not OK.
TEST(BasedNumberTest, ParseInvalidLiterals) {
const std::pair<absl::string_view, absl::string_view> test_cases[] = {
{"", ""},
{"xx", ""},
{"", "96"},
{"1'b", "1"}, // valid literals start with '
};
for (const auto& test : test_cases) {
const BasedNumber actual(test.first, test.second);
EXPECT_FALSE(actual.ok);
}
}
// Tests that human-readable representation of BasedNumber looks right.
TEST(BasedNumberTest, PrintString) {
const BasedNumber test({'b', false, "1"});
std::ostringstream stream;
stream << test;
EXPECT_EQ(stream.str(), "base:b signed:0 literal:1");
}
// Tests an invalid BasedNumber is printed as such.
TEST(BasedNumberTest, PrintStringInvalid) {
const BasedNumber test("xx", "");
std::ostringstream stream;
stream << test;
EXPECT_EQ(stream.str(), "<invalid>");
}
} // namespace
} // namespace analysis
} // namespace verilog