210 lines
7.2 KiB
Python
210 lines
7.2 KiB
Python
import t
|
|
import stdio
|
|
import string
|
|
import c
|
|
|
|
# ============================================================
|
|
# Test 1: 字符串指针迭代 - 逐字符直到\0
|
|
# ============================================================
|
|
def test_string_ptr_iter():
|
|
stdio.printf("--- Test 1: string pointer iteration ---\n")
|
|
s: t.CChar | t.CPtr = "Hello"
|
|
p: t.CChar | t.CPtr = s
|
|
count: t.CInt = 0
|
|
while c.Deref(p) != 0:
|
|
count += 1
|
|
p += 1
|
|
if count == 5:
|
|
stdio.printf("PASS: ptr iter (count=%d)\n", count)
|
|
else:
|
|
stdio.printf("FAIL: ptr iter (count=%d, expect 5)\n", count)
|
|
|
|
# ============================================================
|
|
# Test 2: 字符串指针求和ASCII值
|
|
# ============================================================
|
|
def test_string_ascii_sum():
|
|
stdio.printf("--- Test 2: string ASCII sum ---\n")
|
|
s: t.CChar | t.CPtr = "ABC"
|
|
p: t.CChar | t.CPtr = s
|
|
total: t.CInt = 0
|
|
while c.Deref(p) != 0:
|
|
total += t.CInt(c.Deref(p))
|
|
p += 1
|
|
# A=65, B=66, C=67 => 198
|
|
if total == 198:
|
|
stdio.printf("PASS: ASCII sum (total=%d)\n", total)
|
|
else:
|
|
stdio.printf("FAIL: ASCII sum (total=%d, expect 198)\n", total)
|
|
|
|
# ============================================================
|
|
# Test 3: 手动strcpy - 指针复制
|
|
# ============================================================
|
|
def test_manual_strcpy():
|
|
stdio.printf("--- Test 3: manual strcpy via pointers ---\n")
|
|
src: t.CChar | t.CPtr = "Test"
|
|
dst: list[t.CChar, 32] = [0]
|
|
sp: t.CChar | t.CPtr = src
|
|
dp: t.CChar | t.CPtr = c.Addr(dst)
|
|
while c.Deref(sp) != 0:
|
|
c.Set(c.Deref(dp), c.Deref(sp))
|
|
sp += 1
|
|
dp += 1
|
|
c.Set(c.Deref(dp), 0)
|
|
if dst[0] == 84 and dst[1] == 101 and dst[2] == 115 and dst[3] == 116:
|
|
stdio.printf("PASS: manual strcpy (T=%d e=%d s=%d t=%d)\n", dst[0], dst[1], dst[2], dst[3])
|
|
else:
|
|
stdio.printf("FAIL: manual strcpy (T=%d e=%d s=%d t=%d)\n", dst[0], dst[1], dst[2], dst[3])
|
|
|
|
# ============================================================
|
|
# Test 4: 指针比较和偏移
|
|
# ============================================================
|
|
def test_ptr_offset_compare():
|
|
stdio.printf("--- Test 4: pointer offset and compare ---\n")
|
|
arr: list[t.CInt, 5] = [0]
|
|
for i in range(5):
|
|
arr[i] = i * 10
|
|
p: t.CInt | t.CPtr = c.Addr(arr)
|
|
start: t.CInt | t.CPtr = p
|
|
total: t.CInt = 0
|
|
while p != start + 5:
|
|
total += c.Deref(p)
|
|
p += 1
|
|
if total == 100:
|
|
stdio.printf("PASS: ptr offset (total=%d)\n", total)
|
|
else:
|
|
stdio.printf("FAIL: ptr offset (total=%d, expect 100)\n", total)
|
|
|
|
# ============================================================
|
|
# Test 5: 字符串长度手动计算
|
|
# ============================================================
|
|
def test_manual_strlen():
|
|
stdio.printf("--- Test 5: manual strlen ---\n")
|
|
s: t.CChar | t.CPtr = "Hello World!"
|
|
p: t.CChar | t.CPtr = s
|
|
length: t.CSizeT = 0
|
|
while c.Deref(p) != 0:
|
|
length += 1
|
|
p += 1
|
|
if length == 12:
|
|
stdio.printf("PASS: manual strlen (len=%zu)\n", length)
|
|
else:
|
|
stdio.printf("FAIL: manual strlen (len=%zu, expect 12)\n", length)
|
|
|
|
# ============================================================
|
|
# Test 6: 指针反向遍历字符串
|
|
# ============================================================
|
|
def test_reverse_string_ptr():
|
|
stdio.printf("--- Test 6: reverse string via pointer ---\n")
|
|
src: t.CChar | t.CPtr = "abcd"
|
|
dst: list[t.CChar, 32] = [0]
|
|
# Find end of src
|
|
p: t.CChar | t.CPtr = src
|
|
length: t.CInt = 0
|
|
while c.Deref(p) != 0:
|
|
length += 1
|
|
p += 1
|
|
# Copy in reverse
|
|
dp: t.CChar | t.CPtr = c.Addr(dst)
|
|
p = src + length - 1
|
|
while p >= src:
|
|
c.Set(c.Deref(dp), c.Deref(p))
|
|
dp += 1
|
|
p -= 1
|
|
c.Set(c.Deref(dp), 0)
|
|
# "abcd" reversed = "dcba" = 100,99,98,97
|
|
if dst[0] == 100 and dst[1] == 99 and dst[2] == 98 and dst[3] == 97:
|
|
stdio.printf("PASS: reverse string (d=%d c=%d b=%d a=%d)\n", dst[0], dst[1], dst[2], dst[3])
|
|
else:
|
|
stdio.printf("FAIL: reverse string (d=%d c=%d b=%d a=%d)\n", dst[0], dst[1], dst[2], dst[3])
|
|
|
|
# ============================================================
|
|
# Test 7: 多级指针 - 指向指针的指针
|
|
# ============================================================
|
|
def test_double_pointer():
|
|
stdio.printf("--- Test 7: double pointer ---\n")
|
|
x: t.CInt = 42
|
|
px: t.CInt | t.CPtr = c.Addr(x)
|
|
ppx: t.CInt | t.CPtr | t.CPtr = c.Addr(px)
|
|
# Read through double pointer
|
|
val: t.CInt = c.Deref(c.Deref(ppx))
|
|
if val == 42:
|
|
stdio.printf("PASS: double pointer (val=%d)\n", val)
|
|
else:
|
|
stdio.printf("FAIL: double pointer (val=%d, expect 42)\n", val)
|
|
|
|
# ============================================================
|
|
# Test 8: 指针算术 - 数组元素访问
|
|
# ============================================================
|
|
def test_ptr_arithmetic():
|
|
stdio.printf("--- Test 8: pointer arithmetic ---\n")
|
|
arr: list[t.CInt, 8] = [0]
|
|
for i in range(8):
|
|
arr[i] = (i + 1) * (i + 1)
|
|
p: t.CInt | t.CPtr = c.Addr(arr)
|
|
# Access arr[3] via pointer: p + 3
|
|
val: t.CInt = c.Deref(p + 3)
|
|
# arr[3] = 4*4 = 16
|
|
if val == 16:
|
|
stdio.printf("PASS: ptr arithmetic (arr[3]=%d)\n", val)
|
|
else:
|
|
stdio.printf("FAIL: ptr arithmetic (arr[3]=%d, expect 16)\n", val)
|
|
|
|
# ============================================================
|
|
# Test 9: 字符串比较 - 逐字符指针
|
|
# ============================================================
|
|
def test_manual_strcmp():
|
|
stdio.printf("--- Test 9: manual strcmp via pointers ---\n")
|
|
s1: t.CChar | t.CPtr = "Hello"
|
|
s2: t.CChar | t.CPtr = "Hello"
|
|
p1: t.CChar | t.CPtr = s1
|
|
p2: t.CChar | t.CPtr = s2
|
|
equal: t.CInt = 1
|
|
while c.Deref(p1) != 0 and c.Deref(p2) != 0:
|
|
if c.Deref(p1) != c.Deref(p2):
|
|
equal = 0
|
|
break
|
|
p1 += 1
|
|
p2 += 1
|
|
if c.Deref(p1) != c.Deref(p2):
|
|
equal = 0
|
|
if equal == 1:
|
|
stdio.printf("PASS: manual strcmp (equal=%d)\n", equal)
|
|
else:
|
|
stdio.printf("FAIL: manual strcmp (equal=%d, expect 1)\n", equal)
|
|
|
|
# ============================================================
|
|
# Test 10: 结构体指针访问
|
|
# ============================================================
|
|
class Vec2:
|
|
x: t.CInt
|
|
y: t.CInt
|
|
|
|
def test_struct_ptr_access():
|
|
stdio.printf("--- Test 10: struct pointer access ---\n")
|
|
v: Vec2 = Vec2()
|
|
v.x = 10
|
|
v.y = 20
|
|
p: Vec2 | t.CPtr = c.Addr(v)
|
|
# Modify through pointer
|
|
c.Deref(p).x = 100
|
|
c.Deref(p).y = 200
|
|
if v.x == 100 and v.y == 200:
|
|
stdio.printf("PASS: struct ptr (x=%d y=%d)\n", v.x, v.y)
|
|
else:
|
|
stdio.printf("FAIL: struct ptr (x=%d y=%d, expect 100 200)\n", v.x, v.y)
|
|
|
|
def main() -> t.CInt:
|
|
stdio.printf("=== StringIterTest: String/Pointer Iteration Tests ===\n\n")
|
|
test_string_ptr_iter()
|
|
test_string_ascii_sum()
|
|
test_manual_strcpy()
|
|
test_ptr_offset_compare()
|
|
test_manual_strlen()
|
|
test_reverse_string_ptr()
|
|
test_double_pointer()
|
|
test_ptr_arithmetic()
|
|
test_manual_strcmp()
|
|
test_struct_ptr_access()
|
|
stdio.printf("\n=== StringIterTest Complete ===\n")
|
|
return 0
|