Files
2026-10-17 21:57:03 +08:00

416 lines
8.6 KiB
Plaintext

import viperlib
import stdlib
import platmacro
import win32.win32console
import testcheck
# ============================================================
# DefineTest: CDefine constants/macros, cif/celif/celse chains, defined(), platmacro.
# Prints results for manual inspection.
# ============================================================
define MAX_SIZE = 1024
define MIN_VAL = 0
define MAX_VAL = 100
define MASK_BYTE = 0xFF
define SHIFT_BITS = 8
define PI_INT = 3
define ENABLED = 1
define DISABLED = 0
define PAGE_SIZE = 4096
define BUFFER_LEN = 256
define CODE_SEG: uint16 = 0x08
define DATA_SEG: uint16 = 0x10
define STACK_SEG: uint16 = 0x20
define MAKE_FLAG(bit: int32) -> int32: return 1 << bit
define CLAMP(x: int32, lo: int32, hi: int32) -> int32:
if x < lo: return lo
if x > hi: return hi
return x
def test_basic_define() -> int:
print(MAX_SIZE)
print(MIN_VAL)
print(MAX_VAL)
print(MASK_BYTE)
print(SHIFT_BITS)
return 0
def test_define_arithmetic() -> int:
a: int32 = 10
b: int32 = a + MAX_SIZE
print(b)
c_val: int32 = (a << SHIFT_BITS) & MASK_BYTE
print(c_val)
d: int32 = MAX_VAL - MIN_VAL
print(d)
e: int32 = PAGE_SIZE / 4
print(e)
return 0
def test_define_conditional() -> int:
mode: int32 = ENABLED
if mode == ENABLED:
print(1)
else:
print(0)
if mode == DISABLED:
print(0)
else:
print(1)
val: int32 = 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() -> int:
i: int32 = 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() -> int:
seg: uint16 = CODE_SEG | DATA_SEG | STACK_SEG
print(seg)
cs: uint16 = CODE_SEG
print(cs)
ds: uint16 = DATA_SEG
print(ds)
return 0
def test_define_function() -> int:
flag0 = MAKE_FLAG(0)
print(flag0)
flag3 = MAKE_FLAG(3)
print(flag3)
flag7 = MAKE_FLAG(7)
print(flag7)
return 0
def test_define_clamp() -> int:
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() -> int:
buf: str = 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() -> int:
flags: uint32 = MAKE_FLAG(0) | MAKE_FLAG(1) | MAKE_FLAG(2)
print(flags)
shifted: uint32 = MASK_BYTE << SHIFT_BITS
print(shifted)
total: uint32 = PAGE_SIZE + BUFFER_LEN
print(total)
return 0
def test_cif_basic() -> int:
cif ENABLED:
print(1)
celse:
print(0)
cif DISABLED:
print(0)
celse:
print(1)
cif MAX_SIZE > 512:
print(1)
celse:
print(0)
cif MIN_VAL > 0:
print(0)
celse:
print(1)
return 0
def test_cif_elif_else() -> int:
# A cif chain can only be pruned at compile time, so the chain selector must be a CDefine
# constant (a runtime variable is rejected).
define VAL = 2
cif VAL == 1:
print(1)
celif VAL == 2:
print(2)
celif VAL == 3:
print(3)
celse:
print(0)
cif VAL > 10:
print(10)
celif VAL > 5:
print(5)
celse:
print(VAL)
return 0
def test_cifdef_cifndef() -> int:
cif defined(MAX_SIZE):
print(MAX_SIZE)
celse:
print(0)
cif defined(UNDEFINED_MACRO_XYZ):
print(0)
celse:
print(1)
cif not defined(UNDEFINED_MACRO_XYZ):
print(1)
celse:
print(0)
cif not defined(MAX_SIZE):
print(0)
celse:
print(1)
return 0
def test_cif_platform() -> int:
cif defined(_WIN32):
print(1)
celse:
print(0)
cif defined(__x86_64__):
print(1)
celse:
print(0)
cif SIZEOF_VOID_P == 8:
print(8)
celif SIZEOF_VOID_P == 4:
print(4)
celse:
print(0)
return 0
def test_cif_nested() -> int:
cif ENABLED:
cif MAX_SIZE > 512:
print(1)
celse:
print(0)
cif defined(PAGE_SIZE):
print(PAGE_SIZE)
celse:
print(0)
celse:
print(0)
return 0
def test_cif_expr() -> int:
cif MASK_BYTE & 0x0F:
print(1)
celse:
print(0)
cif (MAX_SIZE >> SHIFT_BITS) > 0:
print(1)
celse:
print(0)
cif ENABLED and MAX_SIZE > 0:
print(1)
celse:
print(0)
cif DISABLED or MAX_SIZE > 0:
print(1)
celse:
print(0)
return 0
def test_platmacro_platform() -> int:
cif defined(platmacro.IS_WINDOWS):
print(1)
celse:
print(0)
cif platmacro.IS_WINDOWS == 1:
print(1)
celse:
print(0)
cif platmacro.IS_LINUX == 0:
print(1)
celse:
print(0)
cif platmacro.IS_X86_64 == 1:
print(1)
celse:
print(0)
return 0
def test_platmacro_sizes() -> int:
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() -> int:
cif platmacro.IS_LITTLE_ENDIAN == 1:
print(1)
celse:
print(0)
cif platmacro.IS_BIG_ENDIAN == 0:
print(1)
celse:
print(0)
cif platmacro.BYTE_ORDER_LE == 1:
print(1)
celse:
print(0)
return 0
def test_platmacro_features() -> int:
cif platmacro.HAS_SSE2 == 1:
print(1)
celse:
print(0)
cif platmacro.HAS_AVX == 1:
print(1)
celse:
print(0)
cif platmacro.HAS_NEON == 0:
print(1)
celse:
print(0)
return 0
def test_platmacro_align() -> int:
print(platmacro.ALIGNOF_INT)
print(platmacro.ALIGNOF_PTR)
print(platmacro.ALIGNOF_DOUBLE)
cif platmacro.ALIGNOF_PTR == 8:
print(1)
celse:
print(0)
return 0
def test_cifdef_cross_module() -> int:
cif defined(platmacro.IS_WINDOWS):
cif platmacro.PTR_SIZE == 8:
print(64)
celse:
print(32)
celse:
print(0)
cif not defined(platmacro.IS_ARM64):
print(1)
celse:
print(0)
cif defined(platmacro.HAS_AVX2):
print(1)
celse:
print(0)
return 0
def test_cifdef_guard() -> int:
define MY_HEADER_GUARD = 1
cif not defined(MY_HEADER_GUARD):
print(0)
celse:
print(1)
cif defined(MY_HEADER_GUARD):
print(1)
celse:
print(0)
return 0
def test_cifdef_undefined() -> int:
cif not defined(NONEXISTENT_MACRO_ABC):
print(1)
celse:
print(0)
cif not defined(THIS_IS_ALSO_NOT_DEFINED):
print(1)
celse:
print(0)
cif defined(NONEXISTENT_MACRO_ABC):
print(0)
celse:
print(1)
return 0
def test_cifdef_complex() -> int:
cif defined(platmacro.IS_WINDOWS) and platmacro.PTR_SIZE == 8:
print(1)
celse:
print(0)
cif not defined(platmacro.IS_ARM64) and platmacro.IS_X86_64 == 1:
print(1)
celse:
print(0)
cif defined(platmacro.HAS_SSE2):
cif defined(platmacro.HAS_AVX):
print(2)
celse:
print(1)
celse:
print(0)
return 0
def main() -> int:
win32.win32console.SetConsoleCP(65001)
win32.win32console.SetConsoleOutputCP(65001)
testcheck.begin("DefineTest: CDefine 宏与 cif 测试")
testcheck.section("CDefine 基础")
test_basic_define()
test_define_arithmetic()
test_define_conditional()
test_define_loop()
test_typed_define()
test_define_function()
test_define_clamp()
test_define_string()
test_define_combined()
testcheck.section("cif / celif / celse")
test_cif_basic()
test_cif_elif_else()
test_cifdef_cifndef()
test_cif_platform()
test_cif_nested()
test_cif_expr()
testcheck.section("platmacro")
test_platmacro_platform()
test_platmacro_sizes()
test_platmacro_endian()
test_platmacro_features()
test_platmacro_align()
testcheck.section("defined() 综合")
test_cifdef_cross_module()
test_cifdef_guard()
test_cifdef_undefined()
test_cifdef_complex()
return testcheck.end()