From f980c70997eb9748ed169d486489ab3d0d2002af Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 15 Feb 2017 18:11:16 +0300 Subject: [PATCH] tests/basic/: Make various tests skippable. To run the testsuite on small ports. --- tests/basics/iter_of_iter.py | 3 +- tests/basics/map.py | 2 +- tests/basics/memoryview1.py | 6 ++ tests/basics/memoryview2.py | 9 +- tests/basics/memoryview_gc.py | 6 ++ tests/basics/namedtuple1.py | 9 +- tests/basics/object_new.py | 8 ++ tests/basics/op_error.py | 1 - tests/basics/op_error_memoryview.py | 19 ++++ tests/basics/set_iter_of_iter.py | 2 + tests/basics/special_methods.py | 40 +------- tests/basics/special_methods2.py | 145 +++++++++++++++++++++++++++ tests/basics/subclass_classmethod.py | 7 ++ tests/basics/sys1.py | 14 ++- 14 files changed, 222 insertions(+), 49 deletions(-) create mode 100644 tests/basics/op_error_memoryview.py create mode 100644 tests/basics/set_iter_of_iter.py create mode 100644 tests/basics/special_methods2.py diff --git a/tests/basics/iter_of_iter.py b/tests/basics/iter_of_iter.py index 70282aa97e..d775b6a44e 100644 --- a/tests/basics/iter_of_iter.py +++ b/tests/basics/iter_of_iter.py @@ -4,5 +4,4 @@ i = iter(iter([1, 2, 3])) print(list(i)) i = iter(iter({1:2, 3:4, 5:6})) print(sorted(i)) -i = iter(iter({1, 2, 3})) -print(sorted(i)) +# set, see set_iter_of_iter.py diff --git a/tests/basics/map.py b/tests/basics/map.py index 62dca44ede..8fce352c28 100644 --- a/tests/basics/map.py +++ b/tests/basics/map.py @@ -1,4 +1,4 @@ print(list(map(lambda x: x & 1, range(-3, 4)))) print(list(map(abs, range(-3, 4)))) -print(list(map(set, [[i] for i in range(-3, 4)]))) +print(list(map(tuple, [[i] for i in range(-3, 4)]))) print(list(map(pow, range(4), range(4)))) diff --git a/tests/basics/memoryview1.py b/tests/basics/memoryview1.py index 1cd411195d..019a1179f8 100644 --- a/tests/basics/memoryview1.py +++ b/tests/basics/memoryview1.py @@ -1,4 +1,10 @@ # test memoryview +try: + memoryview +except: + import sys + print("SKIP") + sys.exit() # test reading from bytes b = b'1234' diff --git a/tests/basics/memoryview2.py b/tests/basics/memoryview2.py index 5117d7a680..edb7c9e640 100644 --- a/tests/basics/memoryview2.py +++ b/tests/basics/memoryview2.py @@ -1,6 +1,11 @@ # test memoryview accessing maximum values for signed/unsigned elements - -from array import array +try: + from array import array + memoryview +except: + import sys + print("SKIP") + sys.exit() print(list(memoryview(b'\x7f\x80\x81\xff'))) print(list(memoryview(array('b', [0x7f, -0x80])))) diff --git a/tests/basics/memoryview_gc.py b/tests/basics/memoryview_gc.py index a1e4baad47..9d4857e362 100644 --- a/tests/basics/memoryview_gc.py +++ b/tests/basics/memoryview_gc.py @@ -1,4 +1,10 @@ # test memoryview retains pointer to original object/buffer +try: + memoryview +except: + import sys + print("SKIP") + sys.exit() b = bytearray(10) m = memoryview(b)[1:] diff --git a/tests/basics/namedtuple1.py b/tests/basics/namedtuple1.py index 346e32fbfc..132dcf96b3 100644 --- a/tests/basics/namedtuple1.py +++ b/tests/basics/namedtuple1.py @@ -1,7 +1,12 @@ try: - from collections import namedtuple + try: + from collections import namedtuple + except ImportError: + from ucollections import namedtuple except ImportError: - from ucollections import namedtuple + import sys + print("SKIP") + sys.exit() T = namedtuple("Tup", ["foo", "bar"]) # CPython prints fully qualified name, what we don't bother to do so far diff --git a/tests/basics/object_new.py b/tests/basics/object_new.py index befb5bfc27..568feccda4 100644 --- a/tests/basics/object_new.py +++ b/tests/basics/object_new.py @@ -2,6 +2,14 @@ # (non-initialized) instance of class. # See e.g. http://infohost.nmt.edu/tcc/help/pubs/python/web/new-new-method.html # TODO: Find reference in CPython docs +try: + # If we don't expose object.__new__ (small ports), there's + # nothing to test. + object.__new__ +except AttributeError: + import sys + print("SKIP") + sys.exit() class Foo: diff --git a/tests/basics/op_error.py b/tests/basics/op_error.py index 19ce04bc52..5ba6a80e26 100644 --- a/tests/basics/op_error.py +++ b/tests/basics/op_error.py @@ -20,7 +20,6 @@ test_exc("False in True", TypeError) test_exc("1 * {}", TypeError) test_exc("1 in 1", TypeError) test_exc("bytearray() // 2", TypeError) -test_exc("m = memoryview(bytearray())\nm += bytearray()", TypeError) # object with buffer protocol needed on rhs test_exc("bytearray(1) + 1", TypeError) diff --git a/tests/basics/op_error_memoryview.py b/tests/basics/op_error_memoryview.py new file mode 100644 index 0000000000..658ededc80 --- /dev/null +++ b/tests/basics/op_error_memoryview.py @@ -0,0 +1,19 @@ +# test errors from bad operations (unary, binary, etc) +try: + memoryview +except: + import sys + print("SKIP") + sys.exit() + +def test_exc(code, exc): + try: + exec(code) + print("no exception") + except exc: + print("right exception") + except: + print("wrong exception") + +# unsupported binary operators +test_exc("m = memoryview(bytearray())\nm += bytearray()", TypeError) diff --git a/tests/basics/set_iter_of_iter.py b/tests/basics/set_iter_of_iter.py new file mode 100644 index 0000000000..e3e91fa456 --- /dev/null +++ b/tests/basics/set_iter_of_iter.py @@ -0,0 +1,2 @@ +i = iter(iter({1, 2, 3})) +print(sorted(i)) diff --git a/tests/basics/special_methods.py b/tests/basics/special_methods.py index 1df7a7c4c7..9f57247c12 100644 --- a/tests/basics/special_methods.py +++ b/tests/basics/special_methods.py @@ -105,42 +105,4 @@ cud1 > cud2 cud1 + cud2 cud1 - cud2 -# the following require MICROPY_PY_ALL_SPECIAL_METHODS -+cud1 --cud1 -~cud1 -cud1 * cud2 -cud1 / cud2 -cud2 // cud1 -cud1 += cud2 -cud1 -= cud2 - -# TODO: the following operations are not supported on every ports -# -# ne is not supported, !(eq) is called instead -#cud1 != cud2 -# -# binary and is not supported -# cud1 & cud2 -# -# binary lshift is not supported -# cud1<<1 -# -# modulus is not supported -# cud1 % 2 -# -# binary or is not supported -# cud1 | cud2 -# -# pow is not supported -# cud1**2 -# -# rshift is not suported -# cud1>>1 -# -# xor is not supported -# cud1^cud2 -# -# in the followin test, cpython still calls __eq__ -# cud3=cud1 -# cud3==cud1 +# more in special_methods2.py diff --git a/tests/basics/special_methods2.py b/tests/basics/special_methods2.py new file mode 100644 index 0000000000..3623b30dcc --- /dev/null +++ b/tests/basics/special_methods2.py @@ -0,0 +1,145 @@ +class Cud(): + + def __init__(self): + #print("__init__ called") + pass + + def __repr__(self): + print("__repr__ called") + return "" + + def __lt__(self, other): + print("__lt__ called") + + def __le__(self, other): + print("__le__ called") + + def __eq__(self, other): + print("__eq__ called") + + def __ne__(self, other): + print("__ne__ called") + + def __ge__(self, other): + print("__ge__ called") + + def __gt__(self, other): + print("__gt__ called") + + def __abs__(self): + print("__abs__ called") + + def __add__(self, other): + print("__add__ called") + + def __and__(self, other): + print("__and__ called") + + def __floordiv__(self, other): + print("__floordiv__ called") + + def __index__(self, other): + print("__index__ called") + + def __inv__(self): + print("__inv__ called") + + def __invert__(self): + print("__invert__ called") + + def __lshift__(self, val): + print("__lshift__ called") + + def __mod__(self, val): + print("__mod__ called") + + def __mul__(self, other): + print("__mul__ called") + + def __matmul__(self, other): + print("__matmul__ called") + + def __neg__(self): + print("__neg__ called") + + def __or__(self, other): + print("__or__ called") + + def __pos__(self): + print("__pos__ called") + + def __pow__(self, val): + print("__pow__ called") + + def __rshift__(self, val): + print("__rshift__ called") + + def __sub__(self, other): + print("__sub__ called") + + def __truediv__(self, other): + print("__truediv__ called") + + def __div__(self, other): + print("__div__ called") + + def __xor__(self, other): + print("__xor__ called") + + def __iadd__(self, other): + print("__iadd__ called") + return self + + def __isub__(self, other): + print("__isub__ called") + return self + +cud1 = Cud() +cud2 = Cud() + +try: + +cud1 +except TypeError: + import sys + print("SKIP") + sys.exit() + +# the following require MICROPY_PY_ALL_SPECIAL_METHODS ++cud1 +-cud1 +~cud1 +cud1 * cud2 +cud1 / cud2 +cud2 // cud1 +cud1 += cud2 +cud1 -= cud2 + +# TODO: the following operations are not supported on every ports +# +# ne is not supported, !(eq) is called instead +#cud1 != cud2 +# +# binary and is not supported +# cud1 & cud2 +# +# binary lshift is not supported +# cud1<<1 +# +# modulus is not supported +# cud1 % 2 +# +# binary or is not supported +# cud1 | cud2 +# +# pow is not supported +# cud1**2 +# +# rshift is not suported +# cud1>>1 +# +# xor is not supported +# cud1^cud2 +# +# in the followin test, cpython still calls __eq__ +# cud3=cud1 +# cud3==cud1 diff --git a/tests/basics/subclass_classmethod.py b/tests/basics/subclass_classmethod.py index ae5fbd1aa3..48f164b364 100644 --- a/tests/basics/subclass_classmethod.py +++ b/tests/basics/subclass_classmethod.py @@ -5,6 +5,13 @@ class Base: def foo(cls): print(cls.__name__) +try: + Base.__name__ +except AttributeError: + import sys + print("SKIP") + sys.exit() + class Sub(Base): pass diff --git a/tests/basics/sys1.py b/tests/basics/sys1.py index 816c8823aa..29ef974d14 100644 --- a/tests/basics/sys1.py +++ b/tests/basics/sys1.py @@ -6,8 +6,18 @@ print(sys.__name__) print(type(sys.path)) print(type(sys.argv)) print(sys.byteorder in ('little', 'big')) -print(sys.maxsize > 100) -print(sys.implementation.name in ('cpython', 'micropython')) + +try: + print(sys.maxsize > 100) +except AttributeError: + # Effectively skip subtests + print(True) + +try: + print(sys.implementation.name in ('cpython', 'micropython')) +except AttributeError: + # Effectively skip subtests + print(True) try: sys.exit()