185 lines
6.7 KiB
Python
185 lines
6.7 KiB
Python
from stdint import *
|
|
import t, c
|
|
import stdio
|
|
import testcheck
|
|
import string
|
|
import viperlib
|
|
import w32.win32console
|
|
|
|
# ============================================================
|
|
# VariadicTest - 测试 snprintf/printf variadic 参数传递
|
|
# ============================================================
|
|
|
|
# --- Test 1: %u 格式化小整数 (CUInt8T -> CUnsignedInt) ---
|
|
def test_snprintf_u8():
|
|
"""测试 CUInt8T 值通过 t.CUnsignedInt() 传递给 %u"""
|
|
testcheck.section("Test 1: %u 格式化小整数 (CUInt8T -> CUnsignedInt)")
|
|
hr: t.CUInt8T = 14
|
|
mn: t.CUInt8T = 30
|
|
sc: t.CUInt8T = 59
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%02u:%02u:%02u", t.CUnsignedInt(hr), t.CUnsignedInt(mn), t.CUnsignedInt(sc))
|
|
stdio.printf("Test1 (CUInt8T->%%u): [%s] (expect 14:30:59)\n", c.Addr(buf))
|
|
testcheck.ok("CUInt8T -> %u (expect 14:30:59)")
|
|
|
|
|
|
# --- Test 2: %u 格式化 CUInt16T ---
|
|
def test_snprintf_u16():
|
|
"""测试 CUInt16T 值通过 t.CUnsignedInt() 传递给 %u"""
|
|
testcheck.section("Test 2: %u 格式化 CUInt16T")
|
|
yr: t.CUInt16T = 2026
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%04u", t.CUnsignedInt(yr))
|
|
stdio.printf("Test2 (CUInt16T->%%u): [%s] (expect 2026)\n", c.Addr(buf))
|
|
testcheck.ok("CUInt16T -> %u (expect 2026)")
|
|
|
|
|
|
# --- Test 3: %lu 格式化 CUInt64T ---
|
|
def test_snprintf_u64():
|
|
"""测试 CUInt64T 值传递给 %lu"""
|
|
testcheck.section("Test 3: %lu 格式化 CUInt64T")
|
|
val: t.CUInt64T = 1234567890123
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%lu", val)
|
|
stdio.printf("Test3 (CUInt64T->%%lu): [%s] (expect 1234567890123)\n", c.Addr(buf))
|
|
testcheck.ok("CUInt64T -> %lu (expect 1234567890123)")
|
|
|
|
|
|
# --- Test 4: %d 格式化负数 ---
|
|
def test_snprintf_d_neg():
|
|
"""测试 %d 格式化负整数"""
|
|
testcheck.section("Test 4: %d 格式化负数")
|
|
val: t.CInt = -42
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%d", val)
|
|
stdio.printf("Test4 (CInt->%%d neg): [%s] (expect -42)\n", c.Addr(buf))
|
|
testcheck.ok("CInt -> %d neg (expect -42)")
|
|
|
|
|
|
# --- Test 5: %d 格式化正数 ---
|
|
def test_snprintf_d_pos():
|
|
testcheck.section("Test 5: %d 格式化正数")
|
|
val: t.CInt = 42
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%d", val)
|
|
stdio.printf("Test5 (CInt->%%d pos): [%s] (expect 42)\n", c.Addr(buf))
|
|
testcheck.ok("CInt -> %d pos (expect 42)")
|
|
|
|
|
|
# --- Test 6: %x 格式化十六进制 ---
|
|
def test_snprintf_x():
|
|
testcheck.section("Test 6: %x 格式化十六进制")
|
|
val: t.CUInt32T = 0xDEADBEEF
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "0x%08x", val)
|
|
stdio.printf("Test6 (%%x): [%s] (expect 0xdeadbeef)\n", c.Addr(buf))
|
|
testcheck.ok("%x hex (expect 0xdeadbeef)")
|
|
|
|
|
|
# --- Test 7: 混合格式化 (模拟 desktop.py 时间格式) ---
|
|
def test_snprintf_mixed():
|
|
"""模拟 desktop.py 的时间格式化"""
|
|
testcheck.section("Test 7: 混合格式化 (模拟 desktop.py 时间格式)")
|
|
hr: t.CUInt8T = 14
|
|
mn: t.CUInt8T = 5
|
|
sc: t.CUInt8T = 9
|
|
day: t.CUInt8T = 14
|
|
mon: t.CUInt8T = 6
|
|
yr: t.CUInt16T = 2026
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%04u-%02u-%02u %02u:%02u:%02u",
|
|
t.CUnsignedInt(yr), t.CUnsignedInt(mon), t.CUnsignedInt(day),
|
|
t.CUnsignedInt(hr), t.CUnsignedInt(mn), t.CUnsignedInt(sc))
|
|
stdio.printf("Test7 (time format): [%s] (expect 2026-06-14 14:05:09)\n", c.Addr(buf))
|
|
testcheck.ok("time format (expect 2026-06-14 14:05:09)")
|
|
|
|
|
|
# --- Test 8: printf 直接输出 variadic ---
|
|
def test_printf_variadic():
|
|
"""测试 printf 直接输出 variadic 参数"""
|
|
testcheck.section("Test 8: printf 直接输出 variadic")
|
|
stdio.printf("Test8 (printf direct): %u %d %lu 0x%x (expect 100 -50 999999999 0xcafe)\n",
|
|
t.CUnsignedInt(100), -50, t.CUInt64T(999999999), 0xCAFE)
|
|
testcheck.ok("printf direct variadic (expect 100 -50 999999999 0xcafe)")
|
|
|
|
|
|
# --- Test 9: 多个 %lu 连续 ---
|
|
def test_snprintf_multi_lu():
|
|
"""测试多个 %lu 连续(模拟 perf 日志格式)"""
|
|
testcheck.section("Test 9: 多个 %lu 连续")
|
|
v1: t.CUInt64T = 16
|
|
v2: t.CUInt64T = 2
|
|
v3: t.CUInt64T = 11
|
|
buf: t.CArray[t.CChar, 128]
|
|
string.memset(c.Addr(buf), 0, 128)
|
|
viperlib.snprintf(c.Addr(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", c.Addr(buf))
|
|
testcheck.ok("multi %lu (expect wall=16 work=2 render=11)")
|
|
|
|
|
|
# --- Test 10: %u 格式化 0 ---
|
|
def test_snprintf_zero():
|
|
testcheck.section("Test 10: %u 格式化 0")
|
|
val: t.CUInt32T = 0
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%u", val)
|
|
stdio.printf("Test10 (%%u zero): [%s] (expect 0)\n", c.Addr(buf))
|
|
testcheck.ok("%u zero (expect 0)")
|
|
|
|
|
|
# --- Test 11: %u 格式化大值 ---
|
|
def test_snprintf_large():
|
|
testcheck.section("Test 11: %u 格式化大值")
|
|
val: t.CUInt32T = 4294967295 # 0xFFFFFFFF
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%u", val)
|
|
stdio.printf("Test11 (%%u max): [%s] (expect 4294967295)\n", c.Addr(buf))
|
|
testcheck.ok("%u max (expect 4294967295)")
|
|
|
|
|
|
# --- Test 12: CUnsignedInt cast from CUInt8T edge values ---
|
|
def test_snprintf_u8_edge():
|
|
"""测试 CUInt8T 边界值"""
|
|
testcheck.section("Test 12: CUnsignedInt cast from CUInt8T edge values")
|
|
v0: t.CUInt8T = 0
|
|
v127: t.CUInt8T = 127
|
|
v128: t.CUInt8T = 128
|
|
v255: t.CUInt8T = 255
|
|
buf: t.CArray[t.CChar, 64]
|
|
string.memset(c.Addr(buf), 0, 64)
|
|
viperlib.snprintf(c.Addr(buf), 64, "%u %u %u %u",
|
|
t.CUnsignedInt(v0), t.CUnsignedInt(v127), t.CUnsignedInt(v128), t.CUnsignedInt(v255))
|
|
stdio.printf("Test12 (CUInt8T edge): [%s] (expect 0 127 128 255)\n", c.Addr(buf))
|
|
testcheck.ok("CUInt8T edge (expect 0 127 128 255)")
|
|
|
|
|
|
def main() -> t.CInt | 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()
|