This repository was archived by the owner on Apr 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathtest_constants.py
More file actions
165 lines (136 loc) · 4.93 KB
/
test_constants.py
File metadata and controls
165 lines (136 loc) · 4.93 KB
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
"""Tests for ISA regex pattern validation."""
import pytest
import re
from riscv_config.constants import isa_regex
class TestISARegex:
@pytest.mark.parametrize("isa_string,expected", [
# Basic ISA strings
("RV32I", True),
("RV64I", True),
("RV128I", True),
("RV32E", True),
# Standard extensions
("RV32IMAFD", True),
("RV64IG", True),
("RV32IMC", True),
("RV64IMAFDC", True),
# Sub-extensions
("RV32I_Zicsr", True),
("RV64I_Zifencei", True),
("RV32I_Zicsr_Zifencei", True),
("RV32I_Svnapot", True),
("RV64I_Smrnmi", True),
# Custom extensions
("RV32I_Xvendor", True),
("RV64I_Xvendor1_Xvendor2", True),
("RV64I_XcustomExt123", True),
# Complex combinations
("RV64IMAFD_Zicsr_Zifencei_Xvendor", True),
("RV32I_Zve32x", True),
("RV64I_Zve64f", True),
("RV32I_Zvl32b", True),
])
def test_valid_isa_strings(self, isa_string, expected):
"""Valid ISA strings should match the regex."""
assert bool(isa_regex.match(isa_string)) == expected
@pytest.mark.parametrize("isa_string,expected", [
# Missing base ISA
("RV64G", False),
("RV32", False),
("RV64", False),
# Invalid widths
("RV16I", False),
("RV256I", False),
# Invalid base ISA
("RV32X", False),
("RV32II", False),
("RV64EI", False),
# Format errors
("RV32IZicsr", False),
("RV32I_", False),
("RV32I__Zicsr", False),
("RV32I_Zicsr_", False),
# Case sensitivity
("rv32i", False),
("RV32i", False),
("RV32I_zicsr", False),
("RV32I_ZICSR", False),
# Unknown extensions
("RV32I_Zunknown", False),
("RV32I_Sunknown", False),
("RV32I_X", False),
("RV32I_Xinvalid-name", False),
# Whitespace and special chars
("", False),
("RV32I ", False),
(" RV32I", False),
("RV32I@", False),
("RV32I_Zicsr!", False),
])
def test_invalid_isa_strings(self, isa_string, expected):
"""Invalid ISA strings should be rejected."""
assert bool(isa_regex.match(isa_string)) == expected
def test_regex_structure(self):
"""Basic regex structure tests."""
assert isinstance(isa_regex, re.Pattern)
assert isa_regex.pattern.startswith("^")
assert isa_regex.pattern.endswith("$")
def test_all_standard_extensions(self):
"""Test all standard extensions work."""
for ext in "ACDFGHJLMNPQSTUV":
isa = f"RV32I{ext}"
assert isa_regex.match(isa), f"Extension {ext} should work"
def test_all_architectures(self):
"""Test all supported architectures."""
for width in ["32", "64", "128"]:
for base in ["I", "E"]:
isa = f"RV{width}{base}"
assert isa_regex.match(isa), f"{isa} should match"
def test_extension_categories(self):
"""Test different extension categories."""
# Z extensions
z_tests = ["RV32I_Zicsr", "RV32I_Zba", "RV32I_Zfh"]
for test in z_tests:
assert isa_regex.match(test), f"{test} should match"
# S extensions
s_tests = ["RV32I_Smrnmi", "RV32I_Svnapot"]
for test in s_tests:
assert isa_regex.match(test), f"{test} should match"
# Vector extensions
v_tests = ["RV32I_Zve32x", "RV32I_Zvl32b"]
for test in v_tests:
assert isa_regex.match(test), f"{test} should match"
class TestRealWorldScenarios:
"""Test realistic ISA configurations."""
def test_common_configurations(self):
"""Test ISA strings from real projects."""
configs = [
"RV32I",
"RV32IMC",
"RV32IMAFD",
"RV64IMAFD",
"RV32I_Zicsr_Zifencei",
"RV64IG_Zicsr_Zifencei",
"RV32I_Zba_Zbb_Zbc_Zbs",
"RV64I_Zfh_Zfa",
"RV32I_Zve32x_Zvl32b",
]
for config in configs:
assert isa_regex.match(config), f"Config {config} should work"
def test_user_mistakes(self):
"""Test common user errors."""
mistakes = [
("RV32G", "G needs I or E"),
("RV32i", "Wrong case"),
("RV32I_zicsr", "Extension case"),
("RV32IZicsr", "Missing underscore"),
("RV32I_Zicsr_", "Trailing underscore"),
("rv32i", "All lowercase"),
]
for mistake, _ in mistakes:
assert not isa_regex.match(mistake), f"{mistake} should fail"
def test_can_import_regex():
"""Test regex imports correctly."""
from riscv_config.constants import isa_regex
assert isa_regex is not None
assert callable(isa_regex.match)