]> sigrok.org Git - pulseview.git/blame - test/data/analogsnapshot.cpp
Removed duplicated __STDC_LIMIT_MACROS define
[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{
36 sr_datafeed_analog analog;
37 analog.num_samples = num_samples;
38
39 float *data = new float[num_samples];
40 analog.data = data;
41 while(num_samples-- != 0)
42 *data++ = value;
43
44 s.append_payload(analog);
45 delete[] (float*)analog.data;
46}
47
48BOOST_AUTO_TEST_CASE(Basic)
49{
50 // Create an empty AnalogSnapshot object
51 sr_datafeed_analog analog;
52 analog.num_samples = 0;
53 analog.data = NULL;
54
55 AnalogSnapshot s(analog);
56
57 //----- Test AnalogSnapshot::push_analog -----//
58
59 BOOST_CHECK(s.get_sample_count() == 0);
60 for (unsigned int i = 0; i < AnalogSnapshot::ScaleStepCount; i++)
61 {
62 const AnalogSnapshot::Envelope &m = s._envelope_levels[i];
63 BOOST_CHECK_EQUAL(m.length, 0);
64 BOOST_CHECK_EQUAL(m.data_length, 0);
65 BOOST_CHECK(m.samples == NULL);
66 }
67
68 // Push 8 samples of all zeros
69 push_analog(s, 8, 0.0f);
70
71 BOOST_CHECK(s.get_sample_count() == 8);
72
73 // There should not be enough samples to have a single mip map sample
74 for (unsigned int i = 0; i < AnalogSnapshot::ScaleStepCount; i++)
75 {
76 const AnalogSnapshot::Envelope &m = s._envelope_levels[i];
77 BOOST_CHECK_EQUAL(m.length, 0);
78 BOOST_CHECK_EQUAL(m.data_length, 0);
79 BOOST_CHECK(m.samples == NULL);
80 }
81
82 // Push 8 samples of 1.0s to bring the total up to 16
83 push_analog(s, 8, 1.0f);
84
85 // There should now be enough data for exactly one sample
86 // in mip map level 0, and that sample should be 0
87 const AnalogSnapshot::Envelope &e0 = s._envelope_levels[0];
88 BOOST_CHECK_EQUAL(e0.length, 1);
89 BOOST_CHECK_EQUAL(e0.data_length, AnalogSnapshot::EnvelopeDataUnit);
90 BOOST_REQUIRE(e0.samples != NULL);
91 BOOST_CHECK_EQUAL(e0.samples[0].min, 0.0f);
92 BOOST_CHECK_EQUAL(e0.samples[0].max, 1.0f);
93
94 // The higher levels should still be empty
95 for (unsigned int i = 1; i < AnalogSnapshot::ScaleStepCount; i++)
96 {
97 const AnalogSnapshot::Envelope &m = s._envelope_levels[i];
98 BOOST_CHECK_EQUAL(m.length, 0);
99 BOOST_CHECK_EQUAL(m.data_length, 0);
100 BOOST_CHECK(m.samples == NULL);
101 }
102
103 // Push 240 samples of all zeros to bring the total up to 256
104 push_analog(s, 240, -1.0f);
105
106 BOOST_CHECK_EQUAL(e0.length, 16);
107 BOOST_CHECK_EQUAL(e0.data_length, AnalogSnapshot::EnvelopeDataUnit);
108
109 for (unsigned int i = 1; i < e0.length; i++) {
110 BOOST_CHECK_EQUAL(e0.samples[i].min, -1.0f);
111 BOOST_CHECK_EQUAL(e0.samples[i].max, -1.0f);
112 }
113
114 const AnalogSnapshot::Envelope &e1 = s._envelope_levels[1];
115 BOOST_CHECK_EQUAL(e1.length, 1);
116 BOOST_CHECK_EQUAL(e1.data_length, AnalogSnapshot::EnvelopeDataUnit);
117 BOOST_REQUIRE(e1.samples != NULL);
118 BOOST_CHECK_EQUAL(e1.samples[0].min, -1.0f);
119 BOOST_CHECK_EQUAL(e1.samples[0].max, 1.0f);
120}
121
122BOOST_AUTO_TEST_SUITE_END()