-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_comm.py
70 lines (44 loc) · 1.78 KB
/
test_comm.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
#!/usr/bin/env python3
"""
Copyright (C) 2016 Interactive Brokers LLC. All rights reserved. This code is
subject to the terms and conditions of the IB API Non-Commercial License or the
IB API Commercial License, as applicable.
"""
import unittest
import struct
import comm
class CommTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_make_msg(self):
text = "ABCD"
msg = comm.make_msg(text)
size = struct.unpack("!I", msg[0:4])[0]
self.assertEqual(size, len(text), "msg size not good")
self.assertEqual(msg[4:].decode(), text, "msg payload not good")
def test_make_field(self):
text = "ABCD"
field = comm.make_field(text)
self.assertEqual(field[-1], "\0", "terminator not good")
self.assertEqual(len(field[0:-1]), len(text), "payload size not good")
self.assertEqual(field[0:-1], text, "payload not good")
def test_read_msg(self):
text = "ABCD"
msg = comm.make_msg(text)
(size, text2, rest) = comm.read_msg(msg)
self.assertEqual(size, len(text), "msg size not good")
self.assertEqual(text2.decode(), text, "msg payload not good")
self.assertEqual(len(rest), 0, "there should be no remainder msg")
def test_readFields(self):
text1 = "ABCD"
text2 = "123"
msg = comm.make_msg(comm.make_field(text1) + comm.make_field(text2))
(size, text, rest) = comm.read_msg(msg)
fields = comm.read_fields(text)
self.assertEqual(len(fields), 2, "incorrect number of fields")
self.assertEqual(fields[0].decode(), text1)
self.assertEqual(fields[1].decode(), text2)
if "__main__" == __name__:
unittest.main()