-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.test.py
More file actions
161 lines (109 loc) · 5.92 KB
/
Copy pathmodels.test.py
File metadata and controls
161 lines (109 loc) · 5.92 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
import sys
import unittest
from pathlib import Path
from typing import Any
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import snesdsp
from snesdsp import chip, errors, models
EVERY_PART = {"dsp1", "dsp1a", "dsp1b", "dsp2", "dsp3", "dsp4"}
class CatalogueTest(unittest.TestCase):
def test_the_package_names_every_part_it_covers(self) -> None:
self.assertEqual(set(models.MODELS), EVERY_PART)
def test_a_part_says_what_it_is(self) -> None:
self.assertTrue(models.lookup("dsp2").summary)
def test_and_which_image_it_runs(self) -> None:
self.assertEqual(models.lookup("dsp2").image, "dsp2")
def test_a_part_that_carries_another_part_program_says_so(self) -> None:
self.assertEqual(models.lookup("dsp1a").image, "dsp1")
def test_and_is_still_a_part_in_its_own_right(self) -> None:
self.assertEqual(models.lookup("dsp1a").name, "dsp1a")
def test_a_part_prints_as_itself_and_the_image_it_runs(self) -> None:
printed = repr(models.lookup("dsp1a"))
self.assertIn("dsp1a", printed)
self.assertIn("dsp1", printed)
def test_every_part_carries_a_summary(self) -> None:
for name in models.MODELS:
self.assertTrue(models.lookup(name).summary, name)
def test_no_two_parts_share_a_name(self) -> None:
self.assertEqual(len(models.MODELS), len(set(models.MODELS)))
class NamingTest(unittest.TestCase):
def test_a_part_is_found_by_its_name(self) -> None:
self.assertEqual(models.lookup("dsp3").name, "dsp3")
def test_and_by_any_name_it_is_also_known_by(self) -> None:
for alias in models.lookup("dsp1").aliases:
self.assertEqual(models.lookup(alias).name, "dsp1")
def test_the_name_is_read_without_regard_to_case(self) -> None:
self.assertEqual(models.lookup("DSP3").name, "dsp3")
def test_a_name_no_part_answers_to_is_refused(self) -> None:
with self.assertRaises(errors.UnknownModelError):
models.lookup("dsp9")
def test_and_the_refusal_names_what_there_is(self) -> None:
with self.assertRaises(errors.UnknownModelError) as raised:
models.lookup("dsp9")
for name in EVERY_PART:
self.assertIn(name, str(raised.exception))
def test_no_alias_belongs_to_two_parts(self) -> None:
seen = [alias for name in models.MODELS for alias in models.lookup(name).aliases]
self.assertEqual(len(seen), len(set(seen)))
class DeclaredImageTest(unittest.TestCase):
"""That every part names an image the processor will recognise and confirm.
This is what a machine with no microcode can still check, and it is the check
that matters most: a user who supplies a file gets it identified by digest
before a byte of it is run, so a wrong file is refused rather than executed.
A part whose image nobody declared would be accepted on trust.
"""
def _manifest(self) -> "dict[str, Any]":
import json
where = Path(__file__).resolve().parent / "artifacts.manifest.json"
held = json.loads(where.read_text())
assert isinstance(held, dict), f"{where} does not hold an object"
return held
def test_every_part_runs_an_image_the_processor_declares(self) -> None:
declared = {one["part"] for one in self._manifest()["artifacts"]}
for name in models.MODELS:
self.assertIn(models.lookup(name).image, declared, name)
def test_every_declared_image_carries_a_deciding_digest(self) -> None:
for one in self._manifest()["artifacts"]:
for accepted in one["accepted"]:
self.assertEqual(len(accepted["sha256"]), 64, one["part"])
def test_and_the_shape_the_processor_needs_to_load_it(self) -> None:
for one in self._manifest()["artifacts"]:
self.assertEqual(
one["bytes"], one["programWords"] * 3 + one["dataWords"] * 2, one["part"]
)
def test_every_part_of_this_family_runs_the_same_processor(self) -> None:
declared = {one["part"]: one["processor"] for one in self._manifest()["artifacts"]}
for name in models.MODELS:
self.assertEqual(declared[models.lookup(name).image], "upd7725", name)
class BuildingTest(unittest.TestCase):
"""What the package hands back, which is a part rather than a description."""
def test_asking_for_a_part_asks_for_its_microcode(self) -> None:
with self.assertRaises((snesdsp.NoFirmware, Exception)) as raised:
_refused_or_built("dsp9")
self.assertTrue(raised.exception)
def test_a_name_no_part_answers_to_is_refused_before_any_image_is_looked_for(self) -> None:
with self.assertRaises(errors.UnknownModelError):
snesdsp.Chip("dsp9")
def test_the_default_part_is_one_the_catalogue_knows(self) -> None:
self.assertIn("dsp1", models.MODELS)
def test_the_package_offers_no_way_to_ask_for_anything_but_the_part(self) -> None:
self.assertNotIn("backend", snesdsp.__all__)
self.assertNotIn("MODELLED", snesdsp.__all__)
def _refused_or_built(name: str) -> "chip.Chip":
return snesdsp.Chip(name)
class NamingNoneTest(unittest.TestCase):
"""That leaving the model out is refused, and refused usefully."""
def test_building_without_naming_a_model_is_refused(self) -> None:
with self.assertRaises(errors.UnknownModelError):
snesdsp.Chip()
def test_and_the_refusal_names_every_model_there_is(self) -> None:
with self.assertRaises(errors.UnknownModelError) as caught:
snesdsp.Chip()
missing = [name for name in snesdsp.MODELS if name not in str(caught.exception)]
self.assertEqual(missing, [])
def test_nothing_named_describe_is_published(self) -> None:
self.assertFalse(hasattr(snesdsp, "describe"))
def test_and_no_default_model_is_published_either(self) -> None:
self.assertFalse(hasattr(snesdsp, "DEFAULT_MODEL"))
if __name__ == "__main__":
unittest.main()