forked from ian-llewellyn/manchester-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanchester-test.py
64 lines (50 loc) · 2.14 KB
/
manchester-test.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import unittest
from manchester import Manchester
bits = '101010'
symbols = '100110011001'
diff_symbols = ['110100101101', '001011010010']
invert_bits = ''.join([i == '1' and '0' or '1' for i in bits])
invert_symbols = ''.join([i == '1' and '0' or '1' for i in symbols])
invert_diff_symbols = ['101101001011', '010010110100']
class ManchesterEncodeTests(unittest.TestCase):
def setUp(self):
self.codec = Manchester()
def test_encode(self):
self.assertEqual(self.codec.encode(bits), symbols)
def test_invert_encode(self):
self.codec.invert()
self.assertEqual(self.codec.encode(bits), invert_symbols)
class ManchesterDecodeTests(unittest.TestCase):
def setUp(self):
self.codec = Manchester()
def test_decode(self):
self.assertEqual(self.codec.decode(symbols), bits)
def test_invert_decode(self):
self.codec.invert()
self.assertEqual(self.codec.decode(symbols), invert_bits)
class DifferentialManchesterEncodeTests(unittest.TestCase):
def setUp(self):
self.codec = Manchester(differential=True)
def test_encode(self):
self.assertIn(self.codec.encode(bits), diff_symbols)
for symbols in diff_symbols:
init_level = symbols[0] == '1' and '0' or '1'
self.assertEqual(self.codec.encode(bits, init_level=init_level), symbols)
def test_invert_encode(self):
self.assertIn(self.codec.encode(invert_bits), invert_diff_symbols)
for symbols in invert_diff_symbols:
init_level = symbols[0] == '1' and '0' or '1'
self.assertEqual(self.codec.encode(invert_bits, init_level=init_level), symbols)
class DifferentialManchesterDecodeTests(unittest.TestCase):
def setUp(self):
self.codec = Manchester(differential=True)
def test_decode(self):
for symbols in diff_symbols:
self.assertEqual(self.codec.decode(symbols), bits)
def test_invert_decode(self):
for invert_symbols in invert_diff_symbols:
self.assertEqual(self.codec.decode(invert_symbols), invert_bits)
if __name__ == '__main__':
unittest.main()