Files
Viper_RegressionTesting/MultiInheritTest/App/main.vp
T
TermiNexus 3c6fb1d246 chore(test): 初始化回归测试仓(新增 .gitignore,TestsOverview.md 改为 README.md)
把 Test/ 纳入 https://git.gvsds.com/GVSDS/Viper_RegressionTesting 。

1) 新增 .gitignore —— 只保留测试源码,剔除全部构建产物:
   目录   **/output*/(output / output_opt / output_orig / output_verify / output_dbg)、
          **/temp*/(temp / temp_opt)、**/.tpv_build/
   二进制 *.exe *.obj *.o *.out *.a *.lib *.dll
   中间物 *.ll *.vpi *.pyi *.deps.txt *.deps.json *.doc.json *_sha1_map.txt
          _pyi_cache_v1.pkl _pyi_cache_v1.sig.json _shared_sym.json _phase1_manifest.json
   其它   build_all.txt(构建日志)、*.log、__pycache__/、*.bak、IDE 目录
   保留   App/*.vp、project.json / project_opt.json / project.vpj、README.md、两个 .py
   实测 `git add -A --dry-run`:196 个文件,无任何构建产物混入。

2) TestsOverview.md → README.md(作为仓库首页文档)。

3) SequentialRegressionTest.py:7 处 TestsOverview.md 引用同步改为 README.md
   (文档串、OVERVIEW 路径、read_test_order 文档与报错文案、argparse description、
   启动横幅);同时修正入口路径 PROJECTRANS —— 旧值 <root>/Projectrans.py 已不存在,
   改为 <root>/TransPyC/Projectrans.py(与 regression_test.py:45-48 记录一致),
   否则本脚本会以 "can't open file" 全量失败。
   regression_test.py 无需改动:它按目录扫描 project.json 找项目,不引用该文档。
2026-09-29 15:14:59 +08:00

200 lines
5.6 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 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:
# 注意:方法体字段访问基于"定义类字段"(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 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. 字段合并 + 属性访问 + 方法调用 ===
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()