1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
|
3
|
|
|
"""Tests for the elementlength class""" |
4
|
|
|
|
5
|
|
|
import unittest |
6
|
|
|
|
7
|
|
|
from starstruct.elementlength import ElementLength |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
# pylint: disable=line-too-long,invalid-name |
11
|
|
|
class TestElementLength(unittest.TestCase): |
12
|
|
|
"""ElementLength module tests""" |
13
|
|
|
|
14
|
|
|
def test_valid(self): |
15
|
|
|
"""Test field formats that are valid ElementLength elements.""" |
16
|
|
|
|
17
|
|
|
test_fields = [ |
18
|
|
|
('a', 'B', 'data'), # unsigned byte: 0, 255 |
19
|
|
|
('b', 'H', 'data'), # unsigned short: 0, 65535 |
20
|
|
|
('d', 'L', 'data'), # unsigned long: 0, 2^32-1 |
21
|
|
|
('d', 'Q', 'data'), # unsigned long long: 0, 2^64-1 |
22
|
|
|
] |
23
|
|
|
|
24
|
|
|
for field in test_fields: |
25
|
|
|
with self.subTest(field): # pylint: disable=no-member |
26
|
|
|
out = ElementLength.valid(field) |
27
|
|
|
self.assertTrue(out) |
28
|
|
|
|
29
|
|
|
def test_not_valid(self): |
30
|
|
|
"""Test field formats that are not valid ElementLength elements.""" |
31
|
|
|
|
32
|
|
|
test_fields = [ |
33
|
|
|
('a', '4x', 'data'), # 4 pad bytes |
34
|
|
|
('b', 'z', 'data'), # invalid |
35
|
|
|
('c', '1', 'data'), # invalid |
36
|
|
|
('d', '10s', 'data'), # 10 byte string |
37
|
|
|
('e', '9S', 'data'), # invalid (must be lowercase) |
38
|
|
|
('f', '/', 'data'), # invalid |
39
|
|
|
('g', '?', 'data'), # bool: 0, 1 |
40
|
|
|
('h', '10s', 'data'), # 10 byte string |
41
|
|
|
('i', 'b', 'data'), # unsigned byte: 0, 255 |
42
|
|
|
('j', 'h', 'data'), # unsigned short: 0, 65535 |
43
|
|
|
('k', 'l', 'data'), # unsigned long: 0, 2^32-1 |
44
|
|
|
('l', 'B'), # unsigned byte (no ref string) |
45
|
|
|
] |
46
|
|
|
|
47
|
|
|
for field in test_fields: |
48
|
|
|
with self.subTest(field): # pylint: disable=no-member |
49
|
|
|
out = ElementLength.valid(field) |
50
|
|
|
self.assertFalse(out) |
51
|
|
|
|