把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
200 lines
5.6 KiB
Plaintext
200 lines
5.6 KiB
Plaintext
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. multiple inheritance field merging ===
|
|
|
|
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
|
|
|
|
|
|
# Multiple inheritance C(A, B): fields flatten to layout [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. diamond dedup + leftmost priority ===
|
|
|
|
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) shares Base: Base.shared flattens once,
|
|
# and who() resolves to Left (leftmost priority).
|
|
class Diamond(Left, Right):
|
|
def __init__(self, s: CInt):
|
|
self.shared = s
|
|
|
|
|
|
# === 3. virtual method multiple-inheritance 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. operator overloading + method inheritance chain (MRO) ===
|
|
|
|
class Vec2M:
|
|
x: CFloat64T
|
|
y: CFloat64T
|
|
|
|
def __new__() -> 'Vec2M' | CPtr:
|
|
return stdlib.malloc(16)
|
|
|
|
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:
|
|
# Field access uses the defining class's fields (TPV model); does not
|
|
# reference Vec2M's x/y (cross-parent field access is out of scope here).
|
|
def extra_val(self) -> CInt:
|
|
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' | CPtr:
|
|
return stdlib.malloc(16)
|
|
|
|
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. 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: 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. 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: 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()
|