Files
Viper_RegressionTesting/VariadicTest/App/main.vp
T
TermiNexus ee6461be46 chore(comments): Test 仓注释英文化(R11)
把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
2026-10-05 22:43:38 +08:00

185 lines
6.2 KiB
Plaintext

from stdint import *
import t, c
import stdio
import testcheck
import string
import viperlib
import w32.win32console
# ============================================================
# VariadicTest - check snprintf/printf variadic argument passing
# ============================================================
# --- Test 1: %u formats small int (CUInt8T -> CUnsignedInt) ---
def test_snprintf_u8():
"""Check a CUInt8T value passed to %u via t.CUnsignedInt()."""
testcheck.section("Test 1: %u 格式化小整数 (CUInt8T -> CUnsignedInt)")
hr: uint8 = 14
mn: uint8 = 30
sc: uint8 = 59
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%02u:%02u:%02u", uint(hr), uint(mn), uint(sc))
stdio.printf("Test1 (CUInt8T->%%u): [%s] (expect 14:30:59)\n", &buf)
testcheck.ok("CUInt8T -> %u (expect 14:30:59)")
# --- Test 2: %u formats CUInt16T ---
def test_snprintf_u16():
"""Check a CUInt16T value passed to %u via t.CUnsignedInt()."""
testcheck.section("Test 2: %u 格式化 CUInt16T")
yr: uint16 = 2026
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%04u", uint(yr))
stdio.printf("Test2 (CUInt16T->%%u): [%s] (expect 2026)\n", &buf)
testcheck.ok("CUInt16T -> %u (expect 2026)")
# --- Test 3: %lu formats CUInt64T ---
def test_snprintf_u64():
"""Check a CUInt64T value passed to %lu."""
testcheck.section("Test 3: %lu 格式化 CUInt64T")
val: uint64 = 1234567890123
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%lu", val)
stdio.printf("Test3 (CUInt64T->%%lu): [%s] (expect 1234567890123)\n", &buf)
testcheck.ok("CUInt64T -> %lu (expect 1234567890123)")
# --- Test 4: %d formats negative int ---
def test_snprintf_d_neg():
"""Check %d formatting of a negative int."""
testcheck.section("Test 4: %d 格式化负数")
val: int = -42
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%d", val)
stdio.printf("Test4 (CInt->%%d neg): [%s] (expect -42)\n", &buf)
testcheck.ok("CInt -> %d neg (expect -42)")
# --- Test 5: %d formats positive int ---
def test_snprintf_d_pos():
testcheck.section("Test 5: %d 格式化正数")
val: int = 42
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%d", val)
stdio.printf("Test5 (CInt->%%d pos): [%s] (expect 42)\n", &buf)
testcheck.ok("CInt -> %d pos (expect 42)")
# --- Test 6: %x formats hex ---
def test_snprintf_x():
testcheck.section("Test 6: %x 格式化十六进制")
val: uint32 = 0xDEADBEEF
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "0x%08x", val)
stdio.printf("Test6 (%%x): [%s] (expect 0xdeadbeef)\n", &buf)
testcheck.ok("%x hex (expect 0xdeadbeef)")
# --- Test 7: mixed formatting (desktop.py time format) ---
def test_snprintf_mixed():
"""Mimic desktop.py time formatting."""
testcheck.section("Test 7: 混合格式化 (模拟 desktop.py 时间格式)")
hr: uint8 = 14
mn: uint8 = 5
sc: uint8 = 9
day: uint8 = 14
mon: uint8 = 6
yr: uint16 = 2026
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%04u-%02u-%02u %02u:%02u:%02u",
uint(yr), uint(mon), uint(day),
uint(hr), uint(mn), uint(sc))
stdio.printf("Test7 (time format): [%s] (expect 2026-06-14 14:05:09)\n", &buf)
testcheck.ok("time format (expect 2026-06-14 14:05:09)")
# --- Test 8: printf direct variadic output ---
def test_printf_variadic():
"""Check printf direct variadic output."""
testcheck.section("Test 8: printf 直接输出 variadic")
stdio.printf("Test8 (printf direct): %u %d %lu 0x%x (expect 100 -50 999999999 0xcafe)\n",
uint(100), -50, uint64(999999999), 0xCAFE)
testcheck.ok("printf direct variadic (expect 100 -50 999999999 0xcafe)")
# --- Test 9: several consecutive %lu ---
def test_snprintf_multi_lu():
"""Check several consecutive %lu (mimics perf log format)."""
testcheck.section("Test 9: 多个 %lu 连续")
v1: uint64 = 16
v2: uint64 = 2
v3: uint64 = 11
buf: t.CArray[char, 128]
string.memset(&buf, 0, 128)
viperlib.snprintf(&buf, 128, "wall=%lu work=%lu render=%lu", v1, v2, v3)
stdio.printf("Test9 (multi %%lu): [%s] (expect wall=16 work=2 render=11)\n", &buf)
testcheck.ok("multi %lu (expect wall=16 work=2 render=11)")
# --- Test 10: %u formats zero ---
def test_snprintf_zero():
testcheck.section("Test 10: %u 格式化 0")
val: uint32 = 0
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%u", val)
stdio.printf("Test10 (%%u zero): [%s] (expect 0)\n", &buf)
testcheck.ok("%u zero (expect 0)")
# --- Test 11: %u formats a large value ---
def test_snprintf_large():
testcheck.section("Test 11: %u 格式化大值")
val: uint32 = 4294967295 # 0xFFFFFFFF
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%u", val)
stdio.printf("Test11 (%%u max): [%s] (expect 4294967295)\n", &buf)
testcheck.ok("%u max (expect 4294967295)")
# --- Test 12: CUnsignedInt cast from CUInt8T edge values ---
def test_snprintf_u8_edge():
"""Check CUInt8T edge values."""
testcheck.section("Test 12: CUnsignedInt cast from CUInt8T edge values")
v0: uint8 = 0
v127: uint8 = 127
v128: uint8 = 128
v255: uint8 = 255
buf: t.CArray[char, 64]
string.memset(&buf, 0, 64)
viperlib.snprintf(&buf, 64, "%u %u %u %u",
uint(v0), uint(v127), uint(v128), uint(v255))
stdio.printf("Test12 (CUInt8T edge): [%s] (expect 0 127 128 255)\n", &buf)
testcheck.ok("CUInt8T edge (expect 0 127 128 255)")
def main() -> int | t.CExport:
w32.win32console.SetConsoleCP(65001)
w32.win32console.SetConsoleOutputCP(65001)
testcheck.begin("VariadicTest: snprintf/printf variadic 参数测试")
test_snprintf_u8()
test_snprintf_u16()
test_snprintf_u64()
test_snprintf_d_neg()
test_snprintf_d_pos()
test_snprintf_x()
test_snprintf_mixed()
test_printf_variadic()
test_snprintf_multi_lu()
test_snprintf_zero()
test_snprintf_large()
test_snprintf_u8_edge()
return testcheck.end()