From af43565322f5c45a1a92df1afceff24c93021998 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 12 Mar 2015 22:47:44 +0000 Subject: [PATCH] tests: Add tests for things that are not already tested. The aim here is to improve coverage of the code. --- tests/basics/assign1.py | 11 +++++++++ tests/basics/decorator.py | 24 +++++++++++++++++++ tests/basics/del_global.py | 38 ++++++++++++++++++++++++++++++ tests/basics/del_subscr.py | 5 ++++ tests/basics/ifcond.py | 24 +++++++++++++++++++ tests/basics/int_small.py | 11 +++++++++ tests/basics/return1.py | 13 ++++++++++ tests/basics/seq_unpack.py | 7 ++++++ tests/basics/string_join.py | 3 +++ tests/basics/unpack1.py | 18 ++++++++++++++ tests/basics/while_cond.py | 18 ++++++++++++++ tests/float/float1.py | 5 ++++ tests/micropython/decorator.py | 7 ++++++ tests/micropython/decorator.py.exp | 1 + 14 files changed, 185 insertions(+) create mode 100644 tests/basics/assign1.py create mode 100644 tests/basics/decorator.py create mode 100644 tests/basics/return1.py create mode 100644 tests/micropython/decorator.py create mode 100644 tests/micropython/decorator.py.exp diff --git a/tests/basics/assign1.py b/tests/basics/assign1.py new file mode 100644 index 0000000000..118747f74a --- /dev/null +++ b/tests/basics/assign1.py @@ -0,0 +1,11 @@ +# test assignments + +a = 1 +print(a) + +a = b = 2 +print(a, b) + +a = b = c = 3 +print(a, b, c) + diff --git a/tests/basics/decorator.py b/tests/basics/decorator.py new file mode 100644 index 0000000000..deb341d709 --- /dev/null +++ b/tests/basics/decorator.py @@ -0,0 +1,24 @@ +# test decorators + +def dec(f): + print('dec') + return f + +def dec_arg(x): + print(x) + return lambda f:f + +# plain decorator +@dec +def f(): + pass + +# decorator with arg +@dec_arg('dec_arg') +def g(): + pass + +# decorator of class +@dec +class A: + pass diff --git a/tests/basics/del_global.py b/tests/basics/del_global.py index 24ecec8e84..77d11cb3c4 100644 --- a/tests/basics/del_global.py +++ b/tests/basics/del_global.py @@ -16,3 +16,41 @@ try: except: # NameError: # FIXME uPy returns KeyError for this print("NameError") + +# delete globals using a list + +a = 1 +del (a,) +try: + print(a) +except NameError: + print("NameError") + +a = 2 +b = 3 +del (a, b) +try: + print(a) +except NameError: + print("NameError") +try: + print(b) +except NameError: + print("NameError") + +a = 1 +b = 2 +c = 3 +del (a, b, c) +try: + print(a) +except NameError: + print("NameError") +try: + print(b) +except NameError: + print("NameError") +try: + print(c) +except NameError: + print("NameError") diff --git a/tests/basics/del_subscr.py b/tests/basics/del_subscr.py index 67910c6234..0e66cf173a 100644 --- a/tests/basics/del_subscr.py +++ b/tests/basics/del_subscr.py @@ -11,3 +11,8 @@ del d[3] print(d) del d[5] print(d) + +# delete nested subscr +d = {0:{0:0}} +del d[0][0] +print(d) diff --git a/tests/basics/ifcond.py b/tests/basics/ifcond.py index 911440efa2..9264aa74dd 100644 --- a/tests/basics/ifcond.py +++ b/tests/basics/ifcond.py @@ -33,6 +33,30 @@ elif 0: else: print(17) +if not False: + print('a') + +if not True: + print('a') +else: + print('b') + +if False: + print('a') +else: + print('b') + +if True: + print('a') + +if (1,): + print('a') + +if not (1,): + print('a') +else: + print('b') + f2 = 0 def f(t1, t2, f1): diff --git a/tests/basics/int_small.py b/tests/basics/int_small.py index 496e830d26..4d47104370 100644 --- a/tests/basics/int_small.py +++ b/tests/basics/int_small.py @@ -24,6 +24,17 @@ print(-1073741823) # Operations tests +# compile-time constexprs +print(1 + 3) +print(3 - 2) +print(2 * 3) +print(1 & 3) +print(1 | 2) +print(1 ^ 3) +print(+3) +print(-3) +print(~3) + a = 0x3fffff print(a) a *= 0x10 diff --git a/tests/basics/return1.py b/tests/basics/return1.py new file mode 100644 index 0000000000..7209d87ce8 --- /dev/null +++ b/tests/basics/return1.py @@ -0,0 +1,13 @@ +# test return statement + +def f(): + return +print(f()) + +def g(): + return 1 +print(g()) + +def f(x): + return 1 if x else 2 +print(f(0), f(1)) diff --git a/tests/basics/seq_unpack.py b/tests/basics/seq_unpack.py index af8b998d05..084ddb9f13 100644 --- a/tests/basics/seq_unpack.py +++ b/tests/basics/seq_unpack.py @@ -14,6 +14,13 @@ print(a, b) [a, b] = 100, 200 print(a, b) +# optimised 3-way swap +a = 1 +b = 2 +c = 3 +a, b, c = b, c, a +print(a, b, c) + try: a, b, c = (1, 2) except ValueError: diff --git a/tests/basics/string_join.py b/tests/basics/string_join.py index 49bbfc5ca0..b8694c01e0 100644 --- a/tests/basics/string_join.py +++ b/tests/basics/string_join.py @@ -22,3 +22,6 @@ try: print(','.join([b'abc', b'123'])) except TypeError: print("TypeError") + +# joined by the compiler +print("a" "b") diff --git a/tests/basics/unpack1.py b/tests/basics/unpack1.py index b2b2ddb7e4..10e01dea06 100644 --- a/tests/basics/unpack1.py +++ b/tests/basics/unpack1.py @@ -2,8 +2,23 @@ a, = 1, ; print(a) a, b = 2, 3 ; print(a, b) +a, b, c = 1, 2, 3; print(a, b, c) +a, = range(1); print(a) a, b = range(2); print(a, b) +a, b, c = range(3); print(a, b, c) + +(a) = range(1); print(a) +(a,) = range(1); print(a) +(a, b) = range(2); print(a, b) +(a, b, c) = range(3); print(a, b, c) + +# lists + +[] = [] +[a] = range(1); print(a) +[a, b] = range(2); print(a, b) +[a, b, c] = range(3); print(a, b, c) # with star @@ -27,6 +42,9 @@ a = [28, 29] *b, = a print(a, b, a == b) +[*a] = [1, 2, 3] +print(a) + try: a, *b, c = (30,) except ValueError: diff --git a/tests/basics/while_cond.py b/tests/basics/while_cond.py index 449c5b66ea..eccc7a638b 100644 --- a/tests/basics/while_cond.py +++ b/tests/basics/while_cond.py @@ -16,3 +16,21 @@ while 2: while -1: print(4) break + +while False: + print('a') +else: + print('b') + +while True: + print('a') + break + +while not False: + print('a') + break + +while not True: + print('a') +else: + print('b') diff --git a/tests/float/float1.py b/tests/float/float1.py index 5971e6d0a7..7b5887ff0b 100644 --- a/tests/float/float1.py +++ b/tests/float/float1.py @@ -2,6 +2,11 @@ x = 1 / 2 print(x) +# /= operator +a = 1 +a /= 2 +print(a) + print(1.0 // 2) print(2.0 // 2) diff --git a/tests/micropython/decorator.py b/tests/micropython/decorator.py new file mode 100644 index 0000000000..bf688968a0 --- /dev/null +++ b/tests/micropython/decorator.py @@ -0,0 +1,7 @@ +# test micropython-specific decorators + +@micropython.bytecode +def f(): + return 'bytecode' + +print(f()) diff --git a/tests/micropython/decorator.py.exp b/tests/micropython/decorator.py.exp new file mode 100644 index 0000000000..b37707e7c1 --- /dev/null +++ b/tests/micropython/decorator.py.exp @@ -0,0 +1 @@ +bytecode