Debian Codegraph @

test/test_endianess.py

raw
1 #!/usr/bin/env python
2 from pathlib import Path
3 import tempfile
4 import typing
5 import unittest
6
7 import numpy as np
8
9 from asammdf import MDF, Signal
10 from asammdf.blocks import mdf_v3, mdf_v4
11 import asammdf.blocks.v2_v3_constants as v23c
12 import asammdf.blocks.v4_constants as v4c
13
14
15 class TestEndianess(unittest.TestCase):
16 tempdir: tempfile.TemporaryDirectory[str]
17
18 @classmethod
19 def setUpClass(cls) -> None:
20 cls.tempdir = tempfile.TemporaryDirectory()
21
22 def test_mixed(self) -> None:
23 t = np.arange(15, dtype="<f8")
24
25 s1 = Signal(np.frombuffer(b"\x00\x00\x00\x02" * 15, dtype=">u4"), t, name="Motorola")
26
27 s2 = Signal(np.frombuffer(b"\x04\x00\x00\x00" * 15, dtype="<u4"), t, name="Intel")
28
29 for version in ("3.30", "4.10"):
30 mdf = MDF(version=version)
31 mdf.append([s1, s2], common_timebase=True)
32 outfile = mdf.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
33 mdf.close()
34
35 with MDF(outfile) as mdf:
36 assert np.array_equal(mdf.get("Motorola").samples, [2] * 15)
37 assert np.array_equal(mdf.get("Intel").samples, [4] * 15)
38
39 for version in ("3.30", "4.10"):
40 mdf = MDF(version=version)
41 mdf.append([s2, s1], common_timebase=True)
42 outfile = mdf.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
43 mdf.close()
44
45 with MDF(outfile) as mdf:
46 assert np.array_equal(mdf.get("Motorola").samples, [2] * 15)
47 assert np.array_equal(mdf.get("Intel").samples, [4] * 15)
48
49 def test_not_aligned_mdf_v3(self) -> None:
50 t = np.arange(15, dtype="<f8")
51
52 s1 = Signal(np.frombuffer(b"\x00\x00\x3f\x02" * 15, dtype=">u4"), t, name="Motorola")
53
54 s2 = Signal(np.frombuffer(b"\x04\xf8\x00\x00" * 15, dtype="<u4"), t, name="Intel")
55
56 s3 = Signal(
57 np.frombuffer(b"\xbb\x55" * 2 * 15, dtype="<u4"),
58 t,
59 name="NotAlignedMotorola",
60 )
61
62 s4 = Signal(np.frombuffer(b"\xbb\x55" * 2 * 15, dtype="<u4"), t, name="NotAlignedIntel")
63
64 with MDF(version="3.30") as mdf_source:
65 mdf_source.append([s1, s2, s3, s4], common_timebase=True)
66
67 mdf3 = typing.cast(mdf_v3.MDF3, mdf_source._mdf)
68
69 ch3 = mdf3.groups[0].channels[3]
70 ch3.start_offset = 24 + t.itemsize * 8
71 ch3.data_type = v23c.DATA_TYPE_UNSIGNED_MOTOROLA
72 ch3.bit_count = 16
73
74 ch4 = mdf3.groups[0].channels[4]
75 ch4.start_offset = 24 + t.itemsize * 8
76 ch4.data_type = v23c.DATA_TYPE_UNSIGNED_INTEL
77 ch4.bit_count = 16
78
79 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
80
81 with MDF(outfile) as mdf:
82 assert np.array_equal(mdf.get("NotAlignedMotorola").samples, [0x0204] * 15)
83 assert np.array_equal(mdf.get("NotAlignedIntel").samples, [0x0402] * 15)
84
85 ch3 = mdf3.groups[0].channels[3]
86 ch3.start_offset = 16 + t.itemsize * 8
87 ch3.data_type = v23c.DATA_TYPE_UNSIGNED_MOTOROLA
88 ch3.bit_count = 24
89
90 ch4 = mdf3.groups[0].channels[4]
91 ch4.start_offset = 16 + t.itemsize * 8
92 ch4.data_type = v23c.DATA_TYPE_UNSIGNED_INTEL
93 ch4.bit_count = 24
94
95 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
96
97 with MDF(outfile) as mdf:
98 assert np.array_equal(mdf.get("NotAlignedMotorola").samples, [0x3F0204] * 15)
99
100 assert np.array_equal(mdf.get("NotAlignedIntel").samples, [0x04023F] * 15)
101
102 ch3 = mdf3.groups[0].channels[3]
103 ch3.start_offset = 16 + t.itemsize * 8 + 5
104 ch3.data_type = v23c.DATA_TYPE_UNSIGNED_MOTOROLA
105 ch3.bit_count = 21
106
107 ch4 = mdf3.groups[0].channels[4]
108 ch4.start_offset = 16 + t.itemsize * 8 + 6
109 ch4.data_type = v23c.DATA_TYPE_UNSIGNED_INTEL
110 ch4.bit_count = 21
111
112 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
113
114 with MDF(outfile) as mdf:
115 assert np.array_equal(
116 mdf.get("NotAlignedMotorola").samples,
117 [(0x3F0204F8 >> 5) & (2**21 - 1)] * 15,
118 )
119
120 assert np.array_equal(
121 mdf.get("NotAlignedIntel").samples,
122 [(0xF8040200 >> 6) & (2**21 - 1)] * 15,
123 )
124
125 def test_not_aligned_mdf_v4(self) -> None:
126 t = np.arange(15, dtype="<f8")
127
128 s1 = Signal(np.frombuffer(b"\x00\x00\x3f\x02" * 15, dtype=">u4"), t, name="Motorola")
129
130 s2 = Signal(np.frombuffer(b"\x04\xf8\x00\x00" * 15, dtype="<u4"), t, name="Intel")
131
132 s3 = Signal(
133 np.frombuffer(b"\xbb\x55" * 2 * 15, dtype="<u4"),
134 t,
135 name="NotAlignedMotorola",
136 )
137
138 s4 = Signal(np.frombuffer(b"\xbb\x55" * 2 * 15, dtype="<u4"), t, name="NotAlignedIntel")
139
140 with MDF(version="4.11") as mdf_source:
141 mdf_source.append([s1, s2, s3, s4], common_timebase=True)
142
143 mdf4 = typing.cast(mdf_v4.MDF4, mdf_source._mdf)
144
145 ch3 = mdf4.groups[0].channels[3]
146 ch3.byte_offset = 3 + t.itemsize
147 ch3.data_type = v4c.DATA_TYPE_UNSIGNED_MOTOROLA
148 ch3.bit_count = 16
149
150 ch4 = mdf4.groups[0].channels[4]
151 ch4.byte_offset = 3 + t.itemsize
152 ch4.data_type = v4c.DATA_TYPE_UNSIGNED_INTEL
153 ch4.bit_count = 16
154
155 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
156
157 with MDF(outfile) as mdf:
158 assert np.array_equal(mdf.get("NotAlignedMotorola").samples, [0x0204] * 15)
159
160 assert np.array_equal(mdf.get("NotAlignedIntel").samples, [0x0402] * 15)
161
162 ch3 = mdf4.groups[0].channels[3]
163 ch3.byte_offset = 2 + t.itemsize
164 ch3.data_type = v4c.DATA_TYPE_UNSIGNED_MOTOROLA
165 ch3.bit_count = 24
166
167 ch4 = mdf4.groups[0].channels[4]
168 ch4.byte_offset = 2 + t.itemsize
169 ch4.data_type = v4c.DATA_TYPE_UNSIGNED_INTEL
170 ch4.bit_count = 24
171
172 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
173
174 with MDF(outfile) as mdf:
175 assert np.array_equal(mdf.get("NotAlignedMotorola").samples, [0x3F0204] * 15)
176
177 assert np.array_equal(mdf.get("NotAlignedIntel").samples, [0x04023F] * 15)
178
179 ch3 = mdf4.groups[0].channels[3]
180 ch3.byte_offset = 2 + t.itemsize
181 ch3.data_type = v4c.DATA_TYPE_UNSIGNED_MOTOROLA
182 ch3.bit_count = 21
183 ch3.bit_offset = 5
184
185 ch4 = mdf4.groups[0].channels[4]
186 ch4.byte_offset = 2 + t.itemsize
187 ch4.data_type = v4c.DATA_TYPE_UNSIGNED_INTEL
188 ch4.bit_count = 21
189 ch4.bit_offset = 6
190
191 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
192
193 with MDF(outfile) as mdf:
194 assert np.array_equal(
195 mdf.get("NotAlignedMotorola").samples,
196 [(0x3F0204F8 >> 5) & (2**21 - 1)] * 15,
197 )
198
199 assert np.array_equal(
200 mdf.get("NotAlignedIntel").samples,
201 [(0xF8040200 >> 6) & (2**21 - 1)] * 15,
202 )
203
204 def test_overlapping_channels_mdf_v3(self) -> None:
205 t = np.arange(15, dtype="<f8")
206
207 s1 = Signal(
208 np.frombuffer(b"\x00\x00\x3f\x02\x04\xf8\x00\x00" * 15, dtype="<u8"),
209 t,
210 name="Parent",
211 )
212
213 s2 = Signal(
214 np.frombuffer(b"\x04\xf8\x00\x00" * 15, dtype="<u4"),
215 t,
216 name="OverlappingMotorola",
217 )
218
219 s3 = Signal(
220 np.frombuffer(b"\x04\x00\x00\x00" * 15, dtype="<u4"),
221 t,
222 name="OverlappingIntel",
223 )
224
225 with MDF(version="3.30") as mdf_source:
226 mdf_source.append([s1, s2, s3], common_timebase=True)
227
228 mdf3 = typing.cast(mdf_v3.MDF3, mdf_source._mdf)
229
230 for parent_data_type in (
231 v23c.DATA_TYPE_DOUBLE_MOTOROLA,
232 v23c.DATA_TYPE_SIGNED_MOTOROLA,
233 v23c.DATA_TYPE_UNSIGNED_MOTOROLA,
234 v23c.DATA_TYPE_DOUBLE_INTEL,
235 v23c.DATA_TYPE_SIGNED_INTEL,
236 v23c.DATA_TYPE_UNSIGNED_INTEL,
237 ):
238 parent = mdf3.groups[0].channels[1]
239 parent.data_type = parent_data_type
240
241 ch3 = mdf3.groups[0].channels[2]
242 ch3.start_offset = 24 + t.itemsize * 8
243 ch3.data_type = v23c.DATA_TYPE_UNSIGNED_MOTOROLA
244 ch3.bit_count = 16
245
246 ch4 = mdf3.groups[0].channels[3]
247 ch4.start_offset = 24 + t.itemsize * 8
248 ch4.data_type = v23c.DATA_TYPE_UNSIGNED_INTEL
249 ch4.bit_count = 16
250
251 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
252
253 with MDF(outfile) as mdf:
254 assert np.array_equal(mdf.get("OverlappingMotorola").samples, [0x0204] * 15)
255
256 assert np.array_equal(mdf.get("OverlappingIntel").samples, [0x0402] * 15)
257
258 ch3 = mdf3.groups[0].channels[2]
259 ch3.start_offset = 16 + t.itemsize * 8
260 ch3.data_type = v23c.DATA_TYPE_UNSIGNED_MOTOROLA
261 ch3.bit_count = 24
262
263 ch4 = mdf3.groups[0].channels[3]
264 ch4.start_offset = 16 + t.itemsize * 8
265 ch4.data_type = v23c.DATA_TYPE_UNSIGNED_INTEL
266 ch4.bit_count = 24
267
268 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
269
270 with MDF(outfile) as mdf:
271 assert np.array_equal(mdf.get("OverlappingMotorola").samples, [0x3F0204] * 15)
272
273 assert np.array_equal(mdf.get("OverlappingIntel").samples, [0x04023F] * 15)
274
275 ch3 = mdf3.groups[0].channels[2]
276 ch3.start_offset = 16 + t.itemsize * 8 + 5
277 ch3.data_type = v23c.DATA_TYPE_UNSIGNED_MOTOROLA
278 ch3.bit_count = 21
279
280 ch4 = mdf3.groups[0].channels[3]
281 ch4.start_offset = 16 + t.itemsize * 8 + 6
282 ch4.data_type = v23c.DATA_TYPE_UNSIGNED_INTEL
283 ch4.bit_count = 21
284
285 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
286
287 with MDF(outfile) as mdf:
288 assert np.array_equal(
289 mdf.get("OverlappingMotorola").samples,
290 [(0x3F0204F8 >> 5) & (2**21 - 1)] * 15,
291 )
292
293 assert np.array_equal(
294 mdf.get("OverlappingIntel").samples,
295 [(0xF8040200 >> 6) & (2**21 - 1)] * 15,
296 )
297
298 def test_overlapping_channels_mdf_v4(self) -> None:
299 t = np.arange(15, dtype="<f8")
300
301 s1 = Signal(
302 np.frombuffer(b"\x00\x00\x3f\x02\x04\xf8\x00\x00" * 15, dtype="<u8"),
303 t,
304 name="Parent",
305 )
306
307 s2 = Signal(
308 np.frombuffer(b"\x04\xf8\x00\x00" * 15, dtype="<u4"),
309 t,
310 name="OverlappingMotorola",
311 )
312
313 s3 = Signal(
314 np.frombuffer(b"\x04\x00\x00\x00" * 15, dtype="<u4"),
315 t,
316 name="OverlappingIntel",
317 )
318
319 with MDF(version="4.11") as mdf_source:
320 mdf_source.append([s1, s2, s3], common_timebase=True)
321
322 mdf4 = typing.cast(mdf_v4.MDF4, mdf_source._mdf)
323
324 for parent_data_type in (
325 v4c.DATA_TYPE_REAL_MOTOROLA,
326 v4c.DATA_TYPE_SIGNED_MOTOROLA,
327 v4c.DATA_TYPE_UNSIGNED_MOTOROLA,
328 v4c.DATA_TYPE_REAL_INTEL,
329 v4c.DATA_TYPE_SIGNED_INTEL,
330 v4c.DATA_TYPE_UNSIGNED_INTEL,
331 ):
332 parent = mdf4.groups[0].channels[1]
333 parent.data_type = parent_data_type
334
335 ch3 = mdf4.groups[0].channels[2]
336 ch3.byte_offset = 3 + t.itemsize
337 ch3.data_type = v4c.DATA_TYPE_UNSIGNED_MOTOROLA
338 ch3.bit_count = 16
339 ch3.bit_offset = 0
340
341 ch4 = mdf4.groups[0].channels[3]
342 ch4.byte_offset = 3 + t.itemsize
343 ch4.data_type = v4c.DATA_TYPE_UNSIGNED_INTEL
344 ch4.bit_count = 16
345 ch4.bit_offset = 0
346
347 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
348
349 with MDF(outfile) as mdf:
350 assert np.array_equal(mdf.get("OverlappingMotorola").samples, [0x0204] * 15)
351
352 assert np.array_equal(mdf.get("OverlappingIntel").samples, [0x0402] * 15)
353
354 ch3 = mdf4.groups[0].channels[2]
355 ch3.byte_offset = 2 + t.itemsize
356 ch3.data_type = v4c.DATA_TYPE_UNSIGNED_MOTOROLA
357 ch3.bit_count = 24
358 ch3.bit_offset = 0
359
360 ch4 = mdf4.groups[0].channels[3]
361 ch4.byte_offset = 2 + t.itemsize
362 ch4.data_type = v4c.DATA_TYPE_UNSIGNED_INTEL
363 ch4.bit_count = 24
364 ch4.bit_offset = 0
365
366 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
367
368 with MDF(outfile) as mdf:
369 assert np.array_equal(mdf.get("OverlappingMotorola").samples, [0x3F0204] * 15)
370
371 assert np.array_equal(mdf.get("OverlappingIntel").samples, [0x04023F] * 15)
372
373 ch3 = mdf4.groups[0].channels[2]
374 ch3.byte_offset = 2 + t.itemsize
375 ch3.data_type = v4c.DATA_TYPE_UNSIGNED_MOTOROLA
376 ch3.bit_count = 21
377 ch3.bit_offset = 5
378
379 ch4 = mdf4.groups[0].channels[3]
380 ch4.byte_offset = 2 + t.itemsize
381 ch4.data_type = v4c.DATA_TYPE_UNSIGNED_INTEL
382 ch4.bit_count = 21
383 ch4.bit_offset = 6
384
385 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
386
387 with MDF(outfile) as mdf:
388 assert np.array_equal(
389 mdf.get("OverlappingMotorola").samples,
390 [(0x3F0204F8 >> 5) & (2**21 - 1)] * 15,
391 )
392
393 assert np.array_equal(
394 mdf.get("OverlappingIntel").samples,
395 [(0xF8040200 >> 6) & (2**21 - 1)] * 15,
396 )
397
398 ch3 = mdf4.groups[0].channels[2]
399 ch3.byte_offset = 3 + t.itemsize
400 ch3.data_type = v4c.DATA_TYPE_UNSIGNED_MOTOROLA
401 ch3.bit_count = 2
402 ch3.bit_offset = 1
403
404 ch4 = mdf4.groups[0].channels[3]
405 ch4.byte_offset = 3 + t.itemsize
406 ch4.data_type = v4c.DATA_TYPE_UNSIGNED_INTEL
407 ch4.bit_count = 2
408 ch4.bit_offset = 1
409
410 outfile = mdf_source.save(Path(TestEndianess.tempdir.name) / "out", overwrite=True)
411
412 with MDF(outfile) as mdf:
413 assert np.array_equal(mdf.get("OverlappingMotorola").samples, [0x1] * 15)
414
415 assert np.array_equal(mdf.get("OverlappingIntel").samples, [0x1] * 15)
416
417
418 if __name__ == "__main__":
419 unittest.main()