Commit 67c77e97 authored by Damien George's avatar Damien George

leon: Update leon-tests from upstream tests.

parent d865f67b
try:
enumerate
except:
import sys
print("SKIP")
sys.exit()
raise SystemExit
print(list(enumerate([])))
print(list(enumerate([1, 2, 3])))
......
try:
filter
except:
import sys
print("SKIP")
sys.exit()
raise SystemExit
print(list(filter(lambda x: x & 1, range(-3, 4))))
print(list(filter(None, range(-3, 4))))
# test builtin hash function, on generators
def gen():
yield
print(type(hash(gen)))
print(type(hash(gen())))
True
False
True
True
True
True
True
True
True
True
True
False
TypeError
# test binary operations on range objects; (in)equality only
# this "feature test" actually tests the implementation but is the best we can do
if range(1) != range(1):
print("SKIP")
raise SystemExit
# basic (in)equality
print(range(1) == range(1))
print(range(1) != range(1))
print(range(1) != range(2))
# empty range
print(range(0) == range(0))
print(range(1, 0) == range(0))
print(range(1, 4, -1) == range(6, 3))
# 1 element range
print(range(1, 4, 10) == range(1, 4, 10))
print(range(1, 4, 10) == range(1, 4, 20))
print(range(1, 4, 10) == range(1, 8, 20))
# more than 1 element
print(range(0, 3, 2) == range(0, 3, 2))
print(range(0, 3, 2) == range(0, 4, 2))
print(range(0, 3, 2) == range(0, 5, 2))
# unsupported binary op
try:
range(1) + 10
except TypeError:
print('TypeError')
123
56
TypeError
AttributeError/TypeError
......@@ -16,3 +16,10 @@ try:
setattr(a, b'var3', 1)
except TypeError:
print('TypeError')
# try setattr on a built-in function
try:
setattr(int, 'to_bytes', 1)
except (AttributeError, TypeError):
# uPy raises AttributeError, CPython raises TypeError
print('AttributeError/TypeError')
......@@ -3,8 +3,7 @@ try:
set
except NameError:
print("SKIP")
import sys
sys.exit()
raise SystemExit
print(list(zip()))
print(list(zip([1], set([2, 3]))))
A(8)
A(5)
L([1, 2, 3, 4])
L([1, 2, 3, 4])
# Case 1: Immutable object (e.g. number-like)
# __iadd__ should not be defined, will be emulated using __add__
class A:
def __init__(self, v):
self.v = v
def __add__(self, o):
return A(self.v + o.v)
def __repr__(self):
return "A(%s)" % self.v
a = A(5)
b = a
a += A(3)
print(a)
# Should be original a's value, i.e. A(5)
print(b)
# Case 2: Mutable object (e.g. list-like)
# __iadd__ should be defined
class L:
def __init__(self, v):
self.v = v
def __add__(self, o):
# Should not be caled in this test
print("L.__add__")
return L(self.v + o.v)
def __iadd__(self, o):
self.v += o.v
return self
def __repr__(self):
return "L(%s)" % self.v
c = L([1, 2])
d = c
c += L([3, 4])
print(c)
# Should be updated c's value, i.e. L([1, 2, 3, 4])
print(d)
# test using the name "super" as a local variable
class A:
def foo(self):
super = [1, 2]
super.pop()
print(super)
A().foo()
# test super with multiple inheritance
class A:
def foo(self):
print('A.foo')
class B:
def foo(self):
print('B.foo')
class C(A, B):
def foo(self):
print('C.foo')
super().foo()
C().foo()
0
1
else
0
0
1
2
3
0
1
else
0
1
else
0
# test for-else statement
# test optimised range with simple else
for i in range(2):
print(i)
else:
print('else')
# test optimised range with break over else
for i in range(2):
print(i)
break
else:
print('else')
# test nested optimised range with continue in the else
for i in range(4):
print(i)
for j in range(4):
pass
else:
continue
break
# test optimised range with non-constant end value
N = 2
for i in range(N):
print(i)
else:
print('else')
# test generic iterator with simple else
for i in [0, 1]:
print(i)
else:
print('else')
# test generic iterator with break over else
for i in [0, 1]:
print(i)
break
else:
print('else')
# test errors from bad function calls
try:
enumerate
except:
print("SKIP")
raise SystemExit
# function with keyword args not given a specific keyword arg
try:
enumerate()
except TypeError:
print('TypeError')
0
1
raised ValueError()
ret was: None
TypeError
def gen():
i = 0
while 1:
yield i
i += 1
g = gen()
try:
g.pend_throw
except AttributeError:
print("SKIP")
raise SystemExit
print(next(g))
print(next(g))
g.pend_throw(ValueError())
v = None
try:
v = next(g)
except Exception as e:
print("raised", repr(e))
print("ret was:", v)
try:
gen().pend_throw(ValueError())
except TypeError:
print("TypeError")
[2130706432, -2147483648]
[2130706432, 2147483648, 2164260864, 4294967295]
# test memoryview accessing maximum values for signed/unsigned elements
try:
from array import array
memoryview
except:
print("SKIP")
raise SystemExit
print(list(memoryview(array('i', [0x7f000000, -0x80000000]))))
print(list(memoryview(array('I', [0x7f000000, 0x80000000, 0x81000000, 0xffffffff]))))
# test errors from bad operations (unary, binary, etc)
def test_exc(code, exc):
try:
exec(code)
print("no exception")
except exc:
print("right exception")
except:
print("wrong exception")
# object with buffer protocol needed on rhs
try:
(1 << 70) in 1
except TypeError:
print('TypeError')
# test errors from bad operations (unary, binary, etc)
try:
memoryview
except:
print("SKIP")
raise SystemExit
# unsupported binary operators
try:
m = memoryview(bytearray())
m += bytearray()
except TypeError:
print('TypeError')
# see https://docs.python.org/3/reference/expressions.html#operator-precedence
# '|' is the least binding numeric operator
# '^'
# OK: 1 | (2 ^ 3) = 1 | 1 = 1
# BAD: (1 | 2) ^ 3 = 3 ^ 3 = 0
print(1 | 2 ^ 3)
# '&'
# OK: 3 ^ (2 & 1) = 3 ^ 0 = 3
# BAD: (3 ^ 2) & 1 = 1 & 1 = 1
print(3 ^ 2 & 1)
# '<<', '>>'
# OK: 2 & (3 << 1) = 2 & 6 = 2
# BAD: (2 & 3) << 1 = 2 << 1 = 4
print(2 & 3 << 1)
# OK: 6 & (4 >> 1) = 6 & 2 = 2
# BAD: (6 & 4) >> 1 = 2 >> 1 = 1
print(6 & 4 >> 1)
# '+', '-'
# OK: 1 << (1 + 1) = 1 << 2 = 4
# BAD: (1 << 1) + 1 = 2 + 1 = 3
print(1 << 1 + 1)
# '*', '/', '//', '%'
# OK: 2 + (2 * 2) = 2 + 4 = 6
# BAD: (2 + 2) * 2 = 4 * 2 = 8
print(2 + 2 * 2)
# '+x', '-x', '~x'
# '**'
# OK: -(2**2) = -4
# BAD: (-2)**2 = 4
print(-2**2)
# OK: 2**(-1) = 0.5
print(2**-1)
# (expr...)
print((2 + 2) * 2)
# MicroPython doesn't support tuple argument
try:
"foobar".endswith(("bar", "sth"))
except TypeError:
print("TypeError")
# MicroPython doesn't support tuple argument
try:
"foobar".startswith(("foo", "sth"))
except TypeError:
print("TypeError")
# test containment operator on subclass of a native type
class mylist(list):
pass
class mydict(dict):
pass
class mybytes(bytes):
pass
l = mylist([1, 2, 3])
print(0 in l)
print(1 in l)
d = mydict({1:1, 2:2})
print(0 in l)
print(1 in l)
b = mybytes(b'1234')
print(0 in b)
print(b'1' in b)
[2, 3]
A.__init__
B.__init__
D.__init__
D.__init__
D.foo
[0, 1, 2, 3, 4]
[0, 1]
# test subclassing a native type and overriding __init__
# overriding list.__init__()
class L(list):
def __init__(self, a, b):
super().__init__([a, b])
print(L(2, 3))
# inherits implicitly from object
class A:
def __init__(self):
print("A.__init__")
super().__init__()
A()
# inherits explicitly from object
class B(object):
def __init__(self):
print("B.__init__")
super().__init__()
B()
# multiple inheritance with object explicitly mentioned
class C:
pass
class D(C, object):
def __init__(self):
print('D.__init__')
super().__init__()
def reinit(self):
print('D.foo')
super().__init__()
a = D()
a.__init__()
a.reinit()
# call __init__() after object is already init'd
class L(list):
def reinit(self):
super().__init__(range(2))
a = L(range(5))
print(a)
a.reinit()
print(a)
......@@ -4,11 +4,21 @@ import sys
print(sys.__name__)
print(sys.byteorder in ('little', 'big'))
print(sys.maxsize > 100)
print(sys.implementation.name in ('cpython', 'micropython'))
try:
sys.exit()
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:
raise SystemExit
except SystemExit as e:
print("SystemExit", e.args)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment