diff options
Diffstat (limited to 'tests/basics')
-rw-r--r-- | tests/basics/fun_calldblstar3.py | 16 | ||||
-rw-r--r-- | tests/basics/int_big_div.py | 7 | ||||
-rw-r--r-- | tests/basics/int_big_mod.py | 11 | ||||
-rw-r--r-- | tests/basics/memoryview2.py | 13 | ||||
-rw-r--r-- | tests/basics/namedtuple1.py | 5 | ||||
-rw-r--r-- | tests/basics/string_center.py | 14 | ||||
-rw-r--r-- | tests/basics/string_splitlines.py | 29 | ||||
-rw-r--r-- | tests/basics/struct1.py | 6 | ||||
-rw-r--r-- | tests/basics/struct2.py | 28 |
9 files changed, 125 insertions, 4 deletions
diff --git a/tests/basics/fun_calldblstar3.py b/tests/basics/fun_calldblstar3.py new file mode 100644 index 0000000000..4367e68df7 --- /dev/null +++ b/tests/basics/fun_calldblstar3.py @@ -0,0 +1,16 @@ +# test passing a user-defined mapping as the argument to ** + +def foo(**kw): + print(sorted(kw.items())) + +class Mapping: + def keys(self): + return ['a', 'b', 'c'] + + def __getitem__(self, key): + if key == 'a': + return 1 + else: + return 2 + +foo(**Mapping()) diff --git a/tests/basics/int_big_div.py b/tests/basics/int_big_div.py index 8dacf495db..642f051d41 100644 --- a/tests/basics/int_big_div.py +++ b/tests/basics/int_big_div.py @@ -1,3 +1,10 @@ for lhs in (1000000000000000000000000, 10000000000100000000000000, 10012003400000000000000007, 12349083434598210349871029923874109871234789): for rhs in range(1, 555): print(lhs // rhs) + +# these check an edge case on 64-bit machines where two mpz limbs +# are used and the most significant one has the MSB set +x = 0x8000000000000000 +print((x + 1) // x) +x = 0x86c60128feff5330 +print((x + 1) // x) diff --git a/tests/basics/int_big_mod.py b/tests/basics/int_big_mod.py index 77c0ffc468..e87221c1c7 100644 --- a/tests/basics/int_big_mod.py +++ b/tests/basics/int_big_mod.py @@ -4,7 +4,14 @@ delta = 100000000000000000000000000000012345 for i in range(11): for j in range(11): - x = delta * (i)# - 5) # TODO reinstate negative number test when % is working with sign correctly - y = delta * (j)# - 5) # TODO reinstate negative number test when % is working with sign correctly + x = delta * (i - 5) + y = delta * (j - 5) if y != 0: print(x % y) + +# these check an edge case on 64-bit machines where two mpz limbs +# are used and the most significant one has the MSB set +x = 0x8000000000000000 +print((x + 1) % x) +x = 0x86c60128feff5330 +print((x + 1) % x) diff --git a/tests/basics/memoryview2.py b/tests/basics/memoryview2.py new file mode 100644 index 0000000000..5117d7a680 --- /dev/null +++ b/tests/basics/memoryview2.py @@ -0,0 +1,13 @@ +# test memoryview accessing maximum values for signed/unsigned elements + +from array import array + +print(list(memoryview(b'\x7f\x80\x81\xff'))) +print(list(memoryview(array('b', [0x7f, -0x80])))) +print(list(memoryview(array('B', [0x7f, 0x80, 0x81, 0xff])))) +print(list(memoryview(array('h', [0x7f00, -0x8000])))) +print(list(memoryview(array('H', [0x7f00, 0x8000, 0x8100, 0xffff])))) + +# these constructors give an internal overflow in uPy +#print(list(memoryview(array('i', [0x7f000000, -0x80000000])))) +#print(list(memoryview(array('I', [0x7f000000, 0x80000000, 0x81000000, 0xffffffff])))) diff --git a/tests/basics/namedtuple1.py b/tests/basics/namedtuple1.py index dfbb79f2eb..346e32fbfc 100644 --- a/tests/basics/namedtuple1.py +++ b/tests/basics/namedtuple1.py @@ -66,6 +66,11 @@ T3 = namedtuple("TupComma", "foo bar") t = T3(1, 2) print(t.foo, t.bar) +# Try tuple +T4 = namedtuple("TupTuple", ("foo", "bar")) +t = T4(1, 2) +print(t.foo, t.bar) + # Try single string with comma field seperator # Not implemented so far #T2 = namedtuple("TupComma", "foo,bar") diff --git a/tests/basics/string_center.py b/tests/basics/string_center.py new file mode 100644 index 0000000000..a2739201ac --- /dev/null +++ b/tests/basics/string_center.py @@ -0,0 +1,14 @@ +try: + str.center +except: + import sys + print("SKIP") + sys.exit() + +print("foo".center(0)) +print("foo".center(1)) +print("foo".center(3)) +print("foo".center(4)) +print("foo".center(5)) +print("foo".center(6)) +print("foo".center(20)) diff --git a/tests/basics/string_splitlines.py b/tests/basics/string_splitlines.py index cb4dacef9f..1d08f6e6d7 100644 --- a/tests/basics/string_splitlines.py +++ b/tests/basics/string_splitlines.py @@ -1,3 +1,5 @@ +# test string.splitlines() method + try: str.splitlines except: @@ -5,9 +7,32 @@ except: print("SKIP") sys.exit() +# test \n as newline print("foo\nbar".splitlines()) print("foo\nbar\n".splitlines()) +print("foo and\nbar\n".splitlines()) +print("foo\nbar\n\n".splitlines()) +print("foo\n\nbar\n\n".splitlines()) +print("\nfoo\nbar\n".splitlines()) + +# test \r as newline +print("foo\rbar\r".splitlines()) +print("\rfoo and\r\rbar\r".splitlines()) + +# test \r\n as newline +print("foo\r\nbar\r\n".splitlines()) +print("\r\nfoo and\r\n\r\nbar\r\n".splitlines()) + +# test keepends arg print("foo\nbar".splitlines(True)) print("foo\nbar\n".splitlines(True)) -print("foo\nbar".splitlines(keepends=True)) -print("foo\nbar\n".splitlines(keepends=True)) +print("foo\nbar\n\n".splitlines(True)) +print("foo\rbar".splitlines(keepends=True)) +print("foo\rbar\r\r".splitlines(keepends=True)) +print("foo\r\nbar".splitlines(keepends=True)) +print("foo\r\nbar\r\n\r\n".splitlines(keepends=True)) + +# test splitting bytes objects +print(b"foo\nbar".splitlines()) +print(b"foo\nbar\n".splitlines()) +print(b"foo\r\nbar\r\n\r\n".splitlines(True)) diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py index 1afea5a96f..857e171c10 100644 --- a/tests/basics/struct1.py +++ b/tests/basics/struct1.py @@ -62,6 +62,12 @@ try: except TypeError: print('TypeError') +# make sure that unknown types are detected +try: + struct.pack("z", 1) +except: + print("Unknown type") + # Initially repitition counters were supported only for strings, # but later were implemented for all. print(struct.unpack("<3B2h", b"foo\x12\x34\xff\xff")) diff --git a/tests/basics/struct2.py b/tests/basics/struct2.py new file mode 100644 index 0000000000..f438bb55d2 --- /dev/null +++ b/tests/basics/struct2.py @@ -0,0 +1,28 @@ +# test ustruct with a count specified before the type + +try: + import ustruct as struct +except: + import struct + +print(struct.calcsize('0s')) +print(struct.unpack('0s', b'')) +print(struct.pack('0s', b'123')) + +print(struct.calcsize('2s')) +print(struct.unpack('2s', b'12')) +print(struct.pack('2s', b'123')) + +print(struct.calcsize('2H')) +print(struct.unpack('<2H', b'1234')) +print(struct.pack('<2H', 258, 515)) + +print(struct.calcsize('0s1s0H2H')) +print(struct.unpack('<0s1s0H2H', b'01234')) +print(struct.pack('<0s1s0H2H', b'abc', b'abc', 258, 515)) + +# check that zero of an unknown type raises an exception +try: + struct.calcsize('0z') +except: + print('Exception') |