Files
TransPyC/Test/GenericTest/App/main.py
2026-07-18 19:25:40 +08:00

168 lines
5.2 KiB
Python

import t
import stdio
import testcheck
# ============================================================
# Test 1: 泛型函数 - 基本加法
# ============================================================
def add[T](a: T, b: T) -> T:
return a + b
def test_generic_add():
testcheck.section("Test 1: generic add")
r1: t.CInt = add(3, 5)
r2: t.CInt = add(100, 200)
testcheck.check(r1 == 8 and r2 == 300, "generic add (3+5=8, 100+200=300)", "generic add expect 8 and 300")
# ============================================================
# Test 2: 泛型函数 - 多类型参数
# ============================================================
def combine[T1, T2](a: T1, b: T2) -> T1:
return a + T1(b)
def test_generic_combine():
testcheck.section("Test 2: generic combine")
r: t.CInt = combine(10, 20)
testcheck.check(r == 30, "generic combine (10+T1(20)=30)", "generic combine expect 30")
# ============================================================
# Test 3: 泛型函数 - 最大值
# ============================================================
def max_val[T](a: T, b: T) -> T:
if a > b:
return a
return b
def test_generic_max():
testcheck.section("Test 3: generic max")
r1: t.CInt = max_val(10, 20)
r2: t.CInt = max_val(50, 30)
testcheck.check(r1 == 20 and r2 == 50, "generic max (max(10,20)=20, max(50,30)=50)", "generic max expect 20 and 50")
# ============================================================
# 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():
testcheck.section("Test 4: generic Box[T]")
b = Box(42)
v: t.CInt = b.get()
testcheck.check(v == 42, "Box[int] (get=42)", "Box[int] expect 42")
# ============================================================
# 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():
testcheck.section("Test 5: generic Pair[T1,T2]")
p = Pair(10, 20)
r: t.CInt = p.sum_as_int()
testcheck.check(r == 30, "Pair[int,int] (sum=30)", "Pair[int,int] expect 30")
# ============================================================
# Test 6: list[T, N] 固定数组
# ============================================================
def test_fixed_array():
testcheck.section("Test 6: list[T, N] fixed array")
arr: t.CArray[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
testcheck.check(total == 100, "fixed array (total=100)", "fixed array expect 100")
# ============================================================
# Test 7: list[t.CChar, N] 字符数组
# ============================================================
def test_char_array():
testcheck.section("Test 7: list[t.CChar, N] char array")
buf: t.CArray[t.CChar, 32] = [0]
buf[0] = 72 # H
buf[1] = 105 # i
buf[2] = 0
testcheck.check(buf[0] == 72 and buf[1] == 105, "char array (H=72 i=105)", "char array expect H=72 i=105")
# ============================================================
# Test 8: 泛型函数多次特化
# ============================================================
def identity[T](x: T) -> T:
return x
def test_generic_identity():
testcheck.section("Test 8: generic identity")
r1: t.CInt = identity(42)
r2: t.CInt = identity(0)
testcheck.check(r1 == 42 and r2 == 0, "identity (42=42, 0=0)", "identity expect 42 and 0")
# ============================================================
# Test 9: 泛型函数嵌套调用
# ============================================================
def double_val[T](x: T) -> T:
return add(x, x)
def test_generic_nested():
testcheck.section("Test 9: generic nested call")
r: t.CInt = double_val(21)
testcheck.check(r == 42, "generic nested (double(21)=42)", "generic nested expect 42")
# ============================================================
# 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():
testcheck.section("Test 10: generic Counter[T]")
c = Counter(0)
c.increment()
c.increment()
c.increment()
r: t.CInt = c.get_count()
testcheck.check(r == 3, "Counter[int] (count=3)", "Counter[int] expect 3")
def main() -> t.CInt:
testcheck.begin("GenericTest: 泛型测试")
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()
return testcheck.end()