修复了大量存在的问题,增加了假鸭子类型等等机制
This commit is contained in:
@@ -4,6 +4,7 @@ import stdio
|
||||
import string
|
||||
import w32.win32console
|
||||
import w32.win32memory
|
||||
import testcheck
|
||||
|
||||
# ============================================================
|
||||
# PointerTest - 测试指针操作、global声明、数组退化、指针算术
|
||||
@@ -49,7 +50,7 @@ def test_pointer_arithmetic():
|
||||
"""测试指针算术:偏移计算是否正确"""
|
||||
buf: t.CUInt32T | t.CPtr = w32.win32memory.VirtualAlloc(None, 256, 0x3000, 0x04)
|
||||
if not buf:
|
||||
stdio.printf("FAIL: VirtualAlloc returned NULL\n")
|
||||
testcheck.fail("pointer arithmetic: VirtualAlloc returned NULL")
|
||||
return
|
||||
string.memset(buf, 0, 256)
|
||||
|
||||
@@ -63,16 +64,15 @@ def test_pointer_arithmetic():
|
||||
v: t.CUInt32T = buf[i]
|
||||
expected: t.CUInt32T = t.CUInt32T(i * 100)
|
||||
if v != expected:
|
||||
stdio.printf("FAIL: buf[%d] = 0x%x, expected 0x%x\n", i, v, expected)
|
||||
stdio.printf("buf[%d] = 0x%x, expected 0x%x\n", i, v, expected)
|
||||
ok = 0
|
||||
if ok:
|
||||
stdio.printf("PASS: pointer arithmetic (buf[i] = i*100)\n")
|
||||
testcheck.check(ok, "pointer arithmetic (buf[i] = i*100)", "pointer arithmetic mismatch")
|
||||
|
||||
|
||||
# --- Test 3: 数组退化为指针 ---
|
||||
def test_array_decay():
|
||||
"""测试 list[t.CChar, N] 传递给 t.CConst | str 参数"""
|
||||
arr: list[t.CChar, 32]
|
||||
arr: t.CArray[t.CChar, 32]
|
||||
string.memset(c.Addr(arr), 0, 32)
|
||||
# 手动写入字符串
|
||||
src: str = "Hello"
|
||||
@@ -81,7 +81,8 @@ def test_array_decay():
|
||||
arr[5] = 0
|
||||
|
||||
# 传递给 print (接受 t.CConst | str)
|
||||
stdio.printf("PASS: array decay -> %s\n", c.Addr(arr))
|
||||
stdio.printf("array decay -> %s\n", c.Addr(arr))
|
||||
testcheck.ok("array decay (Hello)")
|
||||
|
||||
|
||||
# --- Test 4: 指针类型转换 (CVoid/CPtr) ---
|
||||
@@ -89,7 +90,7 @@ def test_pointer_cast():
|
||||
"""测试指针与整数之间的类型转换"""
|
||||
buf: t.CUInt32T | t.CPtr = w32.win32memory.VirtualAlloc(None, 256, 0x3000, 0x04)
|
||||
if not buf:
|
||||
stdio.printf("FAIL: VirtualAlloc returned NULL\n")
|
||||
testcheck.fail("pointer cast: VirtualAlloc returned NULL")
|
||||
return
|
||||
string.memset(buf, 0, 256)
|
||||
|
||||
@@ -101,9 +102,10 @@ def test_pointer_cast():
|
||||
|
||||
# 通过原始指针读取
|
||||
if buf[0] == 0xDEADBEEF:
|
||||
stdio.printf("PASS: pointer cast CVoid -> CUInt32T*\n")
|
||||
testcheck.ok("pointer cast CVoid -> CUInt32T*")
|
||||
else:
|
||||
stdio.printf("FAIL: pointer cast, got 0x%x\n", buf[0])
|
||||
stdio.printf("got 0x%x\n", buf[0])
|
||||
testcheck.fail("pointer cast failed")
|
||||
|
||||
|
||||
# --- Test 5: 指针偏移计算 (byte offset vs element offset) ---
|
||||
@@ -111,7 +113,7 @@ def test_pointer_offset():
|
||||
"""测试指针偏移:CUInt8T* + N 应该偏移 N 字节,CUInt32T* + N 应该偏移 N*4 字节"""
|
||||
buf: t.CUInt8T | t.CPtr = w32.win32memory.VirtualAlloc(None, 256, 0x3000, 0x04)
|
||||
if not buf:
|
||||
stdio.printf("FAIL: VirtualAlloc returned NULL\n")
|
||||
testcheck.fail("pointer offset: VirtualAlloc returned NULL")
|
||||
return
|
||||
string.memset(buf, 0, 256)
|
||||
|
||||
@@ -127,9 +129,10 @@ def test_pointer_offset():
|
||||
|
||||
# little-endian: 0x44434241
|
||||
if val == 0x44434241:
|
||||
stdio.printf("PASS: byte offset vs element offset (0x%x)\n", val)
|
||||
testcheck.ok("byte offset vs element offset (0x44434241)")
|
||||
else:
|
||||
stdio.printf("FAIL: expected 0x44434241, got 0x%x\n", val)
|
||||
stdio.printf("expected 0x44434241, got 0x%x\n", val)
|
||||
testcheck.fail("byte offset vs element offset")
|
||||
|
||||
|
||||
# --- Test 6: 结构体指针访问 ---
|
||||
@@ -146,10 +149,8 @@ def test_struct_pointer():
|
||||
s.c = 0x123456789ABCDEF0
|
||||
|
||||
p: TestStruct | t.CPtr = c.Addr(s)
|
||||
if p.a == 10 and p.b == 20 and p.c == 0x123456789ABCDEF0:
|
||||
stdio.printf("PASS: struct pointer access (a=%d b=%d c=0x%llx)\n", p.a, p.b, p.c)
|
||||
else:
|
||||
stdio.printf("FAIL: struct pointer access (a=%d b=%d c=0x%llx)\n", p.a, p.b, p.c)
|
||||
stdio.printf("struct pointer access (a=%d b=%d c=0x%llx)\n", p.a, p.b, p.c)
|
||||
testcheck.check(p.a == 10 and p.b == 20 and p.c == 0x123456789ABCDEF0, "struct pointer access", "struct pointer access mismatch")
|
||||
|
||||
|
||||
# --- Test 7: memcpy/memset 通过指针 ---
|
||||
@@ -158,7 +159,7 @@ def test_memcpy_pointers():
|
||||
src_buf: t.CUInt32T | t.CPtr = w32.win32memory.VirtualAlloc(None, 64, 0x3000, 0x04)
|
||||
dst_buf: t.CUInt32T | t.CPtr = w32.win32memory.VirtualAlloc(None, 64, 0x3000, 0x04)
|
||||
if not src_buf or not dst_buf:
|
||||
stdio.printf("FAIL: VirtualAlloc returned NULL\n")
|
||||
testcheck.fail("memcpy: VirtualAlloc returned NULL")
|
||||
return
|
||||
|
||||
# 初始化 src
|
||||
@@ -172,10 +173,9 @@ def test_memcpy_pointers():
|
||||
ok: t.CInt = 1
|
||||
for i in range(8):
|
||||
if dst_buf[i] != t.CUInt32T(i + 1000):
|
||||
stdio.printf("FAIL: dst[%d] = %u, expected %u\n", i, dst_buf[i], i + 1000)
|
||||
stdio.printf("dst[%d] = %u, expected %u\n", i, dst_buf[i], i + 1000)
|
||||
ok = 0
|
||||
if ok:
|
||||
stdio.printf("PASS: memcpy between pointers\n")
|
||||
testcheck.check(ok, "memcpy between pointers", "memcpy between pointers failed")
|
||||
|
||||
|
||||
# --- Test 8: 指针作为函数参数 ---
|
||||
@@ -187,7 +187,7 @@ def test_pointer_param():
|
||||
"""测试指针作为函数参数传递"""
|
||||
buf: t.CUInt32T | t.CPtr = w32.win32memory.VirtualAlloc(None, 64, 0x3000, 0x04)
|
||||
if not buf:
|
||||
stdio.printf("FAIL: VirtualAlloc returned NULL\n")
|
||||
testcheck.fail("pointer param: VirtualAlloc returned NULL")
|
||||
return
|
||||
|
||||
fill_array(buf, 8, 0xCAFEBABE)
|
||||
@@ -196,72 +196,77 @@ def test_pointer_param():
|
||||
for i in range(8):
|
||||
if buf[i] != 0xCAFEBABE:
|
||||
ok = 0
|
||||
if ok:
|
||||
stdio.printf("PASS: pointer as function parameter\n")
|
||||
else:
|
||||
stdio.printf("FAIL: pointer as function parameter\n")
|
||||
testcheck.check(ok, "pointer as function parameter", "pointer as function parameter failed")
|
||||
|
||||
|
||||
def main() -> t.CInt | t.CExport:
|
||||
w32.win32console.SetConsoleCP(65001)
|
||||
w32.win32console.SetConsoleOutputCP(65001)
|
||||
|
||||
stdio.printf("=== PointerTest: 指针操作基准测试 ===\n\n")
|
||||
print("你好")
|
||||
l: t.CArray[t.CChar, 2] = 0
|
||||
for i in "hello":
|
||||
l[0] = i
|
||||
print(l)
|
||||
|
||||
u: t.CArray[t.CUInt64T | t.BigEndian, 2] = [0x4141414141414141, 0x0000000000000000]
|
||||
for i in c.Addr(u):
|
||||
# l[0] = i
|
||||
stdio.printf("0x%llx\n", i)
|
||||
|
||||
k: t.CArray[t.CChar, 12] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C']
|
||||
for i in k:
|
||||
stdio.printf("%c\n", i)
|
||||
|
||||
testcheck.begin("PointerTest: 指针操作基准测试")
|
||||
|
||||
# Test 1: global 声明
|
||||
stdio.printf("--- Test 1: global declaration ---\n")
|
||||
testcheck.section("Test 1: global declaration")
|
||||
_g_val = 0
|
||||
test_global_write()
|
||||
stdio.printf("After test_global_write(): _g_val = %d (expect 42)\n", _g_val)
|
||||
test_global_write_no_decl()
|
||||
stdio.printf("After test_global_write_no_decl(): _g_val = %d (expect 42, not 99)\n", _g_val)
|
||||
stdio.printf("\n")
|
||||
testcheck.check(_g_val == 42, "global declaration (_g_val = 42)", "global declaration failed")
|
||||
|
||||
# Test 1b: global pointer
|
||||
stdio.printf("--- Test 1b: global pointer ---\n")
|
||||
testcheck.section("Test 1b: global pointer")
|
||||
test_global_ptr()
|
||||
if _g_ptr:
|
||||
p: t.CUInt32T | t.CPtr = _g_ptr
|
||||
stdio.printf("_g_ptr[0] = 0x%x (expect 0xAABBCCDD)\n", p[0])
|
||||
stdio.printf("_g_ptr[1] = 0x%x (expect 0x11223344)\n", p[1])
|
||||
testcheck.check(p[0] == 0xAABBCCDD and p[1] == 0x11223344, "global pointer values", "global pointer mismatch")
|
||||
test_global_ptr_no_decl()
|
||||
stdio.printf("After test_global_ptr_no_decl(): _g_ptr = %p (expect NULL/0)\n", t.CVoid(t.CUInt64T(_g_ptr), t.CPtr))
|
||||
stdio.printf("\n")
|
||||
testcheck.ok("global ptr no decl (set to NULL)")
|
||||
|
||||
# Test 2: pointer arithmetic
|
||||
stdio.printf("--- Test 2: pointer arithmetic ---\n")
|
||||
testcheck.section("Test 2: pointer arithmetic")
|
||||
test_pointer_arithmetic()
|
||||
stdio.printf("\n")
|
||||
|
||||
# Test 3: array decay
|
||||
stdio.printf("--- Test 3: array decay ---\n")
|
||||
testcheck.section("Test 3: array decay")
|
||||
test_array_decay()
|
||||
stdio.printf("\n")
|
||||
|
||||
# Test 4: pointer cast
|
||||
stdio.printf("--- Test 4: pointer cast ---\n")
|
||||
testcheck.section("Test 4: pointer cast")
|
||||
test_pointer_cast()
|
||||
stdio.printf("\n")
|
||||
|
||||
# Test 5: pointer offset
|
||||
stdio.printf("--- Test 5: pointer offset (byte vs element) ---\n")
|
||||
testcheck.section("Test 5: pointer offset (byte vs element)")
|
||||
test_pointer_offset()
|
||||
stdio.printf("\n")
|
||||
|
||||
# Test 6: struct pointer
|
||||
stdio.printf("--- Test 6: struct pointer access ---\n")
|
||||
testcheck.section("Test 6: struct pointer access")
|
||||
test_struct_pointer()
|
||||
stdio.printf("\n")
|
||||
|
||||
# Test 7: memcpy pointers
|
||||
stdio.printf("--- Test 7: memcpy between pointers ---\n")
|
||||
testcheck.section("Test 7: memcpy between pointers")
|
||||
test_memcpy_pointers()
|
||||
stdio.printf("\n")
|
||||
|
||||
# Test 8: pointer as param
|
||||
stdio.printf("--- Test 8: pointer as function parameter ---\n")
|
||||
testcheck.section("Test 8: pointer as function parameter")
|
||||
test_pointer_param()
|
||||
stdio.printf("\n")
|
||||
|
||||
stdio.printf("=== PointerTest Complete ===\n")
|
||||
return 0
|
||||
return testcheck.end()
|
||||
|
||||
Reference in New Issue
Block a user