test: 新增 DefineTest 独立宏测试项目 + SprintfTest 多整数变参复现

- DefineTest: 从 TestProject3/definetest.vp 提取为独立项目(CDefine/c.CIf/c.CIfdef/c.CIfndef/platmacro, testcheck 分节), TestProject3 移除 definetest 引用
- SprintfTest: 增加 multi-int varargs repro R1-R4(带 check), 复现 viperlib.snprintf 第 2+ 个 int 变参读垃圾(va_arg 8 字节槽位 vs 调用点 i32 未扩展)
This commit is contained in:
TermiNexus committed 2026-08-09 03:39:33 +08:00
1 parent 939f7f2497
commit 61f07d4f9b
4 files changed
+86 -33

No files matched your search

@@ -3,6 +3,17 @@ from t import CInt, CInt32T, CUInt32T, CUInt64T, CFloat64T, CPtr, CChar, CDefine
import viperlib
import stdlib
import platmacro
import w32.win32console
import testcheck
# ============================================================
# DefineTest - CDefine 宏 / c.CIf / platmacro 专项测试
#
# 从 TestProject3/App/definetest.vp 独立提取。
# 验证: CDefine 常量、宏函数、c.CIf/c.CIfdef/c.CIfndef、
# platmacro 平台宏(输出以 print 为准,人工核对)。
# ============================================================
MAX_SIZE: t.CDefine = 1024
@@ -31,7 +42,6 @@ def CLAMP(x: CInt32T, lo: CInt32T, hi: CInt32T) -> t.CDefine:
return hi
return x
def test_basic_define() -> CInt:
print(MAX_SIZE)
print(MIN_VAL)
@@ -370,53 +380,42 @@ def test_cifdef_complex() -> CInt:
return 0
def define_main() -> CInt:
print("=== Basic Define Test ===")
def main() -> t.CInt:
w32.win32console.SetConsoleCP(65001)
w32.win32console.SetConsoleOutputCP(65001)
testcheck.begin("DefineTest: CDefine 宏与 c.CIf 测试")
testcheck.section("CDefine 基础")
test_basic_define()
print("=== Define Arithmetic Test ===")
test_define_arithmetic()
print("=== Define Conditional Test ===")
test_define_conditional()
print("=== Define Loop Test ===")
test_define_loop()
print("=== Typed Define Test ===")
test_typed_define()
print("=== Define Function (Macro) Test ===")
test_define_function()
print("=== Define Clamp (Macro) Test ===")
test_define_clamp()
print("=== Define String Test ===")
test_define_string()
print("=== Define Combined Test ===")
test_define_combined()
print("=== CIf Basic Test ===")
testcheck.section("c.CIf")
test_cif_basic()
print("=== CIf Elif Else Test ===")
test_cif_elif_else()
print("=== CIfdef/CIfndef Test ===")
test_cifdef_cifndef()
print("=== CIf Platform Test ===")
test_cif_platform()
print("=== CIf Nested Test ===")
test_cif_nested()
print("=== CIf Expression Test ===")
test_cif_expr()
print("=== Platmacro Platform Test ===")
testcheck.section("platmacro")
test_platmacro_platform()
print("=== Platmacro Sizes Test ===")
test_platmacro_sizes()
print("=== Platmacro Endian Test ===")
test_platmacro_endian()
print("=== Platmacro Features Test ===")
test_platmacro_features()
print("=== Platmacro Align Test ===")
test_platmacro_align()
print("=== CIfdef Cross-Module Test ===")
testcheck.section("c.CIfdef 综合")
test_cifdef_cross_module()
print("=== CIfdef Guard Test ===")
test_cifdef_guard()
print("=== CIfdef Undefined Test ===")
test_cifdef_undefined()
print("=== CIfdef Complex Test ===")
test_cifdef_complex()
return 0
return testcheck.end()
+29
View File
@@ -0,0 +1,29 @@
{
"$schema": "https://raw.githubusercontent.com/TermiNexus/TransPyC/main/schemas/project-schema.json",
"name": "DefineTest",
"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": "DefineTest.exe"
},
"includes": [
"../../includes"
],
"target": {
"triple": "x86_64-pc-windows-gnu",
"datalayout": "e-m:w-p270:32:32-p271:32:32-p272:64:64-f80:128-n8:16:32:64-S128"
},
"options": {
"slice_level": 3,
"sha1_slice_level": 1,
"target": "llvm",
"strict_mode": true
}
}
+31
View File
@@ -168,5 +168,36 @@ def main() -> int:
viperlib.snprintf(buf, 128, "T10 %a|%.17g", v_pi17, v_pi17)
testcheck.info(buf)
# ============================================================
# 多整数变参复现(DefineTest test_define_string PAGE= 遗留 bug):
# viperlib.snprintf 3+ 个 int 变参时, 第 2 个 %d 读到垃圾
# (va_arg 游标从寄存器区切到溢出区推进错误, 非本次 CIf 引入)
# 预期: R1-R3 目前 FAIL, 修复后应全部 PASS
# ============================================================
testcheck.section("multi-int varargs repro")
# R1: 3 个常量 int(复现 DefineTest "MAX_SIZE=%d PAGE=%d MASK=0x%x" 模式)
viperlib.snprintf(buf, 128, "R1 %d|%d|%d", 1024, 4096, 255)
testcheck.info(buf)
testcheck.check(string.strcmp(buf, "R1 1024|4096|255") == 0, "R1 3-const-int (1024|4096|255)", "R1 FAIL (第2变参被误读)")
# R2: 3 个运行时变量 int(排除常量折叠差异)
ia: t.CInt = 1024
ib: t.CInt = 4096
ic: t.CInt = 255
viperlib.snprintf(buf, 128, "R2 %d|%d|%d", ia, ib, ic)
testcheck.info(buf)
testcheck.check(string.strcmp(buf, "R2 1024|4096|255") == 0, "R2 3-var-int (1024|4096|255)", "R2 FAIL (第2变参被误读)")
# R3: 4 个变参(游标推进进一步暴露)
viperlib.snprintf(buf, 128, "R3 %d|%d|%d|%d", 1, 2, 3, 4)
testcheck.info(buf)
testcheck.check(string.strcmp(buf, "R3 1|2|3|4") == 0, "R3 4-int (1|2|3|4)", "R3 FAIL (变参游标推进错误)")
# R4: 第 2 个变参为 0(值 0 若被截断读也会暴露)
viperlib.snprintf(buf, 128, "R4 %d|%d|%d", 100, 0, 200)
testcheck.info(buf)
testcheck.check(string.strcmp(buf, "R4 100|0|200") == 0, "R4 middle-zero (100|0|200)", "R4 FAIL (第2变参被误读)")
stdlib.free(buf)
return 0
-6
View File
@@ -4,7 +4,6 @@ import t, c
from t import CInt, CPtr, CChar, CInt32T, CUInt64T, CFloat64T, CExport, State
import viperlib
import stdlib
import definetest
import enumtest
import mpooltest
import vectortest
@@ -491,11 +490,6 @@ def main() -> CInt | CExport:
print(buf)
testcheck.ok("新语法类型强转测试完成")
# === CDefine 宏测试 ===
testcheck.section("CDefine 宏测试")
definetest.define_main()
testcheck.ok("CDefine 宏测试完成")
# === Enum 测试 ===
testcheck.section("Enum 测试")
enumtest.enum_main()