62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
import t
|
|
import stdio
|
|
import string
|
|
|
|
def test_for_in_string():
|
|
stdio.printf("--- Test 1: for-in string iteration ---\n")
|
|
s: t.CChar | t.CPtr = "Hello"
|
|
result: t.CInt = 0
|
|
for ch in s:
|
|
result += ch
|
|
# H=72, e=101, l=108, l=108, o=111 => 500
|
|
if result == 500:
|
|
stdio.printf("PASS: for-in string (sum=%d)\n", result)
|
|
else:
|
|
stdio.printf("FAIL: for-in string (sum=%d, expect 500)\n", result)
|
|
|
|
def test_for_in_char_ptr():
|
|
stdio.printf("--- Test 2: for-in char* iteration ---\n")
|
|
s: t.CChar | t.CPtr = "ABC"
|
|
chars: list[t.CInt, 4] = [0]
|
|
idx: t.CInt = 0
|
|
for ch in s:
|
|
if idx < 3:
|
|
chars[idx] = ch
|
|
idx += 1
|
|
if chars[0] == 65 and chars[1] == 66 and chars[2] == 67:
|
|
stdio.printf("PASS: for-in char* (A=%d B=%d C=%d)\n", chars[0], chars[1], chars[2])
|
|
else:
|
|
stdio.printf("FAIL: for-in char* (A=%d B=%d C=%d, expect 65 66 67)\n", chars[0], chars[1], chars[2])
|
|
|
|
def test_strcpy_for_in():
|
|
stdio.printf("--- Test 3: strcpy using for-in ---\n")
|
|
src: t.CChar | t.CPtr = "test"
|
|
# 手动实现 strcpy 用 for-in
|
|
import w32.win32memory
|
|
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)
|
|
# 用 for-in 复制
|
|
p: t.CChar | t.CPtr = buf
|
|
for ch in src:
|
|
p[0] = ch
|
|
p += 1
|
|
p[0] = 0
|
|
cmp_result: t.CInt = string.strcmp(buf, "test")
|
|
if cmp_result == 0:
|
|
stdio.printf("PASS: strcpy for-in (result=%s)\n", buf)
|
|
else:
|
|
stdio.printf("FAIL: strcpy for-in (cmp=%d)\n", cmp_result)
|
|
|
|
import w32.win32memory
|
|
|
|
def main() -> t.CInt:
|
|
stdio.printf("=== IterTest: 指针迭代测试 ===\n\n")
|
|
test_for_in_string()
|
|
test_for_in_char_ptr()
|
|
test_strcpy_for_in()
|
|
stdio.printf("\n=== IterTest Complete ===\n")
|
|
return 0
|