修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -2,21 +2,7 @@ import t
from stdint import *
from stdio import printf
from stdlib import malloc
# ============================================================
# 测试计数器
# ============================================================
_pass_count: t.CInt = 0
_fail_count: t.CInt = 0
def check(name: t.CChar | t.CPtr, condition: bool):
global _pass_count, _fail_count
if condition:
_pass_count += 1
printf(" [PASS] %s\n", name)
else:
_fail_count += 1
printf(" [FAIL] %s\n", name)
import testcheck
# ============================================================
@@ -43,14 +29,13 @@ class Rect:
def test_property_getter():
printf("\n+-- @property getter --+\n")
testcheck.section("@property getter")
r1: Rect = Rect(3, 4)
check("area == 12", r1.area == 12)
check("not square", not r1.is_square)
testcheck.check(r1.area == 12, "area == 12", "area expect 12")
testcheck.check(not r1.is_square, "not square", "should not be square")
r2: Rect = Rect(5, 5)
check("square area == 25", r2.area == 25)
check("is_square", r2.is_square)
printf("+--------------------------------------------+\n")
testcheck.check(r2.area == 25, "square area == 25", "square area expect 25")
testcheck.check(r2.is_square, "is_square", "should be square")
# ============================================================
@@ -79,13 +64,12 @@ class Temperature:
def test_property_setter():
printf("\n+-- @property.setter --+\n")
testcheck.section("@property.setter")
tmp1: Temperature = Temperature(0.0)
check("0C == 32F", tmp1.fahrenheit > 31.9 and tmp1.fahrenheit < 32.1)
testcheck.check(tmp1.fahrenheit > 31.9 and tmp1.fahrenheit < 32.1, "0C == 32F", "0C expect 32F")
tmp1.celsius = 100.0
check("100C == 212F", tmp1.fahrenheit > 211.9 and tmp1.fahrenheit < 212.1)
check("celsius == 100", tmp1.celsius > 99.9 and tmp1.celsius < 100.1)
printf("+--------------------------------------------+\n")
testcheck.check(tmp1.fahrenheit > 211.9 and tmp1.fahrenheit < 212.1, "100C == 212F", "100C expect 212F")
testcheck.check(tmp1.celsius > 99.9 and tmp1.celsius < 100.1, "celsius == 100", "celsius expect 100")
# ============================================================
@@ -110,12 +94,11 @@ class Counter:
def test_property_getter_redef():
printf("\n+-- @property.getter redef --+\n")
testcheck.section("@property.getter redef")
cnt: Counter = Counter()
check("init == 0", cnt.value == 0)
testcheck.check(cnt.value == 0, "init == 0", "init expect 0")
cnt.value = 42
check("set == 42", cnt.value == 42)
printf("+--------------------------------------------+\n")
testcheck.check(cnt.value == 42, "set == 42", "set expect 42")
# ============================================================
@@ -150,14 +133,13 @@ class Config:
def test_property_deleter():
printf("\n+-- @property.deleter --+\n")
testcheck.section("@property.deleter")
cfg: Config = Config(99)
check("init == 99", cfg.value == 99)
check("not deleted", not cfg.is_deleted())
testcheck.check(cfg.value == 99, "init == 99", "init expect 99")
testcheck.check(not cfg.is_deleted(), "not deleted", "should not be deleted")
del cfg.value
check("after del == 0", cfg.value == 0)
check("is_deleted", cfg.is_deleted())
printf("+--------------------------------------------+\n")
testcheck.check(cfg.value == 0, "after del == 0", "after del expect 0")
testcheck.check(cfg.is_deleted(), "is_deleted", "should be deleted")
# ============================================================
@@ -174,11 +156,10 @@ class MathUtils:
def test_staticmethod():
printf("\n+-- @staticmethod --+\n")
testcheck.section("@staticmethod")
# 通过类名调用
check("MathUtils.add(3,4)==7", MathUtils.add(3, 4) == 7)
check("MathUtils.max(5,10)==10", MathUtils.max_val(5, 10) == 10)
printf("+--------------------------------------------+\n")
testcheck.check(MathUtils.add(3, 4) == 7, "MathUtils.add(3,4)==7", "MathUtils.add(3,4) expect 7")
testcheck.check(MathUtils.max_val(5, 10) == 10, "MathUtils.max(5,10)==10", "MathUtils.max(5,10) expect 10")
# ============================================================
@@ -208,18 +189,17 @@ class Point:
def test_classmethod():
printf("\n+-- @classmethod --+\n")
testcheck.section("@classmethod")
# 通过类名调用
p1: Point = Point.origin()
check("origin.x ~= 0", p1.x > -0.01 and p1.x < 0.01)
check("origin.y ~= 0", p1.y > -0.01 and p1.y < 0.01)
check("origin.dist ~= 0", p1.dist_sq() < 0.01)
testcheck.check(p1.x > -0.01 and p1.x < 0.01, "origin.x ~= 0", "origin.x expect ~0")
testcheck.check(p1.y > -0.01 and p1.y < 0.01, "origin.y ~= 0", "origin.y expect ~0")
testcheck.check(p1.dist_sq() < 0.01, "origin.dist ~= 0", "origin.dist expect ~0")
p2: Point = Point.from_int(3, 4)
check("from_int(3,4).x ~= 3", p2.x > 2.99 and p2.x < 3.01)
check("from_int(3,4).y ~= 4", p2.y > 3.99 and p2.y < 4.01)
check("dist_sq ~= 25", p2.dist_sq() > 24.99 and p2.dist_sq() < 25.01)
printf("+--------------------------------------------+\n")
testcheck.check(p2.x > 2.99 and p2.x < 3.01, "from_int(3,4).x ~= 3", "from_int(3,4).x expect ~3")
testcheck.check(p2.y > 3.99 and p2.y < 4.01, "from_int(3,4).y ~= 4", "from_int(3,4).y expect ~4")
testcheck.check(p2.dist_sq() > 24.99 and p2.dist_sq() < 25.01, "dist_sq ~= 25", "dist_sq expect ~25")
# ============================================================
@@ -256,27 +236,26 @@ class Circle:
def test_combined():
printf("\n+-- Combined @property + @staticmethod + @classmethod --+\n")
testcheck.section("Combined @property + @staticmethod + @classmethod")
# 先测试直接构造
cr0: Circle = Circle(1.0)
check("direct radius ~= 1", cr0.radius > 0.99 and cr0.radius < 1.01)
check("direct area ~= pi", cr0.area > 3.14 and cr0.area < 3.15)
testcheck.check(cr0.radius > 0.99 and cr0.radius < 1.01, "direct radius ~= 1", "direct radius expect ~1")
testcheck.check(cr0.area > 3.14 and cr0.area < 3.15, "direct area ~= pi", "direct area expect ~pi")
cr0.radius = 2.0
check("direct radius 2.0", cr0.radius > 1.99 and cr0.radius < 2.01)
check("direct area ~= 4pi", cr0.area > 12.56 and cr0.area < 12.58)
testcheck.check(cr0.radius > 1.99 and cr0.radius < 2.01, "direct radius 2.0", "direct radius expect 2.0")
testcheck.check(cr0.area > 12.56 and cr0.area < 12.58, "direct area ~= 4pi", "direct area expect ~4pi")
# 测试 classmethod
cr1: Circle = Circle.unit()
check("unit radius ~= 1", cr1.radius > 0.99 and cr1.radius < 1.01)
check("is_unit(1.0)", Circle.is_unit(1.0))
check("not is_unit(2.0)", not Circle.is_unit(2.0))
printf("+--------------------------------------------+\n")
testcheck.check(cr1.radius > 0.99 and cr1.radius < 1.01, "unit radius ~= 1", "unit radius expect ~1")
testcheck.check(Circle.is_unit(1.0), "is_unit(1.0)", "is_unit(1.0) should be true")
testcheck.check(not Circle.is_unit(2.0), "not is_unit(2.0)", "is_unit(2.0) should be false")
# ============================================================
# 主函数
# ============================================================
def main() -> t.CInt | t.CExport:
printf("=== Builtin Decorator Test ===\n")
testcheck.begin("Builtin Decorator Test")
test_property_getter()
test_property_setter()
@@ -286,5 +265,4 @@ def main() -> t.CInt | t.CExport:
test_classmethod()
test_combined()
printf("\n=== Results: %d passed, %d failed ===\n", _pass_count, _fail_count)
return 0
return testcheck.end()