diff options
Diffstat (limited to 'tests/extmod')
27 files changed, 499 insertions, 4 deletions
diff --git a/tests/extmod/btree1.py b/tests/extmod/btree1.py index 715f628246..2127554db4 100644 --- a/tests/extmod/btree1.py +++ b/tests/extmod/btree1.py @@ -85,5 +85,6 @@ try: except TypeError: print("TypeError") +db.flush() db.close() f.close() diff --git a/tests/extmod/framebuf1.py b/tests/extmod/framebuf1.py index 52899028c7..cdc7e5b188 100644 --- a/tests/extmod/framebuf1.py +++ b/tests/extmod/framebuf1.py @@ -8,7 +8,10 @@ except ImportError: w = 5 h = 16 buf = bytearray(w * h // 8) -fbuf = framebuf.FrameBuffer1(buf, w, h, w) +fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.MVLSB) + +# access as buffer +print(memoryview(fbuf)[0]) # fill fbuf.fill(1) @@ -30,6 +33,36 @@ print(buf) # get pixel print(fbuf.pixel(0, 0), fbuf.pixel(1, 1)) +# hline +fbuf.fill(0) +fbuf.hline(0, 1, w, 1) +print('hline', buf) + +# vline +fbuf.fill(0) +fbuf.vline(1, 0, h, 1) +print('vline', buf) + +# rect +fbuf.fill(0) +fbuf.rect(1, 1, 3, 3, 1) +print('rect', buf) + +#fill rect +fbuf.fill(0) +fbuf.fill_rect(1, 1, 3, 3, 1) +print('fill_rect', buf) + +# line +fbuf.fill(0) +fbuf.line(1, 1, 3, 3, 1) +print('line', buf) + +# line steep negative gradient +fbuf.fill(0) +fbuf.line(3, 3, 2, 1, 1) +print('line', buf) + # scroll fbuf.fill(0) fbuf.pixel(2, 7, 1) @@ -54,3 +87,13 @@ print(buf) # char out of font range set to chr(127) fbuf.text(str(chr(31)), 0, 0) print(buf) + +# test invalid constructor +try: + fbuf = framebuf.FrameBuffer(buf, w, h, 2, framebuf.MVLSB) +except ValueError: + print("ValueError") + +# test legacy constructor +fbuf = framebuf.FrameBuffer1(buf, w, h) +fbuf = framebuf.FrameBuffer1(buf, w, h, w) diff --git a/tests/extmod/framebuf1.py.exp b/tests/extmod/framebuf1.py.exp index 1577faac8a..83d775d3c4 100644 --- a/tests/extmod/framebuf1.py.exp +++ b/tests/extmod/framebuf1.py.exp @@ -1,8 +1,15 @@ +0 bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') bytearray(b'\x01\x00\x00\x00\x01\x80\x00\x00\x00\x80') bytearray(b'\x01\x00\x00\x00\x01\x80\x00\x00\x00\x00') 1 0 +hline bytearray(b'\x02\x02\x02\x02\x02\x00\x00\x00\x00\x00') +vline bytearray(b'\x00\xff\x00\x00\x00\x00\xff\x00\x00\x00') +rect bytearray(b'\x00\x0e\n\x0e\x00\x00\x00\x00\x00\x00') +fill_rect bytearray(b'\x00\x0e\x0e\x0e\x00\x00\x00\x00\x00\x00') +line bytearray(b'\x00\x02\x04\x08\x00\x00\x00\x00\x00\x00') +line bytearray(b'\x00\x00\x06\x08\x00\x00\x00\x00\x00\x00') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00') bytearray(b'\x00\x00@\x00\x00\x00\x00\x00\x00\x00') bytearray(b'\x00\x00\x00@\x00\x00\x00\x00\x00\x00') @@ -11,3 +18,4 @@ bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01') bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00') +ValueError diff --git a/tests/extmod/framebuf16.py b/tests/extmod/framebuf16.py new file mode 100644 index 0000000000..3aa1d34def --- /dev/null +++ b/tests/extmod/framebuf16.py @@ -0,0 +1,60 @@ +try: + import framebuf +except ImportError: + print("SKIP") + import sys + sys.exit() + +def printbuf(): + print("--8<--") + for y in range(h): + print(buf[y * w * 2:(y + 1) * w * 2]) + print("-->8--") + +w = 4 +h = 5 +buf = bytearray(w * h * 2) +fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.RGB565) + +# fill +fbuf.fill(0xffff) +printbuf() +fbuf.fill(0x0000) +printbuf() + +# put pixel +fbuf.pixel(0, 0, 0xeeee) +fbuf.pixel(3, 0, 0xee00) +fbuf.pixel(0, 4, 0x00ee) +fbuf.pixel(3, 4, 0x0ee0) +printbuf() + +# get pixel +print(fbuf.pixel(0, 4), fbuf.pixel(1, 1)) + +# scroll +fbuf.fill(0x0000) +fbuf.pixel(2, 2, 0xffff) +printbuf() +fbuf.scroll(0, 1) +printbuf() +fbuf.scroll(1, 0) +printbuf() +fbuf.scroll(-1, -2) +printbuf() + +w2 = 2 +h2 = 3 +buf2 = bytearray(w2 * h2 * 2) +fbuf2 = framebuf.FrameBuffer(buf2, w2, h2, framebuf.RGB565) + +fbuf2.fill(0x0000) +fbuf2.pixel(0, 0, 0x0ee0) +fbuf2.pixel(0, 2, 0xee00) +fbuf2.pixel(1, 0, 0x00ee) +fbuf2.pixel(1, 2, 0xe00e) +fbuf.fill(0xffff) +fbuf.blit(fbuf2, 3, 3, 0x0000) +fbuf.blit(fbuf2, -1, -1, 0x0000) +fbuf.blit(fbuf2, 16, 16, 0x0000) +printbuf() diff --git a/tests/extmod/framebuf16.py.exp b/tests/extmod/framebuf16.py.exp new file mode 100644 index 0000000000..c41dc19d07 --- /dev/null +++ b/tests/extmod/framebuf16.py.exp @@ -0,0 +1,57 @@ +--8<-- +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +-->8-- +--8<-- +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +-->8-- +--8<-- +bytearray(b'\xee\xee\x00\x00\x00\x00\x00\xee') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\xee\x00\x00\x00\x00\x00\xe0\x0e') +-->8-- +238 0 +--8<-- +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\xff\xff\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +-->8-- +--8<-- +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\xff\xff\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +-->8-- +--8<-- +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\xff\xff') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +-->8-- +--8<-- +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\xff\xff\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\xff\xff') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00') +-->8-- +--8<-- +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +bytearray(b'\x0e\xe0\xff\xff\xff\xff\xff\xff') +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +bytearray(b'\xff\xff\xff\xff\xff\xff\xe0\x0e') +bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff') +-->8-- diff --git a/tests/extmod/ubinascii_a2b_base64.py b/tests/extmod/ubinascii_a2b_base64.py index 25986b7c0a..97c4519508 100644 --- a/tests/extmod/ubinascii_a2b_base64.py +++ b/tests/extmod/ubinascii_a2b_base64.py @@ -14,6 +14,7 @@ print(binascii.a2b_base64(b'Zm9vYmFy')) print(binascii.a2b_base64(b'AAECAwQFBgc=')) print(binascii.a2b_base64(b'CAkKCwwNDg8=')) print(binascii.a2b_base64(b'f4D/')) +print(binascii.a2b_base64(b'f4D+')) # convert '+' print(binascii.a2b_base64(b'MTIzNEFCQ0RhYmNk')) try: diff --git a/tests/extmod/ubinascii_b2a_base64.py b/tests/extmod/ubinascii_b2a_base64.py index 343c28da22..fdcaf32dd8 100644 --- a/tests/extmod/ubinascii_b2a_base64.py +++ b/tests/extmod/ubinascii_b2a_base64.py @@ -15,3 +15,4 @@ print(binascii.b2a_base64(b'\x00\x01\x02\x03\x04\x05\x06\x07')) print(binascii.b2a_base64(b'\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f')) print(binascii.b2a_base64(b'\x7f\x80\xff')) print(binascii.b2a_base64(b'1234ABCDabcd')) +print(binascii.b2a_base64(b'\x00\x00>')) # convert into '+' diff --git a/tests/extmod/ubinascii_micropython.py b/tests/extmod/ubinascii_micropython.py new file mode 100644 index 0000000000..d68da3205d --- /dev/null +++ b/tests/extmod/ubinascii_micropython.py @@ -0,0 +1,8 @@ +try: + import ubinascii as binascii +except ImportError: + import binascii + +# two arguments supported in uPy but not CPython +a = binascii.hexlify(b'123', ':') +print(a) diff --git a/tests/extmod/ubinascii_micropython.py.exp b/tests/extmod/ubinascii_micropython.py.exp new file mode 100644 index 0000000000..0ae3d768ef --- /dev/null +++ b/tests/extmod/ubinascii_micropython.py.exp @@ -0,0 +1 @@ +b'31:32:33' diff --git a/tests/extmod/ubinascii_unhexlify.py b/tests/extmod/ubinascii_unhexlify.py index 1ec6972c5b..99c2c0208d 100644 --- a/tests/extmod/ubinascii_unhexlify.py +++ b/tests/extmod/ubinascii_unhexlify.py @@ -7,3 +7,13 @@ print(binascii.unhexlify(b'0001020304050607')) print(binascii.unhexlify(b'08090a0b0c0d0e0f')) print(binascii.unhexlify(b'7f80ff')) print(binascii.unhexlify(b'313233344142434461626364')) + +try: + a = binascii.unhexlify(b'0') # odd buffer length +except ValueError: + print('ValueError') + +try: + a = binascii.unhexlify(b'gg') # digit not hex +except ValueError: + print('ValueError') diff --git a/tests/extmod/uctypes_array_assign_native_le.py b/tests/extmod/uctypes_array_assign_native_le.py index 6901654dd8..632c4d252b 100644 --- a/tests/extmod/uctypes_array_assign_native_le.py +++ b/tests/extmod/uctypes_array_assign_native_le.py @@ -15,9 +15,16 @@ desc = { # aligned "arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1), "arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}), + + "arr8": (uctypes.ARRAY | 0, uctypes.INT8 | 1), + "arr9": (uctypes.ARRAY | 0, uctypes.INT16 | 1), + "arr10": (uctypes.ARRAY | 0, uctypes.INT32 | 1), + "arr11": (uctypes.ARRAY | 0, uctypes.INT64 | 1), + "arr12": (uctypes.ARRAY | 0, uctypes.UINT64| 1), + "arr13": (uctypes.ARRAY | 1, 1, {"l": {}}), } -data = bytearray(5) +data = bytearray(8) S = uctypes.struct(uctypes.addressof(data), desc) @@ -44,3 +51,45 @@ assert hex(S.arr5[0]) == "0x66778899" print(S.arr5[0] == S.arr7[0].l) assert S.arr5[0] == S.arr7[0].l +# assign int8 +S.arr8[0] = 0x11 +print(hex(S.arr8[0])) +assert hex(S.arr8[0]) == "0x11" + +# assign int16 +S.arr9[0] = 0x1122 +print(hex(S.arr9[0])) +assert hex(S.arr9[0]) == "0x1122" + +# assign int32 +S.arr10[0] = 0x11223344 +print(hex(S.arr10[0])) +assert hex(S.arr10[0]) == "0x11223344" + +# assign int64 +S.arr11[0] = 0x11223344 +print(hex(S.arr11[0])) +assert hex(S.arr11[0]) == "0x11223344" + +# assign uint64 +S.arr12[0] = 0x11223344 +print(hex(S.arr12[0])) +assert hex(S.arr12[0]) == "0x11223344" + +# index out of range +try: + print(S.arr8[2]) +except IndexError: + print("IndexError") + +# syntax error in descriptor +try: + S.arr13[0].l = 0x11 +except TypeError: + print("TypeError") + +# operation not supported +try: + S.arr13[0] = 0x11 +except TypeError: + print("TypeError") diff --git a/tests/extmod/uctypes_array_assign_native_le.py.exp b/tests/extmod/uctypes_array_assign_native_le.py.exp index 0c3dc50fb0..4efcdec668 100644 --- a/tests/extmod/uctypes_array_assign_native_le.py.exp +++ b/tests/extmod/uctypes_array_assign_native_le.py.exp @@ -3,3 +3,11 @@ 0x4455 0x66778899 True +0x11 +0x1122 +0x11223344 +0x11223344 +0x11223344 +IndexError +TypeError +TypeError diff --git a/tests/extmod/uctypes_le.py b/tests/extmod/uctypes_le.py index ff499476f9..5ae410b011 100644 --- a/tests/extmod/uctypes_le.py +++ b/tests/extmod/uctypes_le.py @@ -66,3 +66,22 @@ assert bytes(data) == b"21" S.bf3 = 5 print(data) assert bytes(data) == b"2Q" + +desc2 = { + "bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, + "bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN +} + +data2 = bytearray(b"0123") + +S2 = uctypes.struct(uctypes.addressof(data2), desc2, uctypes.LITTLE_ENDIAN) + +# bitfield using uint8 as base type +S2.bf8 = 5 +print(data2) +assert bytes(data2) == b"5123" + +# bitfield using uint32 as base type +S2.bf32 = 5 +print(data2) +assert bytes(data2) == b"51R3" diff --git a/tests/extmod/uctypes_le.py.exp b/tests/extmod/uctypes_le.py.exp index 8efd7a6ea0..2598b4eabd 100644 --- a/tests/extmod/uctypes_le.py.exp +++ b/tests/extmod/uctypes_le.py.exp @@ -8,3 +8,5 @@ bf: 48 49 bf 4bit: 3 1 3 0 bytearray(b'21') bytearray(b'2Q') +bytearray(b'5123') +bytearray(b'51R3') diff --git a/tests/extmod/uctypes_native_le.py b/tests/extmod/uctypes_native_le.py index a053b68d55..ef0f9f5e95 100644 --- a/tests/extmod/uctypes_native_le.py +++ b/tests/extmod/uctypes_native_le.py @@ -74,3 +74,22 @@ assert bytes(data) == b"21" S.bf3 = 5 print(data) assert bytes(data) == b"2Q" + +desc2 = { + "bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, + "bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN +} + +data2 = bytearray(b"0123") + +S2 = uctypes.struct(uctypes.addressof(data2), desc2, uctypes.NATIVE) + +# bitfield using uint8 as base type +S2.bf8 = 5 +print(data2) +assert bytes(data2) == b"5123" + +# bitfield using uint32 as base type +S2.bf32 = 5 +print(data2) +assert bytes(data2) == b"51R3" diff --git a/tests/extmod/uctypes_native_le.py.exp b/tests/extmod/uctypes_native_le.py.exp index 8efd7a6ea0..2598b4eabd 100644 --- a/tests/extmod/uctypes_native_le.py.exp +++ b/tests/extmod/uctypes_native_le.py.exp @@ -8,3 +8,5 @@ bf: 48 49 bf 4bit: 3 1 3 0 bytearray(b'21') bytearray(b'2Q') +bytearray(b'5123') +bytearray(b'51R3') diff --git a/tests/extmod/uctypes_print.py b/tests/extmod/uctypes_print.py new file mode 100644 index 0000000000..71981ce7ea --- /dev/null +++ b/tests/extmod/uctypes_print.py @@ -0,0 +1,22 @@ +# test printing of uctypes objects + +import uctypes + +# we use an address of "0" because we just want to print something deterministic +# and don't actually need to set/get any values in the struct + +desc = {"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 1)} +S = uctypes.struct(0, desc) +print(S) + +desc2 = [(uctypes.ARRAY | 0, uctypes.UINT8 | 1)] +S2 = uctypes.struct(0, desc2) +print(S2) + +desc3 = ((uctypes.ARRAY | 0, uctypes.UINT8 | 1)) +S3 = uctypes.struct(0, desc3) +print(S3) + +desc4 = ((uctypes.PTR | 0, uctypes.UINT8 | 1)) +S4 = uctypes.struct(0, desc4) +print(S4) diff --git a/tests/extmod/uctypes_print.py.exp b/tests/extmod/uctypes_print.py.exp new file mode 100644 index 0000000000..63daefc848 --- /dev/null +++ b/tests/extmod/uctypes_print.py.exp @@ -0,0 +1,4 @@ +<struct STRUCT 0> +<struct ERROR 0> +<struct ARRAY 0> +<struct PTR 0> diff --git a/tests/extmod/uctypes_ptr_le.py b/tests/extmod/uctypes_ptr_le.py index 4bff585171..d0216dfb81 100644 --- a/tests/extmod/uctypes_ptr_le.py +++ b/tests/extmod/uctypes_ptr_le.py @@ -14,7 +14,7 @@ desc = { bytes = b"01" addr = uctypes.addressof(bytes) -buf = addr.to_bytes(uctypes.sizeof(desc)) +buf = addr.to_bytes(uctypes.sizeof(desc), "little") S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.LITTLE_ENDIAN) diff --git a/tests/extmod/uctypes_ptr_native_le.py b/tests/extmod/uctypes_ptr_native_le.py index 0d02cfdc8e..6f011c3c2b 100644 --- a/tests/extmod/uctypes_ptr_native_le.py +++ b/tests/extmod/uctypes_ptr_native_le.py @@ -15,7 +15,7 @@ desc = { bytes = b"01" addr = uctypes.addressof(bytes) -buf = addr.to_bytes(uctypes.sizeof(desc)) +buf = addr.to_bytes(uctypes.sizeof(desc), "little") S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.NATIVE) diff --git a/tests/extmod/ujson_loads.py b/tests/extmod/ujson_loads.py index 22f4a02c41..37576e6ae8 100644 --- a/tests/extmod/ujson_loads.py +++ b/tests/extmod/ujson_loads.py @@ -23,6 +23,12 @@ my_print(json.loads('{}')) my_print(json.loads('{"a":true}')) my_print(json.loads('{"a":null, "b":false, "c":true}')) my_print(json.loads('{"a":[], "b":[1], "c":{"3":4}}')) +my_print(json.loads('"abc\\bdef"')) +my_print(json.loads('"abc\\fdef"')) +my_print(json.loads('"abc\\ndef"')) +my_print(json.loads('"abc\\rdef"')) +my_print(json.loads('"abc\\tdef"')) +my_print(json.loads('"abc\\uabcd"')) # whitespace handling my_print(json.loads('{\n\t"a":[]\r\n, "b":[1], "c":{"3":4} \n\r\t\r\r\r\n}')) @@ -32,3 +38,33 @@ try: json.loads('') except ValueError: print('ValueError') + +# string which is not closed +try: + my_print(json.loads('"abc')) +except ValueError: + print('ValueError') + +# unaccompanied closing brace +try: + my_print(json.loads(']')) +except ValueError: + print('ValueError') + +# unspecified object type +try: + my_print(json.loads('a')) +except ValueError: + print('ValueError') + +# bad property name +try: + my_print(json.loads('{{}:"abc"}')) +except ValueError: + print('ValueError') + +# unexpected characters after white space +try: + my_print(json.loads('[null] a')) +except ValueError: + print('ValueError') diff --git a/tests/extmod/utimeq1.py b/tests/extmod/utimeq1.py new file mode 100644 index 0000000000..9af7236744 --- /dev/null +++ b/tests/extmod/utimeq1.py @@ -0,0 +1,103 @@ +# Test for utimeq module which implements task queue with support for +# wraparound time (utime.ticks_ms() style). +try: + from utime import ticks_add, ticks_diff + from utimeq import utimeq +except ImportError: + print("SKIP") + import sys + sys.exit() + +DEBUG = 0 + +MAX = ticks_add(0, -1) +MODULO_HALF = MAX // 2 + 1 + +if DEBUG: + def dprint(*v): + print(*v) +else: + def dprint(*v): + pass + +# Try not to crash on invalid data +h = utimeq(10) +try: + h.push(1) + assert False +except TypeError: + pass + +try: + h.pop(1) + assert False +except IndexError: + pass + + +def pop_all(h): + l = [] + while h: + item = [0, 0, 0] + h.pop(item) + #print("!", item) + l.append(tuple(item)) + dprint(l) + return l + +def add(h, v): + h.push(v, 0, 0) + dprint("-----") + #h.dump() + dprint("-----") + +h = utimeq(10) +add(h, 0) +add(h, MAX) +add(h, MAX - 1) +add(h, 101) +add(h, 100) +add(h, MAX - 2) +dprint(h) +l = pop_all(h) +for i in range(len(l) - 1): + diff = ticks_diff(l[i + 1][0], l[i][0]) + assert diff > 0 + +def edge_case(edge, offset): + h = utimeq(10) + add(h, ticks_add(0, offset)) + add(h, ticks_add(edge, offset)) + dprint(h) + l = pop_all(h) + diff = ticks_diff(l[1][0], l[0][0]) + dprint(diff, diff > 0) + return diff + +dprint("===") +diff = edge_case(MODULO_HALF - 1, 0) +assert diff == MODULO_HALF - 1 +assert edge_case(MODULO_HALF - 1, 100) == diff +assert edge_case(MODULO_HALF - 1, -100) == diff + +# We expect diff to be always positive, per the definition of heappop() which should return +# the smallest value. +# This is the edge case where this invariant breaks, due to assymetry of two's-complement +# range - there's one more negative integer than positive, so heappushing values like below +# will then make ticks_diff() return the minimum negative value. We could make heappop +# return them in a different order, but ticks_diff() result would be the same. Conclusion: +# never add to a heap values where (a - b) == MODULO_HALF (and which are >= MODULO_HALF +# ticks apart in real time of course). +dprint("===") +diff = edge_case(MODULO_HALF, 0) +assert diff == -MODULO_HALF +assert edge_case(MODULO_HALF, 100) == diff +assert edge_case(MODULO_HALF, -100) == diff + +dprint("===") +diff = edge_case(MODULO_HALF + 1, 0) +assert diff == MODULO_HALF - 1 +assert edge_case(MODULO_HALF + 1, 100) == diff +assert edge_case(MODULO_HALF + 1, -100) == diff + +print("OK") diff --git a/tests/extmod/utimeq1.py.exp b/tests/extmod/utimeq1.py.exp new file mode 100644 index 0000000000..d86bac9de5 --- /dev/null +++ b/tests/extmod/utimeq1.py.exp @@ -0,0 +1 @@ +OK diff --git a/tests/extmod/utimeq_stable.py b/tests/extmod/utimeq_stable.py new file mode 100644 index 0000000000..9f6ba76d4a --- /dev/null +++ b/tests/extmod/utimeq_stable.py @@ -0,0 +1,23 @@ +try: + from utimeq import utimeq +except ImportError: + print("SKIP") + import sys + sys.exit() + +h = utimeq(10) + +# Check that for 2 same-key items, the queue is stable (pops items +# in the same order they were pushed). Unfortunately, this no longer +# holds for more same-key values, as the underlying heap structure +# is not stable itself. +h.push(100, 20, 0) +h.push(100, 10, 0) + +res = [0, 0, 0] +h.pop(res) +assert res == [100, 20, 0] +h.pop(res) +assert res == [100, 10, 0] + +print("OK") diff --git a/tests/extmod/utimeq_stable.py.exp b/tests/extmod/utimeq_stable.py.exp new file mode 100644 index 0000000000..d86bac9de5 --- /dev/null +++ b/tests/extmod/utimeq_stable.py.exp @@ -0,0 +1 @@ +OK diff --git a/tests/extmod/vfs_fat_fileio.py b/tests/extmod/vfs_fat_fileio.py index de8d4953c4..f050d94e27 100644 --- a/tests/extmod/vfs_fat_fileio.py +++ b/tests/extmod/vfs_fat_fileio.py @@ -89,6 +89,11 @@ with vfs.open("foo_file.txt") as f2: f2.seek(-2, 2) # SEEK_END print(f2.read(1)) +# using constructor of FileIO type to open a file +FileIO = type(f) +with FileIO("foo_file.txt") as f: + print(f.read()) + # dirs vfs.mkdir("foo_dir") @@ -140,6 +145,14 @@ print(vfs.listdir("foo_dir")) vfs.rename("foo_dir/file.txt", "moved-to-root.txt") print(vfs.listdir()) +# check that renaming to existing file will overwrite it +with vfs.open("temp", "w") as f: + f.write("new text") +vfs.rename("temp", "moved-to-root.txt") +print(vfs.listdir()) +with vfs.open("moved-to-root.txt") as f: + print(f.read()) + # valid removes vfs.remove("foo_dir/sub_file.txt") vfs.remove("foo_file.txt") diff --git a/tests/extmod/vfs_fat_fileio.py.exp b/tests/extmod/vfs_fat_fileio.py.exp index c438bc850c..a09442ae80 100644 --- a/tests/extmod/vfs_fat_fileio.py.exp +++ b/tests/extmod/vfs_fat_fileio.py.exp @@ -9,6 +9,7 @@ h e True d +hello!world! True True True @@ -18,5 +19,7 @@ b'data in file' True ['sub_file.txt', 'file.txt'] ['foo_file.txt', 'foo_dir', 'moved-to-root.txt'] +['foo_file.txt', 'foo_dir', 'moved-to-root.txt'] +new text ['moved-to-root.txt'] ENOSPC: True |