补充
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
|
||||
28
Test/BuiltinDecoratorTest/project.json
Normal file
28
Test/BuiltinDecoratorTest/project.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "BuiltinDecoratorTest",
|
||||
"version": "1.0.0",
|
||||
"source_dir": "./App",
|
||||
"temp_dir": "./temp",
|
||||
"output_dir": "./output",
|
||||
"compiler": {
|
||||
"cmd": "llc",
|
||||
"flags": ["-filetype=obj", "-relocation-model=pic"]
|
||||
},
|
||||
"linker": {
|
||||
"cmd": "clang++",
|
||||
"flags": ["-Wl,--allow-multiple-definition", "-Wl,--unresolved-symbols=ignore-in-object-files", "-lmsvcrt", "-lucrt", "-lpthread", "-lmingwex", "-lkernel32", "-luser32", "-latomic"],
|
||||
"output": "BuiltinDecoratorTest.exe"
|
||||
},
|
||||
"includes": [
|
||||
"../../includes"
|
||||
],
|
||||
"target": {
|
||||
"triple": "x86_64-pc-windows-gnu",
|
||||
"datalayout": "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
|
||||
},
|
||||
"options": {
|
||||
"slice_level": 3,
|
||||
"target": "llvm",
|
||||
"strict_mode": true
|
||||
}
|
||||
}
|
||||
67
Test/BuiltinDecoratorTest/temp/56cdd754a8a09347.pyi
Normal file
67
Test/BuiltinDecoratorTest/temp/56cdd754a8a09347.pyi
Normal file
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
Auto-generated Python stub file from stdint.py
|
||||
Module: stdint
|
||||
"""
|
||||
|
||||
import c
|
||||
|
||||
|
||||
import t
|
||||
|
||||
INT: t.CTypedef = t.CInt
|
||||
INTPTR: t.CTypedef = t.CInt | t.CPtr
|
||||
BOOL: t.CTypedef = t.CInt
|
||||
UINT: t.CTypedef = t.CUnsignedInt
|
||||
UINTPTR: t.CTypedef = UINT | t.CPtr
|
||||
BYTE: t.CTypedef = t.CUnsignedChar
|
||||
BYTEPTR: t.CTypedef = BYTE | t.CPtr
|
||||
WORD: t.CTypedef = t.CUInt16T
|
||||
DWORD: t.CTypedef = t.CUInt32T
|
||||
QWORD: t.CTypedef = t.CUInt64T
|
||||
TCHAR: t.CTypedef = t.CChar
|
||||
CHARLIST: t.CTypedef = str | t.CPtr
|
||||
VOID: t.CTypedef = t.CVoid
|
||||
SHORT: t.CTypedef = t.CShort
|
||||
SHORTPTR: t.CTypedef = t.CShort | t.CPtr
|
||||
USHORT: t.CTypedef = t.CUnsignedShort
|
||||
USHORTPTR: t.CTypedef = t.CUnsignedShort | t.CPtr
|
||||
LONGLONG: t.CTypedef = t.CLong | t.CLong
|
||||
ULONGLONG: t.CTypedef = t.CUnsignedLong | t.CLong
|
||||
LONG: t.CTypedef = t.CLong
|
||||
ULONG: t.CTypedef = t.CUnsignedLong
|
||||
WCHAR: t.CTypedef = WORD
|
||||
WCHARPTR: t.CTypedef = WORD | t.CPtr
|
||||
CHARPTR: t.CTypedef = t.CChar | t.CPtr
|
||||
FSIZE_t: t.CTypedef = DWORD
|
||||
LBA_t: t.CTypedef = DWORD
|
||||
UINTPTR: t.CTypedef = t.CUnsignedInt | t.CPtr
|
||||
VOIDPTR: t.CTypedef = t.CVoid | t.CPtr
|
||||
FLOAT: t.CTypedef = t.CFloat
|
||||
DOUBLE: t.CTypedef = t.CDouble
|
||||
FLOAT8: t.CTypedef = t.CFloat8T
|
||||
FLOAT16: t.CTypedef = t.CFloat16T
|
||||
FLOAT32: t.CTypedef = t.CFloat32T
|
||||
FLOAT64: t.CTypedef = t.CFloat64T
|
||||
FLOAT128: t.CTypedef = t.CFloat128T
|
||||
INT8: t.CTypedef = t.CInt8T
|
||||
INT16: t.CTypedef = t.CInt16T
|
||||
INT32: t.CTypedef = t.CInt32T
|
||||
INT64: t.CTypedef = t.CInt64T
|
||||
UINT8: t.CTypedef = t.CUInt8T
|
||||
UINT16: t.CTypedef = t.CUInt16T
|
||||
UINT32: t.CTypedef = t.CUInt32T
|
||||
UINT64: t.CTypedef = t.CUInt64T
|
||||
INT8PTR: t.CTypedef = t.CInt8T | t.CPtr
|
||||
INT16PTR: t.CTypedef = t.CInt16T | t.CPtr
|
||||
INT32PTR: t.CTypedef = t.CInt32T | t.CPtr
|
||||
INT64PTR: t.CTypedef = t.CInt64T | t.CPtr
|
||||
UINT8PTR: t.CTypedef = t.CUInt8T | t.CPtr
|
||||
UINT16PTR: t.CTypedef = t.CUInt16T | t.CPtr
|
||||
UINT32PTR: t.CTypedef = t.CUInt32T | t.CPtr
|
||||
UINT64PTR: t.CTypedef = t.CUInt64T | t.CPtr
|
||||
CHAR8: t.CTypedef = t.CChar8T
|
||||
CHAR16: t.CTypedef = t.CChar16T
|
||||
CHAR32: t.CTypedef = t.CChar32T
|
||||
CHAR8PTR: t.CTypedef = t.CChar8T | t.CPtr
|
||||
CHAR16PTR: t.CTypedef = t.CChar16T | t.CPtr
|
||||
CHAR32PTR: t.CTypedef = t.CChar32T | t.CPtr
|
||||
28
Test/BuiltinDecoratorTest/temp/73edbcf76e32d00b.pyi
Normal file
28
Test/BuiltinDecoratorTest/temp/73edbcf76e32d00b.pyi
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Auto-generated Python stub file from stdio.py
|
||||
Module: stdio
|
||||
"""
|
||||
|
||||
|
||||
import t, c
|
||||
|
||||
def printf(fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def fprintf(stream: t.CVoid | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def sprintf(buf: t.CChar | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def snprintf(buf: t.CChar | t.CPtr, size: t.CSizeT, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def puts(s: t.CChar | t.CConst | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def fputs(s: t.CChar | t.CConst | t.CPtr, stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def fgets(buf: t.CChar | t.CPtr, size: t.CInt, stream: t.CVoid | t.CPtr) -> t.CChar | t.CPtr | t.CExtern | t.CExport: pass
|
||||
|
||||
def fflush(stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
|
||||
stdin: t.CExtern | t.CVoid | t.CPtr
|
||||
stdout: t.CExtern | t.CVoid | t.CPtr
|
||||
stderr: t.CExtern | t.CVoid | t.CPtr
|
||||
108
Test/BuiltinDecoratorTest/temp/7d5b7ce7c134ada7.pyi
Normal file
108
Test/BuiltinDecoratorTest/temp/7d5b7ce7c134ada7.pyi
Normal file
@@ -0,0 +1,108 @@
|
||||
"""
|
||||
Auto-generated Python stub file from main.py
|
||||
Module: main
|
||||
"""
|
||||
|
||||
import c
|
||||
|
||||
|
||||
import t
|
||||
from stdint import *
|
||||
from stdio import printf
|
||||
|
||||
_pass_count: t.CExtern | t.CInt
|
||||
_fail_count: t.CExtern | t.CInt
|
||||
|
||||
def check(name: t.CChar | t.CPtr, condition: bool) -> t.CInt: pass
|
||||
|
||||
|
||||
class Rect:
|
||||
width: t.CInt
|
||||
height: t.CInt
|
||||
def __init__(self: Rect, w: t.CInt, h: t.CInt) -> t.CInt: pass
|
||||
@property
|
||||
def area(self: Rect) -> t.CInt: pass
|
||||
@property
|
||||
def is_square(self: Rect) -> bool: pass
|
||||
|
||||
def test_property_getter() -> t.CInt: pass
|
||||
|
||||
|
||||
class Temperature:
|
||||
_celsius: t.CDouble
|
||||
def __init__(self: Temperature, c: t.CDouble) -> t.CInt: pass
|
||||
@property
|
||||
def celsius(self: Temperature) -> t.CDouble: pass
|
||||
@celsius.setter
|
||||
def celsius(self: Temperature, val: t.CDouble) -> t.CInt: pass
|
||||
@property
|
||||
def fahrenheit(self: Temperature) -> t.CDouble: pass
|
||||
|
||||
def test_property_setter() -> t.CInt: pass
|
||||
|
||||
|
||||
class Counter:
|
||||
_count: t.CInt
|
||||
def __init__(self: Counter) -> t.CInt: pass
|
||||
@property
|
||||
def value(self: Counter) -> t.CInt: pass
|
||||
@value.setter
|
||||
def value(self: Counter, v: t.CInt) -> t.CInt: pass
|
||||
|
||||
def test_property_getter_redef() -> t.CInt: pass
|
||||
|
||||
|
||||
class Config:
|
||||
_value: t.CInt
|
||||
_deleted: t.CInt
|
||||
def __init__(self: Config, v: t.CInt) -> t.CInt: pass
|
||||
@property
|
||||
def value(self: Config) -> t.CInt: pass
|
||||
@value.setter
|
||||
def value(self: Config, v: t.CInt) -> t.CInt: pass
|
||||
@value.deleter
|
||||
def value(self: Config) -> t.CInt: pass
|
||||
def is_deleted(self: Config) -> bool: pass
|
||||
|
||||
def test_property_deleter() -> t.CInt: pass
|
||||
|
||||
|
||||
class MathUtils:
|
||||
@staticmethod
|
||||
def add(a: t.CInt, b: t.CInt) -> t.CInt: pass
|
||||
@staticmethod
|
||||
def max_val(a: t.CInt, b: t.CInt) -> t.CInt: pass
|
||||
|
||||
def test_staticmethod() -> t.CInt: pass
|
||||
|
||||
|
||||
class Point:
|
||||
x: t.CDouble
|
||||
y: t.CDouble
|
||||
def __init__(self: Point, x: t.CDouble, y: t.CDouble) -> t.CInt: pass
|
||||
@classmethod
|
||||
def origin(cls: Point) -> Point: pass
|
||||
@classmethod
|
||||
def from_int(cls: Point, x: t.CInt, y: t.CInt) -> Point: pass
|
||||
def dist_sq(self: Point) -> t.CDouble: pass
|
||||
|
||||
def test_classmethod() -> t.CInt: pass
|
||||
|
||||
|
||||
class Circle:
|
||||
_radius: t.CDouble
|
||||
def __init__(self: Circle, r: t.CDouble) -> t.CInt: pass
|
||||
@property
|
||||
def radius(self: Circle) -> t.CDouble: pass
|
||||
@radius.setter
|
||||
def radius(self: Circle, val: t.CDouble) -> t.CInt: pass
|
||||
@property
|
||||
def area(self: Circle) -> t.CDouble: pass
|
||||
@staticmethod
|
||||
def is_unit(r: t.CDouble) -> bool: pass
|
||||
@classmethod
|
||||
def unit(cls: Circle) -> Circle: pass
|
||||
|
||||
def test_combined() -> t.CInt: pass
|
||||
|
||||
def main() -> t.CInt | t.CExport: pass
|
||||
3
Test/BuiltinDecoratorTest/temp/_sha1_map.txt
Normal file
3
Test/BuiltinDecoratorTest/temp/_sha1_map.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
56cdd754a8a09347:includes/stdint.py
|
||||
73edbcf76e32d00b:includes/stdio.py
|
||||
7d5b7ce7c134ada7:main.py
|
||||
Reference in New Issue
Block a user