Files
TransPyC/Test/MultiInheritTest/App/main.vp
T
TermiNexus f6f8c4183c test: 新增 MultiInheritTest 多继承专项测试 + wiki 方案
4 组用例: 字段合并 C(A,B) / 菱形去重最左优先(Diamond) /
虚方法 vtable 多继承(ColoredCircle) / 运算符重载 MRO 链(Vec2Both)。
wiki/15-multiple-inheritance.md: Viper 多继承方案(M1-M6)设计文档。
2026-08-10 00:29:13 +08:00

200 lines
5.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from stdint import *
import w32.win32console
import t, c
from t import CInt, CPtr, CChar, CInt32T, CFloat64T, CExport, State
import viperlib
import stdlib
import testcheck
# === 1. 多继承字段合并 ===
class A:
a: CInt
def __init__(self, a: CInt):
self.a = a
def get_a(self) -> CInt:
return self.a
class B:
b: CInt
def __init__(self, b: CInt):
self.b = b
def get_b(self) -> CInt:
return self.b
# 多继承:C(A, B),字段展平 a+b+c,布局 [a, b, c]
class C(A, B):
c: CInt
def __init__(self, a: CInt, b: CInt, cc: CInt):
self.a = a
self.b = b
self.c = cc
def get_c(self) -> CInt:
return self.c
# === 2. 菱形去重 + 最左优先 ===
class Base:
shared: CInt
def who(self) -> CInt:
return 1
def base_method(self) -> CInt:
return 100
class Left(Base):
def who(self) -> CInt:
return 2
class Right(Base):
def who(self) -> CInt:
return 3
# 菱形:Diamond(Left, Right),Left/Right 共享 Base,
# Base.shared 字段只展平一次,who() 用 Left(先声明优先)
class Diamond(Left, Right):
def __init__(self, s: CInt):
self.shared = s
# === 3. 虚方法多继承 vtable ===
@t.CVTable
class ShapeM:
def area(self) -> CFloat64T:
return 0.0
@t.CVTable
class ColorM:
color: CInt
def __init__(self, color: CInt):
self.color = color
def color_code(self) -> CInt:
return self.color
@t.CVTable
class ColoredCircle(ShapeM, ColorM):
r: CFloat64T
def __init__(self, r: CFloat64T, color: CInt):
self.color = color
self.r = r
def area(self) -> CFloat64T:
return 3.14159265 * self.r * self.r
# === 4. 运算符重载 + 方法继承链(MRO) ===
class Vec2M:
x: CFloat64T
y: CFloat64T
def __new__() -> 'Vec2M' | CPtr:
return t.CVoid(t.CUInt64T(stdlib.malloc(16)), CPtr)
def __init__(self, x: CFloat64T, y: CFloat64T):
self.x = x
self.y = y
def __add__(self, b: 'Vec2M' | CPtr) -> 'Vec2M' | CPtr:
return Vec2M(self.x + b.x, self.y + b.y)
class Vec2Extra:
# 注意:方法体字段访问基于"定义类字段"(TPV 模型),
# 不引用 Vec2M 的 x/y(跨父类字段引用是更深层语义,暂不验证)。
def extra_val(self) -> CInt:
return 7
# 多继承:__add__ 继承自 Vec2M(运算符重载 MRO 链),
# extra_val 继承自 Vec2Extra(方法查找 MRO 链)
class Vec2Both(Vec2M, Vec2Extra):
def __new__() -> 'Vec2Both' | CPtr:
return t.CVoid(t.CUInt64T(stdlib.malloc(16)), CPtr)
def __init__(self, x: CFloat64T, y: CFloat64T):
self.x = x
self.y = y
def main() -> CInt | CExport:
w32.win32console.SetConsoleCP(65001)
w32.win32console.SetConsoleOutputCP(65001)
testcheck.begin("MultiInheritTest: 多继承专项测试")
buf: CChar | CPtr = CPtr(stdlib.malloc(256))
# === 1. 字段合并 + 属性访问 + 方法调用 ===
testcheck.section("多继承字段合并")
obj_c = C(1, 2, 3)
viperlib.snprintf(buf, 256, "C: a=%d b=%d c=%d", obj_c.a, obj_c.b, obj_c.c)
print(buf)
testcheck.check(obj_c.a == 1, "C.a 合并 OK", "C.a 合并 FAILED")
testcheck.check(obj_c.b == 2, "C.b 合并 OK", "C.b 合并 FAILED")
testcheck.check(obj_c.c == 3, "C.c 自身 OK", "C.c 自身 FAILED")
testcheck.check(obj_c.get_a() == 1, "C.get_a 继承 OK", "C.get_a 继承 FAILED")
testcheck.check(obj_c.get_b() == 2, "C.get_b 继承 OK", "C.get_b 继承 FAILED")
testcheck.check(obj_c.get_c() == 3, "C.get_c 自身 OK", "C.get_c 自身 FAILED")
testcheck.ok("字段合并测试完成")
# === 2. 菱形去重 + 最左优先 ===
testcheck.section("菱形去重 + 最左优先")
dm = Diamond(5)
viperlib.snprintf(buf, 256, "Diamond: shared=%d who=%d base=%d", dm.shared, dm.who(), dm.base_method())
print(buf)
testcheck.check(dm.shared == 5, "Diamond.shared 菱形去重 OK", "Diamond.shared 菱形去重 FAILED")
testcheck.check(dm.who() == 2, "who() 最左优先(Left) OK", "who() 最左优先 FAILED")
testcheck.check(dm.base_method() == 100, "Base 方法继承 OK", "Base 方法继承 FAILED")
testcheck.ok("菱形去重测试完成")
# === 3. 虚方法多继承 vtable ===
testcheck.section("虚方法多继承 vtable")
cc = ColoredCircle(3.0, 7)
area: CFloat64T = cc.area()
viperlib.snprintf(buf, 256, "ColoredCircle: r=%.1f color=%d area=%.2f code=%d",
cc.r, cc.color, area, cc.color_code())
print(buf)
testcheck.check(cc.color == 7, "ColorM.color 字段 OK", "ColorM.color 字段 FAILED")
testcheck.check(cc.color_code() == 7, "color_code 虚方法分派 OK", "color_code 虚方法分派 FAILED")
testcheck.check(area > 28.0 and area < 28.5, "area 虚方法覆盖 OK", "area 虚方法覆盖 FAILED")
testcheck.ok("虚方法多继承测试完成")
# === 4. 运算符重载 + 方法继承链 ===
testcheck.section("运算符重载 + 方法继承链")
v1 = Vec2Both(3.0, 4.0)
v2 = Vec2Both(1.0, 2.0)
v3 = v1 + v2
viperlib.snprintf(buf, 256, "Vec2Both add: (%.1f, %.1f)", v3.x, v3.y)
print(buf)
testcheck.check(v3.x == 4.0, "Vec2Both __add__ 继承 OK", "Vec2Both __add__ 继承 FAILED")
testcheck.check(v3.y == 6.0, "Vec2Both __add__ y OK", "Vec2Both __add__ y FAILED")
lsq: CInt = v1.extra_val()
viperlib.snprintf(buf, 256, "Vec2Both extra_val: %d", lsq)
print(buf)
testcheck.check(lsq == 7, "extra_val 继承(Vec2Extra) OK", "extra_val 继承(Vec2Extra) FAILED")
testcheck.ok("运算符重载继承链测试完成")
stdlib.free(buf)
return testcheck.end()