可用的回归测试通过的标准版本
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import t
|
||||
from stdint import *
|
||||
from stdio import printf
|
||||
from stdlib import malloc
|
||||
|
||||
# ============================================================
|
||||
# 测试计数器
|
||||
@@ -25,6 +26,9 @@ 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
|
||||
@@ -40,9 +44,9 @@ class Rect:
|
||||
|
||||
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)
|
||||
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)
|
||||
@@ -55,8 +59,11 @@ def test_property_getter():
|
||||
class Temperature:
|
||||
_celsius: t.CDouble
|
||||
|
||||
def __init__(self, c: t.CDouble):
|
||||
self._celsius = c
|
||||
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:
|
||||
@@ -73,11 +80,11 @@ class Temperature:
|
||||
|
||||
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)
|
||||
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")
|
||||
|
||||
|
||||
@@ -87,6 +94,9 @@ def test_property_setter():
|
||||
class Counter:
|
||||
_count: t.CInt
|
||||
|
||||
def __new__(self) -> 'Counter' | t.CPtr:
|
||||
return t.CPtr(malloc(Counter.__sizeof__()))
|
||||
|
||||
def __init__(self):
|
||||
self._count = 0
|
||||
|
||||
@@ -101,10 +111,10 @@ class Counter:
|
||||
|
||||
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)
|
||||
cnt: Counter = Counter()
|
||||
check("init == 0", cnt.value == 0)
|
||||
cnt.value = 42
|
||||
check("set == 42", cnt.value == 42)
|
||||
printf("+--------------------------------------------+\n")
|
||||
|
||||
|
||||
@@ -115,6 +125,9 @@ 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
|
||||
@@ -165,10 +178,6 @@ def test_staticmethod():
|
||||
# 通过类名调用
|
||||
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")
|
||||
|
||||
|
||||
@@ -179,6 +188,9 @@ 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
|
||||
@@ -216,6 +228,9 @@ def test_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
|
||||
|
||||
@@ -242,14 +257,18 @@ class Circle:
|
||||
|
||||
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)
|
||||
# 先测试直接构造
|
||||
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))
|
||||
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")
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"stdint": "56cdd754a8a09347", "stdlib": "6c2029b306556c00", "stdio": "73edbcf76e32d00b"}
|
||||
18
Test/BuiltinDecoratorTest/temp/6c2029b306556c00.pyi
Normal file
18
Test/BuiltinDecoratorTest/temp/6c2029b306556c00.pyi
Normal file
@@ -0,0 +1,18 @@
|
||||
"""
|
||||
Auto-generated Python stub file from stdlib.py
|
||||
Module: stdlib
|
||||
"""
|
||||
|
||||
import c
|
||||
|
||||
|
||||
from stdint import *
|
||||
import t
|
||||
|
||||
def malloc(size: t.CSizeT) -> t.CVoid | t.CPtr | t.State | t.CExtern | t.CExport: pass
|
||||
|
||||
def calloc(nmemb: t.CSizeT, size: t.CSizeT) -> t.CVoid | t.CPtr | t.State | t.CExtern | t.CExport: pass
|
||||
|
||||
def realloc(p: t.CVoid | t.CPtr, size: t.CSizeT) -> t.CVoid | t.CPtr | t.State | t.CExtern | t.CExport: pass
|
||||
|
||||
def free(p: t.CVoid | t.CPtr) -> None | t.State | t.CExtern | t.CExport: pass
|
||||
@@ -1,3 +1,4 @@
|
||||
56cdd754a8a09347:includes/stdint.py
|
||||
6c2029b306556c00:includes/stdlib.py
|
||||
73edbcf76e32d00b:includes/stdio.py
|
||||
7d5b7ce7c134ada7:main.py
|
||||
b6069b01d8c9cc04:main.py
|
||||
|
||||
@@ -9,6 +9,7 @@ import c
|
||||
import t
|
||||
from stdint import *
|
||||
from stdio import printf
|
||||
from stdlib import malloc
|
||||
|
||||
_pass_count: t.CExtern | t.CInt
|
||||
_fail_count: t.CExtern | t.CInt
|
||||
@@ -19,6 +20,7 @@ def check(name: t.CChar | t.CPtr, condition: bool) -> t.CInt: pass
|
||||
class Rect:
|
||||
width: t.CInt
|
||||
height: t.CInt
|
||||
def __new__(self: Rect, w: t.CInt, h: t.CInt) -> 'Rect' | t.CPtr: pass
|
||||
def __init__(self: Rect, w: t.CInt, h: t.CInt) -> t.CInt: pass
|
||||
@property
|
||||
def area(self: Rect) -> t.CInt: pass
|
||||
@@ -30,7 +32,8 @@ def test_property_getter() -> t.CInt: pass
|
||||
|
||||
class Temperature:
|
||||
_celsius: t.CDouble
|
||||
def __init__(self: Temperature, c: t.CDouble) -> t.CInt: pass
|
||||
def __new__(self: Temperature, celsius_val: t.CDouble) -> 'Temperature' | t.CPtr: pass
|
||||
def __init__(self: Temperature, celsius_val: t.CDouble) -> t.CInt: pass
|
||||
@property
|
||||
def celsius(self: Temperature) -> t.CDouble: pass
|
||||
@celsius.setter
|
||||
@@ -43,6 +46,7 @@ def test_property_setter() -> t.CInt: pass
|
||||
|
||||
class Counter:
|
||||
_count: t.CInt
|
||||
def __new__(self: Counter) -> 'Counter' | t.CPtr: pass
|
||||
def __init__(self: Counter) -> t.CInt: pass
|
||||
@property
|
||||
def value(self: Counter) -> t.CInt: pass
|
||||
@@ -55,6 +59,7 @@ def test_property_getter_redef() -> t.CInt: pass
|
||||
class Config:
|
||||
_value: t.CInt
|
||||
_deleted: t.CInt
|
||||
def __new__(self: Config, v: t.CInt) -> 'Config' | t.CPtr: pass
|
||||
def __init__(self: Config, v: t.CInt) -> t.CInt: pass
|
||||
@property
|
||||
def value(self: Config) -> t.CInt: pass
|
||||
@@ -79,6 +84,7 @@ def test_staticmethod() -> t.CInt: pass
|
||||
class Point:
|
||||
x: t.CDouble
|
||||
y: t.CDouble
|
||||
def __new__(self: Point, x: t.CDouble, y: t.CDouble) -> 'Point' | t.CPtr: pass
|
||||
def __init__(self: Point, x: t.CDouble, y: t.CDouble) -> t.CInt: pass
|
||||
@classmethod
|
||||
def origin(cls: Point) -> Point: pass
|
||||
@@ -91,6 +97,7 @@ def test_classmethod() -> t.CInt: pass
|
||||
|
||||
class Circle:
|
||||
_radius: t.CDouble
|
||||
def __new__(self: Circle, r: t.CDouble) -> 'Circle' | t.CPtr: pass
|
||||
def __init__(self: Circle, r: t.CDouble) -> t.CInt: pass
|
||||
@property
|
||||
def radius(self: Circle) -> t.CDouble: pass
|
||||
Reference in New Issue
Block a user