summaryrefslogtreecommitdiffstats
path: root/src/target/trx_toolkit/test_data_msg.py
blob: ec6c2ec1d2a799a0b14a2ceb972fc2ec2f617cdd (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# TRX Toolkit
# Unit test for TRXD message codec
#
# (C) 2019 by Vadim Yanitskiy <axilirator@gmail.com>
#
# All Rights Reserved
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

import unittest

from data_msg import Msg, TxMsg, RxMsg

class Msg_Test(unittest.TestCase):
	# Compare message a with message b
	def _compare_msg(self, a, b):
		# Make sure we're comparing messages of the same type
		self.assertEqual(a.__class__, b.__class__)

		# Compare common header fields
		self.assertEqual(a.ver, b.ver)
		self.assertEqual(a.fn, b.fn)
		self.assertEqual(a.tn, b.tn)

		# Burst bits (if present)
		self.assertEqual(a.burst, b.burst)

		# TxMsg specific fields
		if isinstance(a, TxMsg):
			self.assertEqual(a.pwr, b.pwr)

		# RxMsg specific fields
		if isinstance(a, RxMsg):
			# Version independent fields
			self.assertEqual(a.toa256, b.toa256)
			self.assertEqual(a.rssi, b.rssi)

			# Version specific fields
			if a.ver >= 1:
				self.assertEqual(a.nope_ind, b.nope_ind)
				self.assertEqual(a.mod_type, b.mod_type)
				self.assertEqual(a.tsc_set, b.tsc_set)
				self.assertEqual(a.tsc, b.tsc)
				self.assertEqual(a.ci, b.ci)

	# Make sure that message validation throws a ValueError
	def test_validate(self):
		# Unknown version
		with self.assertRaises(ValueError):
			msg = RxMsg(fn = 0, tn = 0, ver = 100)
			msg.validate()

		# Uninitialized field
		with self.assertRaises(ValueError):
			msg = RxMsg()
			msg.validate()
		with self.assertRaises(ValueError):
			msg = RxMsg(fn = None, tn = 0)
			msg.validate()

		# Out-of-range value(s)
		with self.assertRaises(ValueError):
			msg = RxMsg(fn = -1, tn = 0)
			msg.validate()
		with self.assertRaises(ValueError):
			msg = RxMsg(fn = 0, tn = 10)
			msg.validate()

	# Validate header and burst randomization
	def test_rand_hdr_burst(self):
		tx_msg = TxMsg()
		rx_msg = RxMsg()

		for i in range(100):
			tx_msg.rand_burst()
			rx_msg.rand_burst()
			tx_msg.rand_hdr()
			rx_msg.rand_hdr()

			tx_msg.validate()
			rx_msg.validate()

	def _test_enc_dec(self, msg, legacy = False, nope_ind = False):
		# Prepare a given message (randomize)
		msg.rand_hdr()

		# NOPE.ind contains no burst
		if not nope_ind:
			msg.rand_burst()
		else:
			msg.nope_ind = True
			msg.mod_type = None
			msg.tsc_set = None
			msg.tsc = None

		# Encode a given message to bytes
		msg_enc = msg.gen_msg(legacy)

		# Decode a new message from bytes
		msg_dec = msg.__class__()
		msg_dec.parse_msg(msg_enc)

		# Compare decoded vs the original
		self._compare_msg(msg, msg_dec)

	# Validate encoding and decoding
	def test_enc_dec(self):
		for ver in Msg.KNOWN_VERSIONS:
			with self.subTest("TxMsg", ver = ver):
				msg = TxMsg(ver = ver)
				self._test_enc_dec(msg)

			with self.subTest("RxMsg", ver = ver):
				msg = RxMsg(ver = ver)
				self._test_enc_dec(msg)

			if ver >= 1:
				with self.subTest("RxMsg NOPE.ind", ver = ver):
					msg = RxMsg(ver = ver)
					self._test_enc_dec(msg, nope_ind = True)

		with self.subTest("RxMsg (legacy transceiver)"):
			msg = RxMsg(ver = 0)
			self._test_enc_dec(msg, legacy = True)

	# Validate bit conversations
	def test_bit_conv(self):
		usbits_ref = list(range(0, 256))
		sbits_ref = list(range(-127, 128))

		# Test both usbit2sbit() and sbit2usbit()
		sbits = Msg.usbit2sbit(usbits_ref)
		usbits = Msg.sbit2usbit(sbits)
		self.assertEqual(usbits[:255], usbits_ref[:255])
		self.assertEqual(usbits[255], 254)

		# Test both sbit2ubit() and ubit2sbit()
		ubits = Msg.sbit2ubit(sbits_ref)
		self.assertEqual(ubits, ([1] * 127 + [0] * 128))

		sbits = Msg.ubit2sbit(ubits)
		self.assertEqual(sbits, ([-127] * 127 + [127] * 128))

	def _test_transform(self, msg):
		# Prepare given messages
		msg.rand_hdr()
		msg.rand_burst()

		# Perform message transformation
		if isinstance(msg, TxMsg):
			msg_trans = msg.trans()
		else:
			msg_trans = msg.trans()

		self.assertEqual(msg_trans.ver, msg.ver)
		self.assertEqual(msg_trans.fn, msg.fn)
		self.assertEqual(msg_trans.tn, msg.tn)

		if isinstance(msg, RxMsg):
			burst = Msg.sbit2ubit(msg.burst)
			self.assertEqual(msg_trans.burst, burst)
		else:
			burst = Msg.ubit2sbit(msg.burst)
			self.assertEqual(msg_trans.burst, burst)

	# Validate message transformation
	def test_transform(self):
		for ver in Msg.KNOWN_VERSIONS:
			with self.subTest("TxMsg", ver = ver):
				msg = TxMsg(ver = ver)
				self._test_transform(msg)

			with self.subTest("RxMsg", ver = ver):
				msg = RxMsg(ver = ver)
				self._test_transform(msg)

if __name__ == '__main__':
	unittest.main()