补充
This commit is contained in:
271
Test/BuiltinDecoratorTest/App/main.py
Normal file
271
Test/BuiltinDecoratorTest/App/main.py
Normal file
@@ -0,0 +1,271 @@
|
||||
import t
|
||||
from stdint import *
|
||||
from stdio import printf
|
||||
|
||||
# ============================================================
|
||||
# 测试计数器
|
||||
# ============================================================
|
||||
_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 __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")
|
||||
r: Rect = Rect(3, 4)
|
||||
check("area == 12", r.area == 12)
|
||||
check("not square", not r.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 __init__(self, c: t.CDouble):
|
||||
self._celsius = c
|
||||
|
||||
@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")
|
||||
t1: Temperature = Temperature(0.0)
|
||||
check("0C == 32F", t1.fahrenheit > 31.9 and t1.fahrenheit < 32.1)
|
||||
t1.celsius = 100.0
|
||||
check("100C == 212F", t1.fahrenheit > 211.9 and t1.fahrenheit < 212.1)
|
||||
check("celsius == 100", t1.celsius > 99.9 and t1.celsius < 100.1)
|
||||
printf("+--------------------------------------------+\n")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 3:@property.getter(重新定义 getter)
|
||||
# ============================================================
|
||||
class Counter:
|
||||
_count: t.CInt
|
||||
|
||||
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")
|
||||
c: Counter = Counter()
|
||||
check("init == 0", c.value == 0)
|
||||
c.value = 42
|
||||
check("set == 42", c.value == 42)
|
||||
printf("+--------------------------------------------+\n")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 4:@property.deleter
|
||||
# ============================================================
|
||||
class Config:
|
||||
_value: t.CInt
|
||||
_deleted: t.CInt
|
||||
|
||||
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)
|
||||
# 通过实例调用
|
||||
m: MathUtils = MathUtils()
|
||||
check("instance.add(1,2)==3", m.add(1, 2) == 3)
|
||||
check("instance.max(8,3)==8", m.max_val(8, 3) == 8)
|
||||
printf("+--------------------------------------------+\n")
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 6:@classmethod
|
||||
# ============================================================
|
||||
class Point:
|
||||
x: t.CDouble
|
||||
y: t.CDouble
|
||||
|
||||
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 __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")
|
||||
c: Circle = Circle.unit()
|
||||
check("unit radius ~= 1", c.radius > 0.99 and c.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))
|
||||
check("unit area ~= pi", c.area > 3.14 and c.area < 3.15)
|
||||
c.radius = 2.0
|
||||
check("radius 2.0", c.radius > 1.99 and c.radius < 2.01)
|
||||
check("area ~= 4pi", c.area > 12.56 and c.area < 12.58)
|
||||
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
|
||||
Reference in New Issue
Block a user