klamath/klamath/test_elements.py
2026-03-09 00:15:52 -07:00

110 lines
3.3 KiB
Python

import io
import numpy
from numpy.testing import assert_array_equal
from klamath.elements import Boundary, Path, Text, Reference, Box, Node
def test_boundary_roundtrip() -> None:
xy = numpy.array([[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], dtype=numpy.int32)
b = Boundary(layer=(4, 5), xy=xy, properties={1: b'prop1'})
stream = io.BytesIO()
b.write(stream)
stream.seek(0)
b2 = Boundary.read(stream)
assert b2.layer == b.layer
assert_array_equal(b2.xy, b.xy)
assert b2.properties == b.properties
def test_path_roundtrip() -> None:
xy = numpy.array([[0, 0], [100, 0], [100, 100]], dtype=numpy.int32)
p = Path(layer=(10, 20), xy=xy, properties={2: b'pathprop'},
path_type=4, width=50, extension=(10, 20))
stream = io.BytesIO()
p.write(stream)
stream.seek(0)
p2 = Path.read(stream)
assert p2.layer == p.layer
assert_array_equal(p2.xy, p.xy)
assert p2.properties == p.properties
assert p2.path_type == p.path_type
assert p2.width == p.width
assert p2.extension == p.extension
def test_text_roundtrip() -> None:
xy = numpy.array([[50, 50]], dtype=numpy.int32)
t = Text(layer=(1, 1), xy=xy, string=b"HELLO WORLD", properties={},
presentation=5, path_type=0, width=0, invert_y=True,
mag=2.5, angle_deg=45.0)
stream = io.BytesIO()
t.write(stream)
stream.seek(0)
t2 = Text.read(stream)
assert t2.layer == t.layer
assert_array_equal(t2.xy, t.xy)
assert t2.string == t.string
assert t2.presentation == t.presentation
assert t2.invert_y == t.invert_y
assert t2.mag == t.mag
assert t2.angle_deg == t.angle_deg
def test_reference_sref_roundtrip() -> None:
xy = numpy.array([[100, 200]], dtype=numpy.int32)
r = Reference(struct_name=b"MY_CELL", xy=xy, colrow=None,
properties={5: b'sref'}, invert_y=False, mag=1.0, angle_deg=90.0)
stream = io.BytesIO()
r.write(stream)
stream.seek(0)
r2 = Reference.read(stream)
assert r2.struct_name == r.struct_name
assert_array_equal(r2.xy, r.xy)
assert r2.colrow is None
assert r2.properties == r.properties
assert r2.angle_deg == r.angle_deg
def test_reference_aref_roundtrip() -> None:
xy = numpy.array([[0, 0], [1000, 0], [0, 500]], dtype=numpy.int32)
colrow = (5, 2)
r = Reference(struct_name=b"ARRAY_CELL", xy=xy, colrow=colrow,
properties={}, invert_y=False, mag=1.0, angle_deg=0.0)
stream = io.BytesIO()
r.write(stream)
stream.seek(0)
r2 = Reference.read(stream)
assert r2.struct_name == r.struct_name
assert_array_equal(r2.xy, r.xy)
assert r2.colrow is not None
assert list(r2.colrow) == list(colrow)
assert r2.properties == r.properties
def test_box_roundtrip() -> None:
xy = numpy.array([[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], dtype=numpy.int32)
b = Box(layer=(30, 40), xy=xy, properties={})
stream = io.BytesIO()
b.write(stream)
stream.seek(0)
b2 = Box.read(stream)
assert b2.layer == b.layer
assert_array_equal(b2.xy, b.xy)
def test_node_roundtrip() -> None:
xy = numpy.array([[0, 0], [10, 10]], dtype=numpy.int32)
n = Node(layer=(50, 60), xy=xy, properties={})
stream = io.BytesIO()
n.write(stream)
stream.seek(0)
n2 = Node.read(stream)
assert n2.layer == n.layer
assert_array_equal(n2.xy, n.xy)