- ForAndLoopTest:新增 Test 11-17,覆盖 for-else(迭代耗尽 / break 跳过 / range(0) 空迭代 / list[str] 迭代)与 while-else(条件变假 / 零迭代 / break 跳过) - EqTest:新增 Test 7(int vs None 四种写法 × 零/非零 + None 在左镜像方向)、 Test 8(char / sizet 位宽,防从空指针 load 回归)
90 lines
4.4 KiB
Plaintext
90 lines
4.4 KiB
Plaintext
from stdint import *
|
||
import w32.win32console
|
||
import t, c
|
||
from t import CInt, CExport
|
||
import stdio
|
||
import stdlib
|
||
import string
|
||
import testcheck
|
||
|
||
|
||
def main() -> CInt | CExport:
|
||
w32.win32console.SetConsoleCP(65001)
|
||
w32.win32console.SetConsoleOutputCP(65001)
|
||
|
||
testcheck.begin("== 重载和 is 地址比较测试")
|
||
|
||
# === Test 1: str == str 值比较 ===
|
||
testcheck.section("Test 1: str == str 值比较")
|
||
s1: str = "hello"
|
||
s2: str = "hello"
|
||
s3: str = "world"
|
||
# i8* == 是指针地址比较(C-Like 语义),内容比较需显式 strcmp
|
||
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 == 字面量 ===
|
||
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 地址比较 ===
|
||
testcheck.section("Test 4: str is str address compare")
|
||
# s1 和 s2 是不同的字符串变量,地址不同
|
||
testcheck.check(not (s1 is s2), "str is str (diff addr) OK", "str is str (diff addr) FAILED")
|
||
# s1 is s1 应该为 True(同一变量)
|
||
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 比较 ===
|
||
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: 整型 vs None(整数上的 None 隐式为 0)===
|
||
# 语义(TPC / TPV 统一):x is None ≡ x == None ≡ x == 0;x is not None ≡ x != None ≡ x != 0
|
||
testcheck.section("Test 7: int vs None")
|
||
nz: int = 7
|
||
zr: int = 0
|
||
# 非零整数 ⇒ 不等于 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")
|
||
# 零值 ⇒ 等价于 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")
|
||
# None 在左侧的镜像方向(TPV 此前该方向分支有误,恒判相等)
|
||
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: 其它整型位宽 vs None(i8 / i64,防"从空指针 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()
|