From a94117982698b8fc911421b94f5e71afc0ec1eaf Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Mon, 10 Aug 2026 21:48:41 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=96=B0=E5=A2=9E=20SuperTest=20?= =?UTF-8?q?=E4=B8=93=E9=A1=B9=E6=B5=8B=E8=AF=95=20+=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=20TpvIsinstanceTest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Test/SuperTest:super() 语义 9 断言(构造链/静态绑定/三层继承/MRO) - TpvIsinstanceTest 移除(isinstance 回归统一用 IsinstanceTest) --- Test/SuperTest/App/main.vp | 120 +++++++++++++++++ .../project.json | 4 +- Test/TpvIsinstanceTest/App/main.vp | 125 ------------------ 3 files changed, 122 insertions(+), 127 deletions(-) create mode 100644 Test/SuperTest/App/main.vp rename Test/{TpvIsinstanceTest => SuperTest}/project.json (92%) delete mode 100644 Test/TpvIsinstanceTest/App/main.vp diff --git a/Test/SuperTest/App/main.vp b/Test/SuperTest/App/main.vp new file mode 100644 index 0000000..8c746e9 --- /dev/null +++ b/Test/SuperTest/App/main.vp @@ -0,0 +1,120 @@ +from stdint import * +import t, c +from t import CPtr, CInt, CExport +import testcheck + + +# ============================================================ +# super() 语义专项测试(由 TransPyV.exe 编译) +# +# 覆盖场景: +# 1. super().__init__(args) — 子类构造调父类构造(字段由父类设置) +# 2. super().method() 静态绑定父类实现(不触发子类 override / 虚分派) +# 3. super() 调未覆盖的父类方法(父类字段区前缀对齐,this 无需调整) +# 4. 三层继承链 super().__init__(Leaf → Mid → Grand 逐层传递, +# 每个中间类的 super() 沿各自 CurClassName 的 MRO 跳自身找父类) +# 5. super().method 沿 MRO 找实际定义类(Mid 未定义 Base → 找到 Grand.Base) +# ============================================================ + + +# ========== 单继承: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) # 调 Animal.__init__ 设置 Tag + self.Breed = breed + + def Speak(self) -> CInt: + return 1 + + def ParentSpeak(self) -> CInt: + return super().Speak() # 静态绑定 Animal.Speak → 0(不走虚分派) + + def FullGreet(self) -> CInt: + # super() 调父类 Greet(Dog 未覆盖),再加自身字段 + return super().Greet() + self.Breed + + +# ========== 三层继承链: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) # 调 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) # 调 Mid.__init__(Mid 内部再 super 到 Grand) + + def GetBase(self) -> CInt: + return super().Base() # Mid 未定义 Base → 沿 MRO 找 Grand.Base + + def CallMidOnly(self) -> CInt: + return super().MidOnly() # 调 Mid.MidOnly + + +def main() -> CInt | CExport: + testcheck.begin("SuperTest: super() 语义测试") + + # --- 1. super().__init__ 设置父类字段 --- + 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 静态绑定(不虚分派到子类 override) --- + testcheck.check(d.ParentSpeak() == 0, "super().Speak() 静态绑定父类 OK", + "super().Speak() FAILED") + testcheck.check(d.Speak() == 1, "子类 Speak() override OK", "Speak() FAILED") + + # --- 3. super() 调未覆盖的父类方法(42+1000+7 = 1049) --- + testcheck.check(d.FullGreet() == 1049, "super() 调未覆盖父类方法 OK", + "FullGreet() FAILED") + + # --- 4. 三层继承链 super().__init__(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 沿 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() diff --git a/Test/TpvIsinstanceTest/project.json b/Test/SuperTest/project.json similarity index 92% rename from Test/TpvIsinstanceTest/project.json rename to Test/SuperTest/project.json index 70b06ad..ff2832d 100644 --- a/Test/TpvIsinstanceTest/project.json +++ b/Test/SuperTest/project.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/TermiNexus/TransPyC/main/schemas/project-schema.json", - "name": "TpvIsinstanceTest", + "name": "SuperTest", "version": "1.0.0", "source_dir": "./App", "build_dir": "./.tpv_build", @@ -11,7 +11,7 @@ "linker": { "cmd": "clang++", "flags": ["-Wl,--allow-multiple-definition", "-Wl,--unresolved-symbols=ignore-in-object-files", "-lmsvcrt", "-lucrt", "-lpthread", "-lmingwex", "-lkernel32", "-luser32", "-latomic"], - "output": "TpvIsinstanceTest.exe" + "output": "SuperTest.exe" }, "includes": [ "../../includes" diff --git a/Test/TpvIsinstanceTest/App/main.vp b/Test/TpvIsinstanceTest/App/main.vp deleted file mode 100644 index 8de2865..0000000 --- a/Test/TpvIsinstanceTest/App/main.vp +++ /dev/null @@ -1,125 +0,0 @@ -from stdint import * -import t, c -from t import CPtr, CInt, CExport -import testcheck - - -# ============================================================ -# TPV 侧 isinstance 内建函数测试(由 TransPyV.exe 编译) -# -# 验证 TPV 编译器生成的 isinstance IR: -# 1. CVTable 自身 / 子类→基类 -# 2. 运行时多态(基类指针指向子类对象) -# 3. 元组 (A, B) -# 4. 多继承 -# 5. NoVTable 编译时静态判断 -# ============================================================ - - -# ========== CVTable 继承 ========== - -@t.CVTable -class Animal: - Name: str - - def __init__(self, name: str): - self.Name = name - - def speak(self) -> str: - return "..." - - -@t.CVTable -class Dog(Animal): - def speak(self) -> str: - return "Woof" - - -@t.CVTable -class Cat(Animal): - def speak(self) -> str: - return "Meow" - - -# ========== 多继承 ========== - -@t.CVTable -class A: - a: CInt - - def get_a(self) -> CInt: - return self.a - - -@t.CVTable -class B: - b: CInt - - def get_b(self) -> CInt: - return self.b - - -@t.CVTable -class AB(A, B): - def __init__(self, a: CInt, b: CInt): - self.a = a - self.b = b - - -# ========== NoVTable(编译时静态判断) ========== - -@t.NoVTable -class Plain: - v: CInt - - -def main() -> CInt | CExport: - testcheck.begin("TpvIsinstanceTest: TPV 侧 isinstance 测试") - - # --- 1. 自身类型 + 子类→基类 --- - d: Dog | CPtr = Dog("dog") - testcheck.check(isinstance(d, Dog) == 1, "isinstance(d, Dog) 自身 OK", - "isinstance(d, Dog) FAILED") - testcheck.check(isinstance(d, Animal) == 1, "isinstance(d, Animal) 子类→基类 OK", - "isinstance(d, Animal) FAILED") - testcheck.check(isinstance(d, Cat) == 0, "isinstance(d, Cat) 不匹配 OK", - "isinstance(d, Cat) FAILED") - - # --- 2. 运行时多态:基类指针指向子类对象 --- - a: Animal | CPtr = (Animal | CPtr)(d) - testcheck.check(isinstance(a, Dog) == 1, - "isinstance(Animal*, Dog 对象) 运行时多态 OK", - "isinstance(Animal*, Dog) FAILED") - testcheck.check(isinstance(a, Animal) == 1, - "isinstance(Animal*, Animal) 基类自身 OK", - "isinstance(Animal*, Animal) FAILED") - testcheck.check(isinstance(a, Cat) == 0, - "isinstance(Animal*, Cat) 运行时 False OK", - "isinstance(Animal*, Cat) FAILED") - - # --- 3. 元组 --- - testcheck.check(isinstance(d, (Cat, Dog)) == 1, "isinstance 元组命中 OK", - "isinstance 元组 FAILED") - testcheck.check(isinstance(d, (Cat, A)) == 0, "isinstance 元组未命中 OK", - "isinstance 元组未命中 FAILED") - - # --- 4. 多继承 --- - ab: AB | CPtr = AB(1, 2) - testcheck.check(isinstance(ab, A) == 1, "isinstance(AB, A) 多继承 OK", - "isinstance(AB, A) FAILED") - testcheck.check(isinstance(ab, B) == 1, "isinstance(AB, B) 多继承 OK", - "isinstance(AB, B) FAILED") - testcheck.check(isinstance(ab, Animal) == 0, "isinstance(AB, Animal) False OK", - "isinstance(AB, Animal) FAILED") - testcheck.check(ab.get_a() == 1 and ab.get_b() == 2, "AB 字段/方法正常 OK", - "AB 字段/方法 FAILED") - - # --- 5. NoVTable(编译时静态判断) --- - p: Plain | CPtr = Plain() - p.v = 42 - testcheck.check(isinstance(p, Plain) == 1, "isinstance NoVTable 编译时 OK", - "isinstance NoVTable FAILED") - testcheck.check(p.v == 42, "NoVTable 字段正常 OK", "NoVTable 字段 FAILED") - - testcheck.ok("TPV 侧 isinstance 全部测试完成") - return testcheck.end()