tests/extmod: Add test for blockdev with standard and extended protocol.
This commit is contained in:
parent
73fddb84e5
commit
7c8fb27f38
|
@ -0,0 +1,70 @@
|
|||
# Test for behaviour of combined standard and extended block device
|
||||
|
||||
try:
|
||||
import uos
|
||||
uos.VfsFat
|
||||
uos.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:
|
||||
bdev = RAMBlockDevice(50)
|
||||
except MemoryError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
test(bdev, uos.VfsFat)
|
||||
test(bdev, uos.VfsLfs2)
|
|
@ -0,0 +1,8 @@
|
|||
test <class 'VfsFat'>
|
||||
(512, 512, 16, 16, 16, 0, 0, 0, 0, 255)
|
||||
[('test', 32768, 0, 90)]
|
||||
some datasome datasome datasome datasome datasome datasome datasome datasome datasome data
|
||||
test <class 'VfsLfs2'>
|
||||
(512, 512, 50, 48, 48, 0, 0, 0, 0, 255)
|
||||
[('test', 32768, 0, 90)]
|
||||
some datasome datasome datasome datasome datasome datasome datasome datasome datasome data
|
Loading…
Reference in New Issue