aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-03-19 15:15:39 +1100
committerDamien George <damien.p.george@gmail.com>2018-03-19 15:15:39 +1100
commit22c693aa6fc0f325bc42f4da5904244fefbfc5b1 (patch)
tree36499a9ca440fc915a4173700acfd14b434728ab
parent0abbafd42406e4367a80ad996bdd7047ad1e020d (diff)
tests/pyb/can: Update to test pyb.CAN restart, state, info, inplace recv
-rw-r--r--tests/pyb/can.py87
-rw-r--r--tests/pyb/can.py.exp14
2 files changed, 100 insertions, 1 deletions
diff --git a/tests/pyb/can.py b/tests/pyb/can.py
index 0fd8c8368..8a08ea9a6 100644
--- a/tests/pyb/can.py
+++ b/tests/pyb/can.py
@@ -4,6 +4,8 @@ except ImportError:
print('SKIP')
raise SystemExit
+from array import array
+import micropython
import pyb
# test we can correctly create by id or name
@@ -19,15 +21,27 @@ CAN.initfilterbanks(14)
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)
@@ -44,6 +58,77 @@ except ValueError:
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
diff --git a/tests/pyb/can.py.exp b/tests/pyb/can.py.exp
index 352e5f969..687935e7f 100644
--- a/tests/pyb/can.py.exp
+++ b/tests/pyb/can.py.exp
@@ -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')