diff options
| author | Dan Halbert <halbert@halwitz.org> | 2017-08-25 22:17:07 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2017-08-25 22:17:07 -0400 |
| commit | ef61b5ecb59e9b4e37e3e3c023c62d583c23eb16 (patch) | |
| tree | 45a798fbed0dc673304597b29e0b60174195ce79 /tests/basics | |
| parent | 266be307770abe11c220eb493388807920914b7a (diff) | |
| parent | 1f78e7a43130acfa4bedf16c1007a1b0f37c75c3 (diff) | |
Initial merge of micropython v1.9.2 into circuitpython 2.0.0 (in development) master.
cpx build compiles and loads and works in repl; test suite not run yet
esp8266 not tested yet
Diffstat (limited to 'tests/basics')
| -rw-r--r-- | tests/basics/builtin_exec.py | 32 | ||||
| -rw-r--r-- | tests/basics/builtin_hash_gen.py | 7 | ||||
| -rw-r--r-- | tests/basics/bytearray_slice_assign.py | 2 | ||||
| -rw-r--r-- | tests/basics/containment.py | 11 | ||||
| -rw-r--r-- | tests/basics/exec1.py | 6 | ||||
| -rw-r--r-- | tests/basics/for_else.py | 43 | ||||
| -rw-r--r-- | tests/basics/int_bytes.py | 12 | ||||
| -rw-r--r-- | tests/basics/int_bytes_intbig.py | 2 | ||||
| -rw-r--r-- | tests/basics/int_bytes_notimpl.py | 4 | ||||
| -rw-r--r-- | tests/basics/int_bytes_notimpl.py.exp | 1 | ||||
| -rw-r--r-- | tests/basics/list_slice_assign.py | 2 | ||||
| -rw-r--r-- | tests/basics/namedtuple1.py | 3 | ||||
| -rw-r--r-- | tests/basics/op_precedence.py | 43 | ||||
| -rw-r--r-- | tests/basics/python34.py | 2 | ||||
| -rw-r--r-- | tests/basics/python34.py.exp | 2 | ||||
| -rw-r--r-- | tests/basics/struct1_intbig.py | 2 | ||||
| -rw-r--r-- | tests/basics/struct2.py | 27 | ||||
| -rw-r--r-- | tests/basics/tuple_mult.py | 5 |
18 files changed, 193 insertions, 13 deletions
diff --git a/tests/basics/builtin_exec.py b/tests/basics/builtin_exec.py new file mode 100644 index 000000000..fd4e65c53 --- /dev/null +++ b/tests/basics/builtin_exec.py @@ -0,0 +1,32 @@ +print(exec("def foo(): return 42")) +print(foo()) + +d = {} +exec("def bar(): return 84", d) +print(d["bar"]()) + +# passing None/dict as args to globals/locals +foo = 11 +exec('print(foo)') +exec('print(foo)', None) +exec('print(foo)', {'foo':3}, None) +exec('print(foo)', None, {'foo':3}) +exec('print(foo)', None, {'bar':3}) +exec('print(foo)', {'bar':3}, locals()) + +try: + exec('print(foo)', {'bar':3}, None) +except NameError: + print('NameError') + +# invalid arg passed to globals +try: + exec('print(1)', 'foo') +except TypeError: + print('TypeError') + +# invalid arg passed to locals +try: + exec('print(1)', None, 123) +except TypeError: + print('TypeError') diff --git a/tests/basics/builtin_hash_gen.py b/tests/basics/builtin_hash_gen.py new file mode 100644 index 000000000..d42e5ebfb --- /dev/null +++ b/tests/basics/builtin_hash_gen.py @@ -0,0 +1,7 @@ +# test builtin hash function, on generators + +def gen(): + yield + +print(type(hash(gen))) +print(type(hash(gen()))) diff --git a/tests/basics/bytearray_slice_assign.py b/tests/basics/bytearray_slice_assign.py index 48f5938a5..7f7d1d119 100644 --- a/tests/basics/bytearray_slice_assign.py +++ b/tests/basics/bytearray_slice_assign.py @@ -4,7 +4,7 @@ except TypeError: print("SKIP") raise SystemExit -# test slices; only 2 argument version supported by Micro Python at the moment +# test slices; only 2 argument version supported by MicroPython at the moment x = bytearray(range(10)) # Assignment diff --git a/tests/basics/containment.py b/tests/basics/containment.py index bae366113..4c94a9bae 100644 --- a/tests/basics/containment.py +++ b/tests/basics/containment.py @@ -16,6 +16,17 @@ for needle in [haystack[:i+1] for i in range(len(haystack))]: print(haystack, "in", needle, "::", haystack in needle) print(haystack, "not in", needle, "::", haystack not in needle) +# containment of bytes/ints in bytes +print(b'' in b'123') +print(b'0' in b'123', b'1' in b'123') +print(48 in b'123', 49 in b'123') + +# containment of int in str is an error +try: + 1 in '123' +except TypeError: + print('TypeError') + # until here, the tests would work without the 'second attempt' iteration thing. for i in 1, 2: diff --git a/tests/basics/exec1.py b/tests/basics/exec1.py deleted file mode 100644 index 59de5d69a..000000000 --- a/tests/basics/exec1.py +++ /dev/null @@ -1,6 +0,0 @@ -print(exec("def foo(): return 42")) -print(foo()) - -d = {} -exec("def bar(): return 84", d) -print(d["bar"]()) diff --git a/tests/basics/for_else.py b/tests/basics/for_else.py new file mode 100644 index 000000000..0bb941506 --- /dev/null +++ b/tests/basics/for_else.py @@ -0,0 +1,43 @@ +# 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') diff --git a/tests/basics/int_bytes.py b/tests/basics/int_bytes.py index a96a4e65f..059c16d3f 100644 --- a/tests/basics/int_bytes.py +++ b/tests/basics/int_bytes.py @@ -5,3 +5,15 @@ print((100).to_bytes(10, "little")) # check that extra zero bytes don't change the internal int value print(int.from_bytes(bytes(20), "little") == 0) print(int.from_bytes(b"\x01" + bytes(20), "little") == 1) + +# big-endian conversion +print((10).to_bytes(1, "big")) +print((100).to_bytes(10, "big")) +print(int.from_bytes(b"\0\0\0\0\0\0\0\0\0\x01", "big")) +print(int.from_bytes(b"\x01\0", "big")) + +# negative number of bytes should raise an error +try: + (1).to_bytes(-1, "little") +except ValueError: + print("ValueError") diff --git a/tests/basics/int_bytes_intbig.py b/tests/basics/int_bytes_intbig.py index 7c8a7f4af..ce6fec40a 100644 --- a/tests/basics/int_bytes_intbig.py +++ b/tests/basics/int_bytes_intbig.py @@ -2,6 +2,7 @@ import skip_if skip_if.no_bigint() print((2**64).to_bytes(9, "little")) +print((2**64).to_bytes(9, "big")) b = bytes(range(20)) @@ -10,6 +11,7 @@ ib = int.from_bytes(b, "big") print(il) print(ib) print(il.to_bytes(20, "little")) +print(ib.to_bytes(20, "big")) # check that extra zero bytes don't change the internal int value print(int.from_bytes(b + bytes(10), "little") == int.from_bytes(b, "little")) diff --git a/tests/basics/int_bytes_notimpl.py b/tests/basics/int_bytes_notimpl.py deleted file mode 100644 index b149f4496..000000000 --- a/tests/basics/int_bytes_notimpl.py +++ /dev/null @@ -1,4 +0,0 @@ -try: - print((10).to_bytes(1, "big")) -except Exception as e: - print(type(e)) diff --git a/tests/basics/int_bytes_notimpl.py.exp b/tests/basics/int_bytes_notimpl.py.exp deleted file mode 100644 index 606649a69..000000000 --- a/tests/basics/int_bytes_notimpl.py.exp +++ /dev/null @@ -1 +0,0 @@ -<class 'NotImplementedError'> diff --git a/tests/basics/list_slice_assign.py b/tests/basics/list_slice_assign.py index 1ad1ef27c..885615717 100644 --- a/tests/basics/list_slice_assign.py +++ b/tests/basics/list_slice_assign.py @@ -1,4 +1,4 @@ -# test slices; only 2 argument version supported by Micro Python at the moment +# test slices; only 2 argument version supported by MicroPython at the moment x = list(range(10)) # Assignment diff --git a/tests/basics/namedtuple1.py b/tests/basics/namedtuple1.py index 56c29ddda..1b176b232 100644 --- a/tests/basics/namedtuple1.py +++ b/tests/basics/namedtuple1.py @@ -24,6 +24,9 @@ for t in T(1, 2), T(bar=1, foo=2): print(isinstance(t, tuple)) +# Create using positional and keyword args +print(T(3, bar=4)) + try: t[0] = 200 except TypeError: diff --git a/tests/basics/op_precedence.py b/tests/basics/op_precedence.py new file mode 100644 index 000000000..519a2a113 --- /dev/null +++ b/tests/basics/op_precedence.py @@ -0,0 +1,43 @@ +# 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) diff --git a/tests/basics/python34.py b/tests/basics/python34.py index a23f347d6..d5cc59ad6 100644 --- a/tests/basics/python34.py +++ b/tests/basics/python34.py @@ -20,6 +20,8 @@ def test_syntax(code): print("SyntaxError") test_syntax("f(*a, *b)") # can't have multiple * (in 3.5 we can) test_syntax("f(**a, **b)") # can't have multiple ** (in 3.5 we can) +test_syntax("f(*a, b)") # can't have positional after * +test_syntax("f(**a, b)") # can't have positional after ** test_syntax("() = []") # can't assign to empty tuple (in 3.6 we can) test_syntax("del ()") # can't delete empty tuple (in 3.6 we can) diff --git a/tests/basics/python34.py.exp b/tests/basics/python34.py.exp index f497df3b8..590fc364f 100644 --- a/tests/basics/python34.py.exp +++ b/tests/basics/python34.py.exp @@ -7,5 +7,7 @@ SyntaxError SyntaxError SyntaxError SyntaxError +SyntaxError +SyntaxError 3.4 3 4 diff --git a/tests/basics/struct1_intbig.py b/tests/basics/struct1_intbig.py index b1fec527e..380293f36 100644 --- a/tests/basics/struct1_intbig.py +++ b/tests/basics/struct1_intbig.py @@ -12,6 +12,8 @@ print(struct.pack("<I", 2**32 - 1)) print(struct.pack("<I", 0xffffffff)) # long long ints +print(struct.pack("<Q", 1)) +print(struct.pack(">Q", 1)) print(struct.pack("<Q", 2**64 - 1)) print(struct.pack(">Q", 2**64 - 1)) print(struct.pack("<Q", 0xffffffffffffffff)) diff --git a/tests/basics/struct2.py b/tests/basics/struct2.py index d8234d0d3..3b9dd5c1f 100644 --- a/tests/basics/struct2.py +++ b/tests/basics/struct2.py @@ -40,3 +40,30 @@ try: struct.calcsize('0z') except: print('Exception') + +# check that a count without a type specifier raises an exception + +try: + struct.calcsize('1') +except: + print('Exception') + +try: + struct.pack('1') +except: + print('Exception') + +try: + struct.pack_into('1', bytearray(4), 0, 'xx') +except: + print('Exception') + +try: + struct.unpack('1', 'xx') +except: + print('Exception') + +try: + struct.unpack_from('1', 'xx') +except: + print('Exception') diff --git a/tests/basics/tuple_mult.py b/tests/basics/tuple_mult.py index b128b2968..cac95185a 100644 --- a/tests/basics/tuple_mult.py +++ b/tests/basics/tuple_mult.py @@ -11,6 +11,11 @@ a = (1, 2, 3) c = a * 3 print(a, c) +# inplace multiplication +a = (1, 2) +a *= 2 +print(a) + # unsupported type on RHS try: () * None |
