from stdint import * import win32.win32console import stdio import stdlib import string import testcheck export def main() -> int: win32.win32console.SetConsoleCP(65001) win32.win32console.SetConsoleOutputCP(65001) testcheck.begin("== 重载和 is 地址比较测试") # === Test 1: str == str value compare === testcheck.section("Test 1: str == str 值比较") s1: str = "hello" s2: str = "hello" s3: str = "world" # i8* == compares addresses (C-like); use strcmp for content compare testcheck.check(string.strcmp(s1, s2) == 0, "str == str (same value) OK", "str == str (same value) FAILED") testcheck.check(string.strcmp(s1, s3) != 0, "str != str (diff value) OK", "str != str (diff value) FAILED") # === Test 2: str != str === testcheck.section("Test 2: str != str") testcheck.check(string.strcmp(s1, s3) != 0, "str != str (diff) OK", "str != str (diff) FAILED") testcheck.check(string.strcmp(s1, s2) == 0, "str == str (same) via != OK", "str == str (same) via != FAILED") # === Test 3: str == literal === testcheck.section("Test 3: str == literal") testcheck.check(string.strcmp(s1, "hello") == 0, "str == literal OK", "str == literal FAILED") testcheck.check(string.strcmp(s1, "world") != 0, "str != literal OK", "str != literal FAILED") # === Test 4: str is str address compare === testcheck.section("Test 4: str is str address compare") # s1 and s2 are distinct variables with different addresses testcheck.check(not (s1 is s2), "str is str (diff addr) OK", "str is str (diff addr) FAILED") # s1 is s1 is True (same variable) testcheck.check(s1 is s1, "str is self OK", "str is self FAILED") # === Test 5: int == int === testcheck.section("Test 5: int == int") i1: int = 42 i2: int = 42 i3: int = 99 testcheck.check(i1 == i2, "int == int (same) OK", "int == int (same) FAILED") testcheck.check(not (i1 == i3), "int != int (diff) OK", "int != int (diff) FAILED") # === Test 6: None compare === testcheck.section("Test 6: None compare") null_ptr: str = None testcheck.check(null_ptr == None, "ptr == None OK", "ptr == None FAILED") testcheck.check(not (s1 == None), "str != None OK", "str != None FAILED") # === Test 7: int vs None (None on int is implicitly 0) === # Unified semantics: x is None, x == None and x == 0 agree (and negations) testcheck.section("Test 7: int vs None") nz: int = 7 zr: int = 0 # nonzero int => not None testcheck.check(not (nz is None), "int(7) is None -> false OK", "int(7) is None FAILED") testcheck.check(nz is not None, "int(7) is not None -> true OK", "int(7) is not None FAILED") testcheck.check(not (nz == None), "int(7) == None -> false OK", "int(7) == None FAILED") testcheck.check(nz != None, "int(7) != None -> true OK", "int(7) != None FAILED") # zero => equivalent to None testcheck.check(zr is None, "int(0) is None -> true OK", "int(0) is None FAILED") testcheck.check(not (zr is not None), "int(0) is not None -> false OK", "int(0) is not None FAILED") testcheck.check(zr == None, "int(0) == None -> true OK", "int(0) == None FAILED") testcheck.check(not (zr != None), "int(0) != None -> false OK", "int(0) != None FAILED") # mirrored None-on-left direction testcheck.check(not (None is nz), "None is int(7) -> false OK", "None is int(7) FAILED") testcheck.check(None is zr, "None is int(0) -> true OK", "None is int(0) FAILED") # === Test 8: other int widths vs None (i8/i64, guards null-pointer load) === testcheck.section("Test 8: char / sizet vs None") c8: char = 0 c8nz: char = 65 testcheck.check(c8 is None, "char(0) is None -> true OK", "char(0) is None FAILED") testcheck.check(c8nz is not None, "char(65) is not None -> true OK", "char(65) is not None FAILED") testcheck.check(c8 == None, "char(0) == None -> true OK", "char(0) == None FAILED") s64: sizet = 0 s64nz: sizet = 4096 testcheck.check(s64 is None, "sizet(0) is None -> true OK", "sizet(0) is None FAILED") testcheck.check(s64nz is not None, "sizet(4096) is not None -> true OK", "sizet(4096) is not None FAILED") testcheck.check(s64nz != None, "sizet(4096) != None -> true OK", "sizet(4096) != None FAILED") return testcheck.end()