Initial commit: 2125_GCE project
This commit is contained in:
Executable
Executable
+156
@@ -0,0 +1,156 @@
|
||||
"""
|
||||
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)
|
||||
Executable
+155
@@ -0,0 +1,155 @@
|
||||
"""
|
||||
Tests for SEE — Symbolic Execution Envelope.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from execute_compressed.see import (
|
||||
SymbolicExecutionEnvelope,
|
||||
wrap_code,
|
||||
unwrap_envelope,
|
||||
execute_with_envelope,
|
||||
)
|
||||
|
||||
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("SEE — Symbolic Execution Envelope Tests")
|
||||
print("=" * 60)
|
||||
|
||||
# Test 1: Build envelope
|
||||
code = b"print('hello world')"
|
||||
envelope = SymbolicExecutionEnvelope.build(code=code, glyph_ids=["G001"])
|
||||
test("Build envelope", envelope.code == code)
|
||||
test("Envelope has glyph_ids", envelope.glyph_ids == ["G001"])
|
||||
test("Envelope has integrity_hash", len(envelope.integrity_hash) == 32)
|
||||
test("Envelope has invocation_id", len(envelope.invocation_id) > 0)
|
||||
test("Envelope built_at > 0", envelope.built_at > 0)
|
||||
|
||||
# Test 2: Default values
|
||||
env2 = SymbolicExecutionEnvelope.build(code=b"test")
|
||||
test("Default glyph_ids is empty list", env2.glyph_ids == [])
|
||||
test("Default manifest is empty dict", env2.manifest == {})
|
||||
test("Default mode is symbolic", env2.mode == "symbolic")
|
||||
|
||||
# Test 3: Integrity verification
|
||||
test("Integrity passes for unmodified envelope", envelope.verify_integrity())
|
||||
env2.code = b"tampered"
|
||||
test("Integrity fails for tampered envelope", not env2.verify_integrity())
|
||||
|
||||
# Test 4: Resonance map from glyph_context top-level keys
|
||||
env3 = SymbolicExecutionEnvelope.build(
|
||||
code=b"test",
|
||||
glyph_ids=["G001"],
|
||||
glyph_context={"G001": {"resonance_weight": 0.85}},
|
||||
)
|
||||
test("Resonance map has G001", "G001" in env3.resonance_map)
|
||||
test("Resonance weight from top-level context", abs(env3.resonance_map["G001"] - 0.85) < 0.001)
|
||||
|
||||
# Test 4b: Resonance map from nested glyphs key
|
||||
env3b = SymbolicExecutionEnvelope.build(
|
||||
code=b"test",
|
||||
glyph_ids=["G001"],
|
||||
glyph_context={"glyphs": {"G001": {"resonance_weight": 0.75}}},
|
||||
)
|
||||
test("Resonance map from nested glyphs", "G001" in env3b.resonance_map)
|
||||
test("Resonance weight from nested", abs(env3b.resonance_map["G001"] - 0.75) < 0.001)
|
||||
|
||||
# Test 5: wrap_code convenience
|
||||
env4 = wrap_code(b"hello", glyph_ids=["G015", "G042"])
|
||||
test("wrap_code returns SymbolicExecutionEnvelope", isinstance(env4, SymbolicExecutionEnvelope))
|
||||
test("wrap_code preserves code", env4.code == b"hello")
|
||||
test("wrap_code preserves glyph_ids", env4.glyph_ids == ["G015", "G042"])
|
||||
|
||||
# Test 6: unwrap_envelope
|
||||
code_out, context_out, glyph_ids_out = unwrap_envelope(envelope)
|
||||
test("unwrap returns code bytes", code_out == b"print('hello world')")
|
||||
test("unwrap returns glyph_ids", glyph_ids_out == ["G001"])
|
||||
test("unwrap context has mode", context_out["mode"] == "symbolic")
|
||||
test("unwrap context has resonance_map", "resonance_map" in context_out)
|
||||
|
||||
# Test 7: resolve_glyph_context
|
||||
test("resolve_glyph_context returns None when no context set",
|
||||
envelope.resolve_glyph_context("G001") is None)
|
||||
test("resolve_glyph_context returns None for unknown",
|
||||
envelope.resolve_glyph_context("G999") is None)
|
||||
# Test with explicit glyph context
|
||||
ctx_with_data = env3.resolve_glyph_context("G001")
|
||||
test("resolve_glyph_context with glyph_context data",
|
||||
ctx_with_data is not None and ctx_with_data["glyph_id"] == "G001")
|
||||
|
||||
# Test 8: Glyph context from nested structure
|
||||
env5 = SymbolicExecutionEnvelope.build(
|
||||
code=b"test",
|
||||
glyph_ids=["G001"],
|
||||
glyph_context={
|
||||
"glyphs": {
|
||||
"G001": {"resonance_weight": 0.9, "name": "Ledo"},
|
||||
}
|
||||
},
|
||||
)
|
||||
resolved = env5.resolve_glyph_context("G001")
|
||||
test("resolve_glyph_context works with nested glyphs",
|
||||
resolved is not None and resolved["glyph_id"] == "G001")
|
||||
|
||||
# Test 9: to_dict serialization
|
||||
d = envelope.to_dict()
|
||||
test("to_dict has code_size", d["code_size"] == len(code))
|
||||
test("to_dict has glyph_ids", d["glyph_ids"] == ["G001"])
|
||||
test("to_dict has integrity_hash", d["integrity_hash"] == envelope.integrity_hash)
|
||||
|
||||
# Test 10: execute_with_envelope - integrity failure
|
||||
tampered = SymbolicExecutionEnvelope(
|
||||
code=b"tampered",
|
||||
manifest={},
|
||||
glyph_context={},
|
||||
glyph_ids=[],
|
||||
resonance_map={},
|
||||
mode="symbolic",
|
||||
epoch=None,
|
||||
invocation_id="bad",
|
||||
chain_label=None,
|
||||
integrity_hash="00000000000000000000000000000000",
|
||||
built_at=0.0,
|
||||
metadata={},
|
||||
)
|
||||
result = execute_with_envelope(tampered)
|
||||
test("execute_with_envelope detects tampering",
|
||||
"integrity" in result.get("diagnostics", {}).get("error", ""))
|
||||
|
||||
# Test 11: execute_with_envelope with valid code
|
||||
try:
|
||||
env_valid = SymbolicExecutionEnvelope.build(
|
||||
code=b"Hello from SEE envelope test",
|
||||
glyph_ids=["G001"],
|
||||
metadata={"invocation_id": "test-001"},
|
||||
)
|
||||
result = execute_with_envelope(env_valid)
|
||||
has_output = bool(result.get("output_text"))
|
||||
test("execute_with_envelope returns output", has_output)
|
||||
except Exception as e:
|
||||
test(f"execute_with_envelope did not crash ({e})", False)
|
||||
|
||||
# Summary
|
||||
print()
|
||||
print("=" * 60)
|
||||
print(f"Results: {passed} passed, {failed} failed, {passed + failed} total")
|
||||
if failed == 0:
|
||||
print("✅ ALL SEE TESTS PASSED")
|
||||
else:
|
||||
print(f"❌ {failed} TEST(S) FAILED")
|
||||
sys.exit(0 if failed == 0 else 1)
|
||||
Reference in New Issue
Block a user