Files
Viper_RegressionTesting/IterTest/App/main.vp
T
TermiNexus aa5a7e7250 chore(syntax): Test 仓 R1 + R12 迁移
R1:T | t.CPtr -> T*(40 文件 / 1121 处;含多级 **、强转位 (T*)(x)、下标内 list[AST*]);引号前向引用 'Rect' | t.CPtr 26 处按例外保留,T | t.CPtr | t.State 25 处因 T* 后不能接 | 保持原联合写法。

R12:i = 0 + while i < N: + 尾 i += 1 -> or i in range(N):(10 文件 / 42 处;含 <= -> range(EXPR + 1)、带注解初始化 i: int = 0、尾 i = i + 1)。余 20 处经守卫判定不可安全改写(复合条件 1、尾部非自增 8、体内重绑定/continue 5、while-else 2、循环后仍被读取 2、找不到初始化 2)。
2026-10-05 21:18:37 +08:00

53 lines
1.6 KiB
Plaintext

import t
import stdio
import string
import testcheck
def test_for_in_string():
testcheck.section("Test 1: for-in string iteration")
s: char* = "Hello"
result: int = 0
for ch in s:
result += ch
# H=72, e=101, l=108, l=108, o=111 => 500
testcheck.check(result == 500, "for-in string (sum=500)", "for-in string (sum expect 500)")
def test_for_in_char_ptr():
testcheck.section("Test 2: for-in char* iteration")
s: char* = "ABC"
chars: t.CArray[int, 4] = [0]
idx: int = 0
for ch in s:
if idx < 3:
chars[idx] = ch
idx += 1
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():
testcheck.section("Test 3: strcpy using for-in")
src: char* = "test"
# 手动实现 strcpy 用 for-in
import w32.win32memory
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)
# 用 for-in 复制
p: char* = buf
for ch in src:
p[0] = ch
p += 1
p[0] = 0
# i8* == 是指针地址比较(C-Like 语义),字符串内容比较需显式 strcmp
testcheck.check(string.strcmp(buf, "test") == 0, "strcpy for-in (result=test)", "strcpy for-in (cmp mismatch)")
import w32.win32memory
def main() -> int:
testcheck.begin("IterTest: 指针迭代测试")
test_for_in_string()
test_for_in_char_ptr()
test_strcpy_for_in()
return testcheck.end()