Files
TransPyC/Test/GenericTest/App/main.py
2026-06-16 16:09:42 +08:00

198 lines
6.1 KiB
Python

import t
import stdio
# ============================================================
# Test 1: 泛型函数 - 基本加法
# ============================================================
def add[T](a: T, b: T) -> T:
return a + b
def test_generic_add():
stdio.printf("--- Test 1: generic add ---\n")
r1: t.CInt = add(3, 5)
r2: t.CInt = add(100, 200)
if r1 == 8 and r2 == 300:
stdio.printf("PASS: generic add (3+5=%d, 100+200=%d)\n", r1, r2)
else:
stdio.printf("FAIL: generic add (3+5=%d, 100+200=%d)\n", r1, r2)
# ============================================================
# Test 2: 泛型函数 - 多类型参数
# ============================================================
def combine[T1, T2](a: T1, b: T2) -> T1:
return a + T1(b)
def test_generic_combine():
stdio.printf("--- Test 2: generic combine ---\n")
r: t.CInt = combine(10, 20)
if r == 30:
stdio.printf("PASS: generic combine (10+T1(20)=%d)\n", r)
else:
stdio.printf("FAIL: generic combine (result=%d, expect 30)\n", r)
# ============================================================
# Test 3: 泛型函数 - 最大值
# ============================================================
def max_val[T](a: T, b: T) -> T:
if a > b:
return a
return b
def test_generic_max():
stdio.printf("--- Test 3: generic max ---\n")
r1: t.CInt = max_val(10, 20)
r2: t.CInt = max_val(50, 30)
if r1 == 20 and r2 == 50:
stdio.printf("PASS: generic max (max(10,20)=%d, max(50,30)=%d)\n", r1, r2)
else:
stdio.printf("FAIL: generic max (max(10,20)=%d, max(50,30)=%d)\n", r1, r2)
# ============================================================
# Test 4: 泛型类 - Box[T]
# ============================================================
class Box[T]():
value: T
def __init__(self, v: T):
self.value = T(v)
def get(self) -> T:
return self.value
def set(self, v: T):
self.value = T(v)
def test_generic_box():
stdio.printf("--- Test 4: generic Box[T] ---\n")
b = Box(42)
v: t.CInt = b.get()
if v == 42:
stdio.printf("PASS: Box[int] (get=%d)\n", v)
else:
stdio.printf("FAIL: Box[int] (get=%d, expect 42)\n", v)
# ============================================================
# Test 5: 泛型类 - Pair[T1, T2]
# ============================================================
class Pair[T1, T2]():
first: T1
second: T2
def __init__(self, a: T1, b: T2):
self.first = T1(a)
self.second = T2(b)
def sum_as_int(self) -> t.CInt:
return t.CInt(self.first) + t.CInt(self.second)
def test_generic_pair():
stdio.printf("--- Test 5: generic Pair[T1,T2] ---\n")
p = Pair(10, 20)
r: t.CInt = p.sum_as_int()
if r == 30:
stdio.printf("PASS: Pair[int,int] (sum=%d)\n", r)
else:
stdio.printf("FAIL: Pair[int,int] (sum=%d, expect 30)\n", r)
# ============================================================
# Test 6: list[T, N] 固定数组
# ============================================================
def test_fixed_array():
stdio.printf("--- Test 6: list[T, N] fixed array ---\n")
arr: list[t.CInt, 5] = [0]
for i in range(5):
arr[i] = i * 10
total: t.CInt = 0
for i in range(5):
total += arr[i]
# 0+10+20+30+40 = 100
if total == 100:
stdio.printf("PASS: fixed array (total=%d)\n", total)
else:
stdio.printf("FAIL: fixed array (total=%d, expect 100)\n", total)
# ============================================================
# Test 7: list[t.CChar, N] 字符数组
# ============================================================
def test_char_array():
stdio.printf("--- Test 7: list[t.CChar, N] char array ---\n")
buf: list[t.CChar, 32] = [0]
buf[0] = 72 # H
buf[1] = 105 # i
buf[2] = 0
if buf[0] == 72 and buf[1] == 105:
stdio.printf("PASS: char array (H=%d i=%d)\n", buf[0], buf[1])
else:
stdio.printf("FAIL: char array (H=%d i=%d)\n", buf[0], buf[1])
# ============================================================
# Test 8: 泛型函数多次特化
# ============================================================
def identity[T](x: T) -> T:
return x
def test_generic_identity():
stdio.printf("--- Test 8: generic identity ---\n")
r1: t.CInt = identity(42)
r2: t.CInt = identity(0)
if r1 == 42 and r2 == 0:
stdio.printf("PASS: identity (42=%d, 0=%d)\n", r1, r2)
else:
stdio.printf("FAIL: identity (42=%d, 0=%d)\n", r1, r2)
# ============================================================
# Test 9: 泛型函数嵌套调用
# ============================================================
def double_val[T](x: T) -> T:
return add(x, x)
def test_generic_nested():
stdio.printf("--- Test 9: generic nested call ---\n")
r: t.CInt = double_val(21)
if r == 42:
stdio.printf("PASS: generic nested (double(21)=%d)\n", r)
else:
stdio.printf("FAIL: generic nested (double(21)=%d, expect 42)\n", r)
# ============================================================
# Test 10: 泛型类方法调用
# ============================================================
class Counter[T]():
count: T
def __init__(self, start: T):
self.count = T(start)
def increment(self):
self.count = add(self.count, T(1))
def get_count(self) -> T:
return self.count
def test_generic_counter():
stdio.printf("--- Test 10: generic Counter[T] ---\n")
c = Counter(0)
c.increment()
c.increment()
c.increment()
r: t.CInt = c.get_count()
if r == 3:
stdio.printf("PASS: Counter[int] (count=%d)\n", r)
else:
stdio.printf("FAIL: Counter[int] (count=%d, expect 3)\n", r)
def main() -> t.CInt:
stdio.printf("=== GenericTest: 泛型测试 ===\n\n")
test_generic_add()
test_generic_combine()
test_generic_max()
test_generic_box()
test_generic_pair()
test_fixed_array()
test_char_array()
test_generic_identity()
test_generic_nested()
test_generic_counter()
stdio.printf("\n=== GenericTest Complete ===\n")
return 0