修复了大量存在的问题,增加了假鸭子类型等等机制
This commit is contained in:
@@ -1,35 +1,30 @@
|
||||
import t
|
||||
import stdio
|
||||
import string
|
||||
import testcheck
|
||||
|
||||
def test_for_in_string():
|
||||
stdio.printf("--- Test 1: for-in string iteration ---\n")
|
||||
testcheck.section("Test 1: for-in string iteration")
|
||||
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)
|
||||
testcheck.check(result == 500, "for-in string (sum=500)", "for-in string (sum expect 500)")
|
||||
|
||||
def test_for_in_char_ptr():
|
||||
stdio.printf("--- Test 2: for-in char* iteration ---\n")
|
||||
testcheck.section("Test 2: for-in char* iteration")
|
||||
s: t.CChar | t.CPtr = "ABC"
|
||||
chars: list[t.CInt, 4] = [0]
|
||||
chars: t.CArray[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])
|
||||
testcheck.check(chars[0] == 65 and chars[1] == 66 and chars[2] == 67, "for-in char* (A=65 B=66 C=67)", "for-in char* (expect A=65 B=66 C=67)")
|
||||
|
||||
def test_strcpy_for_in():
|
||||
stdio.printf("--- Test 3: strcpy using for-in ---\n")
|
||||
testcheck.section("Test 3: strcpy using for-in")
|
||||
src: t.CChar | t.CPtr = "test"
|
||||
# 手动实现 strcpy 用 for-in
|
||||
import w32.win32memory
|
||||
@@ -45,17 +40,13 @@ def test_strcpy_for_in():
|
||||
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)
|
||||
testcheck.check(cmp_result == 0, "strcpy for-in (result=test)", "strcpy for-in (cmp mismatch)")
|
||||
|
||||
import w32.win32memory
|
||||
|
||||
def main() -> t.CInt:
|
||||
stdio.printf("=== IterTest: 指针迭代测试 ===\n\n")
|
||||
testcheck.begin("IterTest: 指针迭代测试")
|
||||
test_for_in_string()
|
||||
test_for_in_char_ptr()
|
||||
test_strcpy_for_in()
|
||||
stdio.printf("\n=== IterTest Complete ===\n")
|
||||
return 0
|
||||
return testcheck.end()
|
||||
|
||||
Reference in New Issue
Block a user