]> sigrok.org Git - pulseview.git/blob - test/logicdatasnapshot.cpp
Implemented O(log(N)) wave plotting
[pulseview.git] / test / logicdatasnapshot.cpp
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 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 <boost/test/unit_test.hpp>
22
23 #include "../logicdatasnapshot.h"
24
25 using namespace std;
26
27 void push_logic(LogicDataSnapshot &s, unsigned int length, uint8_t value)
28 {
29         sr_datafeed_logic logic;
30         logic.unitsize = 1;
31         logic.length = length;
32         logic.data = new uint8_t[length];
33         memset(logic.data, value, length * logic.unitsize);
34         s.append_payload(logic);
35         delete[] (uint8_t*)logic.data;
36 }
37
38 BOOST_AUTO_TEST_CASE(LogicDataSnapshotTest)
39 {
40         // Create an empty LogicDataSnapshot object
41         sr_datafeed_logic logic;
42         logic.length = 0;
43         logic.unitsize = 1;
44         logic.data = NULL;
45
46         LogicDataSnapshot s(logic);
47
48         //----- Test LogicDataSnapshot::push_logic -----//
49
50         BOOST_CHECK(s.get_sample_count() == 0);
51         for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
52         {
53                 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
54                 BOOST_CHECK_EQUAL(m.length, 0);
55                 BOOST_CHECK_EQUAL(m.data_length, 0);
56                 BOOST_CHECK(m.data == NULL);
57         }
58
59         // Push 8 samples of all zeros
60         push_logic(s, 8, 0);
61
62         BOOST_CHECK(s.get_sample_count() == 8);
63
64         // There should not be enough samples to have a single mip map sample
65         for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
66         {
67                 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
68                 BOOST_CHECK_EQUAL(m.length, 0);
69                 BOOST_CHECK_EQUAL(m.data_length, 0);
70                 BOOST_CHECK(m.data == NULL);
71         }
72
73         // Push 8 samples of 0x11s to bring the total up to 16
74         push_logic(s, 8, 0x11);
75
76         // There should now be enough data for exactly one sample
77         // in mip map level 0, and that sample should be 0
78         const LogicDataSnapshot::MipMapLevel &m0 = s._mip_map[0];
79         BOOST_CHECK_EQUAL(m0.length, 1);
80         BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
81         BOOST_REQUIRE(m0.data != NULL);
82         BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
83
84         // The higher levels should still be empty
85         for(int i = 1; i < LogicDataSnapshot::ScaleStepCount; i++)
86         {
87                 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
88                 BOOST_CHECK_EQUAL(m.length, 0);
89                 BOOST_CHECK_EQUAL(m.data_length, 0);
90                 BOOST_CHECK(m.data == NULL);
91         }
92
93         // Push 240 samples of all zeros to bring the total up to 256
94         push_logic(s, 240, 0);
95
96         BOOST_CHECK_EQUAL(m0.length, 16);
97         BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
98
99         BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
100         for(int i = 2; i < m0.length; i++)
101                 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
102
103         const LogicDataSnapshot::MipMapLevel &m1 = s._mip_map[1];
104         BOOST_CHECK_EQUAL(m1.length, 1);
105         BOOST_CHECK_EQUAL(m1.data_length, LogicDataSnapshot::MipMapDataUnit);
106         BOOST_REQUIRE(m1.data != NULL);
107         BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);
108
109         //----- Test LogicDataSnapshot::get_subsampled_edges -----//
110
111         // Test a full view at full zoom.
112         vector<LogicDataSnapshot::EdgePair> edges;
113         s.get_subsampled_edges(edges, 0, 255, 1, 0);
114         BOOST_REQUIRE_EQUAL(edges.size(), 4);
115
116         BOOST_CHECK_EQUAL(edges[0].first, 0);
117         BOOST_CHECK_EQUAL(edges[1].first, 8);
118         BOOST_CHECK_EQUAL(edges[2].first, 16);
119         BOOST_CHECK_EQUAL(edges[3].first, 255);
120
121         // Test a subset at high zoom
122         edges.clear();
123         s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
124         BOOST_REQUIRE_EQUAL(edges.size(), 4);
125
126         BOOST_CHECK_EQUAL(edges[0].first, 6);
127         BOOST_CHECK_EQUAL(edges[1].first, 8);
128         BOOST_CHECK_EQUAL(edges[2].first, 16);
129         BOOST_CHECK_EQUAL(edges[3].first, 17);
130 }