interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tools / figures / test_stack_legend.py

65 lines · 2675 bytes Raw Blame History
1#!/usr/bin/env python3
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3"""Check rendered legend geometry. Run with Python 3 and Inkscape installed."""
4import os
5from pathlib import Path
6import shutil
7import subprocess
8import tempfile
9import unittest
10import xml.etree.ElementTree as ET
11
12from make_figures import fig_stack
13
14
15def legend_bounds(svg):
16 """Measure the final swatch/label pairs with the SVG's real font fallback."""
17 root = ET.fromstring(svg)
18 legend = list(root)[-8:]
19 assert [e.tag.rsplit("}", 1)[-1] for e in legend] == ["rect", "text"] * 4
20 for i, element in enumerate(legend):
21 element.set("id", f"legend-{i}")
22 inkscape = shutil.which("inkscape")
23 if not inkscape:
24 raise RuntimeError("Inkscape is required for the rendered legend check")
25 with tempfile.TemporaryDirectory(prefix="cudapp-legend-") as work:
26 path = Path(work) / "stack.svg"
27 ET.ElementTree(root).write(path)
28 env = dict(os.environ, HOME=work, XDG_CACHE_HOME=work)
29 result = subprocess.run([inkscape, str(path), "--query-all"], env=env,
30 check=True, capture_output=True, text=True)
31 bounds = {}
32 for line in result.stdout.splitlines():
33 fields = line.split(",")
34 if fields[0].startswith("legend-"):
35 bounds[fields[0]] = tuple(map(float, fields[1:]))
36 return root, [bounds[f"legend-{i}"] for i in range(len(legend))]
37
38
39def assert_clear(test, svg):
40 root, boxes = legend_bounds(svg)
41 width, height = float(root.get("width")), float(root.get("height"))
42 for i, (x, y, w, h) in enumerate(boxes):
43 test.assertGreaterEqual(x, 0, f"legend item {i} outside left edge")
44 test.assertGreaterEqual(y, 0, f"legend item {i} outside top edge")
45 test.assertLessEqual(x + w, width, f"legend item {i} clipped at right edge")
46 test.assertLessEqual(y + h, height, f"legend item {i} clipped at bottom edge")
47 for j, (xx, yy, ww, hh) in enumerate(boxes[:i]):
48 overlap = x < xx + ww and xx < x + w and y < yy + hh and yy < y + h
49 test.assertFalse(overlap, f"legend items {j} and {i} overlap")
50
51
52class StackLegendTest(unittest.TestCase):
53 def test_rendered_labels_and_swatches_do_not_overlap(self):
54 assert_clear(self, fig_stack().svg())
55
56 def test_overlapping_swatch_is_rejected(self):
57 root = ET.fromstring(fig_stack().svg())
58 legend = list(root)[-8:]
59 legend[4].set("x", legend[3].get("x"))
60 with self.assertRaisesRegex(AssertionError, "overlap"):
61 assert_clear(self, ET.tostring(root, encoding="unicode"))
62
63
64if __name__ == "__main__":
65 unittest.main()