snapshot before regression test
This commit is contained in:
422
Test/TestProject3/App/definetest.py
Normal file
422
Test/TestProject3/App/definetest.py
Normal file
@@ -0,0 +1,422 @@
|
||||
import t, c
|
||||
from t import CInt, CInt32T, CUInt32T, CUInt64T, CFloat64T, CPtr, CChar, CDefine, CUInt16T
|
||||
import viperlib
|
||||
import stdlib
|
||||
import platmacro
|
||||
|
||||
|
||||
MAX_SIZE: t.CDefine = 1024
|
||||
MIN_VAL: t.CDefine = 0
|
||||
MAX_VAL: t.CDefine = 100
|
||||
MASK_BYTE: t.CDefine = 0xFF
|
||||
SHIFT_BITS: t.CDefine = 8
|
||||
PI_INT: t.CDefine = 3
|
||||
ENABLED: t.CDefine = 1
|
||||
DISABLED: t.CDefine = 0
|
||||
PAGE_SIZE: t.CDefine = 4096
|
||||
BUFFER_LEN: t.CDefine = 256
|
||||
|
||||
CODE_SEG: t.CDefine | t.CUInt16T = 0x08
|
||||
DATA_SEG: t.CDefine | t.CUInt16T = 0x10
|
||||
STACK_SEG: t.CDefine | t.CUInt16T = 0x20
|
||||
|
||||
|
||||
def MAKE_FLAG(bit: CInt32T) -> t.CDefine:
|
||||
return 1 << bit
|
||||
|
||||
def CLAMP(x: CInt32T, lo: CInt32T, hi: CInt32T) -> t.CDefine:
|
||||
if x < lo:
|
||||
return lo
|
||||
if x > hi:
|
||||
return hi
|
||||
return x
|
||||
|
||||
|
||||
def test_basic_define() -> CInt:
|
||||
print(MAX_SIZE)
|
||||
print(MIN_VAL)
|
||||
print(MAX_VAL)
|
||||
print(MASK_BYTE)
|
||||
print(SHIFT_BITS)
|
||||
return 0
|
||||
|
||||
def test_define_arithmetic() -> CInt:
|
||||
a: CInt32T = 10
|
||||
b: CInt32T = a + MAX_SIZE
|
||||
print(b)
|
||||
c_val: CInt32T = (a << SHIFT_BITS) & MASK_BYTE
|
||||
print(c_val)
|
||||
d: CInt32T = MAX_VAL - MIN_VAL
|
||||
print(d)
|
||||
e: CInt32T = PAGE_SIZE / 4
|
||||
print(e)
|
||||
return 0
|
||||
|
||||
def test_define_conditional() -> CInt:
|
||||
mode: CInt32T = ENABLED
|
||||
if mode == ENABLED:
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if mode == DISABLED:
|
||||
print(0)
|
||||
else:
|
||||
print(1)
|
||||
val: CInt32T = 50
|
||||
if val > MAX_VAL:
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if val < MAX_VAL:
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_define_loop() -> CInt:
|
||||
i: CInt32T = 0
|
||||
while i < MAX_VAL:
|
||||
i = i + 1
|
||||
if i > 10:
|
||||
break
|
||||
print(i)
|
||||
i = 0
|
||||
while i < MAX_SIZE:
|
||||
i = i + PAGE_SIZE
|
||||
if i >= PAGE_SIZE * 4:
|
||||
break
|
||||
print(i)
|
||||
return 0
|
||||
|
||||
def test_typed_define() -> CInt:
|
||||
seg: CUInt16T = CODE_SEG | DATA_SEG | STACK_SEG
|
||||
print(seg)
|
||||
cs: CUInt16T = CODE_SEG
|
||||
print(cs)
|
||||
ds: CUInt16T = DATA_SEG
|
||||
print(ds)
|
||||
return 0
|
||||
|
||||
def test_define_function() -> CInt:
|
||||
flag0 = MAKE_FLAG(0)
|
||||
print(flag0)
|
||||
flag3 = MAKE_FLAG(3)
|
||||
print(flag3)
|
||||
flag7 = MAKE_FLAG(7)
|
||||
print(flag7)
|
||||
return 0
|
||||
|
||||
def test_define_clamp() -> CInt:
|
||||
v1 = CLAMP(50, 0, 100)
|
||||
print(v1)
|
||||
v2 = CLAMP(-10, 0, 100)
|
||||
print(v2)
|
||||
v3 = CLAMP(150, 0, 100)
|
||||
print(v3)
|
||||
return 0
|
||||
|
||||
def test_define_string() -> CInt:
|
||||
buf: CChar | CPtr = CPtr(stdlib.malloc(BUFFER_LEN))
|
||||
viperlib.snprintf(buf, BUFFER_LEN, "MAX_SIZE=%d PAGE=%d MASK=0x%x", MAX_SIZE, PAGE_SIZE, MASK_BYTE)
|
||||
print(buf)
|
||||
stdlib.free(buf)
|
||||
return 0
|
||||
|
||||
def test_define_combined() -> CInt:
|
||||
flags: CUInt32T = MAKE_FLAG(0) | MAKE_FLAG(1) | MAKE_FLAG(2)
|
||||
print(flags)
|
||||
shifted: CUInt32T = MASK_BYTE << SHIFT_BITS
|
||||
print(shifted)
|
||||
total: CUInt32T = PAGE_SIZE + BUFFER_LEN
|
||||
print(total)
|
||||
return 0
|
||||
|
||||
|
||||
def test_cif_basic() -> CInt:
|
||||
if c.CIf(ENABLED):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(DISABLED):
|
||||
print(0)
|
||||
else:
|
||||
print(1)
|
||||
if c.CIf(MAX_SIZE > 512):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(MIN_VAL > 0):
|
||||
print(0)
|
||||
else:
|
||||
print(1)
|
||||
return 0
|
||||
|
||||
def test_cif_elif_else() -> CInt:
|
||||
val: CInt32T = 2
|
||||
if c.CIf(val == 1):
|
||||
print(1)
|
||||
elif c.CIf(val == 2):
|
||||
print(2)
|
||||
elif c.CIf(val == 3):
|
||||
print(3)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(val > 10):
|
||||
print(10)
|
||||
elif c.CIf(val > 5):
|
||||
print(5)
|
||||
else:
|
||||
print(val)
|
||||
return 0
|
||||
|
||||
def test_cifdef_cifndef() -> CInt:
|
||||
if c.CIfdef(MAX_SIZE):
|
||||
print(MAX_SIZE)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfdef(UNDEFINED_MACRO_XYZ):
|
||||
print(0)
|
||||
else:
|
||||
print(1)
|
||||
if c.CIfndef(UNDEFINED_MACRO_XYZ):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfndef(MAX_SIZE):
|
||||
print(0)
|
||||
else:
|
||||
print(1)
|
||||
return 0
|
||||
|
||||
def test_cif_platform() -> CInt:
|
||||
if c.CIfdef(_WIN32):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfdef(__x86_64__):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(SIZEOF_VOID_P == 8):
|
||||
print(8)
|
||||
elif c.CIf(SIZEOF_VOID_P == 4):
|
||||
print(4)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_cif_nested() -> CInt:
|
||||
if c.CIf(ENABLED):
|
||||
if c.CIf(MAX_SIZE > 512):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfdef(PAGE_SIZE):
|
||||
print(PAGE_SIZE)
|
||||
else:
|
||||
print(0)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_cif_expr() -> CInt:
|
||||
if c.CIf(MASK_BYTE & 0x0F):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf((MAX_SIZE >> SHIFT_BITS) > 0):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(ENABLED and MAX_SIZE > 0):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(DISABLED or MAX_SIZE > 0):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_platmacro_platform() -> CInt:
|
||||
if c.CIfdef(platmacro.IS_WINDOWS):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(platmacro.IS_WINDOWS == 1):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(platmacro.IS_LINUX == 0):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(platmacro.IS_X86_64 == 1):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_platmacro_sizes() -> CInt:
|
||||
print(platmacro.PTR_SIZE)
|
||||
print(platmacro.SIZEOF_INT)
|
||||
print(platmacro.SIZEOF_LONG)
|
||||
print(platmacro.SIZEOF_DOUBLE)
|
||||
print(platmacro.PAGE_SIZE)
|
||||
print(platmacro.CACHE_LINE_SIZE)
|
||||
return 0
|
||||
|
||||
def test_platmacro_endian() -> CInt:
|
||||
if c.CIf(platmacro.IS_LITTLE_ENDIAN == 1):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(platmacro.IS_BIG_ENDIAN == 0):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(platmacro.BYTE_ORDER_LE == 1):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_platmacro_features() -> CInt:
|
||||
if c.CIf(platmacro.HAS_SSE2 == 1):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(platmacro.HAS_AVX == 1):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIf(platmacro.HAS_NEON == 0):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_platmacro_align() -> CInt:
|
||||
print(platmacro.ALIGNOF_INT)
|
||||
print(platmacro.ALIGNOF_PTR)
|
||||
print(platmacro.ALIGNOF_DOUBLE)
|
||||
if c.CIf(platmacro.ALIGNOF_PTR == 8):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_cifdef_cross_module() -> CInt:
|
||||
if c.CIfdef(platmacro.IS_WINDOWS):
|
||||
if c.CIf(platmacro.PTR_SIZE == 8):
|
||||
print(64)
|
||||
else:
|
||||
print(32)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfndef(platmacro.IS_ARM64):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfdef(platmacro.HAS_AVX2):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_cifdef_guard() -> CInt:
|
||||
MY_HEADER_GUARD: t.CDefine = 1
|
||||
if c.CIfndef(MY_HEADER_GUARD):
|
||||
print(0)
|
||||
else:
|
||||
print(1)
|
||||
if c.CIfdef(MY_HEADER_GUARD):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
def test_cifdef_undefined() -> CInt:
|
||||
if c.CIfndef(NONEXISTENT_MACRO_ABC):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfndef(THIS_IS_ALSO_NOT_DEFINED):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfdef(NONEXISTENT_MACRO_ABC):
|
||||
print(0)
|
||||
else:
|
||||
print(1)
|
||||
return 0
|
||||
|
||||
def test_cifdef_complex() -> CInt:
|
||||
if c.CIfdef(platmacro.IS_WINDOWS) and c.CIf(platmacro.PTR_SIZE == 8):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfndef(platmacro.IS_ARM64) and c.CIf(platmacro.IS_X86_64 == 1):
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
if c.CIfdef(platmacro.HAS_SSE2):
|
||||
if c.CIfdef(platmacro.HAS_AVX):
|
||||
print(2)
|
||||
else:
|
||||
print(1)
|
||||
else:
|
||||
print(0)
|
||||
return 0
|
||||
|
||||
|
||||
def define_main() -> CInt:
|
||||
print("=== Basic Define Test ===")
|
||||
test_basic_define()
|
||||
print("=== Define Arithmetic Test ===")
|
||||
test_define_arithmetic()
|
||||
print("=== Define Conditional Test ===")
|
||||
test_define_conditional()
|
||||
print("=== Define Loop Test ===")
|
||||
test_define_loop()
|
||||
print("=== Typed Define Test ===")
|
||||
test_typed_define()
|
||||
print("=== Define Function (Macro) Test ===")
|
||||
test_define_function()
|
||||
print("=== Define Clamp (Macro) Test ===")
|
||||
test_define_clamp()
|
||||
print("=== Define String Test ===")
|
||||
test_define_string()
|
||||
print("=== Define Combined Test ===")
|
||||
test_define_combined()
|
||||
print("=== CIf Basic Test ===")
|
||||
test_cif_basic()
|
||||
print("=== CIf Elif Else Test ===")
|
||||
test_cif_elif_else()
|
||||
print("=== CIfdef/CIfndef Test ===")
|
||||
test_cifdef_cifndef()
|
||||
print("=== CIf Platform Test ===")
|
||||
test_cif_platform()
|
||||
print("=== CIf Nested Test ===")
|
||||
test_cif_nested()
|
||||
print("=== CIf Expression Test ===")
|
||||
test_cif_expr()
|
||||
print("=== Platmacro Platform Test ===")
|
||||
test_platmacro_platform()
|
||||
print("=== Platmacro Sizes Test ===")
|
||||
test_platmacro_sizes()
|
||||
print("=== Platmacro Endian Test ===")
|
||||
test_platmacro_endian()
|
||||
print("=== Platmacro Features Test ===")
|
||||
test_platmacro_features()
|
||||
print("=== Platmacro Align Test ===")
|
||||
test_platmacro_align()
|
||||
print("=== CIfdef Cross-Module Test ===")
|
||||
test_cifdef_cross_module()
|
||||
print("=== CIfdef Guard Test ===")
|
||||
test_cifdef_guard()
|
||||
print("=== CIfdef Undefined Test ===")
|
||||
test_cifdef_undefined()
|
||||
print("=== CIfdef Complex Test ===")
|
||||
test_cifdef_complex()
|
||||
return 0
|
||||
95
Test/TestProject3/App/enumtest.py
Normal file
95
Test/TestProject3/App/enumtest.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import t, c
|
||||
from t import CInt, CPtr, CChar, CInt32T, CExport
|
||||
import viperlib
|
||||
import stdlib
|
||||
|
||||
|
||||
class Color(t.CEnum):
|
||||
Red = 0
|
||||
Green = 1
|
||||
Blue = 2
|
||||
|
||||
|
||||
class Option(t.REnum):
|
||||
class Some:
|
||||
value: CInt
|
||||
Empty = 1
|
||||
|
||||
|
||||
class Result(t.REnum):
|
||||
class Ok:
|
||||
value: CInt
|
||||
class Err:
|
||||
msg: t.CChar | CPtr
|
||||
|
||||
|
||||
class GeoShape(t.REnum):
|
||||
class GCircle:
|
||||
radius: CInt
|
||||
class GRect:
|
||||
w: CInt
|
||||
h: CInt
|
||||
class GPoint:
|
||||
x: CInt
|
||||
y: CInt
|
||||
|
||||
|
||||
def enum_main() -> CInt:
|
||||
buf: CChar | CPtr = CPtr(stdlib.malloc(256))
|
||||
|
||||
c1: CInt = Color.Red
|
||||
c2: CInt = Color.Green
|
||||
c3: CInt = Color.Blue
|
||||
viperlib.snprintf(buf, 256, "CEnum: Red=%d Green=%d Blue=%d", c1, c2, c3)
|
||||
print(buf)
|
||||
|
||||
opt: Option = Option.Some(42)
|
||||
match opt:
|
||||
case Option.Some(v):
|
||||
viperlib.snprintf(buf, 256, "Option.Some: value=%d", v)
|
||||
print(buf)
|
||||
case Option.Empty():
|
||||
pass
|
||||
|
||||
res1: Result = Result.Ok(100)
|
||||
match res1:
|
||||
case Result.Ok(v):
|
||||
viperlib.snprintf(buf, 256, "Result.Ok: value=%d", v)
|
||||
print(buf)
|
||||
case Result.Err(e):
|
||||
pass
|
||||
|
||||
res2: Result = Result.Err("not found")
|
||||
match res2:
|
||||
case Result.Ok(v):
|
||||
pass
|
||||
case Result.Err(e):
|
||||
viperlib.snprintf(buf, 256, "Result.Err: msg=%s", e)
|
||||
print(buf)
|
||||
|
||||
s1: GeoShape = GeoShape.GCircle(5)
|
||||
match s1:
|
||||
case GeoShape.GCircle(r):
|
||||
viperlib.snprintf(buf, 256, "GeoShape.GCircle: radius=%d", r)
|
||||
print(buf)
|
||||
case _:
|
||||
pass
|
||||
|
||||
s2: GeoShape = GeoShape.GRect(10, 20)
|
||||
match s2:
|
||||
case GeoShape.GRect(w, h):
|
||||
viperlib.snprintf(buf, 256, "GeoShape.GRect: w=%d h=%d", w, h)
|
||||
print(buf)
|
||||
case _:
|
||||
pass
|
||||
|
||||
s3: GeoShape = GeoShape.GPoint(3, 4)
|
||||
match s3:
|
||||
case GeoShape.GPoint(x, y):
|
||||
viperlib.snprintf(buf, 256, "GeoShape.GPoint: x=%d y=%d", x, y)
|
||||
print(buf)
|
||||
case _:
|
||||
pass
|
||||
|
||||
stdlib.free(buf)
|
||||
return 0
|
||||
395
Test/TestProject3/App/fileiotest.py
Normal file
395
Test/TestProject3/App/fileiotest.py
Normal file
@@ -0,0 +1,395 @@
|
||||
import t, c
|
||||
from t import CInt, CInt32T, CUInt32T, CPtr, CChar, CExport
|
||||
from stdint import *
|
||||
import stdlib
|
||||
import viperlib
|
||||
import w32.win32base
|
||||
import w32.win32file
|
||||
import w32.win32console
|
||||
import w32.fileio
|
||||
from w32.fileio import File, FileW, MODE, FRESULT, SEEK_SET, SEEK_CUR, SEEK_END
|
||||
|
||||
|
||||
BUF_SIZE: t.CDefine = 4096
|
||||
|
||||
|
||||
def fileio_main():
|
||||
buf: bytes = stdlib.malloc(256)
|
||||
write_buf: bytes = stdlib.malloc(BUF_SIZE)
|
||||
read_buf: bytes = stdlib.malloc(BUF_SIZE)
|
||||
bytes_written: ULONG = 0
|
||||
bytes_read: ULONG = 0
|
||||
|
||||
w32.win32console.SetConsoleCP(65001)
|
||||
w32.win32console.SetConsoleOutputCP(65001)
|
||||
|
||||
hFile: w32.win32base.HANDLE = w32.win32file.CreateFileA(
|
||||
"testfile.txt",
|
||||
w32.win32file.GENERIC_WRITE,
|
||||
w32.win32file.FILE_SHARE_READ,
|
||||
None,
|
||||
w32.win32file.CREATE_ALWAYS,
|
||||
w32.win32file.FILE_ATTRIBUTE_NORMAL,
|
||||
None
|
||||
)
|
||||
|
||||
if hFile == w32.win32base.INVALID_HANDLE_VALUE:
|
||||
print("Failed to create file")
|
||||
return
|
||||
|
||||
viperlib.snprintf(write_buf, BUF_SIZE, "Hello from TransPyC Win32 FileIO!\nLine 2: %d + %d = %d\n", 10, 20, 30)
|
||||
write_len: ULONG = 0
|
||||
i: CInt = 0
|
||||
ch: bytes = write_buf
|
||||
while i < BUF_SIZE:
|
||||
if CInt(c.Deref(ch)) == 0:
|
||||
break
|
||||
write_len = write_len + 1
|
||||
i = i + 1
|
||||
ch = ch + 1
|
||||
|
||||
result: BOOL = w32.win32file.WriteFile(hFile, write_buf, write_len, c.Addr(bytes_written), None)
|
||||
if result == 0:
|
||||
print("WriteFile failed")
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "Written %d bytes to testfile.txt", bytes_written)
|
||||
print(buf)
|
||||
|
||||
w32.win32file.FlushFileBuffers(hFile)
|
||||
w32.win32base.CloseHandle(hFile)
|
||||
|
||||
hFile = w32.win32file.CreateFileA(
|
||||
"testfile.txt",
|
||||
w32.win32file.GENERIC_READ,
|
||||
w32.win32file.FILE_SHARE_READ,
|
||||
None,
|
||||
w32.win32file.OPEN_EXISTING,
|
||||
w32.win32file.FILE_ATTRIBUTE_NORMAL,
|
||||
None
|
||||
)
|
||||
|
||||
if hFile == w32.win32base.INVALID_HANDLE_VALUE:
|
||||
print("Failed to open file for reading")
|
||||
return
|
||||
|
||||
read_len: ULONG = 0
|
||||
result = w32.win32file.ReadFile(hFile, read_buf, BUF_SIZE - 1, c.Addr(read_len), None)
|
||||
if result == 0:
|
||||
print("ReadFile failed")
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "Read %d bytes from testfile.txt", read_len)
|
||||
print(buf)
|
||||
|
||||
read_ptr: bytes = read_buf
|
||||
read_ptr = read_ptr + read_len
|
||||
c.DerefAs(read_ptr, 0)
|
||||
print("--- File content ---")
|
||||
print(read_buf)
|
||||
print("--- End ---")
|
||||
|
||||
fileSize: ULONG = w32.win32file.GetFileSize(hFile, None)
|
||||
viperlib.snprintf(buf, 256, "File size: %d bytes", fileSize)
|
||||
print(buf)
|
||||
|
||||
w32.win32base.CloseHandle(hFile)
|
||||
|
||||
attrs: ULONG = w32.win32file.GetFileAttributesA("testfile.txt")
|
||||
if attrs == w32.win32base.INVALID_HANDLE_VALUE:
|
||||
print("GetFileAttributes failed")
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "File attributes: 0x%X", attrs)
|
||||
print(buf)
|
||||
|
||||
w32.win32file.DeleteFileA("testfile.txt")
|
||||
print("File deleted")
|
||||
|
||||
stdlib.free(write_buf)
|
||||
stdlib.free(read_buf)
|
||||
|
||||
print("=== OOP FileIO: with context manager ===")
|
||||
fileio_with_test()
|
||||
|
||||
print("=== OOP FileIO: f handle style ===")
|
||||
fileio_handle_test()
|
||||
|
||||
print("=== OOP FileIO: RP/WP/AP modes ===")
|
||||
fileio_rw_test()
|
||||
|
||||
|
||||
def fileio_with_test():
|
||||
buf: bytes = stdlib.malloc(256)
|
||||
write_buf: bytes = stdlib.malloc(BUF_SIZE)
|
||||
read_buf: bytes = stdlib.malloc(BUF_SIZE)
|
||||
|
||||
with File("oop_with.txt", MODE.W) as f:
|
||||
viperlib.snprintf(write_buf, BUF_SIZE, "with context manager test!\nLine 2: %d\n", 42)
|
||||
n: LONG = f.write_str(write_buf)
|
||||
if n < 0:
|
||||
print("[with] write failed")
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "[with] wrote %d bytes", n)
|
||||
print(buf)
|
||||
|
||||
with File("oop_with.txt", MODE.R) as f:
|
||||
n = f.read_all(read_buf, BUF_SIZE)
|
||||
if n < 0:
|
||||
print("[with] read failed")
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "[with] read %d bytes", n)
|
||||
print(buf)
|
||||
print("[with] content:")
|
||||
print(read_buf)
|
||||
|
||||
with File("oop_with.txt", MODE.R) as f:
|
||||
line1_len: LONG = f.readline(buf, 256)
|
||||
viperlib.snprintf(read_buf, BUF_SIZE, "[with] Line1(%d): ", line1_len)
|
||||
print(read_buf)
|
||||
print(buf)
|
||||
line2_len: LONG = f.readline(buf, 256)
|
||||
viperlib.snprintf(read_buf, BUF_SIZE, "[with] Line2(%d): ", line2_len)
|
||||
print(read_buf)
|
||||
print(buf)
|
||||
|
||||
with File("oop_with.txt", MODE.A) as f:
|
||||
f.write_str("Appended line!\n")
|
||||
|
||||
with File("oop_with.txt", MODE.R) as f:
|
||||
n = f.read_all(read_buf, BUF_SIZE)
|
||||
print("[with] after append:")
|
||||
print(read_buf)
|
||||
|
||||
with File("oop_with.txt", MODE.R) as f:
|
||||
sz: LONGLONG = f.size()
|
||||
pos: LONG = f.tell()
|
||||
viperlib.snprintf(buf, 256, "[with] Size=%lld Pos=%d", sz, pos)
|
||||
print(buf)
|
||||
f.seek(5, SEEK_SET)
|
||||
pos = f.tell()
|
||||
viperlib.snprintf(buf, 256, "[with] After seek: Pos=%d", pos)
|
||||
print(buf)
|
||||
|
||||
w32.win32file.DeleteFileA("oop_with.txt")
|
||||
print("[with] test file deleted")
|
||||
|
||||
stdlib.free(buf)
|
||||
stdlib.free(write_buf)
|
||||
stdlib.free(read_buf)
|
||||
|
||||
|
||||
def fileio_handle_test():
|
||||
buf: bytes = stdlib.malloc(256)
|
||||
write_buf: bytes = stdlib.malloc(BUF_SIZE)
|
||||
read_buf: bytes = stdlib.malloc(BUF_SIZE)
|
||||
|
||||
fw: File = File("oop_handle.txt", MODE.W)
|
||||
viperlib.snprintf(write_buf, BUF_SIZE, "f handle style test!\nLine 2: %d\n", 99)
|
||||
n: LONG = fw.write_str(write_buf)
|
||||
viperlib.snprintf(buf, 256, "[handle] wrote %d bytes", n)
|
||||
print(buf)
|
||||
fw.flush()
|
||||
fw.close()
|
||||
|
||||
fr: File = File("oop_handle.txt", MODE.R)
|
||||
n = fr.read_all(read_buf, BUF_SIZE)
|
||||
viperlib.snprintf(buf, 256, "[handle] read %d bytes", n)
|
||||
print(buf)
|
||||
print("[handle] content:")
|
||||
print(read_buf)
|
||||
fr.close()
|
||||
|
||||
fr2: File = File("oop_handle.txt", MODE.R)
|
||||
line1_len: LONG = fr2.readline(buf, 256)
|
||||
viperlib.snprintf(read_buf, BUF_SIZE, "[handle] Line1(%d): ", line1_len)
|
||||
print(read_buf)
|
||||
print(buf)
|
||||
line2_len: LONG = fr2.readline(buf, 256)
|
||||
viperlib.snprintf(read_buf, BUF_SIZE, "[handle] Line2(%d): ", line2_len)
|
||||
print(read_buf)
|
||||
print(buf)
|
||||
fr2.close()
|
||||
|
||||
fa: File = File("oop_handle.txt", MODE.A)
|
||||
fa.write_str("Appended by handle!\n")
|
||||
fa.close()
|
||||
|
||||
fr3: File = File("oop_handle.txt", MODE.R)
|
||||
n = fr3.read_all(read_buf, BUF_SIZE)
|
||||
print("[handle] after append:")
|
||||
print(read_buf)
|
||||
fr3.close()
|
||||
|
||||
fr4: File = File("oop_handle.txt", MODE.R)
|
||||
sz: LONGLONG = fr4.size()
|
||||
pos: LONG = fr4.tell()
|
||||
viperlib.snprintf(buf, 256, "[handle] Size=%lld Pos=%d", sz, pos)
|
||||
print(buf)
|
||||
fr4.seek(5, SEEK_SET)
|
||||
pos = fr4.tell()
|
||||
viperlib.snprintf(buf, 256, "[handle] After seek: Pos=%d", pos)
|
||||
print(buf)
|
||||
fr4.close()
|
||||
|
||||
w32.win32file.DeleteFileA("oop_handle.txt")
|
||||
print("[handle] test file deleted")
|
||||
|
||||
stdlib.free(buf)
|
||||
stdlib.free(write_buf)
|
||||
stdlib.free(read_buf)
|
||||
|
||||
|
||||
def fileio_rw_test():
|
||||
buf: bytes = stdlib.malloc(256)
|
||||
|
||||
fw: File = File("rw_test.txt", MODE.W)
|
||||
fw.write_str("original content\n")
|
||||
fw.close()
|
||||
|
||||
frp: File = File("rw_test.txt", MODE.RP)
|
||||
n: LONG = frp.read_all(buf, 256)
|
||||
viperlib.snprintf(buf, 256, "[rp] read %d bytes", n)
|
||||
print(buf)
|
||||
|
||||
frp.seek(0, SEEK_END)
|
||||
frp.write_str("appended via RP\n")
|
||||
frp.seek(0, SEEK_SET)
|
||||
n = frp.read_all(buf, 256)
|
||||
print("[rp] after RP append:")
|
||||
print(buf)
|
||||
frp.close()
|
||||
|
||||
fwp: File = File("rw_test.txt", MODE.WP)
|
||||
fwp.write_str("truncated + new\n")
|
||||
fwp.seek(0, SEEK_SET)
|
||||
n = fwp.read_all(buf, 256)
|
||||
print("[wp] WP (truncate+read):")
|
||||
print(buf)
|
||||
fwp.close()
|
||||
|
||||
fap: File = File("rw_test.txt", MODE.AP)
|
||||
fap.write_str("a+ appended\n")
|
||||
fap.seek(0, SEEK_SET)
|
||||
n = fap.read_all(buf, 256)
|
||||
print("[ap] AP (append+read):")
|
||||
print(buf)
|
||||
fap.close()
|
||||
|
||||
w32.win32file.DeleteFileA("rw_test.txt")
|
||||
print("[rw] test file deleted")
|
||||
|
||||
print("=== OOP FileIO: wide char (u\"...\") ===")
|
||||
fileio_wide_test()
|
||||
|
||||
stdlib.free(buf)
|
||||
|
||||
|
||||
def fileio_wide_test():
|
||||
buf: bytes = stdlib.malloc(256)
|
||||
|
||||
fw: File = File("wide_test.txt", MODE.W)
|
||||
fw.write_str("wide char test\n")
|
||||
fw.close()
|
||||
|
||||
fw2: FileW = FileW(u"wide_test.txt", MODE.R)
|
||||
n: LONG = fw2.read_all(buf, 256)
|
||||
viperlib.snprintf(buf, 256, "[wide] read %d bytes", n)
|
||||
print(buf)
|
||||
fw2.close()
|
||||
|
||||
w32.win32file.DeleteFileA("wide_test.txt")
|
||||
print("[wide] test file deleted")
|
||||
|
||||
stdlib.free(buf)
|
||||
|
||||
print("=== OOP FileIO: Chinese filename & content (u\"...\" = WCHAR_T) ===")
|
||||
fileio_chinese_test()
|
||||
|
||||
|
||||
def fileio_chinese_test():
|
||||
buf: bytes = stdlib.malloc(512)
|
||||
bytes_written: ULONG = 0
|
||||
|
||||
# === T1: Chinese filename (WCHAR_T) + ANSI content ===
|
||||
print("[chinese] T1: Chinese filename + ANSI content")
|
||||
fw: FileW = FileW(u"中文文件.txt", MODE.W)
|
||||
if fw.closed:
|
||||
print("[chinese] T1: create failed")
|
||||
else:
|
||||
fw.write_str("Hello from Chinese filename!\n")
|
||||
fw.close()
|
||||
|
||||
fr: FileW = FileW(u"中文文件.txt", MODE.R)
|
||||
n: LONG = fr.read_all(buf, 512)
|
||||
viperlib.snprintf(buf, 256, "[chinese] T1: read %d bytes", n)
|
||||
print(buf)
|
||||
print("[chinese] T1 content:")
|
||||
print(buf)
|
||||
fr.close()
|
||||
|
||||
w32.win32file.DeleteFileW(u"中文文件.txt")
|
||||
print("[chinese] T1: deleted")
|
||||
|
||||
# === T2: Chinese filename + Chinese wide content (WCHAR_T) ===
|
||||
print("[chinese] T2: Chinese filename + Chinese wide content")
|
||||
fw2: FileW = FileW(u"中文内容.txt", MODE.W)
|
||||
if fw2.closed:
|
||||
print("[chinese] T2: create failed")
|
||||
else:
|
||||
# u"你好世界\n" = 5 WCHARs = 10 bytes (UTF-16LE)
|
||||
w32.win32file.WriteFile(fw2.handle, u"你好世界\n", 10, c.Addr(bytes_written), None)
|
||||
viperlib.snprintf(buf, 256, "[chinese] T2: wrote %d WCHAR bytes", bytes_written)
|
||||
print(buf)
|
||||
fw2.close()
|
||||
|
||||
fr2: FileW = FileW(u"中文内容.txt", MODE.R)
|
||||
n = fr2.read_all(buf, 512)
|
||||
viperlib.snprintf(buf, 256, "[chinese] T2: read %d bytes (UTF-16LE)", n)
|
||||
print(buf)
|
||||
fr2.close()
|
||||
|
||||
w32.win32file.DeleteFileW(u"中文内容.txt")
|
||||
print("[chinese] T2: deleted")
|
||||
|
||||
# === T3: Chinese filename + mixed ANSI + wide content ===
|
||||
print("[chinese] T3: Chinese filename + mixed content")
|
||||
fw3: FileW = FileW(u"混合测试.txt", MODE.WP)
|
||||
if fw3.closed:
|
||||
print("[chinese] T3: create failed")
|
||||
else:
|
||||
fw3.write_str("ASCII line\n")
|
||||
# u"中文行\n" = 4 WCHARs = 8 bytes (UTF-16LE)
|
||||
w32.win32file.WriteFile(fw3.handle, u"中文行\n", 8, c.Addr(bytes_written), None)
|
||||
fw3.seek(0, SEEK_SET)
|
||||
n: LONG = fw3.read_all(buf, 512)
|
||||
viperlib.snprintf(buf, 256, "[chinese] T3: total %d bytes", n)
|
||||
print(buf)
|
||||
fw3.close()
|
||||
|
||||
w32.win32file.DeleteFileW(u"混合测试.txt")
|
||||
print("[chinese] T3: deleted")
|
||||
|
||||
# === T4: Chinese filename + Chinese content via WriteConsoleW ===
|
||||
print("[chinese] T4: Write wide content and print via WriteConsoleW")
|
||||
fw4: FileW = FileW(u"控制台测试.txt", MODE.W)
|
||||
if fw4.closed:
|
||||
print("[chinese] T4: create failed")
|
||||
else:
|
||||
# u"你好,世界!\n" = 7 WCHARs = 14 bytes (UTF-16LE)
|
||||
w32.win32file.WriteFile(fw4.handle, u"你好,世界!\n", 14, c.Addr(bytes_written), None)
|
||||
viperlib.snprintf(buf, 256, "[chinese] T4: wrote %d WCHAR bytes", bytes_written)
|
||||
print(buf)
|
||||
fw4.close()
|
||||
|
||||
fr4: FileW = FileW(u"控制台测试.txt", MODE.R)
|
||||
n = fr4.read_all(buf, 512)
|
||||
viperlib.snprintf(buf, 256, "[chinese] T4: read %d bytes", n)
|
||||
print(buf)
|
||||
# Print wide content to console using WriteConsoleW
|
||||
chars_written: ULONG = 0
|
||||
hOut: w32.win32base.HANDLE = w32.win32file.GetStdHandle(w32.win32file.STD_OUTPUT_HANDLE)
|
||||
w32.win32console.WriteConsoleW(hOut, u"你好,世界!\n", 7, c.Addr(chars_written), None)
|
||||
fr4.close()
|
||||
|
||||
w32.win32file.DeleteFileW(u"控制台测试.txt")
|
||||
print("[chinese] T4: deleted")
|
||||
|
||||
stdlib.free(buf)
|
||||
519
Test/TestProject3/App/main.py
Normal file
519
Test/TestProject3/App/main.py
Normal file
@@ -0,0 +1,519 @@
|
||||
from stdint import *
|
||||
import w32.win32console
|
||||
import t, c
|
||||
from t import CInt, CPtr, CChar, CInt32T, CUInt64T, CFloat64T, CExport, State
|
||||
import viperlib
|
||||
import stdlib
|
||||
import definetest
|
||||
import enumtest
|
||||
import mpooltest
|
||||
import vectortest
|
||||
import fileiotest
|
||||
import testcheck
|
||||
|
||||
|
||||
# === 函数泛型 ===
|
||||
|
||||
def add[T](a: T, b: T) -> T:
|
||||
return a + b
|
||||
|
||||
def add_u64[T](a: T, b: T, c: CUInt64T) -> T:
|
||||
return a + b + T(c)
|
||||
|
||||
def fxi[T1, T2](a: T1, b: T2) -> T1:
|
||||
return a + fxi2(a, b, CUInt64T(178900))
|
||||
|
||||
def fxi2[T1, T2, T3](a: T1, b: T2, c: T3) -> T1:
|
||||
return a + T1(b) + T1(c)
|
||||
|
||||
def wrap_add[T](a: T, b: T) -> T:
|
||||
return add(a, b)
|
||||
|
||||
def double_add[T](a: T, b: T) -> T:
|
||||
return add(a, b) + add(a, b)
|
||||
|
||||
|
||||
# === 结构体泛型 ===
|
||||
|
||||
class A[T]():
|
||||
def __init__(self, a: T):
|
||||
self.a = T(a)
|
||||
def get_a(self) -> T:
|
||||
return self.a
|
||||
|
||||
class Pair[T1, T2]():
|
||||
def __init__(self, first: T1, second: T2):
|
||||
self.first = T1(first)
|
||||
self.second = T2(second)
|
||||
def get_first(self) -> T1:
|
||||
return self.first
|
||||
def get_second(self) -> T2:
|
||||
return self.second
|
||||
|
||||
class Calculator[T]():
|
||||
def __init__(self, init_val: T):
|
||||
self.val = T(init_val)
|
||||
def compute(self, other: T) -> T:
|
||||
return add(self.val, other)
|
||||
def double_compute(self, other: T) -> T:
|
||||
return double_add(self.val, other)
|
||||
|
||||
class Container[T]():
|
||||
def __init__(self, v: T):
|
||||
self.value = T(v)
|
||||
self.flag = CInt32T(1)
|
||||
def get_value(self) -> T:
|
||||
return self.value
|
||||
def get_flag(self) -> CInt32T:
|
||||
return self.flag
|
||||
|
||||
|
||||
# === OOP 继承 + 虚方法(Gargantua 风格) ===
|
||||
|
||||
@t.CVTable
|
||||
class Shape:
|
||||
def __init__(self, x: CFloat64T, y: CFloat64T):
|
||||
self.x = x
|
||||
self.y = y
|
||||
def area(self) -> CFloat64T:
|
||||
return 0.0
|
||||
def perimeter(self) -> CFloat64T:
|
||||
return 0.0
|
||||
def describe(self) -> CFloat64T:
|
||||
return self.area() + self.perimeter()
|
||||
|
||||
@t.CVTable
|
||||
class Circle(Shape):
|
||||
def __init__(self, x: CFloat64T, y: CFloat64T, r: CFloat64T):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.r = r
|
||||
def area(self) -> CFloat64T:
|
||||
return 3.14159265 * self.r * self.r
|
||||
def perimeter(self) -> CFloat64T:
|
||||
return 2.0 * 3.14159265 * self.r
|
||||
def scale(self, factor: CFloat64T) -> 'Circle' | CPtr:
|
||||
self.r = self.r * factor
|
||||
return t.CVoid(c.Addr(self), CPtr)
|
||||
def move(self, dx: CFloat64T, dy: CFloat64T) -> 'Circle' | CPtr:
|
||||
self.x = self.x + dx
|
||||
self.y = self.y + dy
|
||||
return t.CVoid(c.Addr(self), CPtr)
|
||||
|
||||
@t.CVTable
|
||||
class Rect(Shape):
|
||||
def __init__(self, x: CFloat64T, y: CFloat64T, w: CFloat64T, h: CFloat64T):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.w = w
|
||||
self.h = h
|
||||
def area(self) -> CFloat64T:
|
||||
return self.w * self.h
|
||||
def perimeter(self) -> CFloat64T:
|
||||
return 2.0 * (self.w + self.h)
|
||||
def scale(self, factor: CFloat64T) -> 'Rect' | CPtr:
|
||||
self.w = self.w * factor
|
||||
self.h = self.h * factor
|
||||
return t.CVoid(c.Addr(self), CPtr)
|
||||
|
||||
|
||||
# === 运算符重载 + 链式调用(Gargantua V3 风格,堆分配) ===
|
||||
|
||||
class Vec2:
|
||||
x: CFloat64T
|
||||
y: CFloat64T
|
||||
|
||||
def __new__() -> 'Vec2' | CPtr:
|
||||
return t.CVoid(t.CUInt64T(stdlib.malloc(16)), CPtr)
|
||||
|
||||
def __init__(self, x: CFloat64T, y: CFloat64T):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __add__(self, b: 'Vec2' | CPtr) -> 'Vec2' | CPtr:
|
||||
return Vec2(self.x + b.x, self.y + b.y)
|
||||
|
||||
def __sub__(self, b: 'Vec2' | CPtr) -> 'Vec2' | CPtr:
|
||||
return Vec2(self.x - b.x, self.y - b.y)
|
||||
|
||||
def __mul__(self, s: CFloat64T) -> 'Vec2' | CPtr:
|
||||
return Vec2(self.x * s, self.y * s)
|
||||
|
||||
def __neg__(self) -> 'Vec2' | CPtr:
|
||||
return Vec2(-self.x, -self.y)
|
||||
|
||||
def dot(self, b: 'Vec2' | CPtr) -> CFloat64T:
|
||||
return self.x * b.x + self.y * b.y
|
||||
|
||||
def len_sq(self) -> CFloat64T:
|
||||
return self.x * self.x + self.y * self.y
|
||||
|
||||
|
||||
class Vec3:
|
||||
x: CFloat64T
|
||||
y: CFloat64T
|
||||
z: CFloat64T
|
||||
|
||||
def __new__() -> 'Vec3' | CPtr:
|
||||
return t.CVoid(t.CUInt64T(stdlib.malloc(24)), CPtr)
|
||||
|
||||
def __init__(self, x: CFloat64T, y: CFloat64T, z: CFloat64T):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
|
||||
def __add__(self, b: 'Vec3' | CPtr) -> 'Vec3' | CPtr:
|
||||
return Vec3(self.x + b.x, self.y + b.y, self.z + b.z)
|
||||
|
||||
def __sub__(self, b: 'Vec3' | CPtr) -> 'Vec3' | CPtr:
|
||||
return Vec3(self.x - b.x, self.y - b.y, self.z - b.z)
|
||||
|
||||
def __mul__(self, s: CFloat64T) -> 'Vec3' | CPtr:
|
||||
return Vec3(self.x * s, self.y * s, self.z * s)
|
||||
|
||||
def __neg__(self) -> 'Vec3' | CPtr:
|
||||
return Vec3(-self.x, -self.y, -self.z)
|
||||
|
||||
def dot(self, b: 'Vec3' | CPtr) -> CFloat64T:
|
||||
return self.x * b.x + self.y * b.y + self.z * b.z
|
||||
|
||||
def cross(self, b: 'Vec3' | CPtr) -> 'Vec3' | CPtr:
|
||||
return Vec3(self.y * b.z - self.z * b.y,
|
||||
self.z * b.x - self.x * b.z,
|
||||
self.x * b.y - self.y * b.x)
|
||||
|
||||
def len_sq(self) -> CFloat64T:
|
||||
return self.dot(self)
|
||||
|
||||
|
||||
# === 独立类(非继承,避免链接问题) ===
|
||||
|
||||
@t.CVTable
|
||||
class Dog:
|
||||
def __init__(self, name_val: CInt, bark_power: CInt32T):
|
||||
self.name_val = name_val
|
||||
self.health = CInt32T(100)
|
||||
self.bark_power = bark_power
|
||||
def speak(self) -> CInt:
|
||||
return 1
|
||||
def bite(self) -> CInt32T:
|
||||
return self.bark_power
|
||||
def take_damage(self, dmg: CInt32T) -> 'Dog' | CPtr:
|
||||
self.health = self.health - dmg
|
||||
return t.CVoid(c.Addr(self), CPtr)
|
||||
def is_alive(self) -> CInt:
|
||||
if self.health > 0:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
@t.CVTable
|
||||
class Cat:
|
||||
def __init__(self, name_val: CInt, lives: CInt32T):
|
||||
self.name_val = name_val
|
||||
self.health = CInt32T(80)
|
||||
self.lives = lives
|
||||
def speak(self) -> CInt:
|
||||
return 2
|
||||
def scratch(self) -> CInt32T:
|
||||
return CInt32T(15)
|
||||
def take_damage(self, dmg: CInt32T) -> 'Cat' | CPtr:
|
||||
self.health = self.health - dmg
|
||||
return t.CVoid(c.Addr(self), CPtr)
|
||||
def is_alive(self) -> CInt:
|
||||
if self.health > 0:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
# === 嵌套对象组合(堆分配) ===
|
||||
|
||||
class Transform:
|
||||
px: CFloat64T
|
||||
py: CFloat64T
|
||||
pz: CFloat64T
|
||||
scale_x: CFloat64T
|
||||
scale_y: CFloat64T
|
||||
scale_z: CFloat64T
|
||||
|
||||
def __new__() -> 'Transform' | CPtr:
|
||||
return t.CVoid(t.CUInt64T(stdlib.malloc(48)), CPtr)
|
||||
|
||||
def __init__(self, px: CFloat64T, py: CFloat64T, pz: CFloat64T):
|
||||
self.px = px
|
||||
self.py = py
|
||||
self.pz = pz
|
||||
self.scale_x = 1.0
|
||||
self.scale_y = 1.0
|
||||
self.scale_z = 1.0
|
||||
|
||||
def apply_scale(self, sx: CFloat64T, sy: CFloat64T, sz: CFloat64T) -> 'Transform' | CPtr:
|
||||
self.scale_x = self.scale_x * sx
|
||||
self.scale_y = self.scale_y * sy
|
||||
self.scale_z = self.scale_z * sz
|
||||
return t.CVoid(c.Addr(self), CPtr)
|
||||
|
||||
def world_position(self) -> 'Vec3' | CPtr:
|
||||
return Vec3(self.px * self.scale_x, self.py * self.scale_y, self.pz * self.scale_z)
|
||||
|
||||
|
||||
# === 多层继承(子类只重写需要的方法) ===
|
||||
|
||||
@t.CVTable
|
||||
class Vehicle:
|
||||
def __init__(self, speed: CInt32T):
|
||||
self.speed = speed
|
||||
self.fuel = CInt32T(100)
|
||||
def move(self) -> CInt:
|
||||
self.fuel = self.fuel - CInt32T(10)
|
||||
return self.speed
|
||||
def is_running(self) -> CInt:
|
||||
if self.fuel > 0:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
@t.CVTable
|
||||
class Car(Vehicle):
|
||||
def __init__(self, speed: CInt32T, doors: CInt32T):
|
||||
self.speed = speed
|
||||
self.fuel = CInt32T(100)
|
||||
self.doors = doors
|
||||
def honk(self) -> CInt:
|
||||
return 1
|
||||
|
||||
@t.CVTable
|
||||
class ElectricCar(Car):
|
||||
def __init__(self, speed: CInt32T, doors: CInt32T, battery: CInt32T):
|
||||
self.speed = speed
|
||||
self.fuel = CInt32T(100)
|
||||
self.doors = doors
|
||||
self.battery = battery
|
||||
def charge(self):
|
||||
self.battery = self.battery + CInt32T(20)
|
||||
def move(self) -> CInt:
|
||||
self.battery = self.battery - CInt32T(5)
|
||||
return self.speed
|
||||
|
||||
|
||||
def main() -> CInt | CExport:
|
||||
w32.win32console.SetConsoleCP(65001)
|
||||
w32.win32console.SetConsoleOutputCP(65001)
|
||||
|
||||
testcheck.begin("TestProject3: 泛型与OOP综合测试")
|
||||
|
||||
buf: CChar | CPtr = CPtr(stdlib.malloc(256))
|
||||
|
||||
# === 基础函数泛型 ===
|
||||
testcheck.section("基础函数泛型")
|
||||
print(add(3, 5))
|
||||
print(add(1.5, 2.5))
|
||||
print(add_u64(1, 2, CUInt64T(3000000000000000)))
|
||||
print(fxi(0.2, 6))
|
||||
print(wrap_add(10, 20))
|
||||
print(double_add(5, 7))
|
||||
testcheck.ok("基础函数泛型测试完成")
|
||||
|
||||
# === 基础结构体泛型 ===
|
||||
testcheck.section("基础结构体泛型")
|
||||
a = A(100)
|
||||
print(a.get_a())
|
||||
p1 = Pair(10, 20)
|
||||
print(p1.get_first())
|
||||
print(p1.get_second())
|
||||
testcheck.ok("基础结构体泛型测试完成")
|
||||
|
||||
# === OOP 继承测试 ===
|
||||
testcheck.section("OOP 继承测试")
|
||||
c1 = Circle(0.0, 0.0, 5.0)
|
||||
viperlib.snprintf(buf, 256, "Circle area=%.2f perim=%.2f", c1.area(), c1.perimeter())
|
||||
print(buf)
|
||||
|
||||
r1 = Rect(0.0, 0.0, 4.0, 3.0)
|
||||
viperlib.snprintf(buf, 256, "Rect area=%.2f perim=%.2f", r1.area(), r1.perimeter())
|
||||
print(buf)
|
||||
|
||||
# 链式调用: scale -> move
|
||||
c2 = Circle(1.0, 2.0, 3.0)
|
||||
c2.scale(2.0).move(10.0, 20.0)
|
||||
viperlib.snprintf(buf, 256, "Circle after scale+move: x=%.1f y=%.1f r=%.1f area=%.2f",
|
||||
c2.x, c2.y, c2.r, c2.area())
|
||||
print(buf)
|
||||
|
||||
r2 = Rect(0.0, 0.0, 10.0, 5.0)
|
||||
r2.scale(2.0).scale(0.5)
|
||||
viperlib.snprintf(buf, 256, "Rect after double scale: w=%.1f h=%.1f area=%.2f",
|
||||
r2.w, r2.h, r2.area())
|
||||
print(buf)
|
||||
testcheck.ok("OOP 继承测试完成")
|
||||
|
||||
# === Vec2 运算符重载 + 链式调用(堆分配) ===
|
||||
testcheck.section("Vec2 运算符重载 + 链式调用")
|
||||
v1 = Vec2(3.0, 4.0)
|
||||
v2 = Vec2(1.0, 2.0)
|
||||
v3 = v1 + v2
|
||||
viperlib.snprintf(buf, 256, "Vec2 add: (%.1f, %.1f)", v3.x, v3.y)
|
||||
print(buf)
|
||||
|
||||
v4 = v1 - v2
|
||||
viperlib.snprintf(buf, 256, "Vec2 sub: (%.1f, %.1f)", v4.x, v4.y)
|
||||
print(buf)
|
||||
|
||||
v5 = v1 * 2.0
|
||||
viperlib.snprintf(buf, 256, "Vec2 mul: (%.1f, %.1f)", v5.x, v5.y)
|
||||
print(buf)
|
||||
|
||||
v6 = -v1
|
||||
viperlib.snprintf(buf, 256, "Vec2 neg: (%.1f, %.1f)", v6.x, v6.y)
|
||||
print(buf)
|
||||
|
||||
d = v1.dot(v2)
|
||||
viperlib.snprintf(buf, 256, "Vec2 dot: %.1f", d)
|
||||
print(buf)
|
||||
|
||||
# 链式运算: (v1 + v2) * 3.0
|
||||
v_chain = (v1 + v2) * 3.0
|
||||
viperlib.snprintf(buf, 256, "Vec2 chain (v1+v2)*3: (%.1f, %.1f)", v_chain.x, v_chain.y)
|
||||
print(buf)
|
||||
testcheck.ok("Vec2 运算符重载测试完成")
|
||||
|
||||
# === Vec3 运算符重载 + 链式调用(堆分配) ===
|
||||
testcheck.section("Vec3 运算符重载 + 链式调用")
|
||||
a1 = Vec3(1.0, 0.0, 0.0)
|
||||
a2 = Vec3(0.0, 1.0, 0.0)
|
||||
cross_v = a1.cross(a2)
|
||||
viperlib.snprintf(buf, 256, "Vec3 cross: (%.1f, %.1f, %.1f)", cross_v.x, cross_v.y, cross_v.z)
|
||||
print(buf)
|
||||
|
||||
dot_v = a1.dot(a2)
|
||||
viperlib.snprintf(buf, 256, "Vec3 dot: %.1f", dot_v)
|
||||
print(buf)
|
||||
|
||||
# 链式: (a + b).dot(c)
|
||||
a3 = Vec3(1.0, 2.0, 3.0)
|
||||
a4 = Vec3(4.0, 5.0, 6.0)
|
||||
a5 = Vec3(7.0, 8.0, 9.0)
|
||||
chain_dot = (a3 + a4).dot(a5)
|
||||
viperlib.snprintf(buf, 256, "Vec3 chain dot: %.1f", chain_dot)
|
||||
print(buf)
|
||||
|
||||
# 链式: (a * 2.0 - b).cross(c)
|
||||
chain_cross = (a3 * 2.0 - a4).cross(a5)
|
||||
viperlib.snprintf(buf, 256, "Vec3 chain cross: (%.1f, %.1f, %.1f)", chain_cross.x, chain_cross.y, chain_cross.z)
|
||||
print(buf)
|
||||
|
||||
# len_sq 链式
|
||||
lsq = a3.len_sq()
|
||||
viperlib.snprintf(buf, 256, "Vec3 len_sq: %.1f", lsq)
|
||||
print(buf)
|
||||
testcheck.ok("Vec3 运算符重载测试完成")
|
||||
|
||||
# === Dog/Cat 虚方法 ===
|
||||
testcheck.section("Dog/Cat 虚方法")
|
||||
dog = Dog(1, CInt32T(30))
|
||||
cat = Cat(2, CInt32T(9))
|
||||
|
||||
viperlib.snprintf(buf, 256, "Dog speak=%d bite=%d alive=%d", dog.speak(), dog.bite(), dog.is_alive())
|
||||
print(buf)
|
||||
viperlib.snprintf(buf, 256, "Cat speak=%d scratch=%d alive=%d", cat.speak(), cat.scratch(), cat.is_alive())
|
||||
print(buf)
|
||||
|
||||
dog.take_damage(CInt32T(40)).take_damage(CInt32T(30))
|
||||
viperlib.snprintf(buf, 256, "Dog after 70dmg: health=%d alive=%d", dog.health, dog.is_alive())
|
||||
print(buf)
|
||||
|
||||
cat.take_damage(CInt32T(50))
|
||||
viperlib.snprintf(buf, 256, "Cat after 50dmg: health=%d alive=%d", cat.health, cat.is_alive())
|
||||
print(buf)
|
||||
testcheck.ok("Dog/Cat 虚方法测试完成")
|
||||
|
||||
# === Transform 嵌套组合(堆分配,链式调用) ===
|
||||
testcheck.section("Transform 嵌套组合")
|
||||
tr = Transform(10.0, 20.0, 30.0)
|
||||
tr.apply_scale(2.0, 3.0, 4.0)
|
||||
pos = tr.world_position()
|
||||
viperlib.snprintf(buf, 256, "Transform pos: (%.1f, %.1f, %.1f)", pos.x, pos.y, pos.z)
|
||||
print(buf)
|
||||
|
||||
tr2 = Transform(5.0, 10.0, 15.0)
|
||||
tr2.apply_scale(2.0, 2.0, 2.0)
|
||||
pos2 = tr2.world_position()
|
||||
viperlib.snprintf(buf, 256, "Transform2 pos: (%.1f, %.1f, %.1f)", pos2.x, pos2.y, pos2.z)
|
||||
print(buf)
|
||||
testcheck.ok("Transform 嵌套组合测试完成")
|
||||
|
||||
# === 多层继承 ===
|
||||
testcheck.section("多层继承")
|
||||
ec = ElectricCar(CInt32T(120), CInt32T(4), CInt32T(80))
|
||||
viperlib.snprintf(buf, 256, "ECar speed=%d doors=%d battery=%d", ec.speed, ec.doors, ec.battery)
|
||||
print(buf)
|
||||
ec.charge()
|
||||
viperlib.snprintf(buf, 256, "ECar after charge: battery=%d", ec.battery)
|
||||
print(buf)
|
||||
spd = ec.move()
|
||||
viperlib.snprintf(buf, 256, "ECar move: speed=%d battery=%d", spd, ec.battery)
|
||||
print(buf)
|
||||
viperlib.snprintf(buf, 256, "ECar honk=%d running=%d", ec.honk(), ec.is_running())
|
||||
print(buf)
|
||||
testcheck.ok("多层继承测试完成")
|
||||
|
||||
# === 泛型 + OOP 组合 ===
|
||||
testcheck.section("泛型 + OOP 组合")
|
||||
calc1 = Calculator(10)
|
||||
print(calc1.compute(5))
|
||||
calc2 = Calculator(1.5)
|
||||
print(calc2.compute(2.5))
|
||||
|
||||
c1_gen = Container(42)
|
||||
print(c1_gen.get_value())
|
||||
print(c1_gen.get_flag())
|
||||
testcheck.ok("泛型 + OOP 组合测试完成")
|
||||
|
||||
# === snprintf 格式化 ===
|
||||
testcheck.section("snprintf 格式化")
|
||||
viperlib.snprintf(buf, 256, "int=%d, float=%.2f", 42, 3.14)
|
||||
print(buf)
|
||||
viperlib.snprintf(buf, 256, "result=%d", add(100, 200))
|
||||
print(buf)
|
||||
testcheck.ok("snprintf 格式化测试完成")
|
||||
|
||||
# === 新语法类型强转 (t1|t2)(x) ===
|
||||
testcheck.section("新语法类型强转 (t1|t2)(x)")
|
||||
val_i64: CUInt64T = CUInt64T(42)
|
||||
val_i32 = (t.CInt)(val_i64)
|
||||
print(val_i32)
|
||||
|
||||
ptr_raw = stdlib.malloc(8)
|
||||
ptr_typed = (t.CInt | t.CPtr)(ptr_raw)
|
||||
viperlib.snprintf(buf, 256, "cast ptr=%p", ptr_typed)
|
||||
print(buf)
|
||||
|
||||
val_f = (t.CDouble)(42)
|
||||
viperlib.snprintf(buf, 256, "cast float=%.1f", val_f)
|
||||
print(buf)
|
||||
testcheck.ok("新语法类型强转测试完成")
|
||||
|
||||
# === CDefine 宏测试 ===
|
||||
testcheck.section("CDefine 宏测试")
|
||||
definetest.define_main()
|
||||
testcheck.ok("CDefine 宏测试完成")
|
||||
|
||||
# === Enum 测试 ===
|
||||
testcheck.section("Enum 测试")
|
||||
enumtest.enum_main()
|
||||
testcheck.ok("Enum 测试完成")
|
||||
|
||||
# === MPool OOP 测试 ===
|
||||
testcheck.section("MPool OOP 测试")
|
||||
mpooltest.mpool_main()
|
||||
testcheck.ok("MPool OOP 测试完成")
|
||||
|
||||
# === Vector 泛型测试 ===
|
||||
testcheck.section("Vector 泛型测试")
|
||||
vectortest.vector_main()
|
||||
testcheck.ok("Vector 泛型测试完成")
|
||||
|
||||
# === Win32 FileIO 测试 ===
|
||||
testcheck.section("Win32 FileIO 测试")
|
||||
fileiotest.fileio_main()
|
||||
testcheck.ok("Win32 FileIO 测试完成")
|
||||
|
||||
return testcheck.end()
|
||||
91
Test/TestProject3/App/mpooltest.py
Normal file
91
Test/TestProject3/App/mpooltest.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import t, c
|
||||
from t import CInt, CPtr, CChar
|
||||
import viperlib
|
||||
import viperio
|
||||
import stdlib
|
||||
import memhub
|
||||
|
||||
|
||||
def mpool_main() -> CInt:
|
||||
buf: bytes = stdlib.malloc(256)
|
||||
|
||||
arena: bytes = stdlib.malloc(4096)
|
||||
with memhub.MemPool(arena, 4096) as pool:
|
||||
p1: bytes = pool.alloc(32)
|
||||
p2: bytes = pool.alloc(64)
|
||||
p3: bytes = pool.alloc(128)
|
||||
if p1 != None and p2 != None and p3 != None:
|
||||
viperlib.snprintf(buf, 256, "MPool bump: 3 allocs OK")
|
||||
print(buf)
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "MPool bump: alloc FAILED")
|
||||
print(buf)
|
||||
|
||||
pool.reset()
|
||||
p4: bytes = pool.alloc(256)
|
||||
if p4 != None:
|
||||
viperlib.snprintf(buf, 256, "MPool bump: reset+alloc OK")
|
||||
print(buf)
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "MPool bump: reset+alloc FAILED")
|
||||
print(buf)
|
||||
|
||||
stdlib.free(arena)
|
||||
|
||||
arena2: bytes = stdlib.malloc(4096)
|
||||
with memhub.MemSlab(arena2, 4096, 64) as spool:
|
||||
b1: bytes = spool.alloc(64)
|
||||
b2: bytes = spool.alloc(64)
|
||||
b3: bytes = spool.alloc(64)
|
||||
if b1 != None and b2 != None and b3 != None:
|
||||
viperlib.snprintf(buf, 256, "MPool slab: 3 allocs OK")
|
||||
print(buf)
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "MPool slab: alloc FAILED")
|
||||
print(buf)
|
||||
|
||||
spool.free(b2)
|
||||
b4: bytes = spool.alloc(64)
|
||||
if b4 != None:
|
||||
viperlib.snprintf(buf, 256, "MPool slab: free+realloc OK")
|
||||
print(buf)
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "MPool slab: free+realloc FAILED")
|
||||
print(buf)
|
||||
|
||||
stdlib.free(arena2)
|
||||
|
||||
arena3: bytes = stdlib.malloc(4096)
|
||||
hpool: memhub.MemPool | CPtr = memhub.MemPool(arena3, 4096)
|
||||
h1: bytes = hpool.alloc(32)
|
||||
h2: bytes = hpool.alloc(64)
|
||||
if h1 != None and h2 != None:
|
||||
viperlib.snprintf(buf, 256, "MPool manual: 2 allocs OK")
|
||||
print(buf)
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "MPool manual: alloc FAILED")
|
||||
print(buf)
|
||||
hpool.reset()
|
||||
h3: bytes = hpool.alloc(128)
|
||||
if h3 != None:
|
||||
viperlib.snprintf(buf, 256, "MPool manual: reset+alloc OK")
|
||||
print(buf)
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "MPool manual: reset+alloc FAILED")
|
||||
print(buf)
|
||||
|
||||
stdlib.free(arena3)
|
||||
|
||||
arena4: bytes = stdlib.malloc(4096)
|
||||
with memhub.MemPool(arena4, 4096) as bpool:
|
||||
wb: viperio.Buf | CPtr = bpool.alloc_buf(128)
|
||||
if wb.data != None:
|
||||
viperlib.snprintf(wb.cstr(), 128, "MPool alloc_buf: hello from Buf!")
|
||||
print(wb.cstr())
|
||||
else:
|
||||
viperlib.snprintf(buf, 256, "MPool alloc_buf: FAILED")
|
||||
print(buf)
|
||||
|
||||
stdlib.free(arena4)
|
||||
stdlib.free(buf)
|
||||
return 0
|
||||
43
Test/TestProject3/App/vectortest.py
Normal file
43
Test/TestProject3/App/vectortest.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import t, c
|
||||
from t import CInt, CPtr, CChar, CFloat64T
|
||||
import viperlib
|
||||
import stdlib
|
||||
import vector
|
||||
import memhub
|
||||
|
||||
POOL_SIZE: t.CDefine = 4096
|
||||
|
||||
def vector_main() -> CInt:
|
||||
buf: str = stdlib.malloc(256)
|
||||
pool_mem: t.CVoid | t.CPtr = stdlib.malloc(POOL_SIZE)
|
||||
pool: memhub.MemPool | CPtr = memhub.MemPool(pool_mem, POOL_SIZE)
|
||||
|
||||
vi: vector.Vector | CPtr = vector.Vector(pool, 10, CInt(0))
|
||||
vi.push(CInt(10))
|
||||
vi.push(CInt(20))
|
||||
vi.push(CInt(30))
|
||||
viperlib.snprintf(buf, 256, "Vector[int]: len=%d, [0]=%d, [1]=%d, [2]=%d", vi.len(), vi.get(0), vi.get(1), vi.get(2))
|
||||
print(buf)
|
||||
|
||||
vi.set(1, CInt(99))
|
||||
viperlib.snprintf(buf, 256, "Vector[int]: set[1]=99, [1]=%d", vi.get(1))
|
||||
print(buf)
|
||||
|
||||
vi.clear()
|
||||
viperlib.snprintf(buf, 256, "Vector[int]: cleared, len=%d", vi.len())
|
||||
print(buf)
|
||||
|
||||
vi.free()
|
||||
|
||||
vd: vector.Vector | CPtr = vector.Vector(pool, 8, CFloat64T(0.0))
|
||||
vd.push(CFloat64T(1.5))
|
||||
vd.push(CFloat64T(2.7))
|
||||
vd.push(CFloat64T(3.14))
|
||||
viperlib.snprintf(buf, 256, "Vector[double]: len=%d, [0]=%.1f, [1]=%.1f, [2]=%.2f", vd.len(), vd.get(0), vd.get(1), vd.get(2))
|
||||
print(buf)
|
||||
|
||||
vd.free()
|
||||
|
||||
stdlib.free(pool_mem)
|
||||
stdlib.free(buf)
|
||||
return 0
|
||||
Reference in New Issue
Block a user