修复了大量存在的问题,增加了假鸭子类型等等机制
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import t
|
||||
import stdio
|
||||
import testcheck
|
||||
|
||||
# ============================================================
|
||||
# Test 1: 泛型函数 - 基本加法
|
||||
@@ -8,13 +9,10 @@ def add[T](a: T, b: T) -> T:
|
||||
return a + b
|
||||
|
||||
def test_generic_add():
|
||||
stdio.printf("--- Test 1: generic add ---\n")
|
||||
testcheck.section("Test 1: generic add")
|
||||
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)
|
||||
testcheck.check(r1 == 8 and r2 == 300, "generic add (3+5=8, 100+200=300)", "generic add expect 8 and 300")
|
||||
|
||||
# ============================================================
|
||||
# Test 2: 泛型函数 - 多类型参数
|
||||
@@ -23,12 +21,9 @@ def combine[T1, T2](a: T1, b: T2) -> T1:
|
||||
return a + T1(b)
|
||||
|
||||
def test_generic_combine():
|
||||
stdio.printf("--- Test 2: generic combine ---\n")
|
||||
testcheck.section("Test 2: generic combine")
|
||||
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)
|
||||
testcheck.check(r == 30, "generic combine (10+T1(20)=30)", "generic combine expect 30")
|
||||
|
||||
# ============================================================
|
||||
# Test 3: 泛型函数 - 最大值
|
||||
@@ -39,13 +34,10 @@ def max_val[T](a: T, b: T) -> T:
|
||||
return b
|
||||
|
||||
def test_generic_max():
|
||||
stdio.printf("--- Test 3: generic max ---\n")
|
||||
testcheck.section("Test 3: generic max")
|
||||
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)
|
||||
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]
|
||||
@@ -63,13 +55,10 @@ class Box[T]():
|
||||
self.value = T(v)
|
||||
|
||||
def test_generic_box():
|
||||
stdio.printf("--- Test 4: generic Box[T] ---\n")
|
||||
testcheck.section("Test 4: generic Box[T]")
|
||||
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)
|
||||
testcheck.check(v == 42, "Box[int] (get=42)", "Box[int] expect 42")
|
||||
|
||||
# ============================================================
|
||||
# Test 5: 泛型类 - Pair[T1, T2]
|
||||
@@ -86,44 +75,35 @@ class Pair[T1, T2]():
|
||||
return t.CInt(self.first) + t.CInt(self.second)
|
||||
|
||||
def test_generic_pair():
|
||||
stdio.printf("--- Test 5: generic Pair[T1,T2] ---\n")
|
||||
testcheck.section("Test 5: generic Pair[T1,T2]")
|
||||
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)
|
||||
testcheck.check(r == 30, "Pair[int,int] (sum=30)", "Pair[int,int] expect 30")
|
||||
|
||||
# ============================================================
|
||||
# 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]
|
||||
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
|
||||
if total == 100:
|
||||
stdio.printf("PASS: fixed array (total=%d)\n", total)
|
||||
else:
|
||||
stdio.printf("FAIL: fixed array (total=%d, expect 100)\n", total)
|
||||
testcheck.check(total == 100, "fixed array (total=100)", "fixed array expect 100")
|
||||
|
||||
# ============================================================
|
||||
# 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]
|
||||
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
|
||||
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])
|
||||
testcheck.check(buf[0] == 72 and buf[1] == 105, "char array (H=72 i=105)", "char array expect H=72 i=105")
|
||||
|
||||
# ============================================================
|
||||
# Test 8: 泛型函数多次特化
|
||||
@@ -132,13 +112,10 @@ def identity[T](x: T) -> T:
|
||||
return x
|
||||
|
||||
def test_generic_identity():
|
||||
stdio.printf("--- Test 8: generic identity ---\n")
|
||||
testcheck.section("Test 8: generic identity")
|
||||
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)
|
||||
testcheck.check(r1 == 42 and r2 == 0, "identity (42=42, 0=0)", "identity expect 42 and 0")
|
||||
|
||||
# ============================================================
|
||||
# Test 9: 泛型函数嵌套调用
|
||||
@@ -147,12 +124,9 @@ def double_val[T](x: T) -> T:
|
||||
return add(x, x)
|
||||
|
||||
def test_generic_nested():
|
||||
stdio.printf("--- Test 9: generic nested call ---\n")
|
||||
testcheck.section("Test 9: generic nested call")
|
||||
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)
|
||||
testcheck.check(r == 42, "generic nested (double(21)=42)", "generic nested expect 42")
|
||||
|
||||
# ============================================================
|
||||
# Test 10: 泛型类方法调用
|
||||
@@ -170,19 +144,16 @@ class Counter[T]():
|
||||
return self.count
|
||||
|
||||
def test_generic_counter():
|
||||
stdio.printf("--- Test 10: generic Counter[T] ---\n")
|
||||
testcheck.section("Test 10: generic Counter[T]")
|
||||
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)
|
||||
testcheck.check(r == 3, "Counter[int] (count=3)", "Counter[int] expect 3")
|
||||
|
||||
def main() -> t.CInt:
|
||||
stdio.printf("=== GenericTest: 泛型测试 ===\n\n")
|
||||
testcheck.begin("GenericTest: 泛型测试")
|
||||
test_generic_add()
|
||||
test_generic_combine()
|
||||
test_generic_max()
|
||||
@@ -193,5 +164,4 @@ def main() -> t.CInt:
|
||||
test_generic_identity()
|
||||
test_generic_nested()
|
||||
test_generic_counter()
|
||||
stdio.printf("\n=== GenericTest Complete ===\n")
|
||||
return 0
|
||||
return testcheck.end()
|
||||
|
||||
Reference in New Issue
Block a user