]> sigrok.org Git - pulseview.git/blame - test/data/analogsnapshot.cpp
test/CMakeLists.txt: Fix MinGW build of the tests.
[pulseview.git] / test / data / analogsnapshot.cpp
CommitLineData
340bc0a4
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <extdef.h>
22
340bc0a4
JH
23#include <stdint.h>
24
25#include <boost/test/unit_test.hpp>
26
27#include "../../pv/data/analogsnapshot.h"
28
340bc0a4
JH
29using pv::data::AnalogSnapshot;
30
31BOOST_AUTO_TEST_SUITE(AnalogSnapshotTest)
32
33void push_analog(AnalogSnapshot &s, unsigned int num_samples,
34 float value)
35{
1c4a9ec1
JH
36 float *const data = new float[num_samples];
37 for (unsigned int i = 0; i < num_samples; i++)
38 data[i] = value;
340bc0a4 39
1c4a9ec1
JH
40 s.append_interleaved_samples(data, num_samples, 1);
41 delete[] data;
340bc0a4
JH
42}
43
44BOOST_AUTO_TEST_CASE(Basic)
45{
46 // Create an empty AnalogSnapshot object
1c4a9ec1 47 AnalogSnapshot s;
340bc0a4
JH
48
49 //----- Test AnalogSnapshot::push_analog -----//
50
51 BOOST_CHECK(s.get_sample_count() == 0);
52 for (unsigned int i = 0; i < AnalogSnapshot::ScaleStepCount; i++)
53 {
54 const AnalogSnapshot::Envelope &m = s._envelope_levels[i];
55 BOOST_CHECK_EQUAL(m.length, 0);
56 BOOST_CHECK_EQUAL(m.data_length, 0);
57 BOOST_CHECK(m.samples == NULL);
58 }
59
60 // Push 8 samples of all zeros
61 push_analog(s, 8, 0.0f);
62
63 BOOST_CHECK(s.get_sample_count() == 8);
64
65 // There should not be enough samples to have a single mip map sample
66 for (unsigned int i = 0; i < AnalogSnapshot::ScaleStepCount; i++)
67 {
68 const AnalogSnapshot::Envelope &m = s._envelope_levels[i];
69 BOOST_CHECK_EQUAL(m.length, 0);
70 BOOST_CHECK_EQUAL(m.data_length, 0);
71 BOOST_CHECK(m.samples == NULL);
72 }
73
74 // Push 8 samples of 1.0s to bring the total up to 16
75 push_analog(s, 8, 1.0f);
76
77 // There should now be enough data for exactly one sample
78 // in mip map level 0, and that sample should be 0
79 const AnalogSnapshot::Envelope &e0 = s._envelope_levels[0];
80 BOOST_CHECK_EQUAL(e0.length, 1);
81 BOOST_CHECK_EQUAL(e0.data_length, AnalogSnapshot::EnvelopeDataUnit);
82 BOOST_REQUIRE(e0.samples != NULL);
83 BOOST_CHECK_EQUAL(e0.samples[0].min, 0.0f);
84 BOOST_CHECK_EQUAL(e0.samples[0].max, 1.0f);
85
86 // The higher levels should still be empty
87 for (unsigned int i = 1; i < AnalogSnapshot::ScaleStepCount; i++)
88 {
89 const AnalogSnapshot::Envelope &m = s._envelope_levels[i];
90 BOOST_CHECK_EQUAL(m.length, 0);
91 BOOST_CHECK_EQUAL(m.data_length, 0);
92 BOOST_CHECK(m.samples == NULL);
93 }
94
95 // Push 240 samples of all zeros to bring the total up to 256
96 push_analog(s, 240, -1.0f);
97
98 BOOST_CHECK_EQUAL(e0.length, 16);
99 BOOST_CHECK_EQUAL(e0.data_length, AnalogSnapshot::EnvelopeDataUnit);
100
101 for (unsigned int i = 1; i < e0.length; i++) {
102 BOOST_CHECK_EQUAL(e0.samples[i].min, -1.0f);
103 BOOST_CHECK_EQUAL(e0.samples[i].max, -1.0f);
104 }
105
106 const AnalogSnapshot::Envelope &e1 = s._envelope_levels[1];
107 BOOST_CHECK_EQUAL(e1.length, 1);
108 BOOST_CHECK_EQUAL(e1.data_length, AnalogSnapshot::EnvelopeDataUnit);
109 BOOST_REQUIRE(e1.samples != NULL);
110 BOOST_CHECK_EQUAL(e1.samples[0].min, -1.0f);
111 BOOST_CHECK_EQUAL(e1.samples[0].max, 1.0f);
112}
113
114BOOST_AUTO_TEST_SUITE_END()