From cb20d999bc2d4f7e842f3e0b26e8fdc484acf82a Mon Sep 17 00:00:00 2001 From: Alex March Date: Thu, 13 Oct 2016 10:48:54 +0100 Subject: tests/extmod/vfs_fat: Improve VFS test coverage. Covered case: - Stat cases - Invalid read/write/flush/close - Invalid mkdir/rmdir/remove/getcwd - File seek/tell, modes a/x/+, t/b - Writing to a full disk - Full path rename, slash trim - Rename cases - Bytestring listdir - File object printing --- tests/extmod/vfs_fat_fileio.py | 160 ++++++++++++++++++++++++++++++++++++ tests/extmod/vfs_fat_fileio.py.exp | 23 ++++++ tests/extmod/vfs_fat_ramdisk.py | 63 +++++--------- tests/extmod/vfs_fat_ramdisk.py.exp | 14 ++-- 4 files changed, 209 insertions(+), 51 deletions(-) create mode 100644 tests/extmod/vfs_fat_fileio.py create mode 100644 tests/extmod/vfs_fat_fileio.py.exp (limited to 'tests') diff --git a/tests/extmod/vfs_fat_fileio.py b/tests/extmod/vfs_fat_fileio.py new file mode 100644 index 000000000..26fec7828 --- /dev/null +++ b/tests/extmod/vfs_fat_fileio.py @@ -0,0 +1,160 @@ +import sys +import uos +import uerrno +try: + uos.VfsFat +except AttributeError: + print("SKIP") + sys.exit() + + +class RAMFS: + + SEC_SIZE = 512 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.SEC_SIZE) + + def readblocks(self, n, buf): + #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + for i in range(len(buf)): + buf[i] = self.data[n * self.SEC_SIZE + i] + + def writeblocks(self, n, buf): + #print("writeblocks(%s, %x)" % (n, id(buf))) + for i in range(len(buf)): + self.data[n * self.SEC_SIZE + i] = buf[i] + + def ioctl(self, op, arg): + #print("ioctl(%d, %r)" % (op, arg)) + if op == 4: # BP_IOCTL_SEC_COUNT + return len(self.data) // self.SEC_SIZE + if op == 5: # BP_IOCTL_SEC_SIZE + return self.SEC_SIZE + + +try: + bdev = RAMFS(48) +except MemoryError: + print("SKIP") + sys.exit() + +uos.VfsFat.mkfs(bdev) +vfs = uos.VfsFat(bdev, "/ramdisk") + +# file IO +f = vfs.open("foo_file.txt", "w") +print(str(f)[:17], str(f)[-1:]) +f.write("hello!") +f.flush() +f.close() +try: + f.write("world!") +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + f.read() +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + f.flush() +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + f.close() +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + vfs.open("foo_file.txt", "x") +except OSError as e: + print(e.args[0] == uerrno.EEXIST) + +with vfs.open("foo_file.txt", "a") as f: + f.write("world!") + +with vfs.open("foo_file.txt") as f2: + print(f2.read()) + print(f2.tell()) + + f2.seek(0, 0) # SEEK_SET + print(f2.read(1)) + + f2.seek(0, 1) # SEEK_CUR + print(f2.read(1)) + try: + f2.seek(1, 1) # SEEK_END + except OSError as e: + print(e.args[0] == uerrno.EOPNOTSUPP) + + f2.seek(-2, 2) # SEEK_END + print(f2.read(1)) + +# dirs +vfs.mkdir("foo_dir") + +try: + vfs.rmdir("foo_file.txt") +except OSError as e: + print(e.args[0] == 20) # uerrno.ENOTDIR + +try: + vfs.mkdir("foo_dir") +except OSError as e: + print(e.args[0] == uerrno.EEXIST) + +try: + vfs.remove("foo_dir") +except OSError as e: + print(e.args[0] == uerrno.EISDIR) + +try: + vfs.remove("no_file.txt") +except OSError as e: + print(e.args[0] == uerrno.ENOENT) + +try: + vfs.rename("foo_dir", "/null") +except OSError as e: + print(e.args[0] == uerrno.ENODEV) + +# file in dir +with vfs.open("foo_dir/file-in-dir.txt", "w+t") as f: + f.write("data in file") + +with vfs.open("foo_dir/file-in-dir.txt", "r+b") as f: + print(f.read()) + +with vfs.open("foo_dir/sub_file.txt", "w") as f: + f.write("subdir file") + +# directory not empty +try: + vfs.rmdir("foo_dir") +except OSError as e: + print(e.args[0] == uerrno.EACCES) + +# trim full path +vfs.rename("foo_dir/file-in-dir.txt", "/ramdisk/foo_dir/file.txt") +print(vfs.listdir("foo_dir")) + +vfs.rename("foo_dir/file.txt", "moved-to-root.txt") +print(vfs.listdir()) + +# valid removes +vfs.remove("foo_dir/sub_file.txt") +vfs.remove("foo_file.txt") +vfs.rmdir("foo_dir") +print(vfs.listdir()) + +# disk full +try: + bsize = vfs.statvfs("/ramdisk")[0] + free = vfs.statvfs("/ramdisk")[2] + 1 + f = vfs.open("large_file.txt", "wb") + f.write(bytearray(bsize * free)) +except OSError as e: + print("ENOSPC:", e.args[0] == 28) # uerrno.ENOSPC diff --git a/tests/extmod/vfs_fat_fileio.py.exp b/tests/extmod/vfs_fat_fileio.py.exp new file mode 100644 index 000000000..9f0edb31e --- /dev/null +++ b/tests/extmod/vfs_fat_fileio.py.exp @@ -0,0 +1,23 @@ + +True +True +True +True +True +hello!world! +12 +h +e +True +d +True +True +True +True +True +b'data in file' +True +['sub_file.txt', 'file.txt'] +['foo_file.txt', 'foo_dir', 'moved-to-root.txt'] +['moved-to-root.txt'] +ENOSPC: True diff --git a/tests/extmod/vfs_fat_ramdisk.py b/tests/extmod/vfs_fat_ramdisk.py index 6380761c6..184672ff1 100644 --- a/tests/extmod/vfs_fat_ramdisk.py +++ b/tests/extmod/vfs_fat_ramdisk.py @@ -45,55 +45,38 @@ print(b"FOO_FILETXT" not in bdev.data) print(b"hello!" not in bdev.data) vfs = uos.VfsFat(bdev, "/ramdisk") -print("statvfs:", vfs.statvfs("/ramdisk")) - -print("getcwd:", vfs.getcwd()) - -f = vfs.open("foo_file.txt", "w") -f.write("hello!") -f.close() - -f2 = vfs.open("foo_file.txt") -print(f2.read()) -f2.close() - -print(b"FOO_FILETXT" in bdev.data) -print(b"hello!" in bdev.data) - -print(vfs.listdir()) try: - vfs.rmdir("foo_file.txt") + vfs.statvfs("/null") except OSError as e: - print(e.args[0] == 20) # uerrno.ENOTDIR - -vfs.remove('foo_file.txt') -print(vfs.listdir()) + print(e.args[0] == uerrno.ENODEV) -vfs.mkdir("foo_dir") -print(vfs.listdir()) +print("statvfs:", vfs.statvfs("/ramdisk")) +print("getcwd:", vfs.getcwd()) try: - vfs.remove("foo_dir") + vfs.stat("no_file.txt") except OSError as e: - print(e.args[0] == uerrno.EISDIR) + print(e.args[0] == uerrno.ENOENT) -f = vfs.open("foo_dir/file-in-dir.txt", "w") -f.write("data in file") -f.close() +with vfs.open("foo_file.txt", "w") as f: + f.write("hello!") +print(vfs.listdir()) -print(vfs.listdir("foo_dir")) +print("stat root:", vfs.stat("/")) +print("stat disk:", vfs.stat("/ramdisk/")) +print("stat file:", vfs.stat("foo_file.txt")) -vfs.rename("foo_dir/file-in-dir.txt", "moved-to-root.txt") -print(vfs.listdir()) +print(b"FOO_FILETXT" in bdev.data) +print(b"hello!" in bdev.data) +vfs.mkdir("foo_dir") vfs.chdir("foo_dir") print("getcwd:", vfs.getcwd()) print(vfs.listdir()) with vfs.open("sub_file.txt", "w") as f: - f.write("test2") -print(vfs.listdir()) + f.write("subdir file") try: vfs.chdir("sub_file.txt") @@ -103,20 +86,16 @@ except OSError as e: vfs.chdir("..") print("getcwd:", vfs.getcwd()) +vfs.umount() try: - vfs.rmdir("foo_dir") + vfs.listdir() except OSError as e: - print(e.args[0] == uerrno.EACCES) - -vfs.remove("foo_dir/sub_file.txt") -vfs.rmdir("foo_dir") -print(vfs.listdir()) + print(e.args[0] == uerrno.ENODEV) -vfs.umount() try: - vfs.listdir() + vfs.getcwd() except OSError as e: print(e.args[0] == uerrno.ENODEV) vfs = uos.VfsFat(bdev, "/ramdisk") -print(vfs.listdir()) +print(vfs.listdir(b"")) diff --git a/tests/extmod/vfs_fat_ramdisk.py.exp b/tests/extmod/vfs_fat_ramdisk.py.exp index 8a498b2fc..eaf637199 100644 --- a/tests/extmod/vfs_fat_ramdisk.py.exp +++ b/tests/extmod/vfs_fat_ramdisk.py.exp @@ -1,23 +1,19 @@ True True +True statvfs: (512, 512, 14, 14, 14, 0, 0, 0, 0, 255) getcwd: /ramdisk -hello! -True True ['foo_file.txt'] +stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) +stat disk: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) +stat file: (32768, 0, 0, 0, 0, 0, 6, -631238400, -631238400, -631238400) True -[] -['foo_dir'] True -['file-in-dir.txt'] -['foo_dir', 'moved-to-root.txt'] getcwd: /ramdisk/foo_dir [] -['sub_file.txt'] True getcwd: /ramdisk True -['moved-to-root.txt'] True -['moved-to-root.txt'] +[b'foo_file.txt', b'foo_dir'] -- cgit v1.2.3 From 56942019309645781d330312f5944db2d4cb5cd7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 24 Oct 2016 12:59:20 +1100 Subject: extmod/vfs_fat_file: Make file.close() a no-op if file already closed. As per CPython semantics. In particular, file.__del__() should not raise an exception if the file is already closed. --- extmod/vfs_fat_file.c | 9 ++++++--- tests/extmod/vfs_fat_fileio.py | 6 +----- tests/extmod/vfs_fat_fileio.py.exp | 1 - 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index e269ef593..76ac23685 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -120,9 +120,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_flush_obj, file_obj_flush); STATIC mp_obj_t file_obj_close(mp_obj_t self_in) { pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in); - FRESULT res = f_close(&self->fp); - if (res != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); + // if fs==NULL then the file is closed and in that case this method is a no-op + if (self->fp.fs != NULL) { + FRESULT res = f_close(&self->fp); + if (res != FR_OK) { + mp_raise_OSError(fresult_to_errno_table[res]); + } } return mp_const_none; } diff --git a/tests/extmod/vfs_fat_fileio.py b/tests/extmod/vfs_fat_fileio.py index 26fec7828..de8d4953c 100644 --- a/tests/extmod/vfs_fat_fileio.py +++ b/tests/extmod/vfs_fat_fileio.py @@ -48,6 +48,7 @@ print(str(f)[:17], str(f)[-1:]) f.write("hello!") f.flush() f.close() +f.close() # allowed try: f.write("world!") except OSError as e: @@ -63,11 +64,6 @@ try: except OSError as e: print(e.args[0] == uerrno.EINVAL) -try: - f.close() -except OSError as e: - print(e.args[0] == uerrno.EINVAL) - try: vfs.open("foo_file.txt", "x") except OSError as e: diff --git a/tests/extmod/vfs_fat_fileio.py.exp b/tests/extmod/vfs_fat_fileio.py.exp index 9f0edb31e..c438bc850 100644 --- a/tests/extmod/vfs_fat_fileio.py.exp +++ b/tests/extmod/vfs_fat_fileio.py.exp @@ -3,7 +3,6 @@ True True True True -True hello!world! 12 h -- cgit v1.2.3 From bc5b896f2481bbd1d44902573b0d5dd521f66405 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 22 Oct 2016 14:39:03 +1100 Subject: tests/basics/builtin_slice: Add test for "slice" builtin name. --- tests/basics/builtin_slice.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/basics/builtin_slice.py b/tests/basics/builtin_slice.py index 4da1229fa..df84d5c57 100644 --- a/tests/basics/builtin_slice.py +++ b/tests/basics/builtin_slice.py @@ -4,4 +4,8 @@ class A: def __getitem__(self, idx): print(idx) -A()[1:2:3] + return idx +s = A()[1:2:3] + +# check type +print(type(s) is slice) -- cgit v1.2.3 From 25c6fc731be491aa144867995b7b9d5f646414f2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 24 Oct 2016 13:50:39 +1100 Subject: tests/basics: Add test for builtin "delattr". --- tests/basics/builtin_delattr.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/basics/builtin_delattr.py (limited to 'tests') diff --git a/tests/basics/builtin_delattr.py b/tests/basics/builtin_delattr.py new file mode 100644 index 000000000..3743df227 --- /dev/null +++ b/tests/basics/builtin_delattr.py @@ -0,0 +1,18 @@ +# test builtin delattr + +class A: pass +a = A() +a.x = 1 +print(a.x) + +delattr(a, 'x') + +try: + a.x +except AttributeError: + print('AttributeError') + +try: + delattr(a, 'x') +except AttributeError: + print('AttributeError') -- cgit v1.2.3 From 38a9359339805626fd39a59ec6e5249a6b5e2f5a Mon Sep 17 00:00:00 2001 From: Alex March Date: Fri, 21 Oct 2016 10:30:38 +0100 Subject: tests/extmod/vfs_fat_fsusermount: Improve fsusermount test coverage. --- tests/extmod/vfs_fat_fsusermount.py | 96 +++++++++++++++++++++++++++++++++ tests/extmod/vfs_fat_fsusermount.py.exp | 7 +++ 2 files changed, 103 insertions(+) create mode 100644 tests/extmod/vfs_fat_fsusermount.py create mode 100644 tests/extmod/vfs_fat_fsusermount.py.exp (limited to 'tests') diff --git a/tests/extmod/vfs_fat_fsusermount.py b/tests/extmod/vfs_fat_fsusermount.py new file mode 100644 index 000000000..7326172ee --- /dev/null +++ b/tests/extmod/vfs_fat_fsusermount.py @@ -0,0 +1,96 @@ +import sys +import uos +import uerrno +try: + uos.VfsFat +except AttributeError: + print("SKIP") + sys.exit() + + +class RAMFS: + + SEC_SIZE = 512 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.SEC_SIZE) + + def readblocks(self, n, buf): + #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + for i in range(len(buf)): + buf[i] = self.data[n * self.SEC_SIZE + i] + + def writeblocks(self, n, buf): + #print("writeblocks(%s, %x)" % (n, id(buf))) + for i in range(len(buf)): + self.data[n * self.SEC_SIZE + i] = buf[i] + + def ioctl(self, op, arg): + #print("ioctl(%d, %r)" % (op, arg)) + if op == 4: # BP_IOCTL_SEC_COUNT + return len(self.data) // self.SEC_SIZE + if op == 5: # BP_IOCTL_SEC_SIZE + return self.SEC_SIZE + + +try: + bdev = RAMFS(48) +except MemoryError: + print("SKIP") + sys.exit() + +# can't mkfs readonly device +try: + uos.vfs_mkfs(bdev, "/ramdisk", readonly=True) +except OSError as e: + print(e) + +# mount before mkfs +try: + uos.vfs_mount(bdev, "/ramdisk") +except OSError as e: + print(e) + +# invalid umount +try: + uos.vfs_umount("/ramdisk") +except OSError as e: + print(e.args[0] == uerrno.EINVAL) + +try: + uos.vfs_mount(None, "/ramdisk") +except OSError as e: + print(e) + +try: + uos.vfs_mkfs(None, "/ramdisk") +except OSError as e: + print(e) + +# valid mkfs/mount +uos.vfs_mkfs(bdev, "/ramdisk") +uos.vfs_mount(bdev, "/ramdisk") + +# umount by path +uos.vfs_umount("/ramdisk") + +# readonly mount +uos.vfs_mount(bdev, "/ramdisk", readonly=True) +vfs = uos.VfsFat(bdev, "/ramdisk") +try: + f = vfs.open("file.txt", "w") +except OSError as e: + print("EROFS:", e.args[0] == 30) # uerrno.EROFS + +# device is None == umount +uos.vfs_mount(None, "/ramdisk") + +# max mounted devices +dev = [] +try: + for i in range(0,4): + dev.append(RAMFS(48)) + uos.vfs_mkfs(dev[i], "/ramdisk" + str(i)) + uos.vfs_mount(dev[i], "/ramdisk" + str(i)) +except OSError as e: + print(e) diff --git a/tests/extmod/vfs_fat_fsusermount.py.exp b/tests/extmod/vfs_fat_fsusermount.py.exp new file mode 100644 index 000000000..3b30688dd --- /dev/null +++ b/tests/extmod/vfs_fat_fsusermount.py.exp @@ -0,0 +1,7 @@ +can't mkfs +can't mount +True +can't umount +can't umount +EROFS: True +too many devices mounted -- cgit v1.2.3 From fbca4f94b36397f914a663c4ea816990f17711fa Mon Sep 17 00:00:00 2001 From: Alex March Date: Tue, 25 Oct 2016 11:14:38 +0100 Subject: tests/extmod/vfs_fat_oldproto: Test old block device protocol. --- tests/extmod/vfs_fat_oldproto.py | 61 ++++++++++++++++++++++++++++++++++++ tests/extmod/vfs_fat_oldproto.py.exp | 4 +++ 2 files changed, 65 insertions(+) create mode 100644 tests/extmod/vfs_fat_oldproto.py create mode 100644 tests/extmod/vfs_fat_oldproto.py.exp (limited to 'tests') diff --git a/tests/extmod/vfs_fat_oldproto.py b/tests/extmod/vfs_fat_oldproto.py new file mode 100644 index 000000000..bb8dd824c --- /dev/null +++ b/tests/extmod/vfs_fat_oldproto.py @@ -0,0 +1,61 @@ +import sys +import uos +import uerrno +try: + uos.VfsFat +except AttributeError: + print("SKIP") + sys.exit() + +class RAMFS_OLD: + + SEC_SIZE = 512 + + def __init__(self, blocks): + self.data = bytearray(blocks * self.SEC_SIZE) + + def readblocks(self, n, buf): + #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf))) + for i in range(len(buf)): + buf[i] = self.data[n * self.SEC_SIZE + i] + + def writeblocks(self, n, buf): + #print("writeblocks(%s, %x)" % (n, id(buf))) + for i in range(len(buf)): + self.data[n * self.SEC_SIZE + i] = buf[i] + + def sync(self): + pass + + def count(self): + return len(self.data) // self.SEC_SIZE + + +try: + bdev = RAMFS_OLD(48) +except MemoryError: + print("SKIP") + sys.exit() + +uos.vfs_mkfs(bdev, "/ramdisk") +uos.vfs_mount(bdev, "/ramdisk") + +# file io +vfs = uos.VfsFat(bdev, "/ramdisk") +with vfs.open("file.txt", "w") as f: + f.write("hello!") + +print(vfs.listdir()) + +with vfs.open("file.txt", "r") as f: + print(f.read()) + +vfs.remove("file.txt") +print(vfs.listdir()) + +# umount by device +uos.vfs_umount(bdev) +try: + vfs.listdir() +except OSError as e: + print(e.args[0] == uerrno.ENODEV) diff --git a/tests/extmod/vfs_fat_oldproto.py.exp b/tests/extmod/vfs_fat_oldproto.py.exp new file mode 100644 index 000000000..4120c277a --- /dev/null +++ b/tests/extmod/vfs_fat_oldproto.py.exp @@ -0,0 +1,4 @@ +['file.txt'] +hello! +[] +True -- cgit v1.2.3 From 964fb2450e2dd09c8db43fd6c4e1e5e472fa107a Mon Sep 17 00:00:00 2001 From: Alex March Date: Thu, 27 Oct 2016 11:31:24 +0100 Subject: tests/basics/gc1: Garbage collector threshold() coverage. --- tests/basics/gc1.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'tests') diff --git a/tests/basics/gc1.py b/tests/basics/gc1.py index 140c8b0a6..be6c6faed 100644 --- a/tests/basics/gc1.py +++ b/tests/basics/gc1.py @@ -20,3 +20,11 @@ if hasattr(gc, 'mem_free'): # just test they execute and return an int assert type(gc.mem_free()) is int assert type(gc.mem_alloc()) is int + +if hasattr(gc, 'threshold'): + # uPy has this extra function + # check execution and returns + assert(gc.threshold(1) is None) + assert(gc.threshold() == 0) + assert(gc.threshold(-1) is None) + assert(gc.threshold() == -1) -- cgit v1.2.3 From b83ac44e82d77ac77b0f93b5e61dd3ec84b089fd Mon Sep 17 00:00:00 2001 From: Alex March Date: Fri, 28 Oct 2016 13:53:15 +0100 Subject: tests/extmod/uhashlib_sha1: Coverage for SHA1 algorithm. --- tests/extmod/uhashlib_sha1.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/extmod/uhashlib_sha1.py (limited to 'tests') diff --git a/tests/extmod/uhashlib_sha1.py b/tests/extmod/uhashlib_sha1.py new file mode 100644 index 000000000..f12fc649a --- /dev/null +++ b/tests/extmod/uhashlib_sha1.py @@ -0,0 +1,22 @@ +import sys +try: + import uhashlib as hashlib +except ImportError: + try: + import hashlib + except ImportError: + # This is neither uPy, nor cPy, so must be uPy with + # uhashlib module disabled. + print("SKIP") + sys.exit() + +try: + hashlib.sha1 +except AttributeError: + # SHA1 is only available on some ports + print("SKIP") + sys.exit() + +sha1 = hashlib.sha1(b'hello') +sha1.update(b'world') +print(sha1.digest()) -- cgit v1.2.3 From cc0cc67815a70b4b7dd7be62b18e3c07054f9b46 Mon Sep 17 00:00:00 2001 From: Alex March Date: Fri, 28 Oct 2016 13:53:56 +0100 Subject: tests/extmod/uhashlib_sha256: Rename sha256.py test. --- tests/extmod/sha256.py | 36 ------------------------------------ tests/extmod/uhashlib_sha256.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 tests/extmod/sha256.py create mode 100644 tests/extmod/uhashlib_sha256.py (limited to 'tests') diff --git a/tests/extmod/sha256.py b/tests/extmod/sha256.py deleted file mode 100644 index ff51f2ffa..000000000 --- a/tests/extmod/sha256.py +++ /dev/null @@ -1,36 +0,0 @@ -import sys -try: - import uhashlib as hashlib -except ImportError: - try: - import hashlib - except ImportError: - # This is neither uPy, nor cPy, so must be uPy with - # uhashlib module disabled. - print("SKIP") - sys.exit() - - -h = hashlib.sha256() -print(h.digest()) - -h = hashlib.sha256() -h.update(b"123") -print(h.digest()) - -h = hashlib.sha256() -h.update(b"abcd" * 1000) -print(h.digest()) - -print(hashlib.sha256(b"\xff" * 64).digest()) - -# TODO: running .digest() several times in row is not supported() -#h = hashlib.sha256(b'123') -#print(h.digest()) -#print(h.digest()) - -# TODO: partial digests are not supported -#h = hashlib.sha256(b'123') -#print(h.digest()) -#h.update(b'456') -#print(h.digest()) diff --git a/tests/extmod/uhashlib_sha256.py b/tests/extmod/uhashlib_sha256.py new file mode 100644 index 000000000..ff51f2ffa --- /dev/null +++ b/tests/extmod/uhashlib_sha256.py @@ -0,0 +1,36 @@ +import sys +try: + import uhashlib as hashlib +except ImportError: + try: + import hashlib + except ImportError: + # This is neither uPy, nor cPy, so must be uPy with + # uhashlib module disabled. + print("SKIP") + sys.exit() + + +h = hashlib.sha256() +print(h.digest()) + +h = hashlib.sha256() +h.update(b"123") +print(h.digest()) + +h = hashlib.sha256() +h.update(b"abcd" * 1000) +print(h.digest()) + +print(hashlib.sha256(b"\xff" * 64).digest()) + +# TODO: running .digest() several times in row is not supported() +#h = hashlib.sha256(b'123') +#print(h.digest()) +#print(h.digest()) + +# TODO: partial digests are not supported +#h = hashlib.sha256(b'123') +#print(h.digest()) +#h.update(b'456') +#print(h.digest()) -- cgit v1.2.3 From 1ba4db5685b0f5a0b06020a01cf47aa952a52588 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Oct 2016 19:53:31 +0300 Subject: tests/btree1: Fix out of memory error running on esp8266. --- tests/extmod/btree1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/extmod/btree1.py b/tests/extmod/btree1.py index 662983766..715f62824 100644 --- a/tests/extmod/btree1.py +++ b/tests/extmod/btree1.py @@ -9,7 +9,7 @@ except ImportError: #f = open("_test.db", "w+b") f = uio.BytesIO() -db = btree.open(f) +db = btree.open(f, pagesize=512) db[b"foo3"] = b"bar3" db[b"foo1"] = b"bar1" -- cgit v1.2.3 From 8908e505ce23ccd1f0ee49f3ffa80bf78cfaccfa Mon Sep 17 00:00:00 2001 From: Fabio Utzig Date: Sun, 30 Oct 2016 14:25:04 -0200 Subject: py/sequence: Fix reverse slicing of lists. --- py/sequence.c | 49 +++++++++++++++++++++++++++++------------ tests/basics/list_slice_3arg.py | 19 ++++++++++++++++ 2 files changed, 54 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/py/sequence.c b/py/sequence.c index 0acdd25be..bc2cfc077 100644 --- a/py/sequence.c +++ b/py/sequence.c @@ -53,15 +53,35 @@ bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice mp_int_t start, stop; mp_obj_slice_get(slice, &ostart, &ostop, &ostep); + if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) { + indexes->step = mp_obj_get_int(ostep); + if (indexes->step == 0) { + mp_raise_ValueError("slice step cannot be zero"); + } + } else { + indexes->step = 1; + } + if (ostart == mp_const_none) { - start = 0; + if (indexes->step > 0) { + start = 0; + } else { + start = len - 1; + } } else { start = mp_obj_get_int(ostart); } if (ostop == mp_const_none) { - stop = len; + if (indexes->step > 0) { + stop = len; + } else { + stop = 0; + } } else { stop = mp_obj_get_int(ostop); + if (stop >= 0 && indexes->step < 0) { + stop += 1; + } } // Unlike subscription, out-of-bounds slice indexes are never error @@ -70,29 +90,31 @@ bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice if (start < 0) { start = 0; } - } else if ((mp_uint_t)start > len) { + } else if (indexes->step > 0 && (mp_uint_t)start > len) { start = len; + } else if (indexes->step < 0 && (mp_uint_t)start > len - 1) { + start = len - 1; } if (stop < 0) { stop = len + stop; + if (indexes->step < 0) { + stop += 1; + } } else if ((mp_uint_t)stop > len) { stop = len; } // CPython returns empty sequence in such case, or point for assignment is at start - if (start > stop) { + if (indexes->step > 0 && start > stop) { stop = start; + } else if (indexes->step < 0 && start < stop) { + stop = start + 1; } indexes->start = start; indexes->stop = stop; - if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) { - indexes->step = mp_obj_get_int(ostep); - return false; - } - indexes->step = 1; - return true; + return indexes->step == 1; } #endif @@ -106,10 +128,9 @@ mp_obj_t mp_seq_extract_slice(mp_uint_t len, const mp_obj_t *seq, mp_bound_slice mp_obj_t res = mp_obj_new_list(0, NULL); if (step < 0) { - stop--; - while (start <= stop) { - mp_obj_list_append(res, seq[stop]); - stop += step; + while (start >= stop) { + mp_obj_list_append(res, seq[start]); + start += step; } } else { while (start < stop) { diff --git a/tests/basics/list_slice_3arg.py b/tests/basics/list_slice_3arg.py index b98ca3e4f..8578d5855 100644 --- a/tests/basics/list_slice_3arg.py +++ b/tests/basics/list_slice_3arg.py @@ -7,3 +7,22 @@ x = list(range(9)) print(x[::-1]) print(x[::2]) print(x[::-2]) + +x = list(range(5)) +print(x[:0:-1]) +print(x[:1:-1]) +print(x[:2:-1]) +print(x[0::-1]) +print(x[1::-1]) +print(x[2::-1]) + +x = list(range(5)) +print(x[0:0:-1]) +print(x[4:4:-1]) +print(x[5:5:-1]) + +x = list(range(10)) +print(x[-1:-1:-1]) +print(x[-1:-2:-1]) +print(x[-1:-11:-1]) +print(x[-10:-11:-1]) -- cgit v1.2.3 From be6a765c69baf6f0bae77379438fb11c8f2c104a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 30 Oct 2016 21:33:12 +0300 Subject: tests/extmod/ticks_diff: Test for new semantics of ticks_diff(). --- tests/extmod/ticks_diff.py | 33 +++++++++++++++++++++++++++++++++ tests/extmod/ticks_diff.py.exp | 1 + 2 files changed, 34 insertions(+) create mode 100644 tests/extmod/ticks_diff.py create mode 100644 tests/extmod/ticks_diff.py.exp (limited to 'tests') diff --git a/tests/extmod/ticks_diff.py b/tests/extmod/ticks_diff.py new file mode 100644 index 000000000..4d8df83cf --- /dev/null +++ b/tests/extmod/ticks_diff.py @@ -0,0 +1,33 @@ +from utime import ticks_diff, ticks_add + +MAX = ticks_add(0, -1) +# Should be done like this to avoid small int overflow +MODULO_HALF = MAX // 2 + 1 + +# Invariants: +# if ticks_diff(a, b) = c, +# then ticks_diff(b, a) = -c + +assert ticks_diff(1, 0) == 1, ticks_diff(1, 0) +assert ticks_diff(0, 1) == -1 + +assert ticks_diff(0, MAX) == 1 +assert ticks_diff(MAX, 0) == -1 + +assert ticks_diff(0, MAX - 1) == 2 + +# Maximum "positive" distance +assert ticks_diff(MODULO_HALF, 1) == MODULO_HALF - 1, ticks_diff(MODULO_HALF, 1) +# Step further, and it becomes a negative distance +assert ticks_diff(MODULO_HALF, 0) == -MODULO_HALF + +# Offsetting that in either direction doesn't affect the result +off = 100 +# Cheating and skipping to use ticks_add() when we know there's no wraparound +# Real apps should use always it. +assert ticks_diff(MODULO_HALF + off, 1 + off) == MODULO_HALF - 1 +assert ticks_diff(MODULO_HALF + off, 0 + off) == -MODULO_HALF +assert ticks_diff(MODULO_HALF - off, ticks_add(1, -off)) == MODULO_HALF - 1 +assert ticks_diff(MODULO_HALF - off, ticks_add(0, -off)) == -MODULO_HALF + +print("OK") diff --git a/tests/extmod/ticks_diff.py.exp b/tests/extmod/ticks_diff.py.exp new file mode 100644 index 000000000..d86bac9de --- /dev/null +++ b/tests/extmod/ticks_diff.py.exp @@ -0,0 +1 @@ +OK -- cgit v1.2.3 From 94aeba0427cc80e43b59c4155e02e8cb7c327951 Mon Sep 17 00:00:00 2001 From: Alex March Date: Wed, 26 Oct 2016 11:36:06 +0100 Subject: tests/extmod/framebuf1: Test framebuffer pixel clear, and text function. --- tests/extmod/framebuf1.py | 15 +++++++++++++++ tests/extmod/framebuf1.py.exp | 4 ++++ 2 files changed, 19 insertions(+) (limited to 'tests') diff --git a/tests/extmod/framebuf1.py b/tests/extmod/framebuf1.py index f550b6b4f..52899028c 100644 --- a/tests/extmod/framebuf1.py +++ b/tests/extmod/framebuf1.py @@ -23,6 +23,10 @@ fbuf.pixel(0, 15, 1) fbuf.pixel(4, 15, 1) print(buf) +# clear pixel +fbuf.pixel(4, 15, 0) +print(buf) + # get pixel print(fbuf.pixel(0, 0), fbuf.pixel(1, 1)) @@ -39,3 +43,14 @@ fbuf.scroll(-1, 0) print(buf) fbuf.scroll(2, 2) print(buf) + +# print text +fbuf.fill(0) +fbuf.text("hello", 0, 0, 1) +print(buf) +fbuf.text("hello", 0, 0, 0) # clear +print(buf) + +# char out of font range set to chr(127) +fbuf.text(str(chr(31)), 0, 0) +print(buf) diff --git a/tests/extmod/framebuf1.py.exp b/tests/extmod/framebuf1.py.exp index 8fd8c3709..1577faac8 100644 --- a/tests/extmod/framebuf1.py.exp +++ b/tests/extmod/framebuf1.py.exp @@ -1,9 +1,13 @@ 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 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') bytearray(b'\x00\x00@\x00\x00\x00\x00\x00\x00\x00') 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') -- cgit v1.2.3 From fa3a108ed7015a131848ca34e5908d183774baf5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 6 Nov 2016 01:47:44 +0300 Subject: tests/vfs_fat_oldproto: Skip for ports not supporting "oldproto". Otherwise this broke esp8266 testsuite. --- tests/extmod/vfs_fat_oldproto.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/extmod/vfs_fat_oldproto.py b/tests/extmod/vfs_fat_oldproto.py index bb8dd824c..73983567d 100644 --- a/tests/extmod/vfs_fat_oldproto.py +++ b/tests/extmod/vfs_fat_oldproto.py @@ -3,6 +3,8 @@ import uos import uerrno try: uos.VfsFat + uos.vfs_mkfs + uos.vfs_mount except AttributeError: print("SKIP") sys.exit() -- cgit v1.2.3