102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
import io
|
|
import numpy
|
|
from datetime import datetime
|
|
from klamath.library import FileHeader, write_struct, try_read_struct, scan_structs, scan_hierarchy, read_elements
|
|
from klamath.elements import Boundary
|
|
from klamath.records import ENDLIB
|
|
|
|
def test_file_header_roundtrip() -> None:
|
|
h = FileHeader(name=b"MY_LIB", user_units_per_db_unit=0.001, meters_per_db_unit=1e-9,
|
|
mod_time=datetime(2023, 1, 1, 0, 0, 0), acc_time=datetime(2023, 1, 1, 0, 0, 0))
|
|
|
|
stream = io.BytesIO()
|
|
h.write(stream)
|
|
stream.seek(0)
|
|
|
|
h2 = FileHeader.read(stream)
|
|
assert h2.name == h.name
|
|
assert h2.user_units_per_db_unit == h.user_units_per_db_unit
|
|
assert h2.meters_per_db_unit == h.meters_per_db_unit
|
|
assert h2.mod_time == h.mod_time
|
|
|
|
def test_write_read_struct() -> None:
|
|
xy = numpy.array([[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], dtype=numpy.int32)
|
|
b = Boundary(layer=(1, 1), xy=xy, properties={})
|
|
|
|
stream = io.BytesIO()
|
|
# Need a header for some operations, but write_struct works standalone
|
|
write_struct(stream, name=b"CELL_A", elements=[b])
|
|
ENDLIB.write(stream, None)
|
|
stream.seek(0)
|
|
|
|
res = try_read_struct(stream)
|
|
assert res is not None
|
|
name, elements = res
|
|
assert name == b"CELL_A"
|
|
assert len(elements) == 1
|
|
assert isinstance(elements[0], Boundary)
|
|
|
|
def test_scan_structs() -> None:
|
|
stream = io.BytesIO()
|
|
write_struct(stream, name=b"CELL_A", elements=[])
|
|
write_struct(stream, name=b"CELL_B", elements=[])
|
|
ENDLIB.write(stream, None)
|
|
stream.seek(0)
|
|
|
|
positions = scan_structs(stream)
|
|
assert b"CELL_A" in positions
|
|
assert b"CELL_B" in positions
|
|
|
|
# Verify we can seek and read
|
|
stream.seek(positions[b"CELL_B"])
|
|
elements = read_elements(stream)
|
|
assert len(elements) == 0
|
|
|
|
def test_scan_hierarchy() -> None:
|
|
from klamath.elements import Reference
|
|
|
|
stream = io.BytesIO()
|
|
# Struct A has 2 refs to Struct B
|
|
ref_b1 = Reference(struct_name=b"B", xy=numpy.array([[0, 0]], dtype=numpy.int32), colrow=None, properties={},
|
|
invert_y=False, mag=1.0, angle_deg=0.0)
|
|
ref_b2 = Reference(struct_name=b"B", xy=numpy.array([[10, 10]], dtype=numpy.int32), colrow=None, properties={},
|
|
invert_y=False, mag=1.0, angle_deg=0.0)
|
|
write_struct(stream, name=b"A", elements=[ref_b1, ref_b2])
|
|
|
|
# Struct B has a 3x2 AREF of Struct C
|
|
ref_c = Reference(struct_name=b"C", xy=numpy.array([[0, 0], [10, 0], [0, 10]], dtype=numpy.int32),
|
|
colrow=(3, 2), properties={}, invert_y=False, mag=1.0, angle_deg=0.0)
|
|
write_struct(stream, name=b"B", elements=[ref_c])
|
|
|
|
write_struct(stream, name=b"C", elements=[])
|
|
ENDLIB.write(stream, None)
|
|
stream.seek(0)
|
|
|
|
hierarchy = scan_hierarchy(stream)
|
|
assert hierarchy[b"A"] == {b"B": 2}
|
|
assert hierarchy[b"B"] == {b"C": 6}
|
|
assert hierarchy[b"C"] == {}
|
|
|
|
def test_scan_structs_duplicate() -> None:
|
|
import pytest
|
|
from klamath.basic import KlamathError
|
|
stream = io.BytesIO()
|
|
write_struct(stream, name=b"CELL_A", elements=[])
|
|
write_struct(stream, name=b"CELL_A", elements=[])
|
|
ENDLIB.write(stream, None)
|
|
stream.seek(0)
|
|
|
|
with pytest.raises(KlamathError, match="Duplicate structure name"):
|
|
scan_structs(stream)
|
|
|
|
def test_scan_hierarchy_duplicate() -> None:
|
|
import pytest
|
|
from klamath.basic import KlamathError
|
|
stream = io.BytesIO()
|
|
write_struct(stream, name=b"CELL_A", elements=[])
|
|
write_struct(stream, name=b"CELL_A", elements=[])
|
|
ENDLIB.write(stream, None)
|
|
stream.seek(0)
|
|
|
|
with pytest.raises(KlamathError, match="Duplicate structure name"):
|
|
scan_hierarchy(stream)
|