Files
Viper_RegressionTesting/StringTest/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

86 lines
3.0 KiB
Plaintext

import t
import stdio
import string
import testcheck
import w32.win32memory
def test_strlen():
testcheck.section("Test 1: strlen")
s: char* = "Hello, World!"
length: uint64 = string.strlen(s)
testcheck.check(length == 13, "strlen (len=13)", "strlen (expect 13)")
def test_strcpy():
testcheck.section("Test 2: strcpy")
buf: char* = w32.win32memory.VirtualAlloc(None, 64, 12288, 4)
if uint64(buf) == 0:
stdio.printf("SKIP: VirtualAlloc returned NULL\n")
return
string.memset(buf, 0, 64)
string.strcpy(buf, "test string")
cmp_result: int = string.strcmp(buf, "test string")
testcheck.check(cmp_result == 0, "strcpy (result=test string)", "strcpy (cmp mismatch)")
def test_strcat():
testcheck.section("Test 3: strcat (via memcpy)")
buf: char* = w32.win32memory.VirtualAlloc(None, 64, 12288, 4)
if uint64(buf) == 0:
stdio.printf("SKIP: VirtualAlloc returned NULL\n")
return
string.memset(buf, 0, 64)
# Manual concat: copy "Hello" then " World".
hello: char* = "Hello"
world: char* = " World"
string.memcpy(buf, hello, 5)
string.memcpy(t.CPtr(uint64(buf) + 5), world, 6)
cmp_result: int = string.strcmp(buf, "Hello World")
testcheck.check(cmp_result == 0, "strcat via memcpy", "strcat via memcpy (cmp mismatch)")
def test_strcmp():
testcheck.section("Test 4: strcmp")
r1: int = string.strcmp("abc", "abc")
r2: int = string.strcmp("abc", "abd")
r3: int = string.strcmp("abd", "abc")
testcheck.check(r1 == 0 and r2 < 0 and r3 > 0, "strcmp (eq=0 lt<0 gt>0)", "strcmp (expect eq=0 lt<0 gt>0)")
def test_strncpy():
testcheck.section("Test 5: strncpy")
buf: char* = w32.win32memory.VirtualAlloc(None, 64, 12288, 4)
if uint64(buf) == 0:
stdio.printf("SKIP: VirtualAlloc returned NULL\n")
return
string.memset(buf, 0, 64)
string.strncpy(buf, "ABCDEFGHIJ", 5)
cmp_result: int = string.strcmp(buf, "ABCDE")
testcheck.check(cmp_result == 0, "strncpy (result=ABCDE)", "strncpy (cmp mismatch)")
def test_char_access():
testcheck.section("Test 6: char access")
s: char* = "ABCDE"
val: uint8 = s[2] # 'C' = 67
testcheck.check(val == 67, "char access (s[2]=67='C')", "char access (s[2] expect 67)")
def test_snprintf():
testcheck.section("Test 7: snprintf")
buf: char* = w32.win32memory.VirtualAlloc(None, 128, 12288, 4)
if uint64(buf) == 0:
stdio.printf("SKIP: VirtualAlloc returned NULL\n")
return
string.memset(buf, 0, 128)
stdio.snprintf(buf, 128, "value=%d hex=0x%x str=%s", 42, 255, "test")
cmp_result: int = string.strcmp(buf, "value=42 hex=0xff str=test")
testcheck.check(cmp_result == 0, "snprintf (result=value=42 hex=0xff str=test)", "snprintf (result mismatch)")
import w32.win32memory
def main() -> int:
testcheck.begin("StringTest: 字符串操作基准测试")
test_strlen()
test_strcpy()
test_strcat()
test_strcmp()
test_strncpy()
test_char_access()
test_snprintf()
return testcheck.end()