pheon/tests/python/test_packing.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

66 lines
3.4 KiB
Python

import unittest
import numpy as np
from pheon import pack_int2, pack_int4
class PackingTests(unittest.TestCase):
def test_scalars_lists_and_dimensions(self):
for pack, width in [(pack_int2, 2), (pack_int4, 4)]:
for dtype in [np.int64, np.int32, np.uint64, np.uint32, np.float64, np.float32]:
for shape in [(), (1,), (2, 2), (2, 2, 2)]:
count = int(np.prod(shape)) if shape else 1
data = np.arange(count, dtype=dtype).reshape(shape)
before = data.copy()
with self.subTest(width=width, dtype=dtype, shape=shape):
self.assertEqual(pack(data), data.astype(f">i{width}").tobytes())
np.testing.assert_array_equal(data, before)
for data in [5, [1, 2, 3], [[1, 2], [3, 4]], [], [[], []]]:
with self.subTest(width=width, data=data):
self.assertEqual(pack(data), np.asarray(data).astype(f">i{width}").tobytes())
def test_noncontiguous_readonly_and_endian_inputs(self):
original = np.arange(48, dtype=np.int64).reshape(6, 8)
readonly = original.copy()
readonly.setflags(write=False)
for data in [original.T, original[::-1, ::2], np.asfortranarray(original), readonly,
original.astype(">i8"), original.astype(">i2"), original.astype(">i4")]:
before = data.copy()
for pack, width in [(pack_int2, 2), (pack_int4, 4)]:
with self.subTest(width=width, strides=data.strides, dtype=data.dtype):
self.assertEqual(pack(data), data.astype(f">i{width}").tobytes(order="C"))
np.testing.assert_array_equal(data, before)
def test_signed_limits_and_native_integer_widths(self):
for pack, width in [(pack_int2, 2), (pack_int4, 4)]:
low, high = -(1 << (8 * width - 1)), (1 << (8 * width - 1)) - 1
data = np.array([[low, -1], [0, high]], dtype=np.int64)
self.assertEqual(pack(data), data.astype(f">i{width}").tobytes())
for dtype in [np.int8, np.uint8, np.int16, np.uint16]:
data = np.array([[1, 2], [3, 4]], dtype=dtype)
self.assertEqual(pack_int2(data), data.astype(">i2").tobytes())
self.assertEqual(pack_int4(data), data.astype(">i4").tobytes())
def test_failed_conversions_preserve_caller_data(self):
for pack, width in [(pack_int2, 2), (pack_int4, 4)]:
limit = 1 << (8 * width - 1)
for invalid in [limit, -limit - 1]:
data = np.array([[1, 2], [3, invalid]], dtype=np.int64)
before = data.copy()
with self.subTest(width=width, invalid=invalid):
with self.assertRaises(ValueError):
pack(data)
np.testing.assert_array_equal(data, before)
for dtype in [np.float32, np.float64]:
for invalid in [np.nan, np.inf, -np.inf]:
data = np.array([[1, 2], [3, invalid]], dtype=dtype)
before = data.copy()
with self.subTest(width=width, invalid=invalid, dtype=dtype):
with self.assertRaises(ValueError):
pack(data)
np.testing.assert_array_equal(data, before)
if __name__ == "__main__":
unittest.main()