chore(test): 迁移测试夹具到新语法

机械规则 R2/R3/R4/R6/R7/R8/R13 + R16 保守子集,共 86 文件 2127 行。R1(T | t.CPtr)与 R12(while 下标循环)未做,仍列为待办。全量 77/77 顺序回归通过。
This commit is contained in:
TermiNexus committed 2026-10-05 19:59:02 +08:00
1 parent c9d741c202
commit f5da836fbb
86 files changed
+2121 -2121

No files matched your search

+7 -7
View File
@@ -5,26 +5,26 @@ import c
# Test: class with inline list[struct, N] field
class Inner:
code: t.CUnsignedInt
bits: t.CInt
code: uint
bits: int
class Container:
items: t.CArray[Inner, 10]
count: t.CInt
count: int
def __init__(self):
self.count = 0
def fill(self):
for i in range(10):
self.items[i].code = t.CUnsignedInt(i * 100)
self.items[i].code = uint(i * 100)
self.items[i].bits = i
self.count += 1
def check(self):
ok: t.CInt = 1
ok: int = 1
for i in range(10):
if self.items[i].code != t.CUnsignedInt(i * 100):
if self.items[i].code != uint(i * 100):
stdio.printf("FAIL: items[%d].code=%u expect %d\n", i, self.items[i].code, i * 100)
ok = 0
if self.items[i].bits != i:
@@ -35,7 +35,7 @@ class Container:
else:
stdio.printf("FAIL: inline list[struct, N] in Container\n")
def inline_array_test() -> t.CInt:
def inline_array_test() -> int:
stdio.printf("=== InlineArrayTest ===\n")
c1 = Container()
c1.fill()
+54 -54
View File
@@ -10,10 +10,10 @@ import inline_test
# ============================================================
def test_string_ptr_iter():
testcheck.section("Test 1: string pointer iteration")
s: t.CChar | t.CPtr = "Hello"
p: t.CChar | t.CPtr = s
count: t.CInt = 0
while c.Deref(p) != 0:
s: char | t.CPtr = "Hello"
p: char | t.CPtr = s
count: int = 0
while *p != 0:
count += 1
p += 1
testcheck.check(count == 5, "ptr iter (count=5)", "ptr iter (count expect 5)")
@@ -23,11 +23,11 @@ def test_string_ptr_iter():
# ============================================================
def test_string_ascii_sum():
testcheck.section("Test 2: string ASCII sum")
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))
s: char | t.CPtr = "ABC"
p: char | t.CPtr = s
total: int = 0
while *p != 0:
total += int(*p)
p += 1
# A=65, B=66, C=67 => 198
testcheck.check(total == 198, "ASCII sum (total=198)", "ASCII sum (total expect 198)")
@@ -37,15 +37,15 @@ def test_string_ascii_sum():
# ============================================================
def test_manual_strcpy():
testcheck.section("Test 3: manual strcpy via pointers")
src: t.CChar | t.CPtr = "Test"
dst: t.CArray[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))
src: char | t.CPtr = "Test"
dst: t.CArray[char, 32] = [0]
sp: char | t.CPtr = src
dp: char | t.CPtr = &dst
while *sp != 0:
*dp = *sp
sp += 1
dp += 1
c.Set(c.Deref(dp), 0)
*dp = 0
testcheck.check(dst[0] == 84 and dst[1] == 101 and dst[2] == 115 and dst[3] == 116, "manual strcpy (T=84 e=101 s=115 t=116)", "manual strcpy (expect T=84 e=101 s=115 t=116)")
# ============================================================
@@ -53,14 +53,14 @@ def test_manual_strcpy():
# ============================================================
def test_ptr_offset_compare():
testcheck.section("Test 4: pointer offset and compare")
arr: t.CArray[t.CInt, 5] = [0]
arr: t.CArray[int, 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
p: int | t.CPtr = &arr
start: int | t.CPtr = p
total: int = 0
while p != start + 5:
total += c.Deref(p)
total += *p
p += 1
testcheck.check(total == 100, "ptr offset (total=100)", "ptr offset (total expect 100)")
@@ -69,10 +69,10 @@ def test_ptr_offset_compare():
# ============================================================
def test_manual_strlen():
testcheck.section("Test 5: manual strlen")
s: t.CChar | t.CPtr = "Hello World!"
p: t.CChar | t.CPtr = s
length: t.CSizeT = 0
while c.Deref(p) != 0:
s: char | t.CPtr = "Hello World!"
p: char | t.CPtr = s
length: sizet = 0
while *p != 0:
length += 1
p += 1
testcheck.check(length == 12, "manual strlen (len=12)", "manual strlen (len expect 12)")
@@ -82,22 +82,22 @@ def test_manual_strlen():
# ============================================================
def test_reverse_string_ptr():
testcheck.section("Test 6: reverse string via pointer")
src: t.CChar | t.CPtr = "abcd"
dst: t.CArray[t.CChar, 32] = [0]
src: char | t.CPtr = "abcd"
dst: t.CArray[char, 32] = [0]
# Find end of src
p: t.CChar | t.CPtr = src
length: t.CInt = 0
while c.Deref(p) != 0:
p: char | t.CPtr = src
length: int = 0
while *p != 0:
length += 1
p += 1
# Copy in reverse
dp: t.CChar | t.CPtr = c.Addr(dst)
dp: char | t.CPtr = &dst
p = src + length - 1
while p >= src:
c.Set(c.Deref(dp), c.Deref(p))
*dp = *p
dp += 1
p -= 1
c.Set(c.Deref(dp), 0)
*dp = 0
# "abcd" reversed = "dcba" = 100,99,98,97
testcheck.check(dst[0] == 100 and dst[1] == 99 and dst[2] == 98 and dst[3] == 97, "reverse string (d=100 c=99 b=98 a=97)", "reverse string (expect d=100 c=99 b=98 a=97)")
@@ -106,11 +106,11 @@ def test_reverse_string_ptr():
# ============================================================
def test_double_pointer():
testcheck.section("Test 7: double pointer")
x: t.CInt = 42
px: t.CInt | t.CPtr = c.Addr(x)
ppx: t.CInt | t.CPtr | t.CPtr = c.Addr(px)
x: int = 42
px: int | t.CPtr = &x
ppx: int | t.CPtr | t.CPtr = &px
# Read through double pointer
val: t.CInt = c.Deref(c.Deref(ppx))
val: int = *(*ppx)
testcheck.check(val == 42, "double pointer (val=42)", "double pointer (val expect 42)")
# ============================================================
@@ -118,12 +118,12 @@ def test_double_pointer():
# ============================================================
def test_ptr_arithmetic():
testcheck.section("Test 8: pointer arithmetic")
arr: t.CArray[t.CInt, 8] = [0]
arr: t.CArray[int, 8] = [0]
for i in range(8):
arr[i] = (i + 1) * (i + 1)
p: t.CInt | t.CPtr = c.Addr(arr)
p: int | t.CPtr = &arr
# Access arr[3] via pointer: p + 3
val: t.CInt = c.Deref(p + 3)
val: int = *(p + 3)
# arr[3] = 4*4 = 16
testcheck.check(val == 16, "ptr arithmetic (arr[3]=16)", "ptr arithmetic (arr[3] expect 16)")
@@ -132,18 +132,18 @@ def test_ptr_arithmetic():
# ============================================================
def test_manual_strcmp():
testcheck.section("Test 9: manual strcmp via pointers")
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):
s1: char | t.CPtr = "Hello"
s2: char | t.CPtr = "Hello"
p1: char | t.CPtr = s1
p2: char | t.CPtr = s2
equal: int = 1
while *p1 != 0 and *p2 != 0:
if *p1 != *p2:
equal = 0
break
p1 += 1
p2 += 1
if c.Deref(p1) != c.Deref(p2):
if *p1 != *p2:
equal = 0
testcheck.check(equal == 1, "manual strcmp (equal=1)", "manual strcmp (equal expect 1)")
@@ -151,21 +151,21 @@ def test_manual_strcmp():
# Test 10: 结构体指针访问
# ============================================================
class Vec2:
x: t.CInt
y: t.CInt
x: int
y: int
def test_struct_ptr_access():
testcheck.section("Test 10: struct pointer access")
v: Vec2 = Vec2()
v.x = 10
v.y = 20
p: Vec2 | t.CPtr = c.Addr(v)
p: Vec2 | t.CPtr = &v
# Modify through pointer
c.Deref(p).x = 100
c.Deref(p).y = 200
(*p).x = 100
(*p).y = 200
testcheck.check(v.x == 100 and v.y == 200, "struct ptr (x=100 y=200)", "struct ptr (x y expect 100 200)")
def main() -> t.CInt:
def main() -> int:
testcheck.begin("StringIterTest: String/Pointer Iteration Tests")
test_string_ptr_iter()
test_string_ascii_sum()