-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_embedded_constants.py
More file actions
424 lines (407 loc) · 13.7 KB
/
test_embedded_constants.py
File metadata and controls
424 lines (407 loc) · 13.7 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import os
import unittest
import subprocess
import ljmmm
import generate_embedded_constants as genconsts
# Get the register directory and conflict directory data structures
# The register directory is a list of dictonaries. the dictionaries hold:
# "crc": (crc32/POSIX)
# "address": (index in conflict directory if there is a conflict)
# "data_type": (if there is a conflict upper nibble is index location, 0xF if
# no index, lower is conflict location)
# "conflict_mode": (o if no conflict, 1 if there is a conflict)
# "short_name": (register name without numbers)
# The conflict directory is a dictionary whose key is the register name and
# the "values" are dictonaries containing the register:
# "conflict_num":
# "address":
# "data_type":
# "conflict_mode":
# "short_name":
# "all_nums": (list of numbers found in the register name)
reg_dir, conflict_dir, num_dup_registers = genconsts.generate(False)
def generate_test_directories(test_registers, test_conflict_dir, test_reg_dir):
test_dup_registers = 0
for reg in test_registers:
test_reg_dir, test_conflict_dir, test_dup_registers = genconsts.extract_reg_data(
reg,
test_reg_dir,
test_conflict_dir,
test_dup_registers
)
conflict_duplicates = 0
test_conflict_dir, test_reg_dir, conflict_duplicates = genconsts.check_and_sort_registers(
test_conflict_dir,
test_reg_dir
)
test_dup_registers += conflict_duplicates
return (test_conflict_dir, test_reg_dir, test_dup_registers)
class EmbeddedConstantsTests(unittest.TestCase):
# Check crc related items
def test_crc_val_correct(self):
for reg in reg_dir:
# Test a low crc number
if (reg["short_name"] == "ONEWIRE_ROM_BRANCHS_FOUND_H"):
self.assertEqual(reg["crc"], "0x00734919")
if (reg["short_name"] == "STREAM_OUT_BUFFER_F"):
self.assertEqual(reg["crc"], "0x4477BBF5")
if (reg["short_name"] == "IC_DATA_TX"):
self.assertEqual(reg["crc"], "0x02C958F4")
if (reg["short_name"] == "AIN_BIN"):
self.assertEqual(reg["crc"], "0x64EACE2A")
if (reg["short_name"] == "DIO"):
self.assertEqual(reg["crc"], "0x9B684F2B")
# Test a high crc number
if (reg["short_name"] == "IC_SDA_DIONUM"):
self.assertEqual(reg["crc"], "0xFFEA11A5")
def test_crc_order(self):
last_crc = "0X00000000"
for i in range(0,len(reg_dir)):
crc_int = int(reg_dir[i]["crc"], 16)
last_crc_int = int(last_crc, 16)
if (crc_int < last_crc_int):
bad_sort = True
print("bad sort with these crc's:")
print("\tlast: %s current: %s" % (last_crc, reg_dir[i]["crc"]))
else:
bad_sort = False
self.assertFalse(bad_sort)
last_crc = reg_dir[i]["crc"]
def test_dup_crc_vals(self):
for i in range(0,len(reg_dir)):
if (i != 0):
# Register directory is sorted by crc, so if there are
# duplicate crc's they are next to each other
self.assertNotEqual(reg_dir[i-1]["crc"], reg_dir[i]["crc"])
# Check conflict and duplicate register detection
def test_conflict_and_duplicate_detection(self):
test_reg_dir = []
test_conflict_dir = {}
num_conflicts = 0
num_type_2_conflicts = 0
test_registers = [
# Hash conflict and index conflict
{
"name":"TEST(0:3)_INDEX_32",
"address":42343,
"type":"UINT16",
},
{
"name":"TEST(4:9)_INDEX_32",
"address":42344,
"type":"UINT16",
},
{
"name":"TEST(0:3)_INDEX_64",
"address":43242,
"type":"UINT16",
},
# Hash and index conflict scenario 2
{
"name":"TEST_64_INDEX(0:3)",
"address":4324,
"type":"UINT16",
},
{
"name":"TEST_32_INDEX(0:3)",
"address":4325,
"type":"UINT16",
},
# Index conflict
{
"name":"TEST(0:3)",
"address":1233,
"type":"UINT16",
},
{
"name":"TEST(4:9)",
"address":1234,
"type":"UINT16",
},
# Hash conflict
{
"name":"TEST_F32",
"address":1,
"type":"UINT16",
},
{
"name":"TEST_F64",
"address":2,
"type":"UINT16",
},
# Hash conflict scenario 2
{
"name":"2TEST_NAME",
"address":5,
"type":"UINT16",
},
{
"name":"3TEST_NAME",
"address":6,
"type":"UINT16",
},
# No conflict
{
"name":"TEST_A",
"address":43,
"type":"UINT16",
},
# Test duplicate entries (address and hash)
{
"name":"TEST_B",
"address":44,
"type":"UINT16",
},
{
"name":"TEST_C",
"address":44,
"type":"UINT16",
},
{
"name":"TEST_C",
"address":44,
"type":"UINT16",
},
# Conflict type 2
{
"name":"NEW1",
"address":88,
"type":"UINT16",
},
# Conflict type 2 scenario 2
{
"name":"1NEW_A",
"address":88,
"type":"UINT16",
},
# Test NOT conflict type 2, conflict type 1
{
"name":"NEW(0:4)_F32",
"address":87,
"type":"UINT16",
},
# Test NOT conflict type 2, conflict type 1 scenario 2
{
"name":"NEW32(0:4)_B",
"address":86,
"type":"UINT16",
},
]
test_conflict_dir, test_reg_dir, test_dup_registers = generate_test_directories(
test_registers,
test_conflict_dir,
test_reg_dir
)
for reg in test_reg_dir:
if (reg["conflict_mode"] == 1):
num_conflicts += 1
if (reg["conflict_mode"] == 2):
num_type_2_conflicts += 1
self.assertEqual(num_conflicts, 6)
self.assertEqual(num_type_2_conflicts, 2)
self.assertEqual(len(test_conflict_dir), 6)
self.assertEqual(len(test_reg_dir), 12)
self.assertEqual(test_dup_registers, 3)
# Check conflict dir size
def test_conflict_dir_size(self):
conflict_dir_size = 0
for reg in reg_dir:
if(reg["conflict_mode"] == 1):
conflict_dir_size += 1
self.assertEqual(conflict_dir_size, len(conflict_dir))
# Check num registers size (num in register list and conflict tables)
def test_num_registers(self):
modbus_maps = ljmmm.get_device_modbus_maps(
src='LabJack/LJM/ljm_constants.json',
expand_names=False,
expand_alt_names=True,
)
all_reg_names = []
for device in modbus_maps:
for reg in modbus_maps[device]:
name = reg["name"]
if (not name in all_reg_names):
all_reg_names.append(name)
new_num_registers = len(reg_dir) + num_dup_registers
for table_name in conflict_dir:
new_num_registers += len(conflict_dir[table_name])
new_num_registers -= len(conflict_dir)
self.assertEqual(new_num_registers, len(all_reg_names))
# Check register addresses
def test_conflict_reg_addresses(self):
test_reg_dir = []
test_conflict_dir = {}
num_conflicts = 0
test_registers = [
{
"name":"TEST_A",
"address":0,
"type":"UINT16",
},
{
"name":"TEST_F32",
"address":1,
"type":"UINT16",
},
{
"name":"TEST_F64",
"address":2,
"type":"UINT16",
},
{
"name":"TEST_B(0:3)",
"address":3,
"type":"UINT16",
},
{
"name":"TEST_B(4:7)",
"address":4,
"type":"UINT16",
},
{
"name":"TEST1(0:3)",
"address":5,
"type":"UINT16",
},
{
"name":"TEST1(4:7)",
"address":6,
"type":"UINT16",
},
{
"name":"TESTH5(0:3)",
"address":-9,
"type":"UINT16",
},
{
"name":"TESTG4(0:3)",
"address":-9,
"type":"UINT16",
},
{
"name":"TESTF3(0:3)",
"address":-9,
"type":"UINT16",
},
{
"name":"TESTE1(0:3)",
"address":-9,
"type":"UINT16",
},
{
"name":"TESTD1(0:3)",
"address":-9,
"type":"UINT16",
},
]
test_conflict_dir, test_reg_dir, test_dup_registers = generate_test_directories(
test_registers,
test_conflict_dir,
test_reg_dir
)
for reg in test_reg_dir:
if (reg["short_name"] == "TEST_A"):
self.assertEqual(reg["address"], 0)
if (reg["short_name"] == "TEST_F"):
self.assertEqual(reg["address"], 0)
if (reg["short_name"] == "TEST_B"):
self.assertEqual(reg["address"], 3)
if (reg["short_name"] == "TEST"):
self.assertEqual(reg["address"], 1)
if (reg["short_name"] == "TESTD"):
self.assertEqual(reg["address"], 6)
if (reg["short_name"] == "TESTE"):
self.assertEqual(reg["address"], 5)
if (reg["short_name"] == "TESTF"):
self.assertEqual(reg["address"], 4)
if (reg["short_name"] == "TESTG"):
self.assertEqual(reg["address"], 3)
if (reg["short_name"] == "TESTH"):
self.assertEqual(reg["address"], 2)
for table_name in test_conflict_dir:
table = test_conflict_dir[table_name]
if (table_name == "TEST_F"):
self.assertEqual(table[0]["address"], 1)
self.assertEqual(table[1]["address"], 2)
if (table_name == "TEST"):
self.assertEqual(table[0]["address"], 5)
# Check register data types
def test_data_types(self):
test_reg_dir = []
test_conflict_dir = {}
num_conflicts = 0
test_registers = [
{
"name":"TEST_A",
"address":0,
"type":"UINT32",
},
{
"name":"TEST_F32",
"address":1,
"type":"UINT16",
},
{
"name":"TEST_F64",
"address":2,
"type":"UINT16",
},
{
"name":"TEST_B(0:3)",
"address":3,
"type":"BYTE",
},
{
"name":"TEST_B(4:7)",
"address":4,
"type":"BYTE",
},
{
"name":"TEST1(0:3)",
"address":5,
"type":"FLOAT32",
},
{
"name":"TEST1(4:7)",
"address":6,
"type":"FLOAT32",
},
{
"name":"TEST1(0:3)_FB32",
"address":7,
"type":"STRING",
},
{
"name":"TEST1(0:3)_FB64",
"address":8,
"type":"STRING",
},
]
test_conflict_dir, test_reg_dir, test_dup_registers = generate_test_directories(
test_registers,
test_conflict_dir,
test_reg_dir
)
for reg in test_reg_dir:
if (reg["short_name"] == "TEST_A"):
self.assertEqual(reg["data_type"], 1)
if (reg["short_name"] == "TEST_B"):
self.assertEqual(reg["data_type"], 99)
if (reg["short_name"] == "TEST_F"):
self.assertEqual(reg["data_type"], "0xF0")
if (reg["short_name"] == "TEST"):
self.assertEqual(reg["data_type"], "0x10")
if (reg["short_name"] == "TEST_FB"):
self.assertEqual(reg["data_type"], "0x12")
for table_name in test_conflict_dir:
table = test_conflict_dir[table_name]
if (table_name == "TEST_F"):
self.assertEqual(table[0]["data_type"], 0)
self.assertEqual(table[1]["data_type"], 0)
if (table_name == "TEST"):
self.assertEqual(table[0]["data_type"], 5)
if (table_name == "TEST_FB"):
self.assertEqual(table[0]["data_type"], 98)
self.assertEqual(table[1]["data_type"], 98)
if __name__ == "__main__":
unittest.main()