snapshot before regression test
This commit is contained in:
119
TransPyV/Test/App/new_test.py
Normal file
119
TransPyV/Test/App/new_test.py
Normal file
@@ -0,0 +1,119 @@
|
||||
import stdio
|
||||
import t, c
|
||||
|
||||
|
||||
# ============================================================
|
||||
# __new__ 函数测试:验证 __new__ 在创建 OOP 结构体前被调用,
|
||||
# 返回的指针作为结构体存储空间。
|
||||
#
|
||||
# __new__ 签名默认和 __init__ 一样(self + args),返回 Ptr(struct_ty)。
|
||||
# 如果 __new__ 返回 self,则使用默认 alloca 作为存储空间。
|
||||
# ============================================================
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Test 1: __new__ 返回 self(默认 alloca)
|
||||
# 验证 __new__ 被调用且不破坏 __init__ 正常功能
|
||||
# ============================================================
|
||||
class WithNew:
|
||||
value: t.CInt
|
||||
|
||||
def __new__(self, v: t.CInt):
|
||||
stdio.printf("new: WithNew.__new__ called\n")
|
||||
return self
|
||||
|
||||
def __init__(self, v: t.CInt):
|
||||
stdio.printf("new: WithNew.__init__ called\n")
|
||||
self.value = v
|
||||
|
||||
def GetValue(self) -> t.CInt:
|
||||
return self.value
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Test 2: __new__ 不接受额外参数(仅 self)
|
||||
# 验证 __new__ 签名可以与 __init__ 不同
|
||||
# ============================================================
|
||||
class WithNewNoArgs:
|
||||
value: t.CInt
|
||||
|
||||
def __new__(self):
|
||||
stdio.printf("new: WithNewNoArgs.__new__ called\n")
|
||||
return self
|
||||
|
||||
def __init__(self, v: t.CInt):
|
||||
self.value = v
|
||||
|
||||
def GetValue(self) -> t.CInt:
|
||||
return self.value
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Test 3: __new__ + @t.CVTable 虚表类
|
||||
# 验证 __new__ 与虚表机制兼容(__new__ 不进入虚表)
|
||||
# ============================================================
|
||||
@t.CVTable
|
||||
class WithNewVTable:
|
||||
value: t.CInt
|
||||
|
||||
def __new__(self, v: t.CInt):
|
||||
stdio.printf("new: WithNewVTable.__new__ called\n")
|
||||
return self
|
||||
|
||||
def __init__(self, v: t.CInt):
|
||||
self.value = v
|
||||
|
||||
def GetValue(self) -> t.CInt:
|
||||
return self.value
|
||||
|
||||
def Speak(self) -> t.CInt:
|
||||
return self.value + 1
|
||||
|
||||
|
||||
def new_test() -> int:
|
||||
stdio.printf("new: === Test Start ===\n")
|
||||
|
||||
# ============================================================
|
||||
# Test 1: __new__ 返回 self
|
||||
# ============================================================
|
||||
stdio.printf("new: === Test 1: __new__ returns self ===\n")
|
||||
|
||||
w: WithNew = WithNew(42)
|
||||
v: int = w.GetValue()
|
||||
stdio.printf("new: w.GetValue()=%d (expected 42)\n", v)
|
||||
if v != 42:
|
||||
stdio.printf("[FAIL] w.GetValue()=%d expected 42\n", v)
|
||||
return 1
|
||||
|
||||
# ============================================================
|
||||
# Test 2: __new__ 不接受额外参数
|
||||
# ============================================================
|
||||
stdio.printf("new: === Test 2: __new__ no extra args ===\n")
|
||||
|
||||
n: WithNewNoArgs = WithNewNoArgs(99)
|
||||
nv: int = n.GetValue()
|
||||
stdio.printf("new: n.GetValue()=%d (expected 99)\n", nv)
|
||||
if nv != 99:
|
||||
stdio.printf("[FAIL] n.GetValue()=%d expected 99\n", nv)
|
||||
return 1
|
||||
|
||||
# ============================================================
|
||||
# Test 3: __new__ + 虚表类
|
||||
# ============================================================
|
||||
stdio.printf("new: === Test 3: __new__ + vtable ===\n")
|
||||
|
||||
vt: WithNewVTable = WithNewVTable(10)
|
||||
vtv: int = vt.GetValue()
|
||||
stdio.printf("new: vt.GetValue()=%d (expected 10)\n", vtv)
|
||||
if vtv != 10:
|
||||
stdio.printf("[FAIL] vt.GetValue()=%d expected 10\n", vtv)
|
||||
return 1
|
||||
|
||||
vts: int = vt.Speak()
|
||||
stdio.printf("new: vt.Speak()=%d (expected 11)\n", vts)
|
||||
if vts != 11:
|
||||
stdio.printf("[FAIL] vt.Speak()=%d expected 11\n", vts)
|
||||
return 1
|
||||
|
||||
stdio.printf("new: === All Tests Passed ===\n")
|
||||
return 0
|
||||
Reference in New Issue
Block a user