|
| 1 | +"""Tests for ISA regex pattern validation.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import re |
| 5 | +from riscv_config.constants import isa_regex |
| 6 | + |
| 7 | + |
| 8 | +class TestISARegex: |
| 9 | + |
| 10 | + @pytest.mark.parametrize("isa_string,expected", [ |
| 11 | + # Basic ISA strings |
| 12 | + ("RV32I", True), |
| 13 | + ("RV64I", True), |
| 14 | + ("RV128I", True), |
| 15 | + ("RV32E", True), |
| 16 | + |
| 17 | + # Standard extensions |
| 18 | + ("RV32IMAFD", True), |
| 19 | + ("RV64IG", True), |
| 20 | + ("RV32IMC", True), |
| 21 | + ("RV64IMAFDC", True), |
| 22 | + |
| 23 | + # Sub-extensions |
| 24 | + ("RV32I_Zicsr", True), |
| 25 | + ("RV64I_Zifencei", True), |
| 26 | + ("RV32I_Zicsr_Zifencei", True), |
| 27 | + ("RV32I_Svnapot", True), |
| 28 | + ("RV64I_Smrnmi", True), |
| 29 | + |
| 30 | + # Custom extensions |
| 31 | + ("RV32I_Xvendor", True), |
| 32 | + ("RV64I_Xvendor1_Xvendor2", True), |
| 33 | + ("RV64I_XcustomExt123", True), |
| 34 | + |
| 35 | + # Complex combinations |
| 36 | + ("RV64IMAFD_Zicsr_Zifencei_Xvendor", True), |
| 37 | + ("RV32I_Zve32x", True), |
| 38 | + ("RV64I_Zve64f", True), |
| 39 | + ("RV32I_Zvl32b", True), |
| 40 | + ]) |
| 41 | + def test_valid_isa_strings(self, isa_string, expected): |
| 42 | + """Valid ISA strings should match the regex.""" |
| 43 | + assert bool(isa_regex.match(isa_string)) == expected |
| 44 | + |
| 45 | + @pytest.mark.parametrize("isa_string,expected", [ |
| 46 | + # Missing base ISA |
| 47 | + ("RV64G", False), |
| 48 | + ("RV32", False), |
| 49 | + ("RV64", False), |
| 50 | + |
| 51 | + # Invalid widths |
| 52 | + ("RV16I", False), |
| 53 | + ("RV256I", False), |
| 54 | + |
| 55 | + # Invalid base ISA |
| 56 | + ("RV32X", False), |
| 57 | + ("RV32II", False), |
| 58 | + ("RV64EI", False), |
| 59 | + |
| 60 | + # Format errors |
| 61 | + ("RV32IZicsr", False), |
| 62 | + ("RV32I_", False), |
| 63 | + ("RV32I__Zicsr", False), |
| 64 | + ("RV32I_Zicsr_", False), |
| 65 | + |
| 66 | + # Case sensitivity |
| 67 | + ("rv32i", False), |
| 68 | + ("RV32i", False), |
| 69 | + ("RV32I_zicsr", False), |
| 70 | + ("RV32I_ZICSR", False), |
| 71 | + |
| 72 | + # Unknown extensions |
| 73 | + ("RV32I_Zunknown", False), |
| 74 | + ("RV32I_Sunknown", False), |
| 75 | + ("RV32I_X", False), |
| 76 | + ("RV32I_Xinvalid-name", False), |
| 77 | + |
| 78 | + # Whitespace and special chars |
| 79 | + ("", False), |
| 80 | + ("RV32I ", False), |
| 81 | + (" RV32I", False), |
| 82 | + ("RV32I@", False), |
| 83 | + ("RV32I_Zicsr!", False), |
| 84 | + ]) |
| 85 | + def test_invalid_isa_strings(self, isa_string, expected): |
| 86 | + """Invalid ISA strings should be rejected.""" |
| 87 | + assert bool(isa_regex.match(isa_string)) == expected |
| 88 | + |
| 89 | + def test_regex_structure(self): |
| 90 | + """Basic regex structure tests.""" |
| 91 | + assert isinstance(isa_regex, re.Pattern) |
| 92 | + assert isa_regex.pattern.startswith("^") |
| 93 | + assert isa_regex.pattern.endswith("$") |
| 94 | + |
| 95 | + def test_all_standard_extensions(self): |
| 96 | + """Test all standard extensions work.""" |
| 97 | + for ext in "ACDFGHJLMNPQSTUV": |
| 98 | + isa = f"RV32I{ext}" |
| 99 | + assert isa_regex.match(isa), f"Extension {ext} should work" |
| 100 | + |
| 101 | + def test_all_architectures(self): |
| 102 | + """Test all supported architectures.""" |
| 103 | + for width in ["32", "64", "128"]: |
| 104 | + for base in ["I", "E"]: |
| 105 | + isa = f"RV{width}{base}" |
| 106 | + assert isa_regex.match(isa), f"{isa} should match" |
| 107 | + |
| 108 | + def test_extension_categories(self): |
| 109 | + """Test different extension categories.""" |
| 110 | + # Z extensions |
| 111 | + z_tests = ["RV32I_Zicsr", "RV32I_Zba", "RV32I_Zfh"] |
| 112 | + for test in z_tests: |
| 113 | + assert isa_regex.match(test), f"{test} should match" |
| 114 | + |
| 115 | + # S extensions |
| 116 | + s_tests = ["RV32I_Smrnmi", "RV32I_Svnapot"] |
| 117 | + for test in s_tests: |
| 118 | + assert isa_regex.match(test), f"{test} should match" |
| 119 | + |
| 120 | + # Vector extensions |
| 121 | + v_tests = ["RV32I_Zve32x", "RV32I_Zvl32b"] |
| 122 | + for test in v_tests: |
| 123 | + assert isa_regex.match(test), f"{test} should match" |
| 124 | + |
| 125 | + |
| 126 | +class TestRealWorldScenarios: |
| 127 | + """Test realistic ISA configurations.""" |
| 128 | + |
| 129 | + def test_common_configurations(self): |
| 130 | + """Test ISA strings from real projects.""" |
| 131 | + configs = [ |
| 132 | + "RV32I", |
| 133 | + "RV32IMC", |
| 134 | + "RV32IMAFD", |
| 135 | + "RV64IMAFD", |
| 136 | + "RV32I_Zicsr_Zifencei", |
| 137 | + "RV64IG_Zicsr_Zifencei", |
| 138 | + "RV32I_Zba_Zbb_Zbc_Zbs", |
| 139 | + "RV64I_Zfh_Zfa", |
| 140 | + "RV32I_Zve32x_Zvl32b", |
| 141 | + ] |
| 142 | + |
| 143 | + for config in configs: |
| 144 | + assert isa_regex.match(config), f"Config {config} should work" |
| 145 | + |
| 146 | + def test_user_mistakes(self): |
| 147 | + """Test common user errors.""" |
| 148 | + mistakes = [ |
| 149 | + ("RV32G", "G needs I or E"), |
| 150 | + ("RV32i", "Wrong case"), |
| 151 | + ("RV32I_zicsr", "Extension case"), |
| 152 | + ("RV32IZicsr", "Missing underscore"), |
| 153 | + ("RV32I_Zicsr_", "Trailing underscore"), |
| 154 | + ("rv32i", "All lowercase"), |
| 155 | + ] |
| 156 | + |
| 157 | + for mistake, _ in mistakes: |
| 158 | + assert not isa_regex.match(mistake), f"{mistake} should fail" |
| 159 | + |
| 160 | + |
| 161 | +def test_can_import_regex(): |
| 162 | + """Test regex imports correctly.""" |
| 163 | + from riscv_config.constants import isa_regex |
| 164 | + assert isa_regex is not None |
| 165 | + assert callable(isa_regex.match) |
0 commit comments