Files
TransPyV/Test/App/oop_test.py
2026-07-19 13:18:46 +08:00

130 lines
4.0 KiB
Python
Raw Permalink 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
# ============================================================
# OOP 测试:存在方法的 class 自动升级为 OOP 结构体
# ============================================================
# ============================================================
# 测试 1: 基本方法(无 __init____before_init__ 零值填充)
# ============================================================
class Point:
x: t.CInt
y: t.CInt
def MoveTo(self, nx: t.CInt, ny: t.CInt) -> t.CInt:
self.x = nx
self.y = ny
return 0
def GetX(self) -> t.CInt:
return self.x
def GetY(self) -> t.CInt:
return self.y
# ============================================================
# 测试 2: 带默认值的 OOP 结构体__before_init__ 应用默认值)
# ============================================================
class Counter:
count: t.CInt = 0
step: t.CInt = 1
def Increment(self) -> t.CInt:
self.count = self.count + self.step
return self.count
def Reset(self) -> t.CInt:
self.count = 0
return 0
# ============================================================
# 测试 3: __init__ 构造函数
# ============================================================
class Rect:
width: t.CInt
height: t.CInt
def __init__(self, w: t.CInt, h: t.CInt):
self.width = w
self.height = h
def Area(self) -> t.CInt:
return self.width * self.height
# ============================================================
# 主函数
# ============================================================
def oop_test() -> int:
# ============================================================
# 测试 1: 基本方法调用
# ============================================================
stdio.printf("oop: === Test 1: Basic Methods ===\n")
p: Point = Point()
p.MoveTo(10, 20)
stdio.printf("oop: p.x=%d p.y=%d\n", p.GetX(), p.GetY())
if p.GetX() != 10:
stdio.printf("[FAIL] p.GetX()=%d expected 10\n", p.GetX())
return 1
if p.GetY() != 20:
stdio.printf("[FAIL] p.GetY()=%d expected 20\n", p.GetY())
return 1
# 修改字段后再次调用方法
p.MoveTo(100, 200)
stdio.printf("oop: after move p.x=%d p.y=%d\n", p.GetX(), p.GetY())
if p.GetX() != 100:
return 1
# ============================================================
# 测试 2: 默认值 + 方法
# ============================================================
stdio.printf("oop: === Test 2: Defaults + Methods ===\n")
cnt: Counter = Counter()
stdio.printf("oop: initial count=%d step=%d\n", cnt.count, cnt.step)
if cnt.count != 0:
stdio.printf("[FAIL] cnt.count=%d expected 0\n", cnt.count)
return 1
if cnt.step != 1:
stdio.printf("[FAIL] cnt.step=%d expected 1\n", cnt.step)
return 1
r1: int = cnt.Increment()
r2: int = cnt.Increment()
r3: int = cnt.Increment()
stdio.printf("oop: after 3 increments: %d %d %d\n", r1, r2, r3)
if r1 != 1 or r2 != 2 or r3 != 3:
stdio.printf("[FAIL] increments: %d %d %d\n", r1, r2, r3)
return 1
cnt.Reset()
r4: int = cnt.Increment()
stdio.printf("oop: after reset+increment: %d\n", r4)
if r4 != 1:
return 1
# ============================================================
# 测试 3: __init__ 构造函数
# ============================================================
stdio.printf("oop: === Test 3: __init__ ===\n")
rect: Rect = Rect(3, 4)
area: int = rect.Area()
stdio.printf("oop: rect %dx%d area=%d\n", rect.width, rect.height, area)
if area != 12:
stdio.printf("[FAIL] area=%d expected 12\n", area)
return 1
if rect.width != 3:
stdio.printf("[FAIL] width=%d expected 3\n", rect.width)
return 1
if rect.height != 4:
stdio.printf("[FAIL] height=%d expected 4\n", rect.height)
return 1
stdio.printf("oop: === All OOP Tests Passed ===\n")
return 0