forked from danielhrisca/asammdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mdf4.py
134 lines (104 loc) · 3.86 KB
/
test_mdf4.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
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
#!/usr/bin/env python
from pathlib import Path
import tempfile
import unittest
import numpy as np
from asammdf import MDF, Signal
from asammdf.blocks.mdf_v4 import MDF4
CHANNEL_LEN = 100000
class TestMDF4(unittest.TestCase):
tempdir = None
@classmethod
def setUpClass(cls):
cls.tempdir = tempfile.TemporaryDirectory()
def test_measurement(self):
self.assertTrue(MDF4)
def test_read_mdf4_00(self):
seed = np.random.randint(0, 2**31)
np.random.seed(seed)
print("Read 4.00 using seed =", seed)
sig_int = Signal(
np.random.randint(-(2**31), 2**31, CHANNEL_LEN),
np.arange(CHANNEL_LEN),
name="Integer Channel",
unit="unit1",
)
sig_float = Signal(
np.random.random(CHANNEL_LEN),
np.arange(CHANNEL_LEN),
name="Float Channel",
unit="unit2",
)
with MDF(version="4.00") as mdf:
mdf.append([sig_int, sig_float], common_timebase=True)
outfile = mdf.save(Path(TestMDF4.tempdir.name) / "tmp", overwrite=True)
with MDF(outfile) as mdf:
ret_sig_int = mdf.get(sig_int.name)
ret_sig_float = mdf.get(sig_float.name)
self.assertTrue(np.array_equal(ret_sig_int.samples, sig_int.samples))
self.assertTrue(np.array_equal(ret_sig_float.samples, sig_float.samples))
def test_read_mdf4_10(self):
seed = np.random.randint(0, 2**31)
np.random.seed(seed)
print("Read 4.10 using seed =", seed)
sig_int = Signal(
np.random.randint(-(2**31), 2**31, CHANNEL_LEN),
np.arange(CHANNEL_LEN),
name="Integer Channel",
unit="unit1",
)
sig_float = Signal(
np.random.random(CHANNEL_LEN),
np.arange(CHANNEL_LEN),
name="Float Channel",
unit="unit2",
)
with MDF(version="4.10") as mdf:
mdf.append([sig_int, sig_float], common_timebase=True)
outfile = mdf.save(Path(TestMDF4.tempdir.name) / "tmp", overwrite=True)
with MDF(outfile) as mdf:
ret_sig_int = mdf.get(sig_int.name)
ret_sig_float = mdf.get(sig_float.name)
self.assertTrue(np.array_equal(ret_sig_int.samples, sig_int.samples))
self.assertTrue(np.array_equal(ret_sig_float.samples, sig_float.samples))
def test_attachment_blocks_wo_filename(self):
original_data = b"Testing attachemnt block\nTest line 1"
mdf = MDF()
mdf.attach(
original_data,
file_name=None,
comment=None,
compression=True,
mime=r"text/plain",
embedded=True,
)
outfile = mdf.save(
Path(TestMDF4.tempdir.name) / "attachment.mf4", overwrite=True
)
with MDF(outfile) as attachment_mdf:
data, filename, md5_sum = attachment_mdf.extract_attachment(index=0)
self.assertEqual(data, original_data)
self.assertEqual(filename, Path("bin.bin"))
mdf.close()
def test_attachment_blocks_w_filename(self):
original_data = b"Testing attachemnt block\nTest line 1"
original_file_name = "file.txt"
mdf = MDF()
mdf.attach(
original_data,
file_name=original_file_name,
comment=None,
compression=True,
mime=r"text/plain",
embedded=True,
)
outfile = mdf.save(
Path(TestMDF4.tempdir.name) / "attachment.mf4", overwrite=True
)
with MDF(outfile) as attachment_mdf:
data, filename, md5_sum = attachment_mdf.extract_attachment(index=0)
self.assertEqual(data, original_data)
self.assertEqual(filename, Path(original_file_name))
mdf.close()
if __name__ == "__main__":
unittest.main()