67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
import t, c
|
||
import stdio
|
||
import circ_a
|
||
import circ_b
|
||
|
||
|
||
# ============================================================
|
||
# 循环引用测试入口
|
||
#
|
||
# 验证 A → B → A 的循环引用能正确编译和运行:
|
||
# - Test 1: ClassA.MakeB() 创建 ClassB 实例
|
||
# - Test 2: ClassB.MakeA() 创建 ClassA 实例
|
||
# - Test 3: 值传递链 A(20) → B → A
|
||
# ============================================================
|
||
|
||
|
||
def circ_test() -> int:
|
||
stdio.printf("circ: === Test Start ===\n")
|
||
|
||
# ============================================================
|
||
# Test 1: 创建 ClassA,调用 MakeB 获取 ClassB 实例
|
||
# ============================================================
|
||
stdio.printf("circ: === Test 1: A.MakeB() ===\n")
|
||
|
||
a: circ_a.ClassA | t.CPtr = circ_a.ClassA(10)
|
||
av: int = a.GetValue()
|
||
stdio.printf("circ: a.GetValue()=%d (expected 10)\n", av)
|
||
if av != 10:
|
||
stdio.printf("[FAIL] a.GetValue()=%d expected 10\n", av)
|
||
return 1
|
||
|
||
b: circ_b.ClassB | t.CPtr = a.MakeB()
|
||
bv: int = b.GetValue()
|
||
stdio.printf("circ: b.GetValue()=%d (expected 10)\n", bv)
|
||
if bv != 10:
|
||
stdio.printf("[FAIL] b.GetValue()=%d expected 10\n", bv)
|
||
return 1
|
||
|
||
# ============================================================
|
||
# Test 2: 从 B 调用 MakeA 获取 ClassA 实例
|
||
# ============================================================
|
||
stdio.printf("circ: === Test 2: B.MakeA() ===\n")
|
||
|
||
a2: circ_a.ClassA | t.CPtr = b.MakeA()
|
||
av2: int = a2.GetValue()
|
||
stdio.printf("circ: a2.GetValue()=%d (expected 10)\n", av2)
|
||
if av2 != 10:
|
||
stdio.printf("[FAIL] a2.GetValue()=%d expected 10\n", av2)
|
||
return 1
|
||
|
||
# ============================================================
|
||
# Test 3: 值传递链 A(20) → B → A
|
||
# ============================================================
|
||
stdio.printf("circ: === Test 3: value chain A(20)->B->A ===\n")
|
||
|
||
a3: circ_a.ClassA | t.CPtr = circ_a.ClassA(20)
|
||
b3: circ_b.ClassB | t.CPtr = a3.MakeB()
|
||
a4: circ_a.ClassA | t.CPtr = b3.MakeA()
|
||
final: int = a4.GetValue()
|
||
stdio.printf("circ: final=%d (expected 20)\n", final)
|
||
if final != 20:
|
||
stdio.printf("[FAIL] final=%d expected 20\n", final)
|
||
return 1
|
||
|
||
stdio.printf("circ: === All Tests Passed ===\n")
|
||
return 0
|