Files
ViperC/Test/App/inherit_test.vp
T

122 lines
4.0 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.
import stdio
import t, c
# ============================================================
# 继承测试:字段展平 + vtable 继承 + 方法覆盖
# ============================================================
# ============================================================
# 基类 Animal(有虚方法)
# ============================================================
@t.CVTable
class Animal:
name: t.CInt
def __init__(self, n: t.CInt):
self.name = n
def GetName(self) -> t.CInt:
return self.name
def Speak(self) -> t.CInt:
return 0
# ============================================================
# 子类 Dog(继承 Animal,覆盖 Speak,新增字段和方法)
# ============================================================
class Dog(Animal):
breed: t.CInt
def __init__(self, n: t.CInt, b: t.CInt):
self.name = n
self.breed = b
def Speak(self) -> t.CInt:
return 1
def GetBreed(self) -> t.CInt:
return self.breed
# ============================================================
# 子类 Cat(继承 Animal,不覆盖 Speak,新增字段和方法)
# ============================================================
class Cat(Animal):
color: t.CInt
def __init__(self, n: t.CInt, col: t.CInt):
self.name = n
self.color = col
def GetColor(self) -> t.CInt:
return self.color
# ============================================================
# 主函数
# ============================================================
def inherit_test() -> int:
# ============================================================
# 测试 1: 基类 Animal
# ============================================================
stdio.printf("inherit: === Test 1: Base Class ===\n")
a: Animal = Animal(42)
aname: int = a.GetName()
stdio.printf("inherit: a.GetName()=%d (expected 42)\n", aname)
if aname != 42:
stdio.printf("[FAIL] a.GetName()=%d expected 42\n", aname)
return 1
aspeak: int = a.Speak()
stdio.printf("inherit: a.Speak()=%d (expected 0)\n", aspeak)
if aspeak != 0:
stdio.printf("[FAIL] a.Speak()=%d expected 0\n", aspeak)
return 1
# ============================================================
# 测试 2: 子类 Dog(覆盖 Speak,继承 GetName)
# ============================================================
stdio.printf("inherit: === Test 2: Dog (override Speak) ===\n")
d: Dog = Dog(100, 7)
dname: int = d.GetName()
stdio.printf("inherit: d.GetName()=%d (expected 100)\n", dname)
if dname != 100:
stdio.printf("[FAIL] d.GetName()=%d expected 100\n", dname)
return 1
dspeak: int = d.Speak()
stdio.printf("inherit: d.Speak()=%d (expected 1)\n", dspeak)
if dspeak != 1:
stdio.printf("[FAIL] d.Speak()=%d expected 1\n", dspeak)
return 1
dbreed: int = d.GetBreed()
stdio.printf("inherit: d.GetBreed()=%d (expected 7)\n", dbreed)
if dbreed != 7:
stdio.printf("[FAIL] d.GetBreed()=%d expected 7\n", dbreed)
return 1
# ============================================================
# 测试 3: 子类 Cat(不覆盖 Speak,继承 GetName)
# ============================================================
stdio.printf("inherit: === Test 3: Cat (inherit Speak) ===\n")
ct: Cat = Cat(200, 3)
cname: int = ct.GetName()
stdio.printf("inherit: ct.GetName()=%d (expected 200)\n", cname)
if cname != 200:
stdio.printf("[FAIL] ct.GetName()=%d expected 200\n", cname)
return 1
cspeak: int = ct.Speak()
stdio.printf("inherit: ct.Speak()=%d (expected 0)\n", cspeak)
if cspeak != 0:
stdio.printf("[FAIL] ct.Speak()=%d expected 0\n", cspeak)
return 1
ccolor: int = ct.GetColor()
stdio.printf("inherit: ct.GetColor()=%d (expected 3)\n", ccolor)
if ccolor != 3:
stdio.printf("[FAIL] ct.GetColor()=%d expected 3\n", ccolor)
return 1
stdio.printf("inherit: === All Tests Passed ===\n")
return 0