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