236 lines
7.1 KiB
Python
236 lines
7.1 KiB
Python
import stdio
|
||
import t, c
|
||
|
||
|
||
# ============================================================
|
||
# 纯内存结构体(class)测试
|
||
# ============================================================
|
||
|
||
class Point:
|
||
x: t.CInt
|
||
y: t.CInt
|
||
|
||
|
||
class Box:
|
||
width: t.CInt
|
||
height: t.CInt
|
||
depth: t.CInt
|
||
|
||
|
||
class Size:
|
||
w: t.CInt = 100
|
||
h: t.CInt = 200
|
||
|
||
|
||
# ============================================================
|
||
# 枚举(t.CEnum)测试
|
||
# ============================================================
|
||
|
||
class State(t.CEnum):
|
||
Idle: t.State
|
||
Run: t.State
|
||
Stop: t.State
|
||
|
||
|
||
class Color(t.CEnum):
|
||
Red: t.State = 10
|
||
Green: t.State
|
||
Blue: t.State = 20
|
||
Yellow: t.State
|
||
|
||
|
||
class MixedType(t.CEnum):
|
||
Small: t.CInt8T
|
||
Big: t.CInt64T
|
||
Medium: t.CInt16T
|
||
|
||
|
||
# ============================================================
|
||
# 联合体(t.CUnion)测试
|
||
# ============================================================
|
||
|
||
class DataUnion(t.CUnion):
|
||
i: t.CInt
|
||
f: t.CFloat
|
||
l: t.CInt64T
|
||
|
||
|
||
# ============================================================
|
||
# 主函数
|
||
# ============================================================
|
||
def struct_test() -> int:
|
||
# ============================================================
|
||
# 测试 1: 结构体字段读写
|
||
# ============================================================
|
||
p: Point = Point()
|
||
p.x = 10
|
||
p.y = 20
|
||
stdio.printf("struct: p.x=%d p.y=%d\n", p.x, p.y)
|
||
|
||
# 修改字段
|
||
p.x = 100
|
||
p.y = 200
|
||
stdio.printf("struct: modified p.x=%d p.y=%d\n", p.x, p.y)
|
||
|
||
# 字段运算
|
||
sum_xy: int = p.x + p.y
|
||
stdio.printf("struct: sum=%d\n", sum_xy)
|
||
|
||
# ============================================================
|
||
# 测试 2: 多字段结构体
|
||
# ============================================================
|
||
b: Box
|
||
b.width = 3
|
||
b.height = 4
|
||
b.depth = 5
|
||
vol: int = b.width * b.height * b.depth
|
||
stdio.printf("struct: volume=%d\n", vol)
|
||
|
||
# ============================================================
|
||
# 测试 3: t.CArray 数组遍历
|
||
# ============================================================
|
||
arr: t.CArray[t.CInt, 5]
|
||
arr[0] = 100
|
||
arr[1] = 200
|
||
arr[2] = 300
|
||
arr[3] = 400
|
||
arr[4] = 500
|
||
|
||
arr_total: int = 0
|
||
for i in range(5):
|
||
arr_total = arr_total + arr[i]
|
||
stdio.printf("array: total=%d\n", arr_total)
|
||
|
||
# 修改数组元素
|
||
arr[2] = 999
|
||
stdio.printf("array: arr[2]=%d\n", arr[2])
|
||
|
||
# ============================================================
|
||
# 测试 4: 指针遍历(字符串)
|
||
# ============================================================
|
||
s: str = "hello"
|
||
sp: str = s
|
||
slen: int = 0
|
||
while c.Deref(sp) != 0:
|
||
slen = slen + 1
|
||
sp = sp + 1
|
||
stdio.printf("ptr: len(hello)=%d\n", slen)
|
||
|
||
# 累加字符值
|
||
p2: str = "ABC"
|
||
total_ch: int = 0
|
||
while c.Deref(p2) != 0:
|
||
total_ch = total_ch + c.Deref(p2)
|
||
p2 = p2 + 1
|
||
stdio.printf("ptr: sum(A,B,C)=%d\n", total_ch)
|
||
|
||
# ============================================================
|
||
# 测试 5: 结构体构造函数 Point() / Point(x, y)
|
||
# ============================================================
|
||
# 无参数构造:零初始化
|
||
z: Point = Point()
|
||
stdio.printf("ctor: z.x=%d z.y=%d\n", z.x, z.y)
|
||
|
||
# 带参数构造:按位置赋值
|
||
p3: Point = Point(7, 8)
|
||
stdio.printf("ctor: p3.x=%d p3.y=%d\n", p3.x, p3.y)
|
||
|
||
# 多字段构造
|
||
bx: Box = Box(10, 20, 30)
|
||
stdio.printf("ctor: bx.w=%d bx.h=%d bx.d=%d\n", bx.width, bx.height, bx.depth)
|
||
|
||
# ============================================================
|
||
# 测试 6: 结构体关键字参数(乱序传参)
|
||
# ============================================================
|
||
# 全关键字乱序
|
||
p4: Point = Point(y=20, x=10)
|
||
stdio.printf("kw: p4.x=%d p4.y=%d\n", p4.x, p4.y)
|
||
|
||
# 混合:位置 + 关键字
|
||
p5: Point = Point(5, y=15)
|
||
stdio.printf("kw: p5.x=%d p5.y=%d\n", p5.x, p5.y)
|
||
|
||
# 多字段关键字乱序
|
||
bx2: Box = Box(depth=30, width=10, height=20)
|
||
stdio.printf("kw: bx2.w=%d bx2.h=%d bx2.d=%d\n", bx2.width, bx2.height, bx2.depth)
|
||
|
||
# ============================================================
|
||
# 测试 7: 结构体默认赋值
|
||
# ============================================================
|
||
# 无参数构造:使用默认值
|
||
sz: Size = Size()
|
||
stdio.printf("def: sz.w=%d sz.h=%d\n", sz.w, sz.h)
|
||
|
||
# 位置参数覆盖默认值
|
||
sz2: Size = Size(5, 6)
|
||
stdio.printf("def: sz2.w=%d sz2.h=%d\n", sz2.w, sz2.h)
|
||
|
||
# 关键字参数覆盖默认值(乱序)
|
||
sz3: Size = Size(h=999, w=888)
|
||
stdio.printf("def: sz3.w=%d sz3.h=%d\n", sz3.w, sz3.h)
|
||
|
||
# 混合:位置参数 + 默认值(w=7 覆盖默认值,h 保持默认值 200)
|
||
sz4: Size = Size(7)
|
||
stdio.printf("def: sz4.w=%d sz4.h=%d\n", sz4.w, sz4.h)
|
||
|
||
# ============================================================
|
||
# 测试 8: 枚举自动赋值(Idle=0, Run=1, Stop=2)
|
||
# ============================================================
|
||
s_idle: int = State.Idle
|
||
s_run: int = State.Run
|
||
s_stop: int = State.Stop
|
||
stdio.printf("enum: Idle=%d Run=%d Stop=%d\n", s_idle, s_run, s_stop)
|
||
|
||
# ============================================================
|
||
# 测试 9: 枚举手动赋值(Red=10, Green=11, Blue=20, Yellow=21)
|
||
# ============================================================
|
||
c_red: int = Color.Red
|
||
c_green: int = Color.Green
|
||
c_blue: int = Color.Blue
|
||
c_yellow: int = Color.Yellow
|
||
stdio.printf("enum: Red=%d Green=%d Blue=%d Yellow=%d\n",
|
||
c_red, c_green, c_blue, c_yellow)
|
||
|
||
# ============================================================
|
||
# 测试 10: 枚举混用数字类型(基准类型应为 i64)
|
||
# ============================================================
|
||
m_small: int = MixedType.Small
|
||
m_big: int = MixedType.Big
|
||
m_medium: int = MixedType.Medium
|
||
stdio.printf("enum: Small=%d Big=%d Medium=%d\n", m_small, m_big, m_medium)
|
||
|
||
# 枚举参与运算
|
||
s_sum: int = State.Idle + State.Run + State.Stop
|
||
stdio.printf("enum: sum(Idle,Run,Stop)=%d\n", s_sum)
|
||
|
||
# 枚举比较
|
||
if State.Idle == 0:
|
||
stdio.printf("enum: Idle==0 true\n")
|
||
if Color.Red == 10:
|
||
stdio.printf("enum: Red==10 true\n")
|
||
if State.Run != State.Stop:
|
||
stdio.printf("enum: Run!=Stop true\n")
|
||
|
||
# ============================================================
|
||
# 测试 11: 联合体字段读写
|
||
# ============================================================
|
||
u: DataUnion
|
||
u.i = 42
|
||
stdio.printf("union: u.i=%d\n", u.i)
|
||
|
||
# 写入 l 字段(覆盖 i 的内存,因为 l 是 i64)
|
||
u.l = 20015998343868
|
||
stdio.printf("union: u.l=%lld\n", u.l)
|
||
|
||
# ============================================================
|
||
# 测试 12: 联合体共享内存验证
|
||
# ============================================================
|
||
u2: DataUnion
|
||
u2.i = 1
|
||
# 写入 l 后,i 的值已被覆盖(不再是 1)
|
||
u2.l = 100
|
||
if u2.i != 1:
|
||
stdio.printf("union: shared memory verified\n")
|
||
|
||
return 0
|