import t, c from t import CInt, CInt32T, CUInt32T, CUInt64T, CFloat64T, CPtr, CChar, CDefine, CUInt16T 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 CODE_SEG: t.CDefine | uint16 = 0x08 DATA_SEG: t.CDefine | uint16 = 0x10 STACK_SEG: t.CDefine | uint16 = 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: 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() -> CInt: # 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() -> CInt: 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() -> CInt: 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() -> CInt: 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() -> CInt: 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() -> CInt: 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() -> 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: 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() -> CInt: 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() -> CInt: 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() -> CInt: 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() -> CInt: 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() -> CInt: 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() -> CInt: 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()