Files
2026-10-17 21:57:03 +08:00

151 lines
5.2 KiB
Plaintext

import stdio
import testcheck
import stdlib
from g4node import G4Node, new_g4node, g4node_get_next, g4node_set_next
import memhub
import caller1
import caller2
import caller3
# ============================================================
# G4: verify a class inheriting GSListNode[X] from includes keeps its
# inherited Next field (no missing field / type downgrade).
# caller1/caller2/caller3 reproduce a multi-module environment.
# ============================================================
def test_g4node_create():
testcheck.section("Test 1: G4Node create (from includes)")
arena: bytes = stdlib.malloc(4096)
if arena is None:
testcheck.check(False, "malloc arena", "FAIL")
return
pool: memhub.MemBuddy* = memhub.MemBuddy(arena, 4096)
n: G4Node* = new_g4node(pool)
if n is None:
testcheck.check(False, "new_g4node", "FAIL")
return
n.IntVal = 42
v: int = int(n.IntVal)
testcheck.check(v == 42, "G4Node.IntVal = 42", "expect 42")
def test_g4node_next_field():
testcheck.section("Test 2: Next field from GSListNode[G4Node]")
arena: bytes = stdlib.malloc(4096)
if arena is None:
testcheck.check(False, "malloc arena", "FAIL")
return
pool: memhub.MemBuddy* = memhub.MemBuddy(arena, 4096)
n1: G4Node* = new_g4node(pool)
n2: G4Node* = new_g4node(pool)
if n1 is None or n2 is None:
testcheck.check(False, "new_g4node", "FAIL")
return
n1.IntVal = 10
n2.IntVal = 20
g4node_set_next(n1, n2)
next_node: G4Node* = g4node_get_next(n1)
if next_node is None:
testcheck.check(False, "g4node_get_next", "FAIL")
return
v: int = int(next_node.IntVal)
testcheck.check(v == 20, "n1.Next.IntVal = 20", "expect 20")
def test_g4node_chain():
testcheck.section("Test 3: G4Node chain traversal")
arena: bytes = stdlib.malloc(4096)
if arena is None:
testcheck.check(False, "malloc arena", "FAIL")
return
pool: memhub.MemBuddy* = memhub.MemBuddy(arena, 4096)
a: G4Node* = new_g4node(pool)
b: G4Node* = new_g4node(pool)
c: G4Node* = new_g4node(pool)
if a is None or b is None or c is None:
testcheck.check(False, "new_g4node", "FAIL")
return
a.IntVal = 1
b.IntVal = 2
c.IntVal = 3
g4node_set_next(a, b)
g4node_set_next(b, c)
sum: int = int(a.IntVal)
p: G4Node* = g4node_get_next(a)
sum = sum + int(p.IntVal)
p = g4node_get_next(p)
sum = sum + int(p.IntVal)
testcheck.check(sum == 6, "chain sum = 1+2+3 = 6", "expect 6")
def test_caller1_direct_access():
"""caller1: access Next directly (no accessor)."""
testcheck.section("Test 4: caller1 direct Next access")
arena: bytes = stdlib.malloc(8192)
if arena is None:
testcheck.check(False, "malloc arena", "FAIL")
return
pool: memhub.MemBuddy* = memhub.MemBuddy(arena, 8192)
head: G4Node* = caller1.caller1_make_chain(pool)
if head is None:
testcheck.check(False, "caller1_make_chain", "FAIL")
return
total: int = caller1.caller1_sum_chain(head)
stdio.printf("caller1 chain total = %d\n", total)
testcheck.check(total == 600, "caller1 sum = 100+200+300 = 600", "expect 600")
def test_caller2_reverse():
"""caller2: reverse the chain, accessing Next directly."""
testcheck.section("Test 5: caller2 reverse chain")
arena: bytes = stdlib.malloc(8192)
if arena is None:
testcheck.check(False, "malloc arena", "FAIL")
return
pool: memhub.MemBuddy* = memhub.MemBuddy(arena, 8192)
head: G4Node* = caller1.caller1_make_chain(pool)
if head is None:
testcheck.check(False, "caller1_make_chain", "FAIL")
return
count_before: int = caller2.caller2_count_nodes(head)
reversed_head: G4Node* = caller2.caller2_reverse_chain(head)
count_after: int = caller2.caller2_count_nodes(reversed_head)
stdio.printf("caller2 count before=%d after=%d\n", count_before, count_after)
testcheck.check(count_before == 3 and count_after == 3,
"caller2 reverse count OK (3,3)", "caller2 count mismatch")
def test_caller3_append():
"""caller3: append a node, accessing Next directly."""
testcheck.section("Test 6: caller3 append node")
arena: bytes = stdlib.malloc(8192)
if arena is None:
testcheck.check(False, "malloc arena", "FAIL")
return
pool: memhub.MemBuddy* = memhub.MemBuddy(arena, 8192)
head: G4Node* = caller1.caller1_make_chain(pool)
if head is None:
testcheck.check(False, "caller1_make_chain", "FAIL")
return
extra: G4Node* = new_g4node(pool)
if extra is None:
testcheck.check(False, "new_g4node", "FAIL")
return
extra.IntVal = 400
head = caller3.caller3_append(head, extra)
total: int = caller1.caller1_sum_chain(head)
stdio.printf("caller3 after append total = %d\n", total)
testcheck.check(total == 1000, "caller3 append sum = 600+400 = 1000", "expect 1000")
def main() -> int:
testcheck.begin("G4: GSListNode inheritance from includes (Phase1 path)")
test_g4node_create()
test_g4node_next_field()
test_g4node_chain()
test_caller1_direct_access()
test_caller2_reverse()
test_caller3_append()
return testcheck.end()