补充
This commit is contained in:
105
Test/StringTest/App/main.py
Normal file
105
Test/StringTest/App/main.py
Normal file
@@ -0,0 +1,105 @@
|
||||
import t
|
||||
import stdio
|
||||
import string
|
||||
|
||||
def test_strlen():
|
||||
stdio.printf("--- Test 1: strlen ---\n")
|
||||
s: t.CChar | t.CPtr = "Hello, World!"
|
||||
length: t.CUInt64T = string.strlen(s)
|
||||
if length == 13:
|
||||
stdio.printf("PASS: strlen (len=%lu)\n", length)
|
||||
else:
|
||||
stdio.printf("FAIL: strlen (len=%lu, expect 13)\n", length)
|
||||
|
||||
def test_strcpy():
|
||||
stdio.printf("--- Test 2: strcpy ---\n")
|
||||
buf: t.CChar | t.CPtr = w32.win32memory.VirtualAlloc(t.CVoid(0, t.CPtr), 64, 12288, 4)
|
||||
if t.CUInt64T(buf) == 0:
|
||||
stdio.printf("SKIP: VirtualAlloc returned NULL\n")
|
||||
return
|
||||
string.memset(buf, 0, 64)
|
||||
string.strcpy(buf, "test string")
|
||||
cmp_result: t.CInt = string.strcmp(buf, "test string")
|
||||
if cmp_result == 0:
|
||||
stdio.printf("PASS: strcpy (result=%s)\n", buf)
|
||||
else:
|
||||
stdio.printf("FAIL: strcpy (cmp=%d)\n", cmp_result)
|
||||
|
||||
def test_strcat():
|
||||
stdio.printf("--- Test 3: strcat (via memcpy) ---\n")
|
||||
buf: t.CChar | t.CPtr = w32.win32memory.VirtualAlloc(t.CVoid(0, t.CPtr), 64, 12288, 4)
|
||||
if t.CUInt64T(buf) == 0:
|
||||
stdio.printf("SKIP: VirtualAlloc returned NULL\n")
|
||||
return
|
||||
string.memset(buf, 0, 64)
|
||||
# 手动拼接: 先拷贝 "Hello",再拷贝 " World"
|
||||
hello: t.CChar | t.CPtr = "Hello"
|
||||
world: t.CChar | t.CPtr = " World"
|
||||
string.memcpy(buf, hello, 5)
|
||||
string.memcpy(t.CVoid(t.CUInt64T(buf) + 5, t.CPtr), world, 6)
|
||||
cmp_result: t.CInt = string.strcmp(buf, "Hello World")
|
||||
if cmp_result == 0:
|
||||
stdio.printf("PASS: strcat via memcpy\n")
|
||||
else:
|
||||
stdio.printf("FAIL: strcat via memcpy (cmp=%d)\n", cmp_result)
|
||||
|
||||
def test_strcmp():
|
||||
stdio.printf("--- Test 4: strcmp ---\n")
|
||||
r1: t.CInt = string.strcmp("abc", "abc")
|
||||
r2: t.CInt = string.strcmp("abc", "abd")
|
||||
r3: t.CInt = string.strcmp("abd", "abc")
|
||||
if r1 == 0 and r2 < 0 and r3 > 0:
|
||||
stdio.printf("PASS: strcmp (eq=%d lt=%d gt=%d)\n", r1, r2, r3)
|
||||
else:
|
||||
stdio.printf("FAIL: strcmp (eq=%d lt=%d gt=%d)\n", r1, r2, r3)
|
||||
|
||||
def test_strncpy():
|
||||
stdio.printf("--- Test 5: strncpy ---\n")
|
||||
buf: t.CChar | t.CPtr = w32.win32memory.VirtualAlloc(t.CVoid(0, t.CPtr), 64, 12288, 4)
|
||||
if t.CUInt64T(buf) == 0:
|
||||
stdio.printf("SKIP: VirtualAlloc returned NULL\n")
|
||||
return
|
||||
string.memset(buf, 0, 64)
|
||||
string.strncpy(buf, "ABCDEFGHIJ", 5)
|
||||
cmp_result: t.CInt = string.strcmp(buf, "ABCDE")
|
||||
if cmp_result == 0:
|
||||
stdio.printf("PASS: strncpy (result=%s)\n", buf)
|
||||
else:
|
||||
stdio.printf("FAIL: strncpy (cmp=%d)\n", cmp_result)
|
||||
|
||||
def test_char_access():
|
||||
stdio.printf("--- Test 6: char access ---\n")
|
||||
s: t.CChar | t.CPtr = "ABCDE"
|
||||
val: t.CUInt8T = s[2] # 'C' = 67
|
||||
if val == 67:
|
||||
stdio.printf("PASS: char access (s[2]=%d='C')\n", val)
|
||||
else:
|
||||
stdio.printf("FAIL: char access (s[2]=%d, expect 67)\n", val)
|
||||
|
||||
def test_snprintf():
|
||||
stdio.printf("--- Test 7: snprintf ---\n")
|
||||
buf: t.CChar | t.CPtr = w32.win32memory.VirtualAlloc(t.CVoid(0, t.CPtr), 128, 12288, 4)
|
||||
if t.CUInt64T(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: t.CInt = string.strcmp(buf, "value=42 hex=0xff str=test")
|
||||
if cmp_result == 0:
|
||||
stdio.printf("PASS: snprintf (result=%s)\n", buf)
|
||||
else:
|
||||
stdio.printf("FAIL: snprintf (result='%s')\n", buf)
|
||||
|
||||
import w32.win32memory
|
||||
|
||||
def main() -> t.CInt:
|
||||
stdio.printf("=== StringTest: 字符串操作基准测试 ===\n\n")
|
||||
test_strlen()
|
||||
test_strcpy()
|
||||
test_strcat()
|
||||
test_strcmp()
|
||||
test_strncpy()
|
||||
test_char_access()
|
||||
test_snprintf()
|
||||
stdio.printf("\n=== StringTest Complete ===\n")
|
||||
return 0
|
||||
Reference in New Issue
Block a user