Files
Viper_RegressionTesting/FuncTest/App/main.vp
T
TermiNexus 3c6fb1d246 chore(test): 初始化回归测试仓(新增 .gitignore,TestsOverview.md 改为 README.md)
把 Test/ 纳入 https://git.gvsds.com/GVSDS/Viper_RegressionTesting 。

1) 新增 .gitignore —— 只保留测试源码,剔除全部构建产物:
   目录   **/output*/(output / output_opt / output_orig / output_verify / output_dbg)、
          **/temp*/(temp / temp_opt)、**/.tpv_build/
   二进制 *.exe *.obj *.o *.out *.a *.lib *.dll
   中间物 *.ll *.vpi *.pyi *.deps.txt *.deps.json *.doc.json *_sha1_map.txt
          _pyi_cache_v1.pkl _pyi_cache_v1.sig.json _shared_sym.json _phase1_manifest.json
   其它   build_all.txt(构建日志)、*.log、__pycache__/、*.bak、IDE 目录
   保留   App/*.vp、project.json / project_opt.json / project.vpj、README.md、两个 .py
   实测 `git add -A --dry-run`:196 个文件,无任何构建产物混入。

2) TestsOverview.md → README.md(作为仓库首页文档)。

3) SequentialRegressionTest.py:7 处 TestsOverview.md 引用同步改为 README.md
   (文档串、OVERVIEW 路径、read_test_order 文档与报错文案、argparse description、
   启动横幅);同时修正入口路径 PROJECTRANS —— 旧值 <root>/Projectrans.py 已不存在,
   改为 <root>/TransPyC/Projectrans.py(与 regression_test.py:45-48 记录一致),
   否则本脚本会以 "can't open file" 全量失败。
   regression_test.py 无需改动:它按目录扫描 project.json 找项目,不引用该文档。
2026-09-29 15:14:59 +08:00

107 lines
3.0 KiB
Plaintext

import t
import stdio
import testcheck
# 测试基本函数调用
def add(a: t.CInt, b: t.CInt) -> t.CInt:
return a + b
def sub(a: t.CInt, b: t.CInt) -> t.CInt:
return a - b
# 测试多参数
def multi_args(a: t.CInt, b: t.CInt, c: t.CInt, d: t.CInt) -> t.CInt:
return a + b + c + d
# 测试递归
def factorial(n: t.CInt) -> t.CInt:
if n <= 1:
return 1
return n * factorial(n - 1)
# 测试尾递归式斐波那契
def fib(n: t.CInt) -> t.CInt:
if n <= 0:
return 0
if n == 1:
return 1
return fib(n - 1) + fib(n - 2)
# 测试指针参数
def add_via_ptr(p: t.CInt | t.CPtr, val: t.CInt):
p[0] = p[0] + val
# 测试 u64 返回值
def make_u64() -> t.CUInt64T:
return t.CUInt64T(0xDEADBEEF)
# 测试 bool 返回值
def is_even(n: t.CInt) -> t.CBool:
return n % 2 == 0
def test_basic_call():
testcheck.section("Test 1: basic function call")
r: t.CInt = add(10, 20)
testcheck.check(r == 30, "add(10,20)=30", "add(10,20) expect 30")
def test_multi_args():
testcheck.section("Test 2: multi args")
r: t.CInt = multi_args(1, 2, 3, 4)
testcheck.check(r == 10, "multi_args(1,2,3,4)=10", "multi_args(1,2,3,4) expect 10")
def test_recursion():
testcheck.section("Test 3: recursion (factorial)")
r: t.CInt = factorial(10)
# 10! = 3628800
testcheck.check(r == 3628800, "factorial(10)=3628800", "factorial(10) expect 3628800")
def test_fibonacci():
testcheck.section("Test 4: fibonacci")
r: t.CInt = fib(10)
# fib(10) = 55
testcheck.check(r == 55, "fib(10)=55", "fib(10) expect 55")
def test_ptr_param():
testcheck.section("Test 5: pointer parameter")
x: t.CInt = 10
# 需要取 x 的地址
import w32.win32memory
buf: t.CInt | t.CPtr = w32.win32memory.VirtualAlloc(None, 64, 12288, 4)
if t.CUInt64T(buf) == 0:
testcheck.info("SKIP: VirtualAlloc returned NULL")
return
buf[0] = 10
add_via_ptr(buf, 25)
testcheck.check(buf[0] == 35, "ptr param (result=35)", "ptr param expect 35")
def test_u64_return():
testcheck.section("Test 6: u64 return")
r: t.CUInt64T = make_u64()
testcheck.check(r == t.CUInt64T(0xDEADBEEF), "u64 return (0xdeadbeef)", "u64 return expect 0xdeadbeef")
def test_bool_return():
testcheck.section("Test 7: bool return")
r1: t.CBool = is_even(4)
r2: t.CBool = is_even(7)
testcheck.check(r1 and not r2, "bool return (is_even(4)=1, is_even(7)=0)", "bool return expect is_even(4)=1, is_even(7)=0")
def test_nested_call():
testcheck.section("Test 8: nested function call")
r: t.CInt = add(factorial(3), sub(10, 3))
# 3! = 6, 10-3 = 7, 6+7 = 13
testcheck.check(r == 13, "nested call (add(factorial(3), sub(10,3))=13)", "nested call expect 13")
import w32.win32memory
export def main() -> t.CInt:
testcheck.begin("FuncTest: 函数调用基准测试")
test_basic_call()
test_multi_args()
test_recursion()
test_fibonacci()
test_ptr_param()
test_u64_return()
test_bool_return()
test_nested_call()
return testcheck.end()