mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
783 lines
23 KiB
Python
783 lines
23 KiB
Python
# test super:
|
|
class TestSuperBase():
|
|
def __init__(self):
|
|
self.base_attr = 1
|
|
|
|
def base_method(self):
|
|
return self.base_attr
|
|
|
|
def error(self):
|
|
raise Exception('未能拦截错误')
|
|
|
|
|
|
class TestSuperChild1(TestSuperBase):
|
|
def __init__(self):
|
|
super(TestSuperChild1, self).__init__()
|
|
|
|
def child_method(self):
|
|
return super(TestSuperChild1, self).base_method()
|
|
|
|
def error_handling(self):
|
|
try:
|
|
super(TestSuperChild1, self).error()
|
|
except:
|
|
pass
|
|
|
|
class TestSuperChild2(TestSuperBase):
|
|
pass
|
|
|
|
|
|
test_base = TestSuperBase()
|
|
# 测试属性
|
|
assert test_base.base_attr == 1
|
|
# 测试方法
|
|
assert test_base.base_method() == 1
|
|
|
|
test_child1 = TestSuperChild1()
|
|
# 测试继承的属性
|
|
assert test_child1.base_attr == 1
|
|
# 测试继承的方法
|
|
assert test_child1.base_method() == 1
|
|
# 测试子类添加的方法
|
|
assert test_child1.child_method() == 1
|
|
# 测试子类的错误拦截
|
|
test_child1.error_handling()
|
|
|
|
test_child2 = TestSuperChild2()
|
|
# 测试继承的属性
|
|
assert test_child2.base_attr == 1
|
|
# 测试继承的方法
|
|
assert test_child2.base_method() == 1
|
|
|
|
|
|
class TestSuperNoBaseMethod(TestSuperBase):
|
|
def __init__(self):
|
|
super(TestSuperNoBaseMethod, self).append(1)
|
|
|
|
try:
|
|
t = TestSuperNoParent()
|
|
print('未能拦截错误')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
t = TestSuperNoBaseMethod()
|
|
print('未能拦截错误')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
class B():
|
|
pass
|
|
|
|
class C():
|
|
def method(self):
|
|
super(C, self).method()
|
|
|
|
class D():
|
|
def method(self):
|
|
super(B, self).method()
|
|
|
|
try:
|
|
c = C()
|
|
c.method()
|
|
print('未能拦截错误')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
d = D()
|
|
d.method()
|
|
print('未能拦截错误')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
# test hash:
|
|
# 测试整数类型的输入
|
|
assert hash(0) == 0
|
|
assert hash(123) == 123
|
|
assert hash(-456) == -456
|
|
|
|
# 测试字符串类型的输入
|
|
assert type(hash("hello")) is int
|
|
|
|
# 测试浮点数类型的输入
|
|
assert type(hash(3.14)) is int
|
|
assert type(hash(-2.71828)) is int
|
|
|
|
# 测试边界情况
|
|
assert type(hash(None)) is int
|
|
assert hash(True) == 1
|
|
assert hash(False) == 0
|
|
|
|
# 测试元组
|
|
assert type(hash((4, 5, 6, (1234,1122), 2.3983, 'abcd'))) is int
|
|
|
|
# 测试自定义类和对象的输入
|
|
class A():
|
|
pass
|
|
|
|
a = A()
|
|
|
|
assert type(hash(A)) is int
|
|
assert type(hash(a)) is int
|
|
|
|
# 测试函数的输入
|
|
def f():
|
|
pass
|
|
assert type(hash(a)) is int
|
|
|
|
# 测试不可哈希对象
|
|
try:
|
|
hash({1:1})
|
|
print('未能拦截错误')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
hash([1])
|
|
print('未能拦截错误')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
# test chr
|
|
l = []
|
|
for i in range(128):
|
|
l.append(f'{i} {chr(i)}')
|
|
assert l == ['0 \x00', '1 \x01', '2 \x02', '3 \x03', '4 \x04', '5 \x05', '6 \x06', '7 \x07', '8 \x08', '9 \t', '10 \n', '11 \x0b', '12 \x0c', '13 \r', '14 \x0e', '15 \x0f', '16 \x10', '17 \x11', '18 \x12', '19 \x13', '20 \x14', '21 \x15', '22 \x16', '23 \x17', '24 \x18', '25 \x19', '26 \x1a', '27 \x1b', '28 \x1c', '29 \x1d', '30 \x1e', '31 \x1f', '32 ', '33 !', '34 "', '35 #', '36 $', '37 %', '38 &', "39 '", '40 (', '41 )', '42 *', '43 +', '44 ,', '45 -', '46 .', '47 /', '48 0', '49 1', '50 2', '51 3', '52 4', '53 5', '54 6', '55 7', '56 8', '57 9', '58 :', '59 ;', '60 <', '61 =', '62 >', '63 ?', '64 @', '65 A', '66 B', '67 C', '68 D', '69 E', '70 F', '71 G', '72 H', '73 I', '74 J', '75 K', '76 L', '77 M', '78 N', '79 O', '80 P', '81 Q', '82 R', '83 S', '84 T', '85 U', '86 V', '87 W', '88 X', '89 Y', '90 Z', '91 [', '92 \\', '93 ]', '94 ^', '95 _', '96 `', '97 a', '98 b', '99 c', '100 d', '101 e', '102 f', '103 g', '104 h', '105 i', '106 j', '107 k', '108 l', '109 m', '110 n', '111 o', '112 p', '113 q', '114 r', '115 s', '116 t', '117 u', '118 v', '119 w', '120 x', '121 y', '122 z', '123 {', '124 |', '125 }', '126 ~', '127 \x7f']
|
|
|
|
assert type(bin(1234)) is str
|
|
|
|
# test __repr__:
|
|
class A():
|
|
def __init__(self):
|
|
self.attr = 0
|
|
|
|
repr(A())
|
|
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 33600: 318: _vm->bind_constructor<-1>("range", [](VM* vm, ArgsView args) {
|
|
# 16742: 319: args._begin += 1; // skip cls
|
|
# 16742: 320: Range r;
|
|
# 16742: 321: switch (args.size()) {
|
|
# 8735: 322: case 1: r.stop = CAST(i64, args[0]); break;
|
|
# 3867: 323: case 2: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); break;
|
|
# 4140: 324: case 3: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); r.step = CAST(i64, args[2]); break;
|
|
# #####: 325: default: vm->TypeError("expected 1-3 arguments, got " + std::to_string(args.size()));
|
|
# #####: 326: }
|
|
# 33484: 327: return VAR(r);
|
|
# 16742: 328: });
|
|
# -: 329:
|
|
# test range:
|
|
|
|
try:
|
|
range(1,2,3,4)
|
|
print('未能拦截错误, 在测试 range')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
# /************ int ************/
|
|
try:
|
|
int('asad')
|
|
print('未能拦截错误, 在测试 int')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
int(123, 16)
|
|
print('未能拦截错误, 在测试 int')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
assert type(10//11) is int
|
|
|
|
assert type(11%2) is int
|
|
|
|
try:
|
|
float('asad')
|
|
print('未能拦截错误, 在测试 float')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
float([])
|
|
print('未能拦截错误, 在测试 float')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
# /************ str ************/
|
|
# test str.__rmul__:
|
|
assert type(12 * '12') is str
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 554: _vm->bind_method<1>("str", "index", [](VM* vm, ArgsView args) {
|
|
# #####: 555: const Str& self = _CAST(Str&, args[0]);
|
|
# #####: 556: const Str& sub = CAST(Str&, args[1]);
|
|
# #####: 557: int index = self.index(sub);
|
|
# #####: 558: if(index == -1) vm->ValueError("substring not found");
|
|
# #####: 559: return VAR(index);
|
|
# #####: 560: });
|
|
# test str.index:
|
|
assert type('25363546'.index('63')) is int
|
|
try:
|
|
'25363546'.index('err')
|
|
print('未能拦截错误, 在测试 str.index')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 562: _vm->bind_method<1>("str", "find", [](VM* vm, ArgsView args) {
|
|
# #####: 563: const Str& self = _CAST(Str&, args[0]);
|
|
# #####: 564: const Str& sub = CAST(Str&, args[1]);
|
|
# #####: 565: return VAR(self.index(sub));
|
|
# -: 566: });
|
|
# test str.find:
|
|
assert type('25363546'.find('63')) is int
|
|
assert type('25363546'.find('err')) is int
|
|
|
|
|
|
# /************ list ************/
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 174: 615: _vm->bind_constructor<-1>("list", [](VM* vm, ArgsView args) {
|
|
# 29: 616: if(args.size() == 1+0) return VAR(List());
|
|
# 29: 617: if(args.size() == 1+1){
|
|
# 29: 618: return vm->py_list(args[1]);
|
|
# -: 619: }
|
|
# #####: 620: vm->TypeError("list() takes 0 or 1 arguments");
|
|
# #####: 621: return vm->None;
|
|
# 29: 622: });
|
|
# test list:
|
|
try:
|
|
list(1,2)
|
|
print('未能拦截错误, 在测试 list')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
# 未完全测试准确性----------------------------------------------
|
|
# 116: 648: _vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
|
|
# #####: 649: List& self = _CAST(List&, args[0]);
|
|
# #####: 650: PyObject* obj = args[1];
|
|
# #####: 651: for(int i=0; i<self.size(); i++){
|
|
# #####: 652: if(vm->py_eq(self[i], obj)) return VAR(i);
|
|
# -: 653: }
|
|
# #####: 654: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
|
|
# #####: 655: return vm->None;
|
|
# #####: 656: });
|
|
# test list.index:
|
|
assert type([1,2,3,4,5].index(4)) is int
|
|
try:
|
|
[1,2,3,4,5].index(6)
|
|
print('未能拦截错误, 在测试 list.index')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
|
|
|
|
# 未完全测试准确性----------------------------------------------
|
|
# 118: 658: _vm->bind_method<1>("list", "remove", [](VM* vm, ArgsView args) {
|
|
# 1: 659: List& self = _CAST(List&, args[0]);
|
|
# 1: 660: PyObject* obj = args[1];
|
|
# 2: 661: for(int i=0; i<self.size(); i++){
|
|
# 2: 662: if(vm->py_eq(self[i], obj)){
|
|
# 1: 663: self.erase(i);
|
|
# 1: 664: return vm->None;
|
|
# -: 665: }
|
|
# -: 666: }
|
|
# #####: 667: vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
|
|
# #####: 668: return vm->None;
|
|
# 1: 669: });
|
|
# test list.remove:
|
|
try:
|
|
[1,2,3,4,5].remove(6)
|
|
print('未能拦截错误, 在测试 list.remove')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
|
|
# 未完全测试准确性----------------------------------------------
|
|
# 2536: 671: _vm->bind_method<-1>("list", "pop", [](VM* vm, ArgsView args) {
|
|
# 1210: 672: List& self = _CAST(List&, args[0]);
|
|
# 1210: 673: if(args.size() == 1+0){
|
|
# 1208: 674: if(self.empty()) vm->IndexError("pop from empty list");
|
|
# 1208: 675: return self.popx_back();
|
|
# -: 676: }
|
|
# 2: 677: if(args.size() == 1+1){
|
|
# 2: 678: int index = CAST(int, args[1]);
|
|
# 2: 679: index = vm->normalized_index(index, self.size());
|
|
# 2: 680: PyObject* ret = self[index];
|
|
# 2: 681: self.erase(index);
|
|
# -: 682: return ret;
|
|
# -: 683: }
|
|
# #####: 684: vm->TypeError("pop() takes at most 1 argument");
|
|
# #####: 685: return vm->None;
|
|
# 1210: 686: });
|
|
# test list.pop:
|
|
try:
|
|
[1,2,3,4,5].pop(1,2,3,4)
|
|
print('未能拦截错误, 在测试 list.pop')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# test list.__rmul__:
|
|
assert type(12 * [12]) is list
|
|
|
|
|
|
# /************ tuple ************/
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 180: 783: _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) {
|
|
# 32: 784: if(args.size() == 1+0) return VAR(Tuple(0));
|
|
# 32: 785: if(args.size() == 1+1){
|
|
# 32: 786: List list = CAST(List, vm->py_list(args[1]));
|
|
# 32: 787: return VAR(Tuple(std::move(list)));
|
|
# 32: 788: }
|
|
# #####: 789: vm->TypeError("tuple() takes at most 1 argument");
|
|
# #####: 790: return vm->None;
|
|
# 32: 791: });
|
|
# -: 792:
|
|
# test tuple:
|
|
try:
|
|
tuple(1,2)
|
|
print('未能拦截错误, 在测试 tuple')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
assert (1,2,3).__contains__(5) == False
|
|
|
|
assert (1,2,2,3,3,3).count(3) == 3
|
|
assert (1,2,2,3,3,3).count(0) == 0
|
|
|
|
assert repr(True) == 'True'
|
|
assert repr(False) == 'False'
|
|
|
|
assert True & True == 1
|
|
|
|
assert True | True == 1
|
|
|
|
assert (True ^ True) == 0
|
|
|
|
assert (True == True) == 1
|
|
|
|
assert type(hash(bytes([0x41, 0x42, 0x43]))) is int
|
|
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# test bytes.__repr__:
|
|
assert type(repr(bytes([0x41, 0x42, 0x43]))) is str
|
|
|
|
|
|
# /************ slice ************/
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 953: _vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
|
|
# #####: 954: return VAR(Slice(args[1], args[2], args[3]));
|
|
# -: 955: });
|
|
# test slice:
|
|
assert type(slice(0.1, 0.2, 0.3)) is slice
|
|
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 1529: bind_property(_t(tp_slice), "start", [](VM* vm, ArgsView args){
|
|
# #####: 1530: return CAST(Slice&, args[0]).start;
|
|
# -: 1531: });
|
|
# 116: 1532: bind_property(_t(tp_slice), "stop", [](VM* vm, ArgsView args){
|
|
# #####: 1533: return CAST(Slice&, args[0]).stop;
|
|
# -: 1534: });
|
|
# 116: 1535: bind_property(_t(tp_slice), "step", [](VM* vm, ArgsView args){
|
|
# #####: 1536: return CAST(Slice&, args[0]).step;
|
|
# -: 1537: });
|
|
s = slice(1, 2, 3)
|
|
assert type(s) is slice
|
|
assert s.start == 1
|
|
assert s.stop == 2
|
|
assert s.step == 3
|
|
assert slice.__dict__['start'].__signature__ == 'start'
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# test slice.__repr__
|
|
assert type(repr(slice(1,1,1))) is str
|
|
|
|
# /************ mappingproxy ************/
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 968: _vm->bind_method<0>("mappingproxy", "keys", [](VM* vm, ArgsView args) {
|
|
# #####: 969: MappingProxy& self = _CAST(MappingProxy&, args[0]);
|
|
# #####: 970: List keys;
|
|
# #####: 971: for(StrName name : self.attr().keys()) keys.push_back(VAR(name.sv()));
|
|
# #####: 972: return VAR(std::move(keys));
|
|
# #####: 973: });
|
|
# test mappingproxy.keys:
|
|
class A():
|
|
def __init__(self):
|
|
self.a = 10
|
|
def method(self):
|
|
pass
|
|
|
|
|
|
my_mappingproxy = A().__dict__
|
|
assert type(my_mappingproxy.keys()) is list
|
|
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 975: _vm->bind_method<0>("mappingproxy", "values", [](VM* vm, ArgsView args) {
|
|
# #####: 976: MappingProxy& self = _CAST(MappingProxy&, args[0]);
|
|
# #####: 977: List values;
|
|
# #####: 978: for(auto& item : self.attr().items()) values.push_back(item.second);
|
|
# #####: 979: return VAR(std::move(values));
|
|
# #####: 980: });
|
|
# test mappingproxy.values:
|
|
class A():
|
|
def __init__(self):
|
|
self.a = 10
|
|
def method(self):
|
|
pass
|
|
|
|
|
|
my_mappingproxy = A().__dict__
|
|
assert type(my_mappingproxy.values()) is list
|
|
|
|
|
|
class A():
|
|
def __init__(self):
|
|
self.a = 10
|
|
def method(self):
|
|
pass
|
|
|
|
|
|
my_mappingproxy = A().__dict__
|
|
assert type(len(my_mappingproxy)) is int
|
|
|
|
|
|
class A():
|
|
def __init__(self):
|
|
self.a = 10
|
|
def method(self):
|
|
pass
|
|
|
|
|
|
my_mappingproxy = A().__dict__
|
|
|
|
try:
|
|
hash(my_mappingproxy)
|
|
print('未能拦截错误, 在测试 mappingproxy.__hash__')
|
|
exit(1)
|
|
except TypeError:
|
|
pass
|
|
|
|
a = hash(object()) # object is hashable
|
|
a = hash(A()) # A is hashable
|
|
class B:
|
|
def __eq__(self, o): return True
|
|
|
|
try:
|
|
hash(B())
|
|
print('未能拦截错误, 在测试 B.__hash__')
|
|
exit(1)
|
|
except TypeError:
|
|
pass
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# test mappingproxy.__repr__:
|
|
class A():
|
|
def __init__(self):
|
|
self.a = 10
|
|
def method(self):
|
|
pass
|
|
|
|
|
|
my_mappingproxy = A().__dict__
|
|
assert type(repr(my_mappingproxy)) is str
|
|
|
|
|
|
# /************ dict ************/
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 202: 1033: _vm->bind_method<-1>("dict", "__init__", [](VM* vm, ArgsView args){
|
|
# 43: 1034: if(args.size() == 1+0) return vm->None;
|
|
# 42: 1035: if(args.size() == 1+1){
|
|
# 42: 1036: auto _lock = vm->heap.gc_scope_lock();
|
|
# 42: 1037: Dict& self = _CAST(Dict&, args[0]);
|
|
# 42: 1038: List& list = CAST(List&, args[1]);
|
|
# 165: 1039: for(PyObject* item : list){
|
|
# 123: 1040: Tuple& t = CAST(Tuple&, item);
|
|
# 123: 1041: if(t.size() != 2){
|
|
# #####: 1042: vm->ValueError("dict() takes an iterable of tuples (key, value)");
|
|
# #####: 1043: return vm->None;
|
|
# -: 1044: }
|
|
# 123: 1045: self.set(t[0], t[1]);
|
|
# 246: 1046: }
|
|
# 42: 1047: return vm->None;
|
|
# 42: 1048: }
|
|
# #####: 1049: vm->TypeError("dict() takes at most 1 argument");
|
|
# #####: 1050: return vm->None;
|
|
# 43: 1051: });
|
|
# test dict:
|
|
assert type(dict([(1,2)])) is dict
|
|
|
|
try:
|
|
dict([(1, 2, 3)])
|
|
print('未能拦截错误, 在测试 dict')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
dict([(1, 2)], 1)
|
|
print('未能拦截错误, 在测试 dict')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
try:
|
|
hash(dict([(1,2)]))
|
|
print('未能拦截错误, 在测试 dict.__hash__')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
# test dict.__iter__
|
|
for k in {1:2, 2:3, 3:4}:
|
|
assert k in [1,2,3]
|
|
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 166: 1098: _vm->bind_method<-1>("dict", "get", [](VM* vm, ArgsView args) {
|
|
# 25: 1099: Dict& self = _CAST(Dict&, args[0]);
|
|
# 25: 1100: if(args.size() == 1+1){
|
|
# #####: 1101: PyObject* ret = self.try_get(args[1]);
|
|
# #####: 1102: if(ret != nullptr) return ret;
|
|
# #####: 1103: return vm->None;
|
|
# 25: 1104: }else if(args.size() == 1+2){
|
|
# 25: 1105: PyObject* ret = self.try_get(args[1]);
|
|
# 25: 1106: if(ret != nullptr) return ret;
|
|
# 19: 1107: return args[2];
|
|
# -: 1108: }
|
|
# #####: 1109: vm->TypeError("get() takes at most 2 arguments");
|
|
# #####: 1110: return vm->None;
|
|
# 25: 1111: });
|
|
# test dict.get
|
|
|
|
assert {1:2, 3:4}.get(1) == 2
|
|
assert {1:2, 3:4}.get(2) is None
|
|
assert {1:2, 3:4}.get(20, 100) == 100
|
|
|
|
try:
|
|
{1:2, 3:4}.get(1,1, 1)
|
|
print('未能拦截错误, 在测试 dict.get')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# test dict.__repr__
|
|
assert type(repr({1:2, 3:4})) is str
|
|
|
|
# /************ property ************/
|
|
class A():
|
|
def __init__(self):
|
|
self._name = '123'
|
|
|
|
@property
|
|
def value(self):
|
|
return 2
|
|
|
|
def get_name(self):
|
|
'''
|
|
doc string 1
|
|
'''
|
|
return self._name
|
|
|
|
def set_name(self, val):
|
|
'''
|
|
doc string 2
|
|
'''
|
|
self._name = val
|
|
|
|
assert A().value == 2
|
|
assert A.__dict__['value'].__signature__ == ''
|
|
|
|
A.name = property(A.get_name, A.set_name, "name: str")
|
|
assert A.__dict__['name'].__signature__ == 'name: str'
|
|
try:
|
|
property(A.get_name, A.set_name, 1)
|
|
print('未能拦截错误, 在测试 property')
|
|
exit(1)
|
|
except:
|
|
pass
|
|
|
|
class Vector2:
|
|
def __init__(self) -> None:
|
|
self._x = 0
|
|
|
|
@property
|
|
def x(self):
|
|
return self._x
|
|
|
|
@x.setter
|
|
def x(self, val):
|
|
self._x = val
|
|
|
|
v = Vector2()
|
|
assert v.x == 0
|
|
v.x = 10
|
|
assert v.x == 10
|
|
|
|
# /************ module timeit ************/
|
|
import timeit
|
|
|
|
def aaa():
|
|
for i in range(10):
|
|
for j in range(10):
|
|
pass
|
|
|
|
assert type(timeit.timeit(aaa, 2)) is float
|
|
|
|
|
|
# function.__doc__
|
|
def aaa():
|
|
'12345'
|
|
pass
|
|
assert type(aaa.__doc__) is str
|
|
|
|
|
|
# function.__signature__
|
|
def aaa():
|
|
pass
|
|
assert type(aaa.__signature__) is str
|
|
|
|
|
|
# /************ module time ************/
|
|
import time
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
|
|
# #####: 1268: f64 seconds = CAST_F(args[0]);
|
|
# #####: 1269: auto begin = std::chrono::system_clock::now();
|
|
# #####: 1270: while(true){
|
|
# #####: 1271: auto now = std::chrono::system_clock::now();
|
|
# #####: 1272: f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
|
|
# #####: 1273: if(elapsed >= seconds) break;
|
|
# #####: 1274: }
|
|
# #####: 1275: return vm->None;
|
|
# #####: 1276: });
|
|
# test time.time
|
|
assert type(time.time()) is float
|
|
|
|
local_t = time.localtime()
|
|
assert type(local_t.tm_year) is int
|
|
assert type(local_t.tm_mon) is int
|
|
assert type(local_t.tm_mday) is int
|
|
assert type(local_t.tm_hour) is int
|
|
assert type(local_t.tm_min) is int
|
|
assert type(local_t.tm_sec) is int
|
|
assert type(local_t.tm_wday) is int
|
|
assert type(local_t.tm_yday) is int
|
|
assert type(local_t.tm_isdst) is int
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 1267: vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
|
|
# #####: 1268: f64 seconds = CAST_F(args[0]);
|
|
# #####: 1269: auto begin = std::chrono::system_clock::now();
|
|
# #####: 1270: while(true){
|
|
# #####: 1271: auto now = std::chrono::system_clock::now();
|
|
# #####: 1272: f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
|
|
# #####: 1273: if(elapsed >= seconds) break;
|
|
# #####: 1274: }
|
|
# #####: 1275: return vm->None;
|
|
# #####: 1276: });
|
|
# test time.sleep
|
|
time.sleep(0.1)
|
|
|
|
# 未完全测试准确性-----------------------------------------------
|
|
# 116: 1278: vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) {
|
|
# #####: 1279: auto now = std::chrono::system_clock::now();
|
|
# #####: 1280: std::time_t t = std::chrono::system_clock::to_time_t(now);
|
|
# #####: 1281: std::tm* tm = std::localtime(&t);
|
|
# #####: 1282: Dict d(vm);
|
|
# #####: 1283: d.set(VAR("tm_year"), VAR(tm->tm_year + 1900));
|
|
# #####: 1284: d.set(VAR("tm_mon"), VAR(tm->tm_mon + 1));
|
|
# #####: 1285: d.set(VAR("tm_mday"), VAR(tm->tm_mday));
|
|
# #####: 1286: d.set(VAR("tm_hour"), VAR(tm->tm_hour));
|
|
# #####: 1287: d.set(VAR("tm_min"), VAR(tm->tm_min));
|
|
# #####: 1288: d.set(VAR("tm_sec"), VAR(tm->tm_sec + 1));
|
|
# #####: 1289: d.set(VAR("tm_wday"), VAR((tm->tm_wday + 6) % 7));
|
|
# #####: 1290: d.set(VAR("tm_yday"), VAR(tm->tm_yday + 1));
|
|
# #####: 1291: d.set(VAR("tm_isdst"), VAR(tm->tm_isdst));
|
|
# #####: 1292: return VAR(std::move(d));
|
|
# #####: 1293: });
|
|
# 58: 1294:}
|
|
# test time.localtime
|
|
assert type(time.localtime()) is time.struct_time
|
|
|
|
# test min/max
|
|
assert min(1, 2) == 1
|
|
assert min(1, 2, 3) == 1
|
|
assert min([1, 2]) == 1
|
|
assert min([1, 2], key=lambda x: -x) == 2
|
|
|
|
assert max(1, 2) == 2
|
|
assert max(1, 2, 3) == 3
|
|
assert max([1, 2]) == 2
|
|
assert max([1, 2, 3], key=lambda x: -x) == 1
|
|
|
|
assert min([
|
|
(1, 2),
|
|
(1, 3),
|
|
(1, 4),
|
|
]) == (1, 2)
|
|
|
|
assert min(1, 2) == 1
|
|
assert max(1, 2) == 2
|
|
|
|
|
|
# test callable
|
|
assert callable(lambda: 1) is True # function
|
|
assert callable(1) is False # int
|
|
assert callable(object) is True # type
|
|
assert callable(object()) is False
|
|
assert callable([].append) is True # bound method
|
|
assert callable([].__getitem__) is True # bound method
|
|
|
|
class A:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def __call__(self):
|
|
pass
|
|
|
|
assert callable(A) is True # type
|
|
assert callable(A()) is True # instance with __call__
|
|
assert callable(A.__call__) is True # bound method
|
|
assert callable(A.__init__) is True # bound method
|
|
assert callable(print) is True # builtin function
|
|
assert callable(isinstance) is True # builtin function
|
|
|
|
|
|
assert id(0) is None
|
|
assert id(2**62) is not None
|
|
|
|
# test issubclass
|
|
assert issubclass(int, int) is True
|
|
assert issubclass(int, object) is True
|
|
assert issubclass(object, int) is False
|
|
assert issubclass(object, object) is True
|
|
assert issubclass(int, type) is False
|
|
assert issubclass(type, type) is True
|
|
assert issubclass(float, int) is False
|