aboutsummaryrefslogtreecommitdiff
path: root/tests.py
diff options
context:
space:
mode:
authorAlexis Roda <alexis.roda.villalonga@gmail.com>2019-07-26 21:38:40 +0200
committerAlexis Roda <alexis.roda.villalonga@gmail.com>2019-07-26 21:38:40 +0200
commitc4de499db06c6343cd741d27f4ed4d6d878aad46 (patch)
tree9131c18e9bda49e864a55aa318544f0b7d6e4179 /tests.py
parent1c4ea8f147bc458cf34d864655a9a8969bbcdac5 (diff)
downloadz80count-c4de499db06c6343cd741d27f4ed4d6d878aad46.tar.gz
z80count-c4de499db06c6343cd741d27f4ed4d6d878aad46.zip
Optimize table lookup.
Instead of a flat table of regexes this implementation groups regexes by mnemonic, making lookup faster. The implementation is encaptulated in its own class "Parser".
Diffstat (limited to 'tests.py')
-rw-r--r--tests.py42
1 files changed, 29 insertions, 13 deletions
diff --git a/tests.py b/tests.py
index c6da92e..b235ba0 100644
--- a/tests.py
+++ b/tests.py
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
-from z80count import init_table
-from z80count import lookup
+import pytest
+
+from z80count import Parser
data = (
@@ -905,17 +906,32 @@ data = (
)
-def runtests():
- table = init_table()
+@pytest.fixture(scope="module")
+def parser_table():
+ yield Parser()
+
+
+@pytest.mark.parametrize("instruction,cycles", data)
+def test_lookup(instruction, cycles, parser_table):
+ entry = parser_table.lookup(instruction)
+ assert entry is not None, "Not found: {}".format(instruction)
+ assert entry["cycles"] == cycles, "Failed: {} expected '{}' != found '{}'".format(instruction, cycles, entry["cycles"])
+
- for instruction, cycles in data:
- entry = lookup(instruction, table)
- if entry is None:
- print("Not found: {}".format(instruction))
- continue
- if entry["cycles"] != cycles:
- print("Failed: {} expected '{}' != found '{}'".format(instruction, cycles, entry["cycles"]))
+@pytest.mark.parametrize("line,operator", (
+ ("foo: LD A, 1 ; load accumulator", "LD"),
+ ("foo: CALL 0xABCD", "CALL"),
+ ("foo: EI", "EI"),
+ ("LD A, 1 ; load accumulator", "LD"),
+ ("CALL 0xABCE", "CALL"),
+ ("EI", "EI"),
+ ("foo: ; some label", None),
+ ("foo:", None),
+ ("; some comment", None),
+))
+def test_extract_mnemonic(line, operator):
+ assert Parser._extract_mnemonic(line) == operator
-if __name__ == "__main__":
- runtests()
+def test_extract_mnemonic_normalizes_operator():
+ assert Parser._extract_mnemonic("call 0xabcd") == "CALL"