145 lines
3.4 KiB
Python
145 lines
3.4 KiB
Python
import stdio
|
|
import t, c
|
|
import string
|
|
|
|
|
|
# ============================================================
|
|
# eq_test - ==、!=、is、is not、None 比较测试
|
|
#
|
|
# 验证比较表达式的翻译,特别是 None 常量和 is/is not 操作符
|
|
# ============================================================
|
|
|
|
|
|
def test_int_eq():
|
|
stdio.printf("--- Test 1: int == / != ---\n")
|
|
|
|
a: t.CInt = 42
|
|
b: t.CInt = 42
|
|
c3: t.CInt = 99
|
|
|
|
if a == b:
|
|
stdio.printf("int == int (same) OK\n")
|
|
else:
|
|
stdio.printf("int == int (same) FAIL\n")
|
|
|
|
if a != c3:
|
|
stdio.printf("int != int (diff) OK\n")
|
|
else:
|
|
stdio.printf("int != int (diff) FAIL\n")
|
|
|
|
if not (a == c3):
|
|
stdio.printf("not (int == diff) OK\n")
|
|
else:
|
|
stdio.printf("not (int == diff) FAIL\n")
|
|
|
|
|
|
def test_int_cmp():
|
|
stdio.printf("--- Test 2: int < > <= >= ---\n")
|
|
|
|
a: t.CInt = 5
|
|
b: t.CInt = 10
|
|
|
|
if a < b:
|
|
stdio.printf("int < OK\n")
|
|
else:
|
|
stdio.printf("int < FAIL\n")
|
|
|
|
if b > a:
|
|
stdio.printf("int > OK\n")
|
|
else:
|
|
stdio.printf("int > FAIL\n")
|
|
|
|
if a <= 5:
|
|
stdio.printf("int <= OK\n")
|
|
else:
|
|
stdio.printf("int <= FAIL\n")
|
|
|
|
if b >= 10:
|
|
stdio.printf("int >= OK\n")
|
|
else:
|
|
stdio.printf("int >= FAIL\n")
|
|
|
|
|
|
def test_ptr_is_none():
|
|
stdio.printf("--- Test 3: ptr is None / is not None ---\n")
|
|
|
|
# strchr 找到字符返回非空指针,找不到返回 None
|
|
p: str = string.strchr("Hello", 108) # 'l' = 108
|
|
if p is not None:
|
|
stdio.printf("strchr found, is not None OK\n")
|
|
else:
|
|
stdio.printf("strchr found, is not None FAIL\n")
|
|
|
|
p2: str = string.strchr("Hello", 122) # 'z' = 122, not found
|
|
if p2 is None:
|
|
stdio.printf("strchr not found, is None OK\n")
|
|
else:
|
|
stdio.printf("strchr not found, is None FAIL\n")
|
|
|
|
|
|
def test_ptr_assign_none():
|
|
stdio.printf("--- Test 4: assign None and compare ---\n")
|
|
|
|
p: str = None
|
|
if p is None:
|
|
stdio.printf("None assign, is None OK\n")
|
|
else:
|
|
stdio.printf("None assign, is None FAIL\n")
|
|
|
|
if p is not None:
|
|
stdio.printf("None assign, is not None FAIL\n")
|
|
else:
|
|
stdio.printf("None assign, is not None OK\n")
|
|
|
|
|
|
def test_ptr_eq_none():
|
|
stdio.printf("--- Test 5: ptr == None / != None ---\n")
|
|
|
|
p: str = string.strstr("Hello World", "World")
|
|
if p != None:
|
|
stdio.printf("strstr found, != None OK\n")
|
|
else:
|
|
stdio.printf("strstr found, != None FAIL\n")
|
|
|
|
p2: str = string.strstr("Hello", "xyz")
|
|
if p2 == None:
|
|
stdio.printf("strstr not found, == None OK\n")
|
|
else:
|
|
stdio.printf("strstr not found, == None FAIL\n")
|
|
|
|
|
|
def test_bool_logic():
|
|
stdio.printf("--- Test 6: bool and/or ---\n")
|
|
|
|
a: t.CInt = 1
|
|
b: t.CInt = 0
|
|
|
|
if a and b:
|
|
stdio.printf("1 and 0 = true FAIL\n")
|
|
else:
|
|
stdio.printf("1 and 0 = false OK\n")
|
|
|
|
if a or b:
|
|
stdio.printf("1 or 0 = true OK\n")
|
|
else:
|
|
stdio.printf("1 or 0 = true FAIL\n")
|
|
|
|
if not b:
|
|
stdio.printf("not 0 = true OK\n")
|
|
else:
|
|
stdio.printf("not 0 = true FAIL\n")
|
|
|
|
|
|
def eq_test() -> int:
|
|
stdio.printf("=== eq_test: ==、!=、is、None 比较测试 ===\n\n")
|
|
|
|
test_int_eq()
|
|
test_int_cmp()
|
|
test_ptr_is_none()
|
|
test_ptr_assign_none()
|
|
test_ptr_eq_none()
|
|
test_bool_logic()
|
|
|
|
stdio.printf("\n=== eq_test 完成 ===\n")
|
|
return 0
|