Files
2026-10-17 21:57:03 +08:00

195 lines
5.5 KiB
Plaintext

from stdint import *
import win32.win32console
import viperlib
import stdlib
import testcheck
# === 1. multiple inheritance field merging ===
class A:
a: int
def __init__(self, a: int):
self.a = a
def get_a(self) -> int:
return self.a
class B:
b: int
def __init__(self, b: int):
self.b = b
def get_b(self) -> int:
return self.b
# Multiple inheritance C(A, B): fields flatten to layout [a, b, c].
class C(A, B):
c: int
def __init__(self, a: int, b: int, cc: int):
self.a = a
self.b = b
self.c = cc
def get_c(self) -> int:
return self.c
# === 2. diamond dedup + leftmost priority ===
class Base:
shared: int
def who(self) -> int:
return 1
def base_method(self) -> int:
return 100
class Left(Base):
def who(self) -> int:
return 2
class Right(Base):
def who(self) -> int:
return 3
# Diamond(Left, Right) shares Base: Base.shared flattens once,
# and who() resolves to Left (leftmost priority).
class Diamond(Left, Right):
def __init__(self, s: int):
self.shared = s
# === 3. virtual method multiple-inheritance vtable ===
virtual class ShapeM:
def area(self) -> double:
return 0.0
virtual class ColorM:
color: int
def __init__(self, color: int):
self.color = color
def color_code(self) -> int:
return self.color
virtual class ColoredCircle(ShapeM, ColorM):
r: double
def __init__(self, r: double, color: int):
self.color = color
self.r = r
def area(self) -> double:
return 3.14159265 * self.r * self.r
# === 4. operator overloading + method inheritance chain (MRO) ===
class Vec2M:
x: double
y: double
def __new__() -> Vec2M*:
return stdlib.malloc(16)
def __init__(self, x: double, y: double):
self.x = x
self.y = y
def __add__(self, b: Vec2M*) -> Vec2M*:
return Vec2M(self.x + b.x, self.y + b.y)
class Vec2Extra:
# Field access uses the defining class's fields (VPC model); does not
# reference Vec2M's x/y (cross-parent field access is out of scope here).
def extra_val(self) -> int:
return 7
# Multiple inheritance: __add__ comes from Vec2M (operator-overload MRO),
# extra_val from Vec2Extra (method lookup MRO).
class Vec2Both(Vec2M, Vec2Extra):
def __new__() -> Vec2Both*:
return stdlib.malloc(16)
def __init__(self, x: double, y: double):
self.x = x
self.y = y
export def main() -> int:
win32.win32console.SetConsoleCP(65001)
win32.win32console.SetConsoleOutputCP(65001)
testcheck.begin("MultiInheritTest: 多继承专项测试")
buf: str = stdlib.malloc(256)
# === 1. field merge + attribute access + method calls ===
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. diamond dedup + leftmost priority ===
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. virtual method multiple-inheritance vtable ===
testcheck.section("虚方法多继承 vtable")
cc = ColoredCircle(3.0, 7)
area: double = 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. operator overloading + method inheritance chain ===
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: int = 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()