136 lines
5.0 KiB
Plaintext
136 lines
5.0 KiB
Plaintext
from stdint import *
|
|
import testcheck
|
|
import geom_lib
|
|
|
|
|
|
# ============================================================
|
|
# isinstance builtin test: runtime vtable inheritance-chain comparison over
|
|
# self/subclass, polymorphism, tuples, multiple inheritance, and NoVTable.
|
|
# ============================================================
|
|
|
|
|
|
# ========== CVTable inheritance ==========
|
|
virtual class Animal:
|
|
Name: str
|
|
|
|
def __init__(self, name: str):
|
|
self.Name = name
|
|
|
|
def speak(self) -> str:
|
|
return "..."
|
|
|
|
|
|
virtual class Dog(Animal):
|
|
def __init__(self, name: str):
|
|
self.Name = name
|
|
|
|
def speak(self) -> str:
|
|
return "Woof"
|
|
|
|
|
|
virtual class Cat(Animal):
|
|
def speak(self) -> str:
|
|
return "Meow"
|
|
|
|
|
|
# ========== multiple inheritance ==========
|
|
virtual class A:
|
|
a: int
|
|
|
|
def get_a(self) -> int:
|
|
return self.a
|
|
|
|
|
|
virtual class B:
|
|
b: int
|
|
|
|
def get_b(self) -> int:
|
|
return self.b
|
|
|
|
|
|
virtual class AB(A, B):
|
|
def __init__(self, a: int, b: int):
|
|
self.a = a
|
|
self.b = b
|
|
|
|
|
|
# ========== NoVTable (compile-time) ==========
|
|
novirtual class Plain:
|
|
v: int
|
|
|
|
|
|
export def main() -> int:
|
|
testcheck.begin("IsinstanceTest: isinstance 内建函数测试")
|
|
|
|
# --- 1. self type and subclass -> base ---
|
|
d: Dog* = 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. runtime polymorphism: base pointer to subclass object ---
|
|
a: Animal* = Animal*(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. tuple ---
|
|
testcheck.check(isinstance(d, (Cat, Dog)) == 1, "isinstance 元组命中 OK",
|
|
"isinstance 元组 FAILED")
|
|
testcheck.check(isinstance(d, (Cat, A)) == 0, "isinstance 元组未命中 OK",
|
|
"isinstance 元组未命中 FAILED")
|
|
|
|
# --- 4. multiple inheritance ---
|
|
ab: AB* = 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 (compile-time static) ---
|
|
p: Plain* = 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")
|
|
|
|
# --- 6. cross-module isinstance (vtable resolved at link time) ---
|
|
cc: geom_lib.Circle* = geom_lib.Circle(5)
|
|
testcheck.check(isinstance(cc, geom_lib.Circle) == 1,
|
|
"跨模块 isinstance 自身 OK", "跨模块 isinstance 自身 FAILED")
|
|
testcheck.check(isinstance(cc, geom_lib.Shape) == 1,
|
|
"跨模块 isinstance 子类→基类 OK", "跨模块 isinstance 子类→基类 FAILED")
|
|
testcheck.check(isinstance(cc, geom_lib.Square) == 0,
|
|
"跨模块 isinstance 不匹配 OK", "跨模块 isinstance 不匹配 FAILED")
|
|
sq: geom_lib.Square* = geom_lib.Square(3)
|
|
testcheck.check(isinstance(sq, geom_lib.Shape) == 1,
|
|
"跨模块 Square→Shape OK", "跨模块 Square→Shape FAILED")
|
|
testcheck.check(isinstance(sq, geom_lib.Circle) == 0,
|
|
"跨模块 Square vs Circle OK", "跨模块 Square vs Circle FAILED")
|
|
shp: geom_lib.Shape* = geom_lib.Shape*(cc)
|
|
testcheck.check(isinstance(shp, geom_lib.Circle) == 1,
|
|
"跨模块运行时多态 OK", "跨模块运行时多态 FAILED")
|
|
testcheck.check(shp.area() == 5, "跨模块虚方法分派 OK", "跨模块虚方法分派 FAILED")
|
|
testcheck.check(cc.area() == 5, "跨模块 Circle.area OK", "跨模块 Circle.area FAILED")
|
|
|
|
# --- 7. NoVTable cross-module static ---
|
|
ps: geom_lib.PlainShape* = geom_lib.PlainShape()
|
|
testcheck.check(isinstance(ps, geom_lib.PlainShape) == 1,
|
|
"跨模块 NoVTable 静态 OK", "跨模块 NoVTable 静态 FAILED")
|
|
|
|
testcheck.ok("isinstance 全部测试完成")
|
|
return testcheck.end()
|