-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_com.py
108 lines (107 loc) · 3.9 KB
/
test_com.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
""" Test module for com.py """
import unittest
import time
import com
import random
import sys
class TestFonctionDevice(unittest.TestCase):
""" Device functions tests """
def setUp(self):
self.hapticd = com.HDevice("ftdi")
def test_extract(self):
""" Test extract function """
size = 5
for i in range(0, size):
self.hapticd.fifoin.put(bytes([i]))
fifoverif = bytearray(range(0, size))
fifotest = self.hapticd.extract(size)
self.assertEqual(fifotest, fifoverif)
def test_get(self):
"""Test get function"""
var = "toto"
self.hapticd.fifoin.put(var)
self.assertEqual(self.hapticd.get(), var)
def test_readarray(self):
""" Test readarray function """
size = 5
for i in range(0, size):
self.hapticd.fifoin.put(bytes([i]))
fifoverif = bytearray(range(0, size))
fifotest = self.hapticd.readarray(size)
self.assertEqual(fifotest, fifoverif)
def test_incommingsize(self):
"""Test size"""
size = 5
for i in range(0, size):
self.hapticd.fifoin.put(bytes([i]))
time.sleep(0.1)
sizecomp = self.hapticd.incommingsize()
self.assertEqual(size, sizecomp)
def test_writeint(self):
""" test conversion byte """
tosend = 45
bufenvoi = bytearray(4)
bufenvoi[0] = int(tosend) & int('0b00111111', 2)
bufenvoi[1] = ((int(tosend) >> 6) & int('0b00111111', 2)) | int('0b01000000', 2)
bufenvoi[2] = ((int(tosend) >> 12) & int('0b00111111', 2)) | int('0b10000000', 2)
bufenvoi[3] = int('0b11000000', 2)
self.hapticd.writeint(tosend)
self.assertEqual(bufenvoi, self.hapticd.fifoout.get())
def testwrite(self):
""" Test write """
tosend = 45
forcenow = max(min(tosend, 130), -130)
forcenowint = 32767*(1+forcenow/130)
bufenvoi = bytearray(4)
bufenvoi[0] = int(forcenowint) & int('0b00111111', 2)
bufenvoi[1] = ((int(forcenowint) >> 6) & int('0b00111111', 2)) | int('0b01000000', 2)
bufenvoi[2] = ((int(forcenowint) >> 12) & int('0b00111111', 2)) | int('0b10000000', 2)
bufenvoi[3] = int('0b11000000', 2)
self.hapticd.write(tosend)
self.assertEqual(bufenvoi, self.hapticd.fifoout.get())
def teststartstop(self):
""" Launch and quit """
self.hapticd.launch()
time.sleep(1)
self.hapticd.quit()
class TestFonctionDeviceSerial(unittest.TestCase):
""" Device functions tests """
def setUp(self):
self.hapticd = com.HDevice("com8")
def test_extract(self):
""" Test extract function """
size = 5
for i in range(0, size):
self.hapticd.fifoin.put(bytes([i]))
fifoverif = bytearray(range(0, size))
fifotest = self.hapticd.extract(size)
self.assertEqual(fifotest, fifoverif)
def test_get(self):
"""Test get function"""
var = "toto"
self.hapticd.fifoin.put(var)
self.assertEqual(self.hapticd.get(), var)
def test_readascii(self):
"""Test readascii"""
var = "toto"
self.hapticd.fifoin.put(var.encode('ascii', 'backslashreplace'))
self.assertEqual(self.hapticd.readascii(), var)
def test_readsep(self):
"""Test read with separateur"""
size = 8
var = ""
result = []
for i in range(0, size):
result.append(random.uniform(0, 50))
var = var + str(result[i]) + "|"
self.hapticd.fifoin.put(var.encode('ascii', 'backslashreplace'))
retour = self.hapticd.readsep(r"\|", size)[0]
for i in range(0, size):
self.assertEqual(float(retour[i]), result[i])
def teststartstop(self):
""" Launch and quit """
self.hapticd.launch()
time.sleep(1)
self.hapticd.quit()
if __name__ == '__main__':
unittest.main()