Skip to content

Commit

Permalink
tests/pyb/can: Update to test pyb.CAN restart, state, info, inplace recv
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgeorge committed Mar 19, 2018
1 parent 0abbafd commit 22c693a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
87 changes: 86 additions & 1 deletion tests/pyb/can.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
print('SKIP')
raise SystemExit

from array import array
import micropython
import pyb

# test we can correctly create by id or name
Expand All @@ -19,15 +21,27 @@
can = CAN(1)
print(can)

# Test state when de-init'd
print(can.state() == can.STOPPED)

can.init(CAN.LOOPBACK)
print(can)
print(can.any(0))

# Test state when freshly created
print(can.state() == can.ERROR_ACTIVE)

# Test that restart can be called
can.restart()

# Test info returns a sensible value
print(can.info())

# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))

can.send('abcd', 123, timeout=5000)
print(can.any(0))
print(can.any(0), can.info())
print(can.recv(0))

can.send('abcd', -1, timeout=5000)
Expand All @@ -44,6 +58,77 @@
else:
print('failed')

# Test that recv can work without allocating memory on the heap

buf = bytearray(10)
l = [0, 0, 0, memoryview(buf)]
l2 = None

micropython.heap_lock()

can.send('', 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[3]), buf)

can.send('1234', 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[3]), buf)

can.send('01234567', 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[3]), buf)

can.send('abc', 42)
l2 = can.recv(0, l)
assert l is l2
print(l, len(l[3]), buf)

micropython.heap_unlock()

# Test that recv can work with different arrays behind the memoryview
can.send('abc', 1)
print(bytes(can.recv(0, [0, 0, 0, memoryview(array('B', range(8)))])[3]))
can.send('def', 1)
print(bytes(can.recv(0, [0, 0, 0, memoryview(array('b', range(8)))])[3]))

# Test for non-list passed as second arg to recv
can.send('abc', 1)
try:
can.recv(0, 1)
except TypeError:
print('TypeError')

# Test for too-short-list passed as second arg to recv
can.send('abc', 1)
try:
can.recv(0, [0, 0, 0])
except ValueError:
print('ValueError')

# Test for non-memoryview passed as 4th element to recv
can.send('abc', 1)
try:
can.recv(0, [0, 0, 0, 0])
except TypeError:
print('TypeError')

# Test for read-only-memoryview passed as 4th element to recv
can.send('abc', 1)
try:
can.recv(0, [0, 0, 0, memoryview(bytes(8))])
except ValueError:
print('ValueError')

# Test for bad-typecode-memoryview passed as 4th element to recv
can.send('abc', 1)
try:
can.recv(0, [0, 0, 0, memoryview(array('i', range(8)))])
except ValueError:
print('ValueError')

del can

# Testing extended IDs
Expand Down
14 changes: 14 additions & 0 deletions tests/pyb/can.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ CAN YA
CAN YB
ValueError YC
CAN(1)
True
CAN(1, CAN.LOOPBACK, extframe=False, auto_restart=False)
False
True
[0, 0, 0, 0, 0, 0, 0, 0]
True [0, 0, 0, 0, 0, 0, 1, 0]
(123, False, 0, b'abcd')
(2047, False, 0, b'abcd')
(0, False, 0, b'abcd')
passed
[42, False, 0, <memoryview>] 0 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
[42, False, 0, <memoryview>] 4 bytearray(b'1234\x00\x00\x00\x00\x00\x00')
[42, False, 0, <memoryview>] 8 bytearray(b'01234567\x00\x00')
[42, False, 0, <memoryview>] 3 bytearray(b'abc34567\x00\x00')
b'abc'
b'def'
TypeError
ValueError
TypeError
ValueError
ValueError
CAN(1, CAN.LOOPBACK, extframe=True, auto_restart=False)
passed
('0x8', '0x1c', '0xa', b'ok')
Expand Down

0 comments on commit 22c693a

Please sign in to comment.