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) # ============================================================ # 测试 1:@property getter # ============================================================ class Rect: width: t.CInt height: t.CInt def __new__(self, w: t.CInt, h: t.CInt) -> 'Rect' | t.CPtr: return t.CPtr(malloc(Rect.__sizeof__())) def __init__(self, w: t.CInt, h: t.CInt): self.width = w self.height = h @property def area(self) -> t.CInt: return self.width * self.height @property def is_square(self) -> bool: return self.width == self.height def test_property_getter(): printf("\n+-- @property getter --+\n") r1: Rect = Rect(3, 4) check("area == 12", r1.area == 12) check("not square", not r1.is_square) r2: Rect = Rect(5, 5) check("square area == 25", r2.area == 25) check("is_square", r2.is_square) printf("+--------------------------------------------+\n") # ============================================================ # 测试 2:@property.setter # ============================================================ class Temperature: _celsius: t.CDouble def __new__(self, celsius_val: t.CDouble) -> 'Temperature' | t.CPtr: return t.CPtr(malloc(Temperature.__sizeof__())) def __init__(self, celsius_val: t.CDouble): self._celsius = celsius_val @property def celsius(self) -> t.CDouble: return self._celsius @celsius.setter def celsius(self, val: t.CDouble): self._celsius = val @property def fahrenheit(self) -> t.CDouble: return self._celsius * 1.8 + 32.0 def test_property_setter(): printf("\n+-- @property.setter --+\n") tmp1: Temperature = Temperature(0.0) check("0C == 32F", tmp1.fahrenheit > 31.9 and tmp1.fahrenheit < 32.1) 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") # ============================================================ # 测试 3:@property.getter(重新定义 getter) # ============================================================ class Counter: _count: t.CInt def __new__(self) -> 'Counter' | t.CPtr: return t.CPtr(malloc(Counter.__sizeof__())) def __init__(self): self._count = 0 @property def value(self) -> t.CInt: return self._count @value.setter def value(self, v: t.CInt): self._count = v def test_property_getter_redef(): printf("\n+-- @property.getter redef --+\n") cnt: Counter = Counter() check("init == 0", cnt.value == 0) cnt.value = 42 check("set == 42", cnt.value == 42) printf("+--------------------------------------------+\n") # ============================================================ # 测试 4:@property.deleter # ============================================================ class Config: _value: t.CInt _deleted: t.CInt def __new__(self, v: t.CInt) -> 'Config' | t.CPtr: return t.CPtr(malloc(Config.__sizeof__())) def __init__(self, v: t.CInt): self._value = v self._deleted = 0 @property def value(self) -> t.CInt: return self._value @value.setter def value(self, v: t.CInt): self._value = v @value.deleter def value(self): self._value = 0 self._deleted = 1 def is_deleted(self) -> bool: return self._deleted != 0 def test_property_deleter(): printf("\n+-- @property.deleter --+\n") cfg: Config = Config(99) check("init == 99", cfg.value == 99) check("not deleted", not cfg.is_deleted()) del cfg.value check("after del == 0", cfg.value == 0) check("is_deleted", cfg.is_deleted()) printf("+--------------------------------------------+\n") # ============================================================ # 测试 5:@staticmethod # ============================================================ class MathUtils: @staticmethod def add(a: t.CInt, b: t.CInt) -> t.CInt: return a + b @staticmethod def max_val(a: t.CInt, b: t.CInt) -> t.CInt: return a if a > b else b def test_staticmethod(): printf("\n+-- @staticmethod --+\n") # 通过类名调用 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") # ============================================================ # 测试 6:@classmethod # ============================================================ class Point: x: t.CDouble y: t.CDouble def __new__(self, x: t.CDouble, y: t.CDouble) -> 'Point' | t.CPtr: return t.CPtr(malloc(Point.__sizeof__())) def __init__(self, x: t.CDouble, y: t.CDouble): self.x = x self.y = y @classmethod def origin(cls: Point) -> Point: return Point(0.0, 0.0) @classmethod def from_int(cls: Point, x: t.CInt, y: t.CInt) -> Point: return Point(x * 1.0, y * 1.0) def dist_sq(self) -> t.CDouble: return self.x * self.x + self.y * self.y def test_classmethod(): printf("\n+-- @classmethod --+\n") # 通过类名调用 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) 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") # ============================================================ # 测试 7:综合测试 — @property + @staticmethod + @classmethod # ============================================================ class Circle: _radius: t.CDouble def __new__(self, r: t.CDouble) -> 'Circle' | t.CPtr: return t.CPtr(malloc(Circle.__sizeof__())) def __init__(self, r: t.CDouble): self._radius = r @property def radius(self) -> t.CDouble: return self._radius @radius.setter def radius(self, val: t.CDouble): self._radius = val @property def area(self) -> t.CDouble: return 3.14159265 * self._radius * self._radius @staticmethod def is_unit(r: t.CDouble) -> bool: return r > 0.99 and r < 1.01 @classmethod def unit(cls: Circle) -> Circle: return Circle(1.0) def test_combined(): printf("\n+-- Combined @property + @staticmethod + @classmethod --+\n") # 先测试直接构造 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) 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) # 测试 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") # ============================================================ # 主函数 # ============================================================ def main() -> t.CInt | t.CExport: printf("=== Builtin Decorator Test ===\n") test_property_getter() test_property_setter() test_property_getter_redef() test_property_deleter() test_staticmethod() test_classmethod() test_combined() printf("\n=== Results: %d passed, %d failed ===\n", _pass_count, _fail_count) return 0