117 lines
3.5 KiB
Python
117 lines
3.5 KiB
Python
import stdio
|
||
import t, c
|
||
|
||
|
||
# ============================================================
|
||
# 场景 A: @t.NoVTable 类 + 函数级 @t.CVTable
|
||
#
|
||
# NoVTableClass 标记 @t.NoVTable(类级别禁用虚表)
|
||
# 但 VirtualMethod 标记 @t.CVTable → 该方法单独进入虚表
|
||
# NormalMethod 无装饰器 → 不进入虚表
|
||
# 预期: 虚表 1 个方法 (VirtualMethod)
|
||
# ============================================================
|
||
@t.NoVTable
|
||
class NoVTableClass:
|
||
val: t.CInt
|
||
|
||
def __init__(self, v: t.CInt):
|
||
self.val = v
|
||
|
||
@t.CVTable
|
||
def VirtualMethod(self) -> t.CInt:
|
||
return self.val + 10
|
||
|
||
def NormalMethod(self) -> t.CInt:
|
||
return self.val + 20
|
||
|
||
|
||
# ============================================================
|
||
# 场景 B: @t.CVTable 类 + 函数级 @t.NoVTable
|
||
#
|
||
# CVTableClass 标记 @t.CVTable(所有方法进入虚表)
|
||
# KeptMethod 无装饰器 → 进入虚表
|
||
# ExcludedMethod 标记 @t.NoVTable → 排除出虚表
|
||
# 预期: 虚表 1 个方法 (KeptMethod)
|
||
# ============================================================
|
||
@t.CVTable
|
||
class CVTableClass:
|
||
val: t.CInt
|
||
|
||
def __init__(self, v: t.CInt):
|
||
self.val = v
|
||
|
||
def KeptMethod(self) -> t.CInt:
|
||
return self.val + 30
|
||
|
||
@t.NoVTable
|
||
def ExcludedMethod(self) -> t.CInt:
|
||
return self.val + 40
|
||
|
||
|
||
# ============================================================
|
||
# 场景 C: 默认类(无装饰器无继承)+ 函数级 @t.CVTable
|
||
#
|
||
# DefaultClass 无装饰器(默认无虚表)
|
||
# VirtualMethod 标记 @t.CVTable → 单独进入虚表
|
||
# NormalMethod 无装饰器 → 不进入虚表
|
||
# 预期: 虚表 1 个方法 (VirtualMethod)
|
||
# ============================================================
|
||
class DefaultClass:
|
||
val: t.CInt
|
||
|
||
def __init__(self, v: t.CInt):
|
||
self.val = v
|
||
|
||
@t.CVTable
|
||
def VirtualMethod(self) -> t.CInt:
|
||
return self.val + 50
|
||
|
||
def NormalMethod(self) -> t.CInt:
|
||
return self.val + 60
|
||
|
||
|
||
def func_vtable_test() -> int:
|
||
stdio.printf("funcvt: === Test Start ===\n")
|
||
|
||
# 场景 A: NoVTableClass
|
||
nvt: NoVTableClass = NoVTableClass(5)
|
||
a1: int = nvt.VirtualMethod()
|
||
stdio.printf("funcvt: A.VirtualMethod()=%d (expected 15)\n", a1)
|
||
if a1 != 15:
|
||
stdio.printf("[FAIL] A.VirtualMethod()=%d expected 15\n", a1)
|
||
return 1
|
||
a2: int = nvt.NormalMethod()
|
||
stdio.printf("funcvt: A.NormalMethod()=%d (expected 25)\n", a2)
|
||
if a2 != 25:
|
||
stdio.printf("[FAIL] A.NormalMethod()=%d expected 25\n", a2)
|
||
return 1
|
||
|
||
# 场景 B: CVTableClass
|
||
cvt: CVTableClass = CVTableClass(7)
|
||
b1: int = cvt.KeptMethod()
|
||
stdio.printf("funcvt: B.KeptMethod()=%d (expected 37)\n", b1)
|
||
if b1 != 37:
|
||
stdio.printf("[FAIL] B.KeptMethod()=%d expected 37\n", b1)
|
||
return 1
|
||
b2: int = cvt.ExcludedMethod()
|
||
stdio.printf("funcvt: B.ExcludedMethod()=%d (expected 47)\n", b2)
|
||
if b2 != 47:
|
||
stdio.printf("[FAIL] B.ExcludedMethod()=%d expected 47\n", b2)
|
||
return 1
|
||
|
||
# 场景 C: DefaultClass
|
||
dfc: DefaultClass = DefaultClass(9)
|
||
c1: int = dfc.VirtualMethod()
|
||
stdio.printf("funcvt: C.VirtualMethod()=%d (expected 59)\n", c1)
|
||
if c1 != 59:
|
||
stdio.printf("[FAIL] C.VirtualMethod()=%d expected 59\n", c1)
|
||
return 1
|
||
c2: int = dfc.NormalMethod()
|
||
stdio.printf("funcvt: C.NormalMethod()=%d (expected 69)\n", c2)
|
||
if c2 != 69:
|
||
stdio.printf("[FAIL] C.NormalMethod()=%d expected 69\n", c2)
|
||
return 1
|
||
|
||
stdio.printf("funcvt: === All Tests Passed ===\n")
|
||
return 0
|