import t import stdio import c # ============================================================ # Test 1: 泛型函数 - 不同整数类型特化 # ============================================================ def square[T](x: T) -> T: return x * x def test_generic_square_types(): stdio.printf("--- Test 1: generic square with different types ---\n") r1: t.CInt = square(5) r2: t.CInt = square(10) if r1 == 25 and r2 == 100: stdio.printf("PASS: square (5^2=%d 10^2=%d)\n", r1, r2) else: stdio.printf("FAIL: square (5^2=%d 10^2=%d, expect 25 100)\n", r1, r2) # ============================================================ # Test 2: 泛型函数 - 三参数 # ============================================================ def clamp[T](val: T, lo: T, hi: T) -> T: if val < lo: return lo if val > hi: return hi return val def test_generic_clamp(): stdio.printf("--- Test 2: generic clamp ---\n") r1: t.CInt = clamp(5, 0, 10) r2: t.CInt = clamp(-3, 0, 10) r3: t.CInt = clamp(15, 0, 10) if r1 == 5 and r2 == 0 and r3 == 10: stdio.printf("PASS: clamp (%d %d %d)\n", r1, r2, r3) else: stdio.printf("FAIL: clamp (%d %d %d, expect 5 0 10)\n", r1, r2, r3) # ============================================================ # Test 3: 泛型类 - Stack[T] # ============================================================ class Stack[T](): data: list[t.CInt, 16] top: t.CInt def __init__(self): self.top = 0 def push(self, val: T): self.data[self.top] = t.CInt(val) self.top += 1 def pop(self) -> T: self.top -= 1 return T(self.data[self.top]) def is_empty(self) -> t.CInt: if self.top == 0: return 1 return 0 def test_generic_stack(): stdio.printf("--- Test 3: generic Stack[T] ---\n") s = Stack() s.push(10) s.push(20) s.push(30) r1: t.CInt = s.pop() r2: t.CInt = s.pop() r3: t.CInt = s.pop() if r1 == 30 and r2 == 20 and r3 == 10: stdio.printf("PASS: Stack (pop=%d %d %d)\n", r1, r2, r3) else: stdio.printf("FAIL: Stack (pop=%d %d %d, expect 30 20 10)\n", r1, r2, r3) # ============================================================ # Test 4: 泛型函数 - swap via pointer # ============================================================ class IntPair: a: t.CInt b: t.CInt def generic_swap[T](p: IntPair | t.CPtr): tmp: t.CInt = c.Deref(p).a c.Deref(p).a = c.Deref(p).b c.Deref(p).b = tmp def test_generic_swap(): stdio.printf("--- Test 4: generic swap via pointer ---\n") p: IntPair = IntPair() p.a = 100 p.b = 200 generic_swap(c.Addr(p)) if p.a == 200 and p.b == 100: stdio.printf("PASS: swap (a=%d b=%d)\n", p.a, p.b) else: stdio.printf("FAIL: swap (a=%d b=%d, expect 200 100)\n", p.a, p.b) # ============================================================ # Test 5: 泛型类 - Range[T] 迭代器 # ============================================================ class Range[T](): start: T end_val: T step: T def __init__(self, s: T, e: T, st: T): self.start = T(s) self.end_val = T(e) self.step = T(st) def sum_all(self) -> T: result: T = T(0) i: T = self.start while i < self.end_val: result = result + i i = i + self.step return result def test_generic_range(): stdio.printf("--- Test 5: generic Range[T] ---\n") r = Range(0, 10, 1) # 0+1+2+...+9=45 s: t.CInt = r.sum_all() if s == 45: stdio.printf("PASS: Range sum (0..9=%d)\n", s) else: stdio.printf("FAIL: Range sum (0..9=%d, expect 45)\n", s) # ============================================================ # Test 6: 泛型函数 - 数组求和(非泛型数组参数) # ============================================================ def array_sum(arr: list[t.CInt, 8], n: t.CInt) -> t.CInt: result: t.CInt = 0 for i in range(n): result += arr[i] return result def test_generic_array_sum(): stdio.printf("--- Test 6: array sum ---\n") arr: list[t.CInt, 8] = [0] for i in range(8): arr[i] = (i + 1) * 10 s: t.CInt = array_sum(arr, 8) if s == 360: stdio.printf("PASS: array sum (%d)\n", s) else: stdio.printf("FAIL: array sum (%d, expect 360)\n", s) # ============================================================ # Test 7: 泛型函数 - 比较并返回较大值 # ============================================================ def max_of_three[T](a: T, b: T, c_val: T) -> T: m: T = a if b > m: m = b if c_val > m: m = c_val return m def test_generic_max_three(): stdio.printf("--- Test 7: generic max of three ---\n") r1: t.CInt = max_of_three(1, 2, 3) r2: t.CInt = max_of_three(10, 30, 20) r3: t.CInt = max_of_three(50, 50, 50) if r1 == 3 and r2 == 30 and r3 == 50: stdio.printf("PASS: max3 (%d %d %d)\n", r1, r2, r3) else: stdio.printf("FAIL: max3 (%d %d %d, expect 3 30 50)\n", r1, r2, r3) # ============================================================ # Test 8: 泛型类 - 带方法的容器 # ============================================================ class Container[T](): values: list[t.CInt, 8] size: t.CInt def __init__(self): self.size = 0 def add(self, v: T): self.values[self.size] = t.CInt(v) self.size += 1 def get(self, idx: t.CInt) -> T: return T(self.values[idx]) def contains(self, v: T) -> t.CInt: for i in range(self.size): if self.values[i] == t.CInt(v): return 1 return 0 def total(self) -> T: result: T = T(0) for i in range(self.size): result = result + T(self.values[i]) return result def test_generic_container(): stdio.printf("--- Test 8: generic Container[T] ---\n") c = Container() c.add(10) c.add(20) c.add(30) has20: t.CInt = c.contains(20) has99: t.CInt = c.contains(99) total: t.CInt = c.total() if has20 == 1 and has99 == 0 and total == 60: stdio.printf("PASS: Container (has20=%d has99=%d total=%d)\n", has20, has99, total) else: stdio.printf("FAIL: Container (has20=%d has99=%d total=%d, expect 1 0 60)\n", has20, has99, total) # ============================================================ # Test 9: 泛型函数 - 阶乘 # ============================================================ def factorial[T](n: T) -> T: result: T = T(1) i: T = T(2) while i <= n: result = result * i i = i + T(1) return result def test_generic_factorial(): stdio.printf("--- Test 9: generic factorial ---\n") r1: t.CInt = factorial(5) r2: t.CInt = factorial(10) if r1 == 120 and r2 == 3628800: stdio.printf("PASS: factorial (5!=%d 10!=%d)\n", r1, r2) else: stdio.printf("FAIL: factorial (5!=%d 10!=%d, expect 120 3628800)\n", r1, r2) # ============================================================ # Test 10: 泛型类 - 累加器 # ============================================================ class Accumulator[T](): total: T def __init__(self, start: T): self.total = T(start) def add_val(self, v: T): self.total = self.total + v def get_result(self) -> T: return self.total def test_generic_accumulator(): stdio.printf("--- Test 10: generic Accumulator[T] ---\n") a = Accumulator(0) a.add_val(10) a.add_val(20) a.add_val(30) r: t.CInt = a.get_result() if r == 60: stdio.printf("PASS: Accumulator (result=%d)\n", r) else: stdio.printf("FAIL: Accumulator (result=%d, expect 60)\n", r) def main() -> t.CInt: stdio.printf("=== GenericTest2: Advanced Generic Tests ===\n\n") test_generic_square_types() test_generic_clamp() test_generic_stack() test_generic_swap() test_generic_range() test_generic_array_sum() test_generic_max_three() test_generic_container() test_generic_factorial() test_generic_accumulator() stdio.printf("\n=== GenericTest2 Complete ===\n") return 0