105 lines
3.1 KiB
Plaintext
105 lines
3.1 KiB
Plaintext
from stdint import *
|
|
import testcheck
|
|
|
|
|
|
# ===== super() semantics test =====
|
|
# Covers super().__init__ chaining, static method binding, MRO lookup, 3-level chains.
|
|
|
|
|
|
# ========== Single inheritance: super().__init__ + super().method ==========
|
|
|
|
virtual class Animal:
|
|
Tag: int
|
|
|
|
def __init__(self, tag: int):
|
|
self.Tag = tag
|
|
|
|
def Speak(self) -> int:
|
|
return 0
|
|
|
|
def Greet(self) -> int:
|
|
return self.Tag + 1000
|
|
|
|
|
|
virtual class Dog(Animal):
|
|
Breed: int
|
|
|
|
def __init__(self, tag: int, breed: int):
|
|
super().__init__(tag) # Call Animal.__init__ to set Tag
|
|
self.Breed = breed
|
|
|
|
def Speak(self) -> int:
|
|
return 1
|
|
|
|
def ParentSpeak(self) -> int:
|
|
return super().Speak() # Statically bind Animal.Speak -> 0 (no virtual dispatch)
|
|
|
|
def FullGreet(self) -> int:
|
|
# Call parent Greet (not overridden by Dog), then add own field
|
|
return super().Greet() + self.Breed
|
|
|
|
|
|
# ========== Three-level chain: Grand -> Mid -> Leaf ==========
|
|
|
|
virtual class Grand:
|
|
G: int
|
|
|
|
def __init__(self, v: int):
|
|
self.G = v
|
|
|
|
def Base(self) -> int:
|
|
return self.G
|
|
|
|
|
|
virtual class Mid(Grand):
|
|
M: int
|
|
|
|
def __init__(self, v: int):
|
|
super().__init__(v + 1) # Call Grand.__init__
|
|
self.M = v
|
|
|
|
def MidOnly(self) -> int:
|
|
return self.M
|
|
|
|
|
|
virtual class Leaf(Mid):
|
|
def __init__(self, v: int):
|
|
super().__init__(v + 2) # Call Mid.__init__ (which supers to Grand)
|
|
|
|
def GetBase(self) -> int:
|
|
return super().Base() # Mid has no Base -> find Grand.Base along MRO
|
|
|
|
def CallMidOnly(self) -> int:
|
|
return super().MidOnly() # Call Mid.MidOnly
|
|
|
|
|
|
export def main() -> int:
|
|
testcheck.begin("SuperTest: super() 语义测试")
|
|
|
|
# --- 1. super().__init__ sets parent fields ---
|
|
d: Dog* = 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* = 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()
|