Files
Viper_RegressionTesting/PointerTest/App/main.vp
T
TermiNexus ee6461be46 chore(comments): Test 仓注释英文化(R11)
把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
2026-10-05 22:43:38 +08:00

274 lines
8.8 KiB
Plaintext

from stdint import *
import t, c
import stdio
import string
import w32.win32console
import w32.win32memory
import testcheck
# PointerTest: pointer operations, global declarations, array decay, pointer arithmetic
# --- Test 1: global declaration effects on module-level variables ---
_g_val: int = 0
_g_ptr: uint32* = None
def test_global_write():
"""Write to module-level _g_val through the global declaration."""
global _g_val
_g_val = 42
def test_global_write_no_decl():
"""Local assignment without global leaves the module-level variable unchanged.
Uses a non-conflicting local name; same-name annotated redeclaration is a compile error.
"""
local_val: int = 99 # standalone local, unrelated to global _g_val
def test_global_ptr():
global _g_ptr
buf: uint32* = w32.win32memory.VirtualAlloc(None, 256, 0x3000, 0x04)
if buf:
string.memset(buf, 0, 256)
_g_ptr = buf
p: uint32* = _g_ptr
p[0] = 0xAABBCCDD
p[1] = 0x11223344
def test_global_ptr_no_decl():
"""Same-name annotated redeclaration of a module variable is a compile error.
Legal forms: global + unannotated assignment, or a non-conflicting local name.
"""
local_ptr: uint32* = None # standalone local, does not touch global _g_ptr
# --- Test 2: pointer arithmetic and array indexing ---
def test_pointer_arithmetic():
"""Verify pointer arithmetic computes offsets correctly."""
buf: uint32* = w32.win32memory.VirtualAlloc(None, 256, 0x3000, 0x04)
if not buf:
testcheck.fail("pointer arithmetic: VirtualAlloc returned NULL")
return
string.memset(buf, 0, 256)
# Write values
for i in range(8):
buf[i] = uint32(i * 100)
# Read back and verify
ok: int = 1
for i in range(8):
v: uint32 = buf[i]
expected: uint32 = uint32(i * 100)
if v != expected:
stdio.printf("buf[%d] = 0x%x, expected 0x%x\n", i, v, expected)
ok = 0
testcheck.check(ok, "pointer arithmetic (buf[i] = i*100)", "pointer arithmetic mismatch")
# --- Test 3: array decay to pointer ---
def test_array_decay():
"""Pass list[t.CChar, N] to a t.CConst | str parameter."""
arr: t.CArray[char, 32]
string.memset(&arr, 0, 32)
# Write string manually
src: str = "Hello"
for i in range(5):
arr[i] = char(ord(src[i]))
arr[5] = 0
# Pass to print (accepts t.CConst | str)
stdio.printf("array decay -> %s\n", &arr)
testcheck.ok("array decay (Hello)")
# --- Test 4: pointer type casts (CVoid/CPtr) ---
def test_pointer_cast():
"""Cast between pointers and integers."""
buf: uint32* = w32.win32memory.VirtualAlloc(None, 256, 0x3000, 0x04)
if not buf:
testcheck.fail("pointer cast: VirtualAlloc returned NULL")
return
string.memset(buf, 0, 256)
# Write through CVoid pointer
raw: t.CPtr = t.CPtr(uint64(buf))
# Cast CVoid pointer back to CUInt32T pointer
p32: uint32* = (uint32*)(raw)
p32[0] = 0xDEADBEEF
# Read through raw pointer
if buf[0] == 0xDEADBEEF:
testcheck.ok("pointer cast CVoid -> CUInt32T*")
else:
stdio.printf("got 0x%x\n", buf[0])
testcheck.fail("pointer cast failed")
# --- Test 5: pointer offset (byte offset vs element offset) ---
def test_pointer_offset():
"""CUInt8T* + N advances N bytes; CUInt32T* + N advances N*4 bytes."""
buf: uint8* = w32.win32memory.VirtualAlloc(None, 256, 0x3000, 0x04)
if not buf:
testcheck.fail("pointer offset: VirtualAlloc returned NULL")
return
string.memset(buf, 0, 256)
# Write 4 bytes
buf[0] = 0x41 # 'A'
buf[1] = 0x42 # 'B'
buf[2] = 0x43 # 'C'
buf[3] = 0x44 # 'D'
# Read via CUInt32T* (0x44434241 on little-endian)
p32: uint32* = (uint32*)(t.CPtr(uint64(buf)))
val: uint32 = p32[0]
# little-endian: 0x44434241
if val == 0x44434241:
testcheck.ok("byte offset vs element offset (0x44434241)")
else:
stdio.printf("expected 0x44434241, got 0x%x\n", val)
testcheck.fail("byte offset vs element offset")
# --- Test 6: struct pointer access ---
class TestStruct():
a: int
b: int
c: uint64
def test_struct_pointer():
"""Access struct members through a pointer."""
s: TestStruct
s.a = 10
s.b = 20
s.c = 0x123456789ABCDEF0
p: TestStruct* = &s
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 through pointers ---
def test_memcpy_pointers():
"""Copy data between pointers with memcpy."""
src_buf: uint32* = w32.win32memory.VirtualAlloc(None, 64, 0x3000, 0x04)
dst_buf: uint32* = w32.win32memory.VirtualAlloc(None, 64, 0x3000, 0x04)
if not src_buf or not dst_buf:
testcheck.fail("memcpy: VirtualAlloc returned NULL")
return
# Initialize src
for i in range(8):
src_buf[i] = uint32(i + 1000)
# Copy
string.memcpy(t.CPtr(uint64(dst_buf)), t.CPtr(uint64(src_buf)), 32)
# Verify
ok: int = 1
for i in range(8):
if dst_buf[i] != uint32(i + 1000):
stdio.printf("dst[%d] = %u, expected %u\n", i, dst_buf[i], i + 1000)
ok = 0
testcheck.check(ok, "memcpy between pointers", "memcpy between pointers failed")
# --- Test 8: pointer as function parameter ---
def fill_array(arr: uint32*, count: int, val: uint32):
for i in range(count):
arr[i] = val
def test_pointer_param():
"""Pass a pointer as a function parameter."""
buf: uint32* = w32.win32memory.VirtualAlloc(None, 64, 0x3000, 0x04)
if not buf:
testcheck.fail("pointer param: VirtualAlloc returned NULL")
return
fill_array(buf, 8, 0xCAFEBABE)
ok: int = 1
for i in range(8):
if buf[i] != 0xCAFEBABE:
ok = 0
testcheck.check(ok, "pointer as function parameter", "pointer as function parameter failed")
def main() -> int | t.CExport:
w32.win32console.SetConsoleCP(65001)
w32.win32console.SetConsoleOutputCP(65001)
print("你好")
l: t.CArray[char, 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[char, 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 declaration
testcheck.section("Test 1: global declaration")
global _g_val
_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)
testcheck.check(_g_val == 42, "global declaration (_g_val = 42)", "global declaration failed")
# Test 1b: global pointer
testcheck.section("Test 1b: global pointer")
test_global_ptr()
if _g_ptr:
p: uint32* = _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:局部遮蔽未改全局)\n", t.CPtr(uint64(_g_ptr)))
if _g_ptr:
testcheck.ok("global ptr no decl (global untouched by local shadow)")
else:
testcheck.fail("global ptr no decl: 无 global 声明的局部遮蔽不应修改全局 _g_ptr")
# Test 2: pointer arithmetic
testcheck.section("Test 2: pointer arithmetic")
test_pointer_arithmetic()
# Test 3: array decay
testcheck.section("Test 3: array decay")
test_array_decay()
# Test 4: pointer cast
testcheck.section("Test 4: pointer cast")
test_pointer_cast()
# Test 5: pointer offset
testcheck.section("Test 5: pointer offset (byte vs element)")
test_pointer_offset()
# Test 6: struct pointer
testcheck.section("Test 6: struct pointer access")
test_struct_pointer()
# Test 7: memcpy pointers
testcheck.section("Test 7: memcpy between pointers")
test_memcpy_pointers()
# Test 8: pointer as param
testcheck.section("Test 8: pointer as function parameter")
test_pointer_param()
return testcheck.end()