chore(comments): Test 仓注释英文化(R11)

把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
This commit is contained in:
TermiNexus committed 2026-10-05 22:43:38 +08:00
1 parent aa5a7e7250
commit ee6461be46
81 files changed
+1231 -1925

No files matched your search

+36 -44
View File
@@ -6,27 +6,23 @@ import w32.win32console
import w32.win32memory
import testcheck
# ============================================================
# PointerTest - 测试指针操作、global声明、数组退化、指针算术
# ============================================================
# PointerTest: pointer operations, global declarations, array decay, pointer arithmetic
# --- Test 1: global 声明对模块级变量的影响 ---
# --- Test 1: global declaration effects on module-level variables ---
_g_val: int = 0
_g_ptr: uint32* = None
def test_global_write():
"""测试:缺少 global 声明时,赋值是否修改模块级变量"""
"""Write to module-level _g_val through the global declaration."""
global _g_val
_g_val = 42
def test_global_write_no_decl():
"""测试:没有 global 声明时,函数内局部赋值不修改模块级变量
"""Local assignment without global leaves the module-level variable unchanged.
新规则:模块级已有 `_g_val` 时,函数内再写 `_g_val: int = ...`(注解同名)
一律判为"重复声明"并终止编译(TypeError)——**不管**注解类型是否与全局相同。
因此这里改用**不重名**的局部变量验证"函数内局部赋值不影响全局"。
Uses a non-conflicting local name; same-name annotated redeclaration is a compile error.
"""
local_val: int = 99 # 独立局部变量,与全局 _g_val 无关
local_val: int = 99 # standalone local, unrelated to global _g_val
def test_global_ptr():
global _g_ptr
@@ -39,31 +35,27 @@ def test_global_ptr():
p[1] = 0x11223344
def test_global_ptr_no_decl():
"""测试:没有 global 声明时,同名注解声明是编译期错误(此处演示合法写法)
"""Same-name annotated redeclaration of a module variable is a compile error.
新规则:模块级已有 `_g_ptr` 时,函数内写 `_g_ptr: t.CUInt32T* = None`
(无 global)或 `global _g_ptr` 之后仍带类型注解声明,都判为"重复声明"并终止
编译(TypeError)——不管注解类型是否与全局相同。合法写法只有两种:
- 修改全局:`global _g_ptr` + **无注解**赋值(见 test_global_ptr)
- 独立局部:换成不与全局重名的变量名(本函数)
Legal forms: global + unannotated assignment, or a non-conflicting local name.
"""
local_ptr: uint32* = None # 独立局部变量,不触碰全局 _g_ptr
local_ptr: uint32* = None # standalone local, does not touch global _g_ptr
# --- Test 2: 指针算术和数组索引 ---
# --- 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]
@@ -74,38 +66,38 @@ def test_pointer_arithmetic():
testcheck.check(ok, "pointer arithmetic (buf[i] = i*100)", "pointer arithmetic mismatch")
# --- Test 3: 数组退化为指针 ---
# --- Test 3: array decay to pointer ---
def test_array_decay():
"""测试 list[t.CChar, N] 传递给 t.CConst | str 参数"""
"""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
# 传递给 print (接受 t.CConst | str)
# Pass to print (accepts t.CConst | str)
stdio.printf("array decay -> %s\n", &arr)
testcheck.ok("array decay (Hello)")
# --- Test 4: 指针类型转换 (CVoid/CPtr) ---
# --- 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)
# 通过 CVoid 指针写入
# Write through CVoid pointer
raw: t.CPtr = t.CPtr(uint64(buf))
# 将 CVoid 指针转回 CUInt32T 指针
# 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:
@@ -113,22 +105,22 @@ def test_pointer_cast():
testcheck.fail("pointer cast failed")
# --- Test 5: 指针偏移计算 (byte offset vs element offset) ---
# --- Test 5: pointer offset (byte offset vs element offset) ---
def test_pointer_offset():
"""测试指针偏移:CUInt8T* + N 应该偏移 N 字节,CUInt32T* + N 应该偏移 N*4 字节"""
"""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)
# 写入 4 字节
# Write 4 bytes
buf[0] = 0x41 # 'A'
buf[1] = 0x42 # 'B'
buf[2] = 0x43 # 'C'
buf[3] = 0x44 # 'D'
# 通过 CUInt32T* 读取(应该读到 0x44434241 on little-endian)
# Read via CUInt32T* (0x44434241 on little-endian)
p32: uint32* = (uint32*)(t.CPtr(uint64(buf)))
val: uint32 = p32[0]
@@ -140,14 +132,14 @@ def test_pointer_offset():
testcheck.fail("byte offset vs element offset")
# --- Test 6: 结构体指针访问 ---
# --- 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
@@ -158,23 +150,23 @@ def test_struct_pointer():
testcheck.check(p.a == 10 and p.b == 20 and p.c == 0x123456789ABCDEF0, "struct pointer access", "struct pointer access mismatch")
# --- Test 7: memcpy/memset 通过指针 ---
# --- Test 7: memcpy/memset through pointers ---
def test_memcpy_pointers():
"""测试 memcpy 在指针之间的数据复制"""
"""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
# 初始化 src
# 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):
@@ -183,13 +175,13 @@ def test_memcpy_pointers():
testcheck.check(ok, "memcpy between pointers", "memcpy between pointers failed")
# --- Test 8: 指针作为函数参数 ---
# --- 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")
@@ -225,7 +217,7 @@ def main() -> int | t.CExport:
testcheck.begin("PointerTest: 指针操作基准测试")
# Test 1: global 声明
# Test 1: global declaration
testcheck.section("Test 1: global declaration")
global _g_val
_g_val = 0