micropython/tests/extmod/vfs_blockdev.py

77 lines
1.6 KiB
Python

# Test for behaviour of combined standard and extended block device
try:
import os
os.VfsFat
os.VfsLfs2
except (ImportError, AttributeError):
print("SKIP")
raise SystemExit
class RAMBlockDevice:
ERASE_BLOCK_SIZE = 512
def __init__(self, blocks):
self.data = bytearray(blocks * self.ERASE_BLOCK_SIZE)
def readblocks(self, block, buf, off=0):
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=None):
if off is None:
# erase, then write
off = 0
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
return len(self.data) // self.ERASE_BLOCK_SIZE
if op == 5: # block size
return self.ERASE_BLOCK_SIZE
if op == 6: # erase block
return 0
def test(bdev, vfs_class):
print("test", vfs_class)
# mkfs
vfs_class.mkfs(bdev)
# construction
vfs = vfs_class(bdev)
# statvfs
print(vfs.statvfs("/"))
# open, write close
f = vfs.open("test", "w")
for i in range(10):
f.write("some data")
f.close()
# ilistdir
print(list(vfs.ilistdir()))
# read
with vfs.open("test", "r") as f:
print(f.read())
try:
import os
bdev = RAMBlockDevice(50)
except MemoryError:
print("SKIP")
raise SystemExit
test(bdev, os.VfsFat)
test(bdev, os.VfsLfs2)