Files
Viper_RegressionTesting/GenericTest/App/main.vp
T
2026-10-17 21:57:03 +08:00

166 lines
5.2 KiB
Plaintext

import testcheck
# ============================================================
# Test 1: generic function, basic addition
# ============================================================
def add[T](a: T, b: T) -> T:
return a + b
def test_generic_add():
testcheck.section("Test 1: generic add")
r1: int = add(3, 5)
r2: int = 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: generic function, multiple type parameters
# ============================================================
def combine[T1, T2](a: T1, b: T2) -> T1:
return a + T1(b)
def test_generic_combine():
testcheck.section("Test 2: generic combine")
r: int = combine(10, 20)
testcheck.check(r == 30, "generic combine (10+T1(20)=30)", "generic combine expect 30")
# ============================================================
# Test 3: generic function, maximum
# ============================================================
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: int = max_val(10, 20)
r2: int = 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: generic class 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: int = b.get()
testcheck.check(v == 42, "Box[int] (get=42)", "Box[int] expect 42")
# ============================================================
# Test 5: generic class 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) -> int:
return int(self.first) + int(self.second)
def test_generic_pair():
testcheck.section("Test 5: generic Pair[T1,T2]")
p = Pair(10, 20)
r: int = p.sum_as_int()
testcheck.check(r == 30, "Pair[int,int] (sum=30)", "Pair[int,int] expect 30")
# ============================================================
# Test 6: list[T, N] fixed array
# ============================================================
def test_fixed_array():
testcheck.section("Test 6: list[T, N] fixed array")
arr: [5]int = [0]
for i in range(5):
arr[i] = i * 10
total: int = 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] char array
# ============================================================
def test_char_array():
testcheck.section("Test 7: list[t.CChar, N] char array")
buf: [32]char = [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: generic function specialized multiple times
# ============================================================
def identity[T](x: T) -> T:
return x
def test_generic_identity():
testcheck.section("Test 8: generic identity")
r1: int = identity(42)
r2: int = identity(0)
testcheck.check(r1 == 42 and r2 == 0, "identity (42=42, 0=0)", "identity expect 42 and 0")
# ============================================================
# Test 9: generic function nested call
# ============================================================
def double_val[T](x: T) -> T:
return add(x, x)
def test_generic_nested():
testcheck.section("Test 9: generic nested call")
r: int = double_val(21)
testcheck.check(r == 42, "generic nested (double(21)=42)", "generic nested expect 42")
# ============================================================
# Test 10: generic class method call
# ============================================================
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: int = c.get_count()
testcheck.check(r == 3, "Counter[int] (count=3)", "Counter[int] expect 3")
def main() -> int:
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()