Files
2026-10-17 21:57:03 +08:00

135 lines
3.3 KiB
Plaintext

from stdint import *
import win32.win32console
import viperlib
import stdlib
import testcheck
# === Generic functions ===
def add[T](a: T, b: T) -> T:
return a + b
def add_u64[T](a: T, b: T, c: u64) -> T:
return a + b + T(c)
def fxi[T1, T2](a: T1, b: T2) -> T1:
return a + fxi2(a, b, u64(178900))
def fxi2[T1, T2, T3](a: T1, b: T2, c: T3) -> T1:
return a + T1(b) + T1(c)
def wrap_add[T](a: T, b: T) -> T:
return add(a, b)
def double_add[T](a: T, b: T) -> T:
return add(a, b) + add(a, b)
# === Generic structs ===
class A[T]():
a: T
def __init__(self, a: T):
self.a = T(a)
def get_a(self) -> T:
return self.a
class Pair[T1, T2]():
first: T1
second: T2
def __init__(self, first: T1, second: T2):
self.first = T1(first)
self.second = T2(second)
def get_first(self) -> T1:
return self.first
def get_second(self) -> T2:
return self.second
class Calculator[T]():
val: T
def __init__(self, init_val: T):
self.val = T(init_val)
def compute(self, other: T) -> T:
return add(self.val, other)
def double_compute(self, other: T) -> T:
return double_add(self.val, other)
class Container[T]():
value: T
flag: u32
def __init__(self, v: T):
self.value = T(v)
self.flag = u32(1)
def get_value(self) -> T:
return self.value
def get_flag(self) -> u32:
return self.flag
export def main() -> int:
win32.win32console.SetConsoleCP(65001)
win32.win32console.SetConsoleOutputCP(65001)
testcheck.begin("GenericOopTest: 泛型专项测试")
buf: char = stdlib.malloc(256)
# === Basic generic functions ===
testcheck.section("基础函数泛型")
print(add(3, 5))
print(add(1.5, 2.5))
print(add_u64(1, 2, u64(3000000000000000)))
print(fxi(0.2, 6))
print(wrap_add(10, 20))
print(double_add(5, 7))
testcheck.ok("基础函数泛型测试完成")
# === Basic generic structs ===
testcheck.section("基础结构体泛型")
a = A(100)
print(a.get_a())
p1 = Pair(10, 20)
print(p1.get_first())
print(p1.get_second())
testcheck.ok("基础结构体泛型测试完成")
# === Generic + OOP combined ===
testcheck.section("泛型 + OOP 组合")
calc1 = Calculator(10)
print(calc1.compute(5))
calc2 = Calculator(2)
print(calc2.compute(3))
c1_gen = Container(42)
print(c1_gen.get_value())
print(c1_gen.get_flag())
testcheck.ok("泛型 + OOP 组合测试完成")
# === snprintf formatting ===
testcheck.section("snprintf 格式化")
viperlib.snprintf(buf, 256, "int=%d, float=%.2f", 42, 3.14)
print(buf)
viperlib.snprintf(buf, 256, "result=%d", add(100, 200))
print(buf)
testcheck.ok("snprintf 格式化测试完成")
# === New cast syntax (t1|t2)(x) ===
testcheck.section("新语法类型强转 (t1|t2)(x)")
val_i64: u64 = u64(42)
val_i32 = (int)(val_i64)
print(val_i32)
ptr_raw = stdlib.malloc(8)
ptr_typed = (int*)(ptr_raw)
viperlib.snprintf(buf, 256, "cast ptr=%p", ptr_typed)
print(buf)
val_f = (double)(42)
viperlib.snprintf(buf, 256, "cast float=%.1f", val_f)
print(buf)
testcheck.ok("新语法类型强转测试完成")
stdlib.free(buf)
return testcheck.end()