From d4abdd40420083f9620ba8c4e5dff55a0054a76e Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sun, 9 Aug 2026 17:10:38 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=96=B0=E5=A2=9E=20ExpandKeywordsTest?= =?UTF-8?q?(fn/fun/func/function=20=E4=B8=93=E9=A1=B9);=20test=5Fzlib=20?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=9D=A1=E4=BB=B6=E4=BF=AE=E5=A4=8D;=20WebTe?= =?UTF-8?q?st=20=E8=A1=A5=20global=20=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test/ExpandKeywordsTest/App/main.vp | 75 ++++++++++++++++++++++++++++ Test/ExpandKeywordsTest/project.json | 29 +++++++++++ Test/TestProject/App/test_zlib.vp | 4 +- Test/WebTest/App/main.vp | 1 + 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 Test/ExpandKeywordsTest/App/main.vp create mode 100644 Test/ExpandKeywordsTest/project.json diff --git a/Test/ExpandKeywordsTest/App/main.vp b/Test/ExpandKeywordsTest/App/main.vp new file mode 100644 index 0000000..8163618 --- /dev/null +++ b/Test/ExpandKeywordsTest/App/main.vp @@ -0,0 +1,75 @@ +from stdint import * +import t, c +import stdio +import testcheck + + +# ============================================================ +# ExpandKeywordsTest - 验证 def 别名关键字 fn/fun/func/function +# 语义与 def 完全相同(定义/调用/嵌套/泛型/空函数体) +# ============================================================ + +# fn: 单参数返回 +fn add_one(x: t.CInt) -> t.CInt: + return x + 1 + + +# fun: 双参数返回 +fun add_two(a: t.CInt, b: t.CInt) -> t.CInt: + return a + b + + +# func: 浮点参数返回 +func mul_float(a: t.CDouble, b: t.CDouble) -> t.CDouble: + return a * b + + +# function: 内部用 def 定义再调用(别名与 def 混用) +function get_ten() -> t.CInt: + def inner() -> t.CInt: + return 10 + return inner() + + +# fn 泛型(PEP 695 风格类型参数) +fn ident[T](x: T) -> T: + return x + + +# fn 空函数体(pass) +fn noop() -> t.CVoid: + pass + + +# fun 递归 +fun fact(n: t.CInt) -> t.CInt: + if n <= 1: + return 1 + return n * fact(n - 1) + + +# func 多语句 + 局部变量 +func avg3(a: t.CDouble, b: t.CDouble, c: t.CDouble) -> t.CDouble: + total: t.CDouble = a + b + c + return total / 3.0 + +# func12344444 avg3(a: t.CDouble, b: t.CDouble, c: t.CDouble) -> t.CDouble: +# total: t.CDouble = a + b + c +# return total / 3.0 + + +def main() -> t.CInt: + testcheck.begin("ExpandKeywordsTest: fn/fun/func/function") + testcheck.section("function def keywords") + + testcheck.check(add_one(41) == 42, "fn add_one(41)==42", "FAIL fn add_one") + testcheck.check(add_two(20, 22) == 42, "fun add_two==42", "FAIL fun add_two") + testcheck.check(mul_float(2.5, 4.0) == 10.0, "func mul_float==10", "FAIL func mul_float") + testcheck.check(get_ten() == 10, "function get_ten==10", "FAIL function get_ten") + testcheck.check(ident(7) == 7, "fn ident[T](7)==7", "FAIL fn ident") + testcheck.check(fact(5) == 120, "fun fact(5)==120", "FAIL fun fact") + testcheck.check(avg3(1.0, 2.0, 3.0) == 2.0, "func avg3==2", "FAIL func avg3") + noop() + testcheck.check(True, "fn noop() ran", "FAIL fn noop") + + return testcheck.end() diff --git a/Test/ExpandKeywordsTest/project.json b/Test/ExpandKeywordsTest/project.json new file mode 100644 index 0000000..ed0c325 --- /dev/null +++ b/Test/ExpandKeywordsTest/project.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://raw.githubusercontent.com/TermiNexus/TransPyC/main/schemas/project-schema.json", + "name": "ExpandKeywordsTest", + "version": "1.0.0", + "source_dir": "./App", + "build_dir": "./.tpv_build", + "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": "ExpandKeywordsTest.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, + "sha1_slice_level": 1, + "target": "llvm", + "strict_mode": true + } +} diff --git a/Test/TestProject/App/test_zlib.vp b/Test/TestProject/App/test_zlib.vp index 705aa32..7833ffe 100644 --- a/Test/TestProject/App/test_zlib.vp +++ b/Test/TestProject/App/test_zlib.vp @@ -413,7 +413,7 @@ def test_adler32(): incremental = pyzlib.zlib_adler32(BYTEPTR("lo"), 2, incremental) printf(" adler32(\"lo\", prev) = 0x%08lX\n", incremental) check("adler32 incremental matches one-shot", incremental == checksum, "incremental != one-shot") - printf(" Incremental == One-shot: %s\n", incremental == "YES" if checksum else "NO") + printf(" Incremental == One-shot: %s\n", incremental == checksum if checksum else "NO") section_footer() @@ -430,7 +430,7 @@ def test_crc32(): incremental = pyzlib.zlib_crc32(BYTEPTR("lo"), 2, incremental) printf(" crc32(\"lo\", prev) = 0x%08lX\n", incremental) check("crc32 incremental matches one-shot", incremental == checksum, "incremental != one-shot") - printf(" Incremental == One-shot: %s\n", incremental == "YES" if checksum else "NO") + printf(" Incremental == One-shot: %s\n", incremental == checksum if checksum else "NO") section_footer() diff --git a/Test/WebTest/App/main.vp b/Test/WebTest/App/main.vp index 743f8b2..3ef4c2d 100644 --- a/Test/WebTest/App/main.vp +++ b/Test/WebTest/App/main.vp @@ -13,6 +13,7 @@ _mbuddy: memhub.MemBuddy | t.CPtr @t.CExport def main() -> int: + global _mbuddy stdio.printf("WebTest: HTTP client library test\n") # 初始化内存管理器(16MB arena)