pheon/tests/python/test_ffi.py
Jan Petykiewicz b3346b40a4 Introduce Pheon GDSII parsing and layout review tools
Implement native GDSII reading and writing with Arrow interchange, hierarchy
scans and selective cell loading. Validate numeric packing and array pitches,
and preserve normalized REAL8 encodings exactly within the supported range.

Add standalone dump, structural diff, hierarchy, layer-name annotation and Git
diff commands. Provide scoped text and JSON reports, deterministic comparison,
and record-preserving metadata edits.

Package the shared library and optional Python Arrow readers as pheon using
CFFI ABI mode and Python-independent platform wheels. Include locked source
builds, native API checks, cross-platform artifact workflows, documentation
and regression coverage. Require Rust 1.88 or newer and Python 3.11 or newer.

Validation: 87 native tests, 14 binding tests on Python 3.11/3.13/3.14,
and 71 installed companion integration tests pass. Full, scanned and selected
Arrow layouts match the previous schemas.
2026-11-16 16:21:21 -08:00

70 lines
2.7 KiB
Python

"""Exercise the explicit C ABI declarations against the actual shared library."""
import struct
import tempfile
from pathlib import Path
import unittest
from pheon._native import ffi, lib
def record(tag, data=b""):
return struct.pack(">HH", len(data) + 4, tag) + data
def layout():
return b"".join([
record(0x0002, struct.pack(">h", 600)),
record(0x0102, b"\0" * 24),
record(0x0206, b"test"),
record(0x0305, bytes.fromhex("3e4189374bc6a7f03944b82fa09b5a54")),
record(0x0502, b"\0" * 24),
record(0x0606, b"cell"),
record(0x0700),
record(0x0400),
])
class FfiTests(unittest.TestCase):
def check_export(self, call):
array = ffi.new("struct ArrowArray *")
schema = ffi.new("struct ArrowSchema *")
try:
self.assertEqual(call(array, schema), 0)
self.assertEqual(lib.last_error_message(), ffi.NULL)
self.assertEqual(array.length, 1)
self.assertEqual(ffi.string(schema.format), b"+s")
self.assertNotEqual(array.release, ffi.NULL)
self.assertNotEqual(schema.release, ffi.NULL)
self.assertGreaterEqual(array.n_children, 7)
self.assertEqual(array.n_children, schema.n_children)
self.assertEqual(ffi.string(schema.children[0].name), b"meters_per_db_unit")
finally:
if array.release != ffi.NULL:
array.release(array)
if schema.release != ffi.NULL:
schema.release(schema)
def test_scan_selected_read_and_path_exports(self):
data = layout()
self.check_export(lambda a, s: lib.scan_bytes(data, len(data), a, s))
self.check_export(lambda a, s: lib.read_bytes(data, len(data), a, s))
# The library header is 62 bytes; the one cell ends just before ENDLIB.
ranges = ffi.new("uint64_t[]", [62, len(data) - 4])
self.check_export(lambda a, s: lib.read_cells_bytes(data, len(data), ranges, 1, a, s))
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "file with spaces.gds"
path.write_bytes(data)
self.check_export(lambda a, s: lib.read_path(str(path).encode(), a, s))
def test_failures_leave_outputs_empty_and_expose_error_message(self):
array = ffi.new("struct ArrowArray *")
schema = ffi.new("struct ArrowSchema *")
self.assertEqual(lib.scan_bytes(ffi.NULL, 0, array, schema), -1)
self.assertNotEqual(lib.last_error_message(), ffi.NULL)
self.assertIn(b"scan_bytes", ffi.string(lib.last_error_message()))
self.assertEqual(array.release, ffi.NULL)
self.assertEqual(schema.release, ffi.NULL)
if __name__ == "__main__":
unittest.main()