Skip to content

Commit 22c693a

Browse files
committed
tests/pyb/can: Update to test pyb.CAN restart, state, info, inplace recv
1 parent 0abbafd commit 22c693a

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

tests/pyb/can.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
print('SKIP')
55
raise SystemExit
66

7+
from array import array
8+
import micropython
79
import pyb
810

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

24+
# Test state when de-init'd
25+
print(can.state() == can.STOPPED)
26+
2227
can.init(CAN.LOOPBACK)
2328
print(can)
2429
print(can.any(0))
2530

31+
# Test state when freshly created
32+
print(can.state() == can.ERROR_ACTIVE)
33+
34+
# Test that restart can be called
35+
can.restart()
36+
37+
# Test info returns a sensible value
38+
print(can.info())
39+
2640
# Catch all filter
2741
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
2842

2943
can.send('abcd', 123, timeout=5000)
30-
print(can.any(0))
44+
print(can.any(0), can.info())
3145
print(can.recv(0))
3246

3347
can.send('abcd', -1, timeout=5000)
@@ -44,6 +58,77 @@
4458
else:
4559
print('failed')
4660

61+
# Test that recv can work without allocating memory on the heap
62+
63+
buf = bytearray(10)
64+
l = [0, 0, 0, memoryview(buf)]
65+
l2 = None
66+
67+
micropython.heap_lock()
68+
69+
can.send('', 42)
70+
l2 = can.recv(0, l)
71+
assert l is l2
72+
print(l, len(l[3]), buf)
73+
74+
can.send('1234', 42)
75+
l2 = can.recv(0, l)
76+
assert l is l2
77+
print(l, len(l[3]), buf)
78+
79+
can.send('01234567', 42)
80+
l2 = can.recv(0, l)
81+
assert l is l2
82+
print(l, len(l[3]), buf)
83+
84+
can.send('abc', 42)
85+
l2 = can.recv(0, l)
86+
assert l is l2
87+
print(l, len(l[3]), buf)
88+
89+
micropython.heap_unlock()
90+
91+
# Test that recv can work with different arrays behind the memoryview
92+
can.send('abc', 1)
93+
print(bytes(can.recv(0, [0, 0, 0, memoryview(array('B', range(8)))])[3]))
94+
can.send('def', 1)
95+
print(bytes(can.recv(0, [0, 0, 0, memoryview(array('b', range(8)))])[3]))
96+
97+
# Test for non-list passed as second arg to recv
98+
can.send('abc', 1)
99+
try:
100+
can.recv(0, 1)
101+
except TypeError:
102+
print('TypeError')
103+
104+
# Test for too-short-list passed as second arg to recv
105+
can.send('abc', 1)
106+
try:
107+
can.recv(0, [0, 0, 0])
108+
except ValueError:
109+
print('ValueError')
110+
111+
# Test for non-memoryview passed as 4th element to recv
112+
can.send('abc', 1)
113+
try:
114+
can.recv(0, [0, 0, 0, 0])
115+
except TypeError:
116+
print('TypeError')
117+
118+
# Test for read-only-memoryview passed as 4th element to recv
119+
can.send('abc', 1)
120+
try:
121+
can.recv(0, [0, 0, 0, memoryview(bytes(8))])
122+
except ValueError:
123+
print('ValueError')
124+
125+
# Test for bad-typecode-memoryview passed as 4th element to recv
126+
can.send('abc', 1)
127+
try:
128+
can.recv(0, [0, 0, 0, memoryview(array('i', range(8)))])
129+
except ValueError:
130+
print('ValueError')
131+
47132
del can
48133

49134
# Testing extended IDs

tests/pyb/can.py.exp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@ CAN YA
77
CAN YB
88
ValueError YC
99
CAN(1)
10+
True
1011
CAN(1, CAN.LOOPBACK, extframe=False, auto_restart=False)
1112
False
1213
True
14+
[0, 0, 0, 0, 0, 0, 0, 0]
15+
True [0, 0, 0, 0, 0, 0, 1, 0]
1316
(123, False, 0, b'abcd')
1417
(2047, False, 0, b'abcd')
1518
(0, False, 0, b'abcd')
1619
passed
20+
[42, False, 0, <memoryview>] 0 bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
21+
[42, False, 0, <memoryview>] 4 bytearray(b'1234\x00\x00\x00\x00\x00\x00')
22+
[42, False, 0, <memoryview>] 8 bytearray(b'01234567\x00\x00')
23+
[42, False, 0, <memoryview>] 3 bytearray(b'abc34567\x00\x00')
24+
b'abc'
25+
b'def'
26+
TypeError
27+
ValueError
28+
TypeError
29+
ValueError
30+
ValueError
1731
CAN(1, CAN.LOOPBACK, extframe=True, auto_restart=False)
1832
passed
1933
('0x8', '0x1c', '0xa', b'ok')

0 commit comments

Comments
 (0)