mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:38:36 +00:00
191 lines
9.1 KiB
Python
191 lines
9.1 KiB
Python
"""tools/d3d12_switches.py: which LEONIDA_* names of the D3D12 runtime it finds and what it says of each.
|
|
|
|
python -I tests/test_d3d12_switches.py
|
|
|
|
Sources written by hand here; CommandTest runs on the repository's own. No build, no GPU, no game files.
|
|
"""
|
|
import contextlib
|
|
import io
|
|
import os
|
|
import re
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
|
import d3d12_switches as ds # noqa: E402
|
|
|
|
GPU = "d3d12/crates/leonida-gpu/src/"
|
|
SOURCE = """//! A module. `LEONIDA_FAST=1`: the quick way; off by default. Another sentence.
|
|
/// Whether the old way is kept
|
|
/// (`LEONIDA_OLD_WAY=0`: not).
|
|
fn old() -> bool { crate::toggled!("LEONIDA_OLD_WAY") }
|
|
fn fast() -> bool { crate::asked!("LEONIDA_FAST") }
|
|
fn slow() -> bool { crate::switch::on("LEONIDA_SLOW") }
|
|
fn size() -> u64 {
|
|
std::env::var(
|
|
"LEONIDA_SIZE_MB").unwrap().parse().unwrap()
|
|
}
|
|
#[cfg(test)]
|
|
mod other;
|
|
fn after() { std::env::var_os("LEONIDA_AFTER_OUTLINE"); }
|
|
#[cfg(test)]
|
|
mod tests {
|
|
fn t() { std::env::var("LEONIDA_TEST_CHILD"); }
|
|
}
|
|
fn last() { std::env::var("LEONIDA_LAST"); }
|
|
"""
|
|
|
|
|
|
class CollectTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.found = ds.collect({GPU + "a.rs": SOURCE}, launcher=" LEONIDA_FAST = '1'\n", findings="x `LEONIDA_SLOW` y")
|
|
|
|
def kind(self, name):
|
|
return ds.kind(self.found[name]["uses"], name)
|
|
|
|
def test_the_kind_is_told_from_how_the_name_is_read(self):
|
|
self.assertEqual(self.kind("LEONIDA_OLD_WAY"), "on, `0` turns it off")
|
|
self.assertEqual(self.kind("LEONIDA_FAST"), "off unless set and not `0`")
|
|
self.assertEqual(self.kind("LEONIDA_SLOW"), "off unless set and not `0`")
|
|
self.assertEqual(self.kind("LEONIDA_SIZE_MB"), "see the code")
|
|
|
|
def test_only_a_name_the_ab_macros_take_is_marked_for_ab(self):
|
|
self.assertTrue(self.found["LEONIDA_OLD_WAY"]["ab"])
|
|
self.assertTrue(self.found["LEONIDA_FAST"]["ab"])
|
|
self.assertFalse(self.found["LEONIDA_SLOW"]["ab"])
|
|
|
|
def test_test_modules_are_told_from_code_after_them_and_from_outline_modules(self):
|
|
self.assertFalse(self.found["LEONIDA_TEST_CHILD"]["read"])
|
|
self.assertTrue(self.found["LEONIDA_AFTER_OUTLINE"]["read"])
|
|
self.assertTrue(self.found["LEONIDA_LAST"]["read"])
|
|
|
|
def test_a_test_file_is_all_tests(self):
|
|
found = ds.collect({GPU + "x_tests.rs": 'fn f() { std::env::var("LEONIDA_ONLY_HERE"); }'})
|
|
self.assertTrue(found["LEONIDA_ONLY_HERE"]["tests"])
|
|
|
|
def test_the_sentence_that_names_it_is_taken_over_line_ends(self):
|
|
self.assertEqual(self.found["LEONIDA_OLD_WAY"]["said"], "Whether the old way is kept (`LEONIDA_OLD_WAY=0`: not).")
|
|
self.assertEqual(self.found["LEONIDA_FAST"]["said"], "`LEONIDA_FAST=1`: the quick way; off by default.")
|
|
|
|
def test_launcher_and_findings_are_marked(self):
|
|
self.assertTrue(self.found["LEONIDA_FAST"]["launcher"])
|
|
self.assertFalse(self.found["LEONIDA_SLOW"]["launcher"])
|
|
self.assertTrue(self.found["LEONIDA_SLOW"]["findings"])
|
|
|
|
def test_a_longer_name_is_not_taken_for_a_shorter_one(self):
|
|
self.assertEqual(ds.sentence("`LEONIDA_FAST_N` is a count.", "LEONIDA_FAST"), "")
|
|
|
|
def test_a_prefix_is_not_a_name(self):
|
|
self.assertEqual(ds.NAME.findall("`LEONIDA_VIPATCH_`<name> and LEONIDA_X"), ["LEONIDA_X"])
|
|
|
|
def test_every_way_the_launcher_assigns_is_seen(self):
|
|
names = ["LEONIDA_A1", "LEONIDA_A2", "LEONIDA_A3", "LEONIDA_A4", "LEONIDA_A5", "LEONIDA_A6"]
|
|
source = "\n".join(f'v("{n}")' for n in names)
|
|
launcher = ("$settings = @{ LEONIDA_A1 = '1'; LEONIDA_A2 = '1' }\n $settings.LEONIDA_A3 = '1'\n"
|
|
"if ($settings.LEONIDA_A4 -eq '1') { }\n# LEONIDA_A5 = '1'\n$env:LEONIDA_A6 = '1'\n")
|
|
found = ds.collect({GPU + "a.rs": source}, launcher=launcher)
|
|
self.assertEqual([n for n in names if found[n]["launcher"]],
|
|
["LEONIDA_A1", "LEONIDA_A2", "LEONIDA_A3", "LEONIDA_A6"])
|
|
|
|
def test_a_switch_read_by_turns_elsewhere_is_for_ab_and_of_no_known_kind(self):
|
|
source = """fn a() { leonida_gpu::switched("LEONIDA_HLE") }
|
|
const SWITCH: &str = "LEONIDA_BY_CONST";
|
|
fn b() { if let Some(on) = crate::ab::pick(SWITCH) { } }
|
|
const SWITCH: &str = "LEONIDA_NOT_AB";
|
|
fn wide(switch: &'static str) -> bool {
|
|
if let Some(on) = crate::ab::pick(switch) { return on; }
|
|
false
|
|
}
|
|
fn c() { wide("LEONIDA_BY_FN"); narrow("LEONIDA_OTHER_FN") }
|
|
const NAMES: [&str; 2] = ["LEONIDA_L_A", "LEONIDA_L_B"];
|
|
fn d() { NAMES.iter().map(|name| crate::ab::bit(name)) }
|
|
fn e() { asked_for!("LEONIDA_BY_MACRO") }
|
|
"""
|
|
found = ds.collect({GPU + "a.rs": source})
|
|
self.assertEqual({n for n, e in found.items() if e["ab"]},
|
|
{"LEONIDA_HLE", "LEONIDA_BY_CONST", "LEONIDA_BY_FN", "LEONIDA_L_A", "LEONIDA_L_B", "LEONIDA_BY_MACRO"})
|
|
self.assertEqual(ds.kind(found["LEONIDA_HLE"]["uses"], "LEONIDA_HLE"), "see the code")
|
|
|
|
def test_a_brace_in_a_raw_string_does_not_end_a_test_module(self):
|
|
source = '#[cfg(all(test, windows))]\nmod tests {\n const S: &str = r#"\nvoid main() {\n}\n"#;\n' \
|
|
' fn f() { v("LEONIDA_AFTER_RAW") }\n}\nfn g() { v("LEONIDA_REAL") }\n'
|
|
found = ds.collect({GPU + "a.rs": source})
|
|
self.assertFalse(found["LEONIDA_AFTER_RAW"]["read"])
|
|
self.assertTrue(found["LEONIDA_REAL"]["read"])
|
|
|
|
def test_an_outline_test_module_is_tests(self):
|
|
found = ds.collect({GPU + "a.rs": "#[cfg(test)]\nmod bench;\n", GPU + "a/bench.rs": 'v("LEONIDA_B1")',
|
|
GPU + "b/bench.rs": 'v("LEONIDA_B2")'})
|
|
self.assertFalse(found["LEONIDA_B1"]["read"])
|
|
self.assertTrue(found["LEONIDA_B2"]["read"])
|
|
|
|
def test_a_name_only_in_a_comment_is_not_listed_as_read_and_a_placeholder_not_at_all(self):
|
|
found = ds.collect({GPU + "a.rs": "// `LEONIDA_OLD` was a switch once.\n// `LEONIDA_X` is any.\n"})
|
|
self.assertFalse(found["LEONIDA_OLD"]["read"])
|
|
self.assertNotIn("LEONIDA_X", found)
|
|
self.assertNotIn("| `LEONIDA_OLD`", ds.page(found))
|
|
self.assertEqual(ds.difference(found, "| `LEONIDA_OLD` | x"), ([], ["LEONIDA_OLD"]))
|
|
|
|
def test_a_piece_of_a_sentence_that_says_nothing_is_not_taken(self):
|
|
self.assertEqual(ds.sentence("The joined lists (a pool of them: `LEONIDA_J`, 48). Next.", "LEONIDA_J"),
|
|
"The joined lists (a pool of them: `LEONIDA_J`, 48).")
|
|
self.assertEqual(ds.sentence("`LEONIDA_J`.", "LEONIDA_J"), "")
|
|
|
|
def test_a_launcher_value_is_never_copied(self):
|
|
found = ds.collect({GPU + "a.rs": 'x("LEONIDA_LIST")'}, launcher="LEONIDA_LIST = 'a long value'")
|
|
self.assertTrue(found["LEONIDA_LIST"]["launcher"])
|
|
self.assertNotIn("a long value", ds.page(found))
|
|
|
|
|
|
class PageTest(unittest.TestCase):
|
|
def test_cells_do_not_break_the_table(self):
|
|
found = ds.collect({GPU + "a.rs": '/// `LEONIDA_WAY=<a>|<b>`: one of two.\nfn f() { v("LEONIDA_WAY"); }'})
|
|
row = next(line for line in ds.page(found).splitlines() if line.startswith("| `LEONIDA_WAY`"))
|
|
self.assertEqual(len(re.findall(r"(?<!\\)\|", row)), 8)
|
|
|
|
def test_a_page_has_no_difference_from_the_sources_it_was_made_of(self):
|
|
found = ds.collect({GPU + "a.rs": SOURCE})
|
|
self.assertEqual(ds.difference(found, ds.page(found)), ([], []))
|
|
|
|
def test_a_name_added_or_gone_is_told(self):
|
|
text = ds.page(ds.collect({GPU + "a.rs": 'v("LEONIDA_GONE")\nv("LEONIDA_KEPT")'}))
|
|
now = ds.collect({GPU + "a.rs": 'v("LEONIDA_KEPT")\nv("LEONIDA_NEW")'})
|
|
self.assertEqual(ds.difference(now, text), (["LEONIDA_NEW"], ["LEONIDA_GONE"]))
|
|
|
|
|
|
class CommandTest(unittest.TestCase):
|
|
def test_the_repository_is_listed_and_an_unwritable_path_is_one_line(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out, err = io.StringIO(), io.StringIO()
|
|
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
|
code = ds.main(["--out", os.path.join(tmp, "page.md")])
|
|
bad = ds.main(["--out", os.path.join(tmp, "none", "page.md")])
|
|
same = ds.main(["--check", os.path.join(tmp, "page.md")])
|
|
self.assertEqual((code, bad, same), (0, 2, 0), err.getvalue())
|
|
self.assertEqual(len(err.getvalue().splitlines()), 1)
|
|
with open(os.path.join(tmp, "page.md"), "rb") as f:
|
|
data = f.read()
|
|
self.assertNotIn(b"\r", data)
|
|
# A moved folder must not turn the list into an empty one.
|
|
self.assertGreater(data.count(b"| `LEONIDA_"), 300)
|
|
|
|
def test_the_tracked_switches_page_matches_the_sources(self):
|
|
switches = os.path.join(ROOT, "d3d12", "docs", "switches.md")
|
|
out, err = io.StringIO(), io.StringIO()
|
|
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
|
code = ds.main(["--root", ROOT, "--check", switches])
|
|
self.assertEqual(
|
|
code,
|
|
0,
|
|
"d3d12/docs/switches.md is out of date (regenerate with "
|
|
"`python -I tools/d3d12_switches.py --out d3d12/docs/switches.md`):\n"
|
|
+ out.getvalue()
|
|
+ err.getvalue(),
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|