Skip to content

Commit b69bae7

Browse files
authored
tests: Add a few tests of waveforms backed by memory-mapped files (#25)
* tests: Add a few tests of waveforms backed by memory-mapped files * test: Remove a debugging assertion
1 parent b17f855 commit b69bae7

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

tests/acceptance/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Acceptance tests for the nitypes package."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Acceptance tests for the nitypes.waveform package."""
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from __future__ import annotations
2+
3+
import struct
4+
from pathlib import Path
5+
6+
import numpy as np
7+
import pytest
8+
9+
from nitypes.waveform import AnalogWaveform
10+
11+
12+
@pytest.mark.parametrize("copy", [False, True])
13+
def test___memmap_array_1d___create_waveform_from_array___waveform_contains_memmap_data(
14+
tmp_path: Path, copy: bool
15+
) -> None:
16+
memmap_path = tmp_path / "memmap_array.bin"
17+
memmap_path.write_bytes(struct.pack("4d", 1.23, -4.56, 7e89, 1e-23))
18+
memmap_array = np.memmap(memmap_path, np.float64)
19+
20+
waveform = AnalogWaveform.from_array_1d(memmap_array, copy=copy)
21+
22+
assert list(waveform.raw_data) == [1.23, -4.56, 7e89, 1e-23]
23+
24+
25+
@pytest.mark.parametrize("copy", [False, True])
26+
def test___memmap_array_2d___create_waveforms_from_array___waveforms_contains_memmap_data(
27+
tmp_path: Path, copy: bool
28+
) -> None:
29+
memmap_path = tmp_path / "memmap_array.bin"
30+
memmap_path.write_bytes(struct.pack("6d", 1.23, -4.56, 7e89, 1e-23, 456.0, 7.89))
31+
memmap_array = np.memmap(memmap_path, np.float64, shape=(2, 3))
32+
33+
waveforms = AnalogWaveform.from_array_2d(memmap_array, copy=copy)
34+
35+
assert len(waveforms) == 2
36+
assert list(waveforms[0].raw_data) == [1.23, -4.56, 7e89]
37+
assert list(waveforms[1].raw_data) == [1e-23, 456.0, 7.89]
38+
39+
40+
def test___memmap_waveform___append___waveform_writes_to_memmap(tmp_path: Path) -> None:
41+
memmap_path = tmp_path / "memmap_array.bin"
42+
memmap_array = np.memmap(memmap_path, np.float64, "w+", shape=10)
43+
waveform = AnalogWaveform.from_array_1d(memmap_array, copy=False, sample_count=0)
44+
45+
waveform.append(np.array([1.23, -4.56, 7e89, 1e-23]))
46+
memmap_array.flush()
47+
48+
memmap_bytes = memmap_path.read_bytes()
49+
memmap_data = struct.unpack_from("4d", memmap_bytes)
50+
assert memmap_data == (1.23, -4.56, 7e89, 1e-23)
51+
52+
53+
def test___memmap_waveforms___append___waveforms_write_to_memmap(tmp_path: Path) -> None:
54+
memmap_path = tmp_path / "memmap_array.bin"
55+
memmap_array = np.memmap(memmap_path, np.float64, "w+", shape=(2, 10))
56+
waveforms = AnalogWaveform.from_array_2d(memmap_array, copy=False, sample_count=0)
57+
58+
waveforms[0].append(np.array([1.23, -4.56, 7e89]))
59+
waveforms[1].append(np.array([1e-23, 456.0, 7.89]))
60+
memmap_array.flush()
61+
62+
memmap_bytes = memmap_path.read_bytes()
63+
memmap_data0 = struct.unpack_from("3d", memmap_bytes, offset=0)
64+
memmap_data1 = struct.unpack_from("3d", memmap_bytes, offset=80)
65+
assert memmap_data0 == (1.23, -4.56, 7e89)
66+
assert memmap_data1 == (1e-23, 456.0, 7.89)

0 commit comments

Comments
 (0)