/
test_util.py
executable file
·63 lines (52 loc) · 2.06 KB
/
test_util.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
"""
Tests for rg16 utilities.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.builtins import * # NOQA
import unittest
from io import BytesIO
from obspy.io.rg16.util import _read, _read_bcd, _read_binary
class TestRG16Util(unittest.TestCase):
def byte_io(self, byte_str):
"""
write byte_str to BytesIO object, return.
"""
return BytesIO(byte_str)
def test_read_bcd(self):
"""
Ensure bcd encoding returns expected values.
"""
bcd = [(b'\x99', 1, True, 99), (b'\x99\x01', 2, True, 9901),
(b'\x91', 0.5, True, 9), (b'\x91', 0.5, False, 1),
(b'\x99\01', 1.5, True, 990), (b'\x99\01', 1.5, False, 901)]
for byte, length, left_part, answer in bcd:
out = _read_bcd(BytesIO(byte), length, left_part)
self.assertEqual(out, answer)
with self.assertRaises(ValueError) as e:
_read_bcd(BytesIO(b'\xFF'), 1, True)
self.assertIn('invalid bcd values', str(e.exception))
def test_read_binary(self):
"""
Ensure binary encoding return expected values.
"""
binary = [(b'\x99', 1, True, 153), (b'\x99\x01', 2, True, 39169),
(b'\x91', 0.5, True, 9), (b'\x91', 0.5, False, 1),
(b'\x76\x23\x14', 3, True, 7742228),
(b'\x00\x10\x00\x10', 4, True, 1048592),
(b'\x10\xed\x7f\x01\x00\x00\x08\xc0', 8,
True, 1219770716358969536)]
for byte, length, left_part, answer in binary:
out = _read_binary(BytesIO(byte), length, left_part)
self.assertEqual(out, answer)
def test_read(self):
"""
Ensure IEEE float are well returned in the function _read.
"""
ieee = b'\x40\x48\xf5\xc3'
out = _read(BytesIO(ieee), 0, 4, 'IEEE')
self.assertAlmostEqual(out, 3.14, delta=1e-6)
def suite():
return unittest.makeSuite(TestRG16Util, 'test')
if __name__ == '__main__':
unittest.main()