Files
Viper_RegressionTesting/SuperTest/App/main.vp
T
TermiNexus ee6461be46 chore(comments): Test 仓注释英文化(R11)
把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
2026-10-05 22:43:38 +08:00

112 lines
3.2 KiB
Plaintext

from stdint import *
import t, c
from t import CPtr, CInt, CExport
import testcheck
# ===== super() semantics test =====
# Covers super().__init__ chaining, static method binding, MRO lookup, 3-level chains.
# ========== Single inheritance: super().__init__ + super().method ==========
@t.CVTable
class Animal:
Tag: CInt
def __init__(self, tag: CInt):
self.Tag = tag
def Speak(self) -> CInt:
return 0
def Greet(self) -> CInt:
return self.Tag + 1000
@t.CVTable
class Dog(Animal):
Breed: CInt
def __init__(self, tag: CInt, breed: CInt):
super().__init__(tag) # Call Animal.__init__ to set Tag
self.Breed = breed
def Speak(self) -> CInt:
return 1
def ParentSpeak(self) -> CInt:
return super().Speak() # Statically bind Animal.Speak -> 0 (no virtual dispatch)
def FullGreet(self) -> CInt:
# Call parent Greet (not overridden by Dog), then add own field
return super().Greet() + self.Breed
# ========== Three-level chain: Grand -> Mid -> Leaf ==========
@t.CVTable
class Grand:
G: CInt
def __init__(self, v: CInt):
self.G = v
def Base(self) -> CInt:
return self.G
@t.CVTable
class Mid(Grand):
M: CInt
def __init__(self, v: CInt):
super().__init__(v + 1) # Call Grand.__init__
self.M = v
def MidOnly(self) -> CInt:
return self.M
@t.CVTable
class Leaf(Mid):
def __init__(self, v: CInt):
super().__init__(v + 2) # Call Mid.__init__ (which supers to Grand)
def GetBase(self) -> CInt:
return super().Base() # Mid has no Base -> find Grand.Base along MRO
def CallMidOnly(self) -> CInt:
return super().MidOnly() # Call Mid.MidOnly
def main() -> CInt | CExport:
testcheck.begin("SuperTest: super() 语义测试")
# --- 1. super().__init__ sets parent fields ---
d: Dog | CPtr = Dog(42, 7)
testcheck.check(d.Tag == 42, "super().__init__ 设置父类字段 OK",
"super().__init__ FAILED")
testcheck.check(d.Breed == 7, "子类字段 OK", "子类字段 FAILED")
# --- 2. super().method static binding (no dispatch to child override) ---
testcheck.check(d.ParentSpeak() == 0, "super().Speak() 静态绑定父类 OK",
"super().Speak() FAILED")
testcheck.check(d.Speak() == 1, "子类 Speak() override OK", "Speak() FAILED")
# --- 3. super() calls uncovered parent method (42+1000+7 = 1049) ---
testcheck.check(d.FullGreet() == 1049, "super() 调未覆盖父类方法 OK",
"FullGreet() FAILED")
# --- 4. three-level super().__init__ chain (Leaf(10) -> Mid(12) -> Grand(13)) ---
lf: Leaf | CPtr = Leaf(10)
testcheck.check(lf.G == 13, "三层 super().__init__ 链 G OK", "三层 G FAILED")
testcheck.check(lf.M == 12, "中间类 M OK", "中间类 M FAILED")
# --- 5. super().method finds the defining class along MRO ---
testcheck.check(lf.GetBase() == 13, "super().Base() 沿 MRO OK", "GetBase() FAILED")
testcheck.check(lf.CallMidOnly() == 12, "super().MidOnly() OK", "CallMidOnly() FAILED")
testcheck.ok("super() 全部测试完成")
return testcheck.end()