This commit is contained in:
2026-06-16 16:09:42 +08:00
commit bffb0cb6b7
644 changed files with 86620 additions and 0 deletions

View File

@@ -0,0 +1,197 @@
import t
import stdio
# ============================================================
# Test 1: 泛型函数 - 基本加法
# ============================================================
def add[T](a: T, b: T) -> T:
return a + b
def test_generic_add():
stdio.printf("--- Test 1: generic add ---\n")
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)
# ============================================================
# Test 2: 泛型函数 - 多类型参数
# ============================================================
def combine[T1, T2](a: T1, b: T2) -> T1:
return a + T1(b)
def test_generic_combine():
stdio.printf("--- Test 2: generic combine ---\n")
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)
# ============================================================
# Test 3: 泛型函数 - 最大值
# ============================================================
def max_val[T](a: T, b: T) -> T:
if a > b:
return a
return b
def test_generic_max():
stdio.printf("--- Test 3: generic max ---\n")
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)
# ============================================================
# 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():
stdio.printf("--- Test 4: generic Box[T] ---\n")
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)
# ============================================================
# 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():
stdio.printf("--- Test 5: generic Pair[T1,T2] ---\n")
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)
# ============================================================
# 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]
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)
# ============================================================
# 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]
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])
# ============================================================
# Test 8: 泛型函数多次特化
# ============================================================
def identity[T](x: T) -> T:
return x
def test_generic_identity():
stdio.printf("--- Test 8: generic identity ---\n")
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)
# ============================================================
# Test 9: 泛型函数嵌套调用
# ============================================================
def double_val[T](x: T) -> T:
return add(x, x)
def test_generic_nested():
stdio.printf("--- Test 9: generic nested call ---\n")
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)
# ============================================================
# 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():
stdio.printf("--- Test 10: generic Counter[T] ---\n")
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)
def main() -> t.CInt:
stdio.printf("=== GenericTest: 泛型测试 ===\n\n")
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()
stdio.printf("\n=== GenericTest Complete ===\n")
return 0

View File

@@ -0,0 +1 @@
{"stdio": "73edbcf76e32d00b"}

View File

@@ -0,0 +1,29 @@
{
"$schema": "https://raw.githubusercontent.com/TermiNexus/TransPyC/main/schemas/project-schema.json",
"name": "GenericTest",
"version": "1.0.0",
"source_dir": "./App",
"temp_dir": "./temp",
"output_dir": "./output",
"compiler": {
"cmd": "llc",
"flags": ["-filetype=obj", "-relocation-model=pic"]
},
"linker": {
"cmd": "clang++",
"flags": ["-Wl,--allow-multiple-definition", "-Wl,--unresolved-symbols=ignore-in-object-files", "-lmsvcrt", "-lucrt", "-lpthread", "-lmingwex", "-lkernel32", "-luser32", "-latomic"],
"output": "GenericTest.exe"
},
"includes": [
"../../includes"
],
"target": {
"triple": "x86_64-pc-windows-gnu",
"datalayout": "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
},
"options": {
"slice_level": 3,
"target": "llvm",
"strict_mode": true
}
}

View File

@@ -0,0 +1,63 @@
"""
Auto-generated Python stub file from main.py
Module: main
"""
import c
import t
import stdio
def add[T](a: T, b: T) -> T: pass
def test_generic_add() -> t.CInt: pass
def combine[T1, T2](a: T1, b: T2) -> T1: pass
def test_generic_combine() -> t.CInt: pass
def max_val[T](a: T, b: T) -> T: pass
def test_generic_max() -> t.CInt: pass
class Box[T]:
value: T
def __init__(self: Box, v: T) -> t.CInt: pass
def get(self: Box) -> T: pass
def set(self: Box, v: T) -> t.CInt: pass
def test_generic_box() -> t.CInt: pass
class Pair[T1, T2]:
first: T1
second: T2
def __init__(self: Pair, a: T1, b: T2) -> t.CInt: pass
def sum_as_int(self: Pair) -> t.CInt: pass
def test_generic_pair() -> t.CInt: pass
def test_fixed_array() -> t.CInt: pass
def test_char_array() -> t.CInt: pass
def identity[T](x: T) -> T: pass
def test_generic_identity() -> t.CInt: pass
def double_val[T](x: T) -> T: pass
def test_generic_nested() -> t.CInt: pass
class Counter[T]:
count: T
def __init__(self: Counter, start: T) -> t.CInt: pass
def increment(self: Counter) -> t.CInt: pass
def get_count(self: Counter) -> T: pass
def test_generic_counter() -> t.CInt: pass
def main() -> t.CInt: pass

View File

@@ -0,0 +1,28 @@
"""
Auto-generated Python stub file from stdio.py
Module: stdio
"""
import t, c
def printf(fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def fprintf(stream: t.CVoid | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def sprintf(buf: t.CChar | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def snprintf(buf: t.CChar | t.CPtr, size: t.CSizeT, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def puts(s: t.CChar | t.CConst | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
def fputs(s: t.CChar | t.CConst | t.CPtr, stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
def fgets(buf: t.CChar | t.CPtr, size: t.CInt, stream: t.CVoid | t.CPtr) -> t.CChar | t.CPtr | t.CExtern | t.CExport: pass
def fflush(stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
stdin: t.CExtern | t.CVoid | t.CPtr
stdout: t.CExtern | t.CVoid | t.CPtr
stderr: t.CExtern | t.CVoid | t.CPtr

View File

@@ -0,0 +1,2 @@
47df358e08cc5c0e:main.py
73edbcf76e32d00b:includes/stdio.py