diff options
Diffstat (limited to 'tests/test_gpu_shift64.py')
| -rw-r--r-- | tests/test_gpu_shift64.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/test_gpu_shift64.py b/tests/test_gpu_shift64.py new file mode 100644 index 000000000..1ef33d475 --- /dev/null +++ b/tests/test_gpu_shift64.py | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | """GPU-VALU64-SHIFTS: readback and runner verdicts, without needing a GPU.""" | ||
| 2 | import contextlib | ||
| 3 | import io | ||
| 4 | from pathlib import Path | ||
| 5 | import struct | ||
| 6 | import sys | ||
| 7 | import tempfile | ||
| 8 | from types import SimpleNamespace | ||
| 9 | import unittest | ||
| 10 | from unittest.mock import patch | ||
| 11 | |||
| 12 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) | ||
| 13 | import run_gpu_tests as rgt | ||
| 14 | import check | ||
| 15 | |||
| 16 | |||
| 17 | class Shift64Readback(unittest.TestCase): | ||
| 18 | def test_oracle_boundaries(self): | ||
| 19 | initial, expected = check.shift64_words() | ||
| 20 | self.assertEqual(len(initial), 640) | ||
| 21 | self.assertEqual(len(expected), 640) | ||
| 22 | self.assertEqual(initial[:384], [0xEEEEEEEE] * 384) | ||
| 23 | self.assertEqual(initial[384:], expected[384:]) | ||
| 24 | # 1 << 32, 1 >> 32, signed(1) >> 32, each as low/high words. | ||
| 25 | self.assertEqual(expected[66:72], [0, 1, 0, 0, 0, 0]) | ||
| 26 | # INT64_MIN >> 63 is 1 logically and -1 arithmetically. | ||
| 27 | self.assertEqual(expected[270:276], [0, 0, 1, 0, 0xFFFFFFFF, 0xFFFFFFFF]) | ||
| 28 | # A count of 64 leaves the operand unchanged for all three shifts. | ||
| 29 | self.assertEqual(expected[372:378], [0x89ABCDEF, 0x81234567] * 3) | ||
| 30 | self.assertEqual(expected[378:384], [0x13579BDE, 0x02468ACF, 0xC4D5E6F7, 0x4091A2B3, | ||
| 31 | 0xC4D5E6F7, 0xC091A2B3]) | ||
| 32 | |||
| 33 | def run_readback(self, mutate=None, code=0, output="", missing=False): | ||
| 34 | paths = [] | ||
| 35 | |||
| 36 | def launch(*args, env): | ||
| 37 | paths.append(Path(env["VKC_HEAP_INIT"])) | ||
| 38 | initial, expected = check.shift64_words() | ||
| 39 | self.assertEqual(paths[-1].read_bytes(), struct.pack("<640I", *initial)) | ||
| 40 | self.assertEqual(env["VKC_SUBGROUP_SIZE"], "32") | ||
| 41 | self.assertEqual(env["VKC_OUT_WORDS"], "640") | ||
| 42 | self.assertEqual(env["LEONIDA_VK_DEVICE"], "synthetic device") | ||
| 43 | if mutate: | ||
| 44 | mutate(expected) | ||
| 45 | if not missing: | ||
| 46 | Path(env["VKC_DUMP"]).write_bytes(struct.pack(f"<{len(expected)}I", *expected)) | ||
| 47 | return code, output | ||
| 48 | |||
| 49 | with patch.object(check, "run", side_effect=launch): | ||
| 50 | result = check.shift64_test("unused", "unused", {"LEONIDA_VK_DEVICE": "synthetic device"}) | ||
| 51 | self.assertEqual(len(paths), 1) | ||
| 52 | self.assertFalse(paths[0].exists(), "private heap input must be cleaned up") | ||
| 53 | return result | ||
| 54 | |||
| 55 | def test_complete_readback_passes(self): | ||
| 56 | self.assertEqual(self.run_readback(), "") | ||
| 57 | |||
| 58 | def test_late_result_and_untouched_input_corruption_fail(self): | ||
| 59 | for word in (383, 639): | ||
| 60 | with self.subTest(word=word): | ||
| 61 | result = self.run_readback(lambda data: data.__setitem__(word, data[word] ^ 1)) | ||
| 62 | self.assertIn(f"word {word}:", result) | ||
| 63 | |||
| 64 | def test_truncated_and_missing_dump_fail(self): | ||
| 65 | self.assertIn("expected 2560 dump bytes, got 2556", self.run_readback(lambda data: data.pop())) | ||
| 66 | self.assertIn("no VKC_DUMP", self.run_readback(missing=True)) | ||
| 67 | |||
| 68 | def test_unsupported_int64_skips_but_nonzero_exit_fails(self): | ||
| 69 | self.assertIsInstance(self.run_readback(code=77, output="unsupported: no Int64\n"), check.Skip) | ||
| 70 | self.assertIn("exit 1", self.run_readback(code=1, output="dispatch failed")) | ||
| 71 | |||
| 72 | |||
| 73 | class Shift64Runner(unittest.TestCase): | ||
| 74 | def test_filtered_runner_counts_pass_failure_and_skip(self): | ||
| 75 | for detail, expected, verdict in (("", 0, "1/1 passed"), ("word 383: mismatch", 1, "0/1 passed"), | ||
| 76 | (check.Skip("SKIP (no Int64)"), 0, "0/1 passed, 1 skipped")): | ||
| 77 | with self.subTest(detail=detail), tempfile.TemporaryDirectory() as tmp: | ||
| 78 | args = SimpleNamespace(filter="cs_w32_shift64", build=tmp, llvm_mc="unused") | ||
| 79 | |||
| 80 | def translate(_cmd, **_kwargs): | ||
| 81 | (Path(tmp) / "spirv" / "cs_w32_shift64.spv").touch() | ||
| 82 | return 0, "" | ||
| 83 | |||
| 84 | output = io.StringIO() | ||
| 85 | with (patch.object(rgt, "assemble_to_s", side_effect=lambda _mc, _src, dst, _name: Path(dst).touch()), | ||
| 86 | patch.object(rgt, "spirv_as"), patch.object(rgt, "run_cmd", side_effect=translate), | ||
| 87 | patch.object(rgt, "shift64_test", return_value=detail) as readback, | ||
| 88 | contextlib.redirect_stdout(output)): | ||
| 89 | self.assertEqual(rgt.run_suite(args, tmp), expected) | ||
| 90 | readback.assert_called_once() | ||
| 91 | self.assertIn(verdict, output.getvalue()) | ||
| 92 | self.assertIn("cs_w32_shift64", output.getvalue()) | ||
| 93 | |||
| 94 | |||
| 95 | if __name__ == "__main__": | ||
| 96 | unittest.main() | ||