Files
2125_GCE/execute_compressed/tests/test_gaml.py
T

157 lines
5.5 KiB
Python
Raw Normal View History

2026-07-09 12:54:44 -04:00
"""
Tests for GAML — Glyph-Aligned Memory Layout.
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from execute_compressed.gaml import (
GlyphAlignedMemoryLayout,
MemoryRegion,
build_layout,
get_glyph_address,
PAGE_SIZE,
)
passed = 0
failed = 0
def test(name: str, ok: bool):
global passed, failed
if ok:
passed += 1
print(f" ✅ PASS: {name}")
else:
failed += 1
print(f" ❌ FAIL: {name}")
print("=" * 60)
print("GAML — Glyph-Aligned Memory Layout Tests")
print("=" * 60)
# Test 1: Build layout with single glyph
layout = GlyphAlignedMemoryLayout.build(["G001"])
test("Layout builds with G001", len(layout.regions) > 0)
test("Layout has glyph_map", "G001" in layout.glyph_map)
test("Layout total_size is 65536", layout.total_size == 65536)
# Test 2: G001 gets aether tier placement
g001_region = layout.get_region("G001")
test("G001 region exists", g001_region is not None)
test("G001 base is AETHER_NODE_BASE (0x0100)",
g001_region is not None and g001_region.base == 0x0100)
test("G001 has rx permissions",
g001_region is not None and g001_region.permissions == "rx")
# Test 3: Layout with standard glyphs
layout2 = GlyphAlignedMemoryLayout.build(["G015", "G042", "G100"])
test("Layout with standard glyphs", len(layout2.glyph_map) == 3)
test("Standard glyphs at >= STANDARD_BASE",
all(r.base >= 0x4000 for gid, r in layout2.glyph_map.items()))
# Test 4: Mixed tiers
layout3 = GlyphAlignedMemoryLayout.build(["G001", "G050", "G200"])
test("Mixed tier layout", "G001" in layout3.glyph_map)
test("G050 in layout", "G050" in layout3.glyph_map)
test("G200 in layout", "G200" in layout3.glyph_map)
g001 = layout3.get_region("G001")
g050 = layout3.get_region("G050")
g200 = layout3.get_region("G200")
test("G001 before G050",
g001 is not None and g050 is not None and g001.base < g050.base)
test("G050 before G200",
g050 is not None and g200 is not None and g050.base < g200.base)
# Test 5: get_offset
offset = layout3.get_offset("G001")
test("get_offset returns int for G001", isinstance(offset, int))
test("get_offset returns None for unknown", layout3.get_offset("G999") is None)
# Test 6: get_region_for_address
reserved = layout3.get_region_for_address(0x0050)
test("Address 0x0050 is in reserved region",
reserved is not None and reserved.glyph_id == "__reserved__")
g001_region_check = layout3.get_region_for_address(0x0100)
test("Address 0x0100 is in G001 region",
g001_region_check is not None and g001_region_check.glyph_id == "G001")
stack = layout3.get_region_for_address(0xF000)
test("Address 0xF000 is in stack region",
stack is not None and stack.glyph_id == "__stack__")
# Test 7: map_segments
segments_data = [
{"id": "seg_0", "glyph_id": "G001", "size": 512},
{"id": "seg_1", "glyph_id": "G050", "size": 256},
{"id": "seg_2", "glyph_id": "G200", "size": 128},
]
mappings = layout3.map_segments(segments_data)
test("map_segments returns all segments", len(mappings) == 3)
test("Segment seg_0 maps to G001 region",
mappings[0]["glyph_id"] == "G001" and mappings[0]["address"] == 0x0100)
test("Segment seg_1 maps to G050 region",
mappings[1]["glyph_id"] == "G050")
test("Segment addresses are in order",
mappings[0]["address"] < mappings[1]["address"] < mappings[2]["address"])
# Test 8: map_segments respects region bounds
segments_big = [
{"id": "seg_big", "glyph_id": "G001", "size": 100000},
]
mappings_big = layout3.map_segments(segments_big)
test("map_segments caps size to region max",
mappings_big[0]["size"] <= g001_region.size if g001_region else False)
# Test 9: Determinism — same input = same output
layout4a = GlyphAlignedMemoryLayout.build(["G001", "G015", "G042"])
layout4b = GlyphAlignedMemoryLayout.build(["G001", "G015", "G042"])
test("Deterministic layout: same region count",
len(layout4a.regions) == len(layout4b.regions))
test("Deterministic layout: same addresses",
all(
r1.base == r2.base and r1.size == r2.size
for r1, r2 in zip(layout4a.regions, layout4b.regions)
))
# Test 10: build_layout convenience function
layout5 = build_layout(["G001"])
test("build_layout returns GlyphAlignedMemoryLayout",
isinstance(layout5, GlyphAlignedMemoryLayout))
# Test 11: get_glyph_address convenience function
addr = get_glyph_address(layout5, "G001")
test("get_glyph_address returns int", isinstance(addr, int))
# Test 12: to_dict serialization
d = layout5.to_dict()
test("to_dict has total_size", d["total_size"] == 65536)
test("to_dict has region_count", d["region_count"] > 0)
test("to_dict has glyph_count", d["glyph_count"] > 0)
test("to_dict regions have hex base",
all(r["base"].startswith("0x") for r in d["regions"]))
# Test 13: With explicit glyph_data override
custom_data = {
"G001": {"name": "Ledo", "priority": 10, "band": "A", "score": 300,
"specialized_type": "aether_node"},
"G050": {"name": "TestGlyph", "priority": 5, "band": "B", "score": 150},
}
layout6 = GlyphAlignedMemoryLayout.build(["G001", "G050"], glyph_data=custom_data)
test("Custom glyph_data layout", "G001" in layout6.glyph_map)
test("G001 has large size from aether tier",
layout6.get_region("G001").size == PAGE_SIZE * 32)
# Summary
print()
print("=" * 60)
print(f"Results: {passed} passed, {failed} failed, {passed + failed} total")
if failed == 0:
print("✅ ALL GAML TESTS PASSED")
else:
print(f"❌ {failed} TEST(S) FAILED")
sys.exit(0 if failed == 0 else 1)