diff options
Diffstat (limited to 'tests/basics')
-rw-r--r-- | tests/basics/builtin_chr.py | 9 | ||||
-rw-r--r-- | tests/basics/builtin_compile.py | 25 | ||||
-rw-r--r-- | tests/basics/builtin_dir.py | 9 | ||||
-rw-r--r-- | tests/basics/builtin_divmod.py | 16 | ||||
-rw-r--r-- | tests/basics/builtin_minmax.py | 6 | ||||
-rw-r--r-- | tests/basics/builtin_ord.py | 9 | ||||
-rw-r--r-- | tests/basics/builtin_pow.py | 11 | ||||
-rw-r--r-- | tests/basics/builtin_property.py (renamed from tests/basics/property.py) | 26 | ||||
-rw-r--r-- | tests/basics/builtin_slice.py | 7 | ||||
-rw-r--r-- | tests/basics/builtin_sorted.py | 10 | ||||
-rw-r--r-- | tests/basics/class_descriptor.py | 7 | ||||
-rw-r--r-- | tests/basics/fun_error.py | 31 | ||||
-rw-r--r-- | tests/basics/int_big_mul.py | 15 | ||||
-rw-r--r-- | tests/basics/module1.py | 13 | ||||
-rw-r--r-- | tests/basics/module2.py | 6 | ||||
-rw-r--r-- | tests/basics/module2.py.exp | 1 | ||||
-rw-r--r-- | tests/basics/object1.py | 7 | ||||
-rw-r--r-- | tests/basics/op_error.py | 3 | ||||
-rw-r--r-- | tests/basics/sorted.py | 2 | ||||
-rw-r--r-- | tests/basics/string1.py | 8 | ||||
-rw-r--r-- | tests/basics/sys1.py | 21 |
21 files changed, 233 insertions, 9 deletions
diff --git a/tests/basics/builtin_chr.py b/tests/basics/builtin_chr.py new file mode 100644 index 0000000000..02d783ae66 --- /dev/null +++ b/tests/basics/builtin_chr.py @@ -0,0 +1,9 @@ +# test builtin chr (whether or not we support unicode) + +print(chr(65)) + +try: + chr(0x110000) +except ValueError: + print("ValueError") + diff --git a/tests/basics/builtin_compile.py b/tests/basics/builtin_compile.py index ef3ff014d4..32c6667d8b 100644 --- a/tests/basics/builtin_compile.py +++ b/tests/basics/builtin_compile.py @@ -7,10 +7,9 @@ def have_compile(): except NameError: return False -# global variable for compiled code to access -x = 1 - def test(): + global x + c = compile("print(x)", "file", "exec") try: @@ -18,11 +17,31 @@ def test(): except NameError: print("NameError") + # global variable for compiled code to access + x = 1 + exec(c) exec(c, {"x":2}) exec(c, {}, {"x":3}) + # single/eval mode + exec(compile('print(1 + 1)', 'file', 'single')) + print(eval(compile('1 + 1', 'file', 'eval'))) + + # bad mode + try: + compile('1', 'file', '') + except ValueError: + print("ValueError") + + # exception within compiled code + try: + exec(compile('noexist', 'file', 'exec')) + except NameError: + print("NameError") + print(x) # check 'x' still exists as a global + if have_compile(): test() else: diff --git a/tests/basics/builtin_dir.py b/tests/basics/builtin_dir.py new file mode 100644 index 0000000000..c7064c8cab --- /dev/null +++ b/tests/basics/builtin_dir.py @@ -0,0 +1,9 @@ +# test builtin dir + +# dir of locals +print('__name__' in dir()) + +# dir of module +import sys +print('platform' in dir(sys)) + diff --git a/tests/basics/builtin_divmod.py b/tests/basics/builtin_divmod.py new file mode 100644 index 0000000000..f159691b7b --- /dev/null +++ b/tests/basics/builtin_divmod.py @@ -0,0 +1,16 @@ +# test builtin divmod + +print(divmod(0, 2)) +print(divmod(3, 4)) +print(divmod(20, 3)) + +try: + divmod(1, 0) +except ZeroDivisionError: + print("ZeroDivisionError") + +try: + divmod('a', 'b') +except TypeError: + print("TypeError") + diff --git a/tests/basics/builtin_minmax.py b/tests/basics/builtin_minmax.py index a5f035b909..e2bb0ed7b0 100644 --- a/tests/basics/builtin_minmax.py +++ b/tests/basics/builtin_minmax.py @@ -23,3 +23,9 @@ print(max(lst, key=lambda x:x)) print(max(lst, key=lambda x:-x)) print(max(1, 2, 3, 4, key=lambda x:-x)) print(max(4, 3, 2, 1, key=lambda x:-x)) + +# need at least 1 item in the iterable +try: + min([]) +except ValueError: + print("ValueError") diff --git a/tests/basics/builtin_ord.py b/tests/basics/builtin_ord.py new file mode 100644 index 0000000000..6347aa4ec0 --- /dev/null +++ b/tests/basics/builtin_ord.py @@ -0,0 +1,9 @@ +# test builtin ord (whether or not we support unicode) + +print(ord('a')) + +try: + ord('') +except TypeError: + print("TypeError") + diff --git a/tests/basics/builtin_pow.py b/tests/basics/builtin_pow.py new file mode 100644 index 0000000000..a19ab8c843 --- /dev/null +++ b/tests/basics/builtin_pow.py @@ -0,0 +1,11 @@ +# test builtin pow() with integral values + +# 2 arg version +print(pow(0, 1)) +print(pow(1, 0)) +print(pow(-2, 3)) +print(pow(3, 8)) + +# 3 arg version +print(pow(3, 4, 7)) + diff --git a/tests/basics/property.py b/tests/basics/builtin_property.py index 7f3c833ad3..4df9842a3e 100644 --- a/tests/basics/property.py +++ b/tests/basics/builtin_property.py @@ -1,3 +1,16 @@ +# test builtin property + +# create a property object explicitly +property() +property(1, 2, 3) + +# use its accessor methods +p = property() +p.getter(1) +p.setter(2) +p.deleter(3) + +# basic use as a decorator class A: def __init__(self, x): self._x = x @@ -15,6 +28,7 @@ try: except AttributeError: print("AttributeError") +# explicit use within a class class B: def __init__(self, x): self._x = x @@ -27,13 +41,18 @@ class B: print("x set") self._x = value - x = property(xget, xset) + def xdel(self): + print("x del") + + x = property(xget, xset, xdel) b = B(3) print(b.x) b.x = 4 print(b.x) +del b.x +# full use as a decorator class C: def __init__(self, x): self._x = x @@ -48,7 +67,12 @@ class C: print("x set") self._x = value + @x.deleter + def x(self): + print("x del") + c = C(5) print(c.x) c.x = 6 print(c.x) +del c.x diff --git a/tests/basics/builtin_slice.py b/tests/basics/builtin_slice.py new file mode 100644 index 0000000000..4da1229fa0 --- /dev/null +++ b/tests/basics/builtin_slice.py @@ -0,0 +1,7 @@ +# test builtin slice + +# print slice +class A: + def __getitem__(self, idx): + print(idx) +A()[1:2:3] diff --git a/tests/basics/builtin_sorted.py b/tests/basics/builtin_sorted.py new file mode 100644 index 0000000000..a4f71a15eb --- /dev/null +++ b/tests/basics/builtin_sorted.py @@ -0,0 +1,10 @@ +# test builtin sorted + +print(sorted(set(range(100)))) +print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2))) + +# need to use keyword argument +try: + sorted([], None) +except TypeError: + print("TypeError") diff --git a/tests/basics/class_descriptor.py b/tests/basics/class_descriptor.py index 27907411db..25b373e47e 100644 --- a/tests/basics/class_descriptor.py +++ b/tests/basics/class_descriptor.py @@ -1,13 +1,19 @@ class Descriptor: def __get__(self, obj, cls): + print('get') print(type(obj) is Main) print(cls is Main) return 'result' def __set__(self, obj, val): + print('set') print(type(obj) is Main) print(val) + def __delete__(self, obj): + print('delete') + print(type(obj) is Main) + class Main: Forward = Descriptor() @@ -18,4 +24,5 @@ if 'Descriptor' in repr(r.__class__): else: print(r) m.Forward = 'a' + del m.Forward diff --git a/tests/basics/fun_error.py b/tests/basics/fun_error.py new file mode 100644 index 0000000000..02af7b1bba --- /dev/null +++ b/tests/basics/fun_error.py @@ -0,0 +1,31 @@ +# test errors from bad function calls + +def test_exc(code, exc): + try: + exec(code) + print("no exception") + except exc: + print("right exception") + except: + print("wrong exception") + +# function doesn't take keyword args +test_exc("[].append(x=1)", TypeError) + +# function with variable number of positional args given too few +test_exc("round()", TypeError) + +# function with variable number of positional args given too many +test_exc("round(1, 2, 3)", TypeError) + +# function with fixed number of positional args given wrong number +test_exc("[].append(1, 2)", TypeError) + +# function with keyword args given extra positional args +test_exc("[].sort(1)", TypeError) + +# function with keyword args given extra keyword args +test_exc("[].sort(noexist=1)", TypeError) + +# function with keyword args not given a specific keyword arg +test_exc("enumerate()", TypeError) diff --git a/tests/basics/int_big_mul.py b/tests/basics/int_big_mul.py index f8d3dcd800..6010075c4e 100644 --- a/tests/basics/int_big_mul.py +++ b/tests/basics/int_big_mul.py @@ -6,3 +6,18 @@ for rhs in range(2, 11): print(lhs, '*', rhs, '=', res) lhs = res +# below tests pos/neg combinations that overflow small int + +# 31-bit overflow +i = 1 << 20 +print(i * i) +print(i * -i) +print(-i * i) +print(-i * -i) + +# 63-bit overflow +i = 1 << 40 +print(i * i) +print(i * -i) +print(-i * i) +print(-i * -i) diff --git a/tests/basics/module1.py b/tests/basics/module1.py new file mode 100644 index 0000000000..c158af52e2 --- /dev/null +++ b/tests/basics/module1.py @@ -0,0 +1,13 @@ +# test behaviour of module objects + +# this module should always exist +import __main__ + +# print module +print(repr(__main__).startswith("<module '__main__'")) + +# store new attribute +__main__.x = 1 + +# delete attribute +del __main__.x diff --git a/tests/basics/module2.py b/tests/basics/module2.py new file mode 100644 index 0000000000..a135601579 --- /dev/null +++ b/tests/basics/module2.py @@ -0,0 +1,6 @@ +# uPy behaviour only: builtin modules are read-only +import sys +try: + sys.x = 1 +except AttributeError: + print("AttributeError") diff --git a/tests/basics/module2.py.exp b/tests/basics/module2.py.exp new file mode 100644 index 0000000000..d169edffb4 --- /dev/null +++ b/tests/basics/module2.py.exp @@ -0,0 +1 @@ +AttributeError diff --git a/tests/basics/object1.py b/tests/basics/object1.py new file mode 100644 index 0000000000..cbc7e92ead --- /dev/null +++ b/tests/basics/object1.py @@ -0,0 +1,7 @@ +# test builtin object() + +# creation +object() + +# printing +print(repr(object())[:7]) diff --git a/tests/basics/op_error.py b/tests/basics/op_error.py index cfd6ffa8c4..19ce04bc52 100644 --- a/tests/basics/op_error.py +++ b/tests/basics/op_error.py @@ -11,6 +11,7 @@ def test_exc(code, exc): # unsupported unary operators test_exc("~None", TypeError) +test_exc("~''", TypeError) test_exc("~[]", TypeError) test_exc("~bytearray()", TypeError) @@ -28,6 +29,8 @@ test_exc("(1 << 70) in 1", TypeError) # unsupported subscription test_exc("1[0]", TypeError) test_exc("1[0] = 1", TypeError) +test_exc("''['']", TypeError) +test_exc("'a'[0] = 1", TypeError) test_exc("del 1[0]", TypeError) # not callable diff --git a/tests/basics/sorted.py b/tests/basics/sorted.py deleted file mode 100644 index bbec319460..0000000000 --- a/tests/basics/sorted.py +++ /dev/null @@ -1,2 +0,0 @@ -print(sorted(set(range(100)))) -print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2))) diff --git a/tests/basics/string1.py b/tests/basics/string1.py index b617cc786d..f58fcd401a 100644 --- a/tests/basics/string1.py +++ b/tests/basics/string1.py @@ -26,9 +26,11 @@ except IndexError: # iter print(list('str')) +# comparison print('123' + '789' == '123789') print('a' + 'b' != 'a' + 'b ') +print('1' + '2' > '2') +print('1' + '2' < '2') -# Not implemented so far -# print('1' + '2' > '2') -# print('1' + '2' < '2') +# printing quote char in string +print(repr('\'\"')) diff --git a/tests/basics/sys1.py b/tests/basics/sys1.py new file mode 100644 index 0000000000..fa81e14ecd --- /dev/null +++ b/tests/basics/sys1.py @@ -0,0 +1,21 @@ +# test sys module + +import sys + +print(sys.__name__) +print(type(sys.path)) +print(type(sys.argv)) +print(sys.version[:3]) +print(sys.version_info[0], sys.version_info[1]) +print(sys.byteorder in ('little', 'big')) +print(sys.maxsize > 100) + +try: + sys.exit() +except SystemExit as e: + print("SystemExit", e.args) + +try: + sys.exit(42) +except SystemExit as e: + print("SystemExit", e.args) |