修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -10,7 +10,7 @@ class Inner:
@t.Object
class Container:
items: list[Inner, 10]
items: t.CArray[Inner, 10]
count: t.CInt
def __init__(self):

View File

@@ -2,28 +2,26 @@ import t
import stdio
import string
import c
import testcheck
# ============================================================
# Test 1: 字符串指针迭代 - 逐字符直到\0
# ============================================================
def test_string_ptr_iter():
stdio.printf("--- Test 1: string pointer iteration ---\n")
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:
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)
testcheck.check(count == 5, "ptr iter (count=5)", "ptr iter (count expect 5)")
# ============================================================
# Test 2: 字符串指针求和ASCII值
# ============================================================
def test_string_ascii_sum():
stdio.printf("--- Test 2: string ASCII sum ---\n")
testcheck.section("Test 2: string ASCII sum")
s: t.CChar | t.CPtr = "ABC"
p: t.CChar | t.CPtr = s
total: t.CInt = 0
@@ -31,16 +29,13 @@ def test_string_ascii_sum():
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)
testcheck.check(total == 198, "ASCII sum (total=198)", "ASCII sum (total expect 198)")
# ============================================================
# Test 3: 手动strcpy - 指针复制
# ============================================================
def test_manual_strcpy():
stdio.printf("--- Test 3: manual strcpy via pointers ---\n")
testcheck.section("Test 3: manual strcpy via pointers")
src: t.CChar | t.CPtr = "Test"
dst: list[t.CChar, 32] = [0]
sp: t.CChar | t.CPtr = src
@@ -50,16 +45,13 @@ def test_manual_strcpy():
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])
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)")
# ============================================================
# Test 4: 指针比较和偏移
# ============================================================
def test_ptr_offset_compare():
stdio.printf("--- Test 4: pointer offset and compare ---\n")
testcheck.section("Test 4: pointer offset and compare")
arr: list[t.CInt, 5] = [0]
for i in range(5):
arr[i] = i * 10
@@ -69,32 +61,26 @@ def test_ptr_offset_compare():
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)
testcheck.check(total == 100, "ptr offset (total=100)", "ptr offset (total expect 100)")
# ============================================================
# Test 5: 字符串长度手动计算
# ============================================================
def test_manual_strlen():
stdio.printf("--- Test 5: manual strlen ---\n")
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:
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)
testcheck.check(length == 12, "manual strlen (len=12)", "manual strlen (len expect 12)")
# ============================================================
# Test 6: 指针反向遍历字符串
# ============================================================
def test_reverse_string_ptr():
stdio.printf("--- Test 6: reverse string via pointer ---\n")
testcheck.section("Test 6: reverse string via pointer")
src: t.CChar | t.CPtr = "abcd"
dst: list[t.CChar, 32] = [0]
# Find end of src
@@ -112,31 +98,25 @@ def test_reverse_string_ptr():
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])
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)")
# ============================================================
# Test 7: 多级指针 - 指向指针的指针
# ============================================================
def test_double_pointer():
stdio.printf("--- Test 7: double pointer ---\n")
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)
# 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)
testcheck.check(val == 42, "double pointer (val=42)", "double pointer (val expect 42)")
# ============================================================
# Test 8: 指针算术 - 数组元素访问
# ============================================================
def test_ptr_arithmetic():
stdio.printf("--- Test 8: pointer arithmetic ---\n")
testcheck.section("Test 8: pointer arithmetic")
arr: list[t.CInt, 8] = [0]
for i in range(8):
arr[i] = (i + 1) * (i + 1)
@@ -144,16 +124,13 @@ def test_ptr_arithmetic():
# 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)
testcheck.check(val == 16, "ptr arithmetic (arr[3]=16)", "ptr arithmetic (arr[3] expect 16)")
# ============================================================
# Test 9: 字符串比较 - 逐字符指针
# ============================================================
def test_manual_strcmp():
stdio.printf("--- Test 9: manual strcmp via pointers ---\n")
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
@@ -167,10 +144,7 @@ def test_manual_strcmp():
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)
testcheck.check(equal == 1, "manual strcmp (equal=1)", "manual strcmp (equal expect 1)")
# ============================================================
# Test 10: 结构体指针访问
@@ -180,7 +154,7 @@ class Vec2:
y: t.CInt
def test_struct_ptr_access():
stdio.printf("--- Test 10: struct pointer access ---\n")
testcheck.section("Test 10: struct pointer access")
v: Vec2 = Vec2()
v.x = 10
v.y = 20
@@ -188,13 +162,10 @@ def test_struct_ptr_access():
# 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)
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:
stdio.printf("=== StringIterTest: String/Pointer Iteration Tests ===\n\n")
testcheck.begin("StringIterTest: String/Pointer Iteration Tests")
test_string_ptr_iter()
test_string_ascii_sum()
test_manual_strcpy()
@@ -205,5 +176,4 @@ def main() -> t.CInt:
test_ptr_arithmetic()
test_manual_strcmp()
test_struct_ptr_access()
stdio.printf("\n=== StringIterTest Complete ===\n")
return 0
return testcheck.end()

View File

@@ -1 +0,0 @@
{"stdint": "56cdd754a8a09347", "string": "6aee24fdefa3cbc0", "stdio": "73edbcf76e32d00b"}

View File

@@ -1 +0,0 @@
{"main": "2255fdda7aed5595", "stdint": "56cdd754a8a09347", "string": "6aee24fdefa3cbc0", "stdio": "73edbcf76e32d00b"}

View File

@@ -0,0 +1 @@
{"stdint": "49bd8cba55979f76", "stdio": "73edbcf76e32d00b", "testcheck": "9dbecd0942a39782", "string": "b5f1dc665c52a1bd"}

View File

@@ -0,0 +1 @@
{"stdint": "49bd8cba55979f76", "stdio": "73edbcf76e32d00b", "main": "924ed8cd4ea5c4c2", "testcheck": "9dbecd0942a39782", "string": "b5f1dc665c52a1bd"}

Binary file not shown.

View File

@@ -0,0 +1 @@
{"stdint": "49bd8cba55979f76", "stdio": "73edbcf76e32d00b", "main": "924ed8cd4ea5c4c2", "testcheck": "9dbecd0942a39782", "string": "b5f1dc665c52a1bd"}

View File

@@ -64,4 +64,12 @@ CHAR16: t.CTypedef = t.CChar16T
CHAR32: t.CTypedef = t.CChar32T
CHAR8PTR: t.CTypedef = t.CChar8T | t.CPtr
CHAR16PTR: t.CTypedef = t.CChar16T | t.CPtr
CHAR32PTR: t.CTypedef = t.CChar32T | t.CPtr
CHAR32PTR: t.CTypedef = t.CChar32T | t.CPtr
i8: t.CTypedef = t.CInt8T
i16: t.CTypedef = t.CInt16T
i32: t.CTypedef = t.CInt32T
i64: t.CTypedef = t.CInt64T
u8: t.CTypedef = t.CUInt8T
u16: t.CTypedef = t.CUInt16T
u32: t.CTypedef = t.CUInt32T
u64: t.CTypedef = t.CUInt64T

View File

@@ -8,6 +8,7 @@ import t
import stdio
import string
import c
import testcheck
def test_string_ptr_iter() -> t.CInt: pass

View File

@@ -0,0 +1,25 @@
"""
Auto-generated Python stub file from testcheck.py
Module: testcheck
"""
import t, c
import stdio
_pass_count: t.CExtern | t.CInt
_fail_count: t.CExtern | t.CInt
def begin(name: str) -> t.CInt: pass
def section(name: str) -> t.CInt: pass
def ok(msg: str) -> t.CInt: pass
def fail(msg: str) -> t.CInt: pass
def check(cond: t.CInt, ok_msg: str, fail_msg: str) -> t.CInt: pass
def info(msg: str) -> t.CInt: pass
def end() -> t.CInt: pass

View File

@@ -1,4 +1,5 @@
2255fdda7aed5595:main.py
56cdd754a8a09347:includes/stdint.py
6aee24fdefa3cbc0:includes/string.py
49bd8cba55979f76:includes/stdint.py
73edbcf76e32d00b:includes/stdio.py
924ed8cd4ea5c4c2:main.py
9dbecd0942a39782:includes/testcheck.py
b5f1dc665c52a1bd:includes/string.py

View File

@@ -41,4 +41,4 @@ def atoll(src: str) -> t.CInt64T: pass
def atof(src: str) -> t.CDouble: pass
def split(s: str, delim: str, result: list[str]) -> int: pass
def split(s: str, delim: str, result: t.CArray[str]) -> int: pass