micropython/tests/extmod/vfs_lfs_file.py

122 wiersze
2.5 KiB
Python
Czysty Zwykły widok Historia

2019-10-18 06:25:48 +00:00
# Test for VfsLittle using a RAM device, file IO
try:
import vfs
vfs.VfsLfs1
vfs.VfsLfs2
2019-10-18 06:25:48 +00:00
except (ImportError, AttributeError):
print("SKIP")
raise SystemExit
2019-10-18 06:25:48 +00:00
class RAMBlockDevice:
ERASE_BLOCK_SIZE = 1024
def __init__(self, blocks):
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
def readblocks(self, block, buf, off):
addr = block * self.ERASE_BLOCK_SIZE + off
for i in range(len(buf)):
buf[i] = self.data[addr + i]
def writeblocks(self, block, buf, off):
addr = block * self.ERASE_BLOCK_SIZE + off
for i in range(len(buf)):
self.data[addr + i] = buf[i]
def ioctl(self, op, arg):
if op == 4: # block count
2019-10-18 06:25:48 +00:00
return len(self.data) // self.ERASE_BLOCK_SIZE
if op == 5: # block size
2019-10-18 06:25:48 +00:00
return self.ERASE_BLOCK_SIZE
if op == 6: # erase block
2019-10-18 06:25:48 +00:00
return 0
2019-10-18 06:25:48 +00:00
def test(bdev, vfs_class):
print("test", vfs_class)
2019-10-18 06:25:48 +00:00
# mkfs
vfs_class.mkfs(bdev)
# construction
fs = vfs_class(bdev)
2019-10-18 06:25:48 +00:00
# create text, print, write, close
f = fs.open("test.txt", "wt")
2019-10-18 06:25:48 +00:00
print(f)
f.write("littlefs")
2019-10-18 06:25:48 +00:00
f.close()
# close already-closed file
f.close()
# create binary, print, write, flush, close
f = fs.open("test.bin", "wb")
2019-10-18 06:25:48 +00:00
print(f)
f.write("littlefs")
2019-10-18 06:25:48 +00:00
f.flush()
f.close()
# create for append
f = fs.open("test.bin", "ab")
f.write("more")
2019-10-18 06:25:48 +00:00
f.close()
# create exclusive
f = fs.open("test2.bin", "xb")
2019-10-18 06:25:48 +00:00
f.close()
# create exclusive with error
try:
fs.open("test2.bin", "x")
2019-10-18 06:25:48 +00:00
except OSError:
print("open OSError")
2019-10-18 06:25:48 +00:00
# read default
with fs.open("test.txt", "") as f:
2019-10-18 06:25:48 +00:00
print(f.read())
# read text
with fs.open("test.txt", "rt") as f:
2019-10-18 06:25:48 +00:00
print(f.read())
# read binary
with fs.open("test.bin", "rb") as f:
2019-10-18 06:25:48 +00:00
print(f.read())
# create read and write
with fs.open("test.bin", "r+b") as f:
2019-10-18 06:25:48 +00:00
print(f.read(8))
f.write("MORE")
with fs.open("test.bin", "rb") as f:
2019-10-18 06:25:48 +00:00
print(f.read())
# seek and tell
f = fs.open("test.txt", "r")
2019-10-18 06:25:48 +00:00
print(f.tell())
f.seek(3, 0)
print(f.tell())
f.close()
# open nonexistent
try:
fs.open("noexist", "r")
2019-10-18 06:25:48 +00:00
except OSError:
print("open OSError")
2019-10-18 06:25:48 +00:00
# open multiple files at the same time
f1 = fs.open("test.txt", "")
f2 = fs.open("test.bin", "b")
2019-10-18 06:25:48 +00:00
print(f1.read())
print(f2.read())
f1.close()
f2.close()
2019-10-18 06:25:48 +00:00
bdev = RAMBlockDevice(30)
test(bdev, vfs.VfsLfs1)
test(bdev, vfs.VfsLfs2)