2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <boost/test/unit_test.hpp>
23 #include "../logicdatasnapshot.h"
27 BOOST_AUTO_TEST_SUITE(LogicDataSnapshotTest)
29 void push_logic(LogicDataSnapshot &s, unsigned int length, uint8_t value)
31 sr_datafeed_logic logic;
33 logic.length = length;
34 logic.data = new uint8_t[length];
35 memset(logic.data, value, length * logic.unitsize);
36 s.append_payload(logic);
37 delete[] (uint8_t*)logic.data;
40 BOOST_AUTO_TEST_CASE(Basic)
42 // Create an empty LogicDataSnapshot object
43 sr_datafeed_logic logic;
48 LogicDataSnapshot s(logic);
50 //----- Test LogicDataSnapshot::push_logic -----//
52 BOOST_CHECK(s.get_sample_count() == 0);
53 for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
55 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
56 BOOST_CHECK_EQUAL(m.length, 0);
57 BOOST_CHECK_EQUAL(m.data_length, 0);
58 BOOST_CHECK(m.data == NULL);
61 // Push 8 samples of all zeros
64 BOOST_CHECK(s.get_sample_count() == 8);
66 // There should not be enough samples to have a single mip map sample
67 for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
69 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
70 BOOST_CHECK_EQUAL(m.length, 0);
71 BOOST_CHECK_EQUAL(m.data_length, 0);
72 BOOST_CHECK(m.data == NULL);
75 // Push 8 samples of 0x11s to bring the total up to 16
76 push_logic(s, 8, 0x11);
78 // There should now be enough data for exactly one sample
79 // in mip map level 0, and that sample should be 0
80 const LogicDataSnapshot::MipMapLevel &m0 = s._mip_map[0];
81 BOOST_CHECK_EQUAL(m0.length, 1);
82 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
83 BOOST_REQUIRE(m0.data != NULL);
84 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
86 // The higher levels should still be empty
87 for(int i = 1; i < LogicDataSnapshot::ScaleStepCount; i++)
89 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
90 BOOST_CHECK_EQUAL(m.length, 0);
91 BOOST_CHECK_EQUAL(m.data_length, 0);
92 BOOST_CHECK(m.data == NULL);
95 // Push 240 samples of all zeros to bring the total up to 256
96 push_logic(s, 240, 0);
98 BOOST_CHECK_EQUAL(m0.length, 16);
99 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
101 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
102 for(int i = 2; i < m0.length; i++)
103 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
105 const LogicDataSnapshot::MipMapLevel &m1 = s._mip_map[1];
106 BOOST_CHECK_EQUAL(m1.length, 1);
107 BOOST_CHECK_EQUAL(m1.data_length, LogicDataSnapshot::MipMapDataUnit);
108 BOOST_REQUIRE(m1.data != NULL);
109 BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);
111 //----- Test LogicDataSnapshot::get_subsampled_edges -----//
113 // Test a full view at full zoom.
114 vector<LogicDataSnapshot::EdgePair> edges;
115 s.get_subsampled_edges(edges, 0, 255, 1, 0);
116 BOOST_REQUIRE_EQUAL(edges.size(), 4);
118 BOOST_CHECK_EQUAL(edges[0].first, 0);
119 BOOST_CHECK_EQUAL(edges[1].first, 8);
120 BOOST_CHECK_EQUAL(edges[2].first, 16);
121 BOOST_CHECK_EQUAL(edges[3].first, 255);
123 // Test a subset at high zoom
125 s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
126 BOOST_REQUIRE_EQUAL(edges.size(), 4);
128 BOOST_CHECK_EQUAL(edges[0].first, 6);
129 BOOST_CHECK_EQUAL(edges[1].first, 8);
130 BOOST_CHECK_EQUAL(edges[2].first, 16);
131 BOOST_CHECK_EQUAL(edges[3].first, 17);
134 BOOST_AUTO_TEST_CASE(LargeData)
137 const int Length = 1000000;
139 sr_datafeed_logic logic;
141 logic.length = Length;
142 logic.data = new uint8_t[Length];
143 uint8_t *data = (uint8_t*)logic.data;
145 for(int i = 0; i < Length; i++)
146 *data++ = (uint8_t)(i >> 8);
148 LogicDataSnapshot s(logic);
149 delete[] (uint8_t*)logic.data;
151 BOOST_CHECK(s.get_sample_count() == Length);
153 // Check mip map level 0
154 BOOST_CHECK_EQUAL(s._mip_map[0].length, 62500);
155 BOOST_CHECK_EQUAL(s._mip_map[0].data_length,
156 LogicDataSnapshot::MipMapDataUnit);
157 BOOST_REQUIRE(s._mip_map[0].data != NULL);
160 for(int i = 0; i < s._mip_map[0].length;)
162 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
164 const uint8_t sample = (uint8_t)((i*16) >> 8);
165 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[0].data + i++),
166 prev_sample ^ sample);
167 prev_sample = sample;
169 for(int j = 1; i < s._mip_map[0].length && j < 16; j++)
171 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
172 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[0].data + i++), 0);
176 // Check mip map level 1
177 BOOST_CHECK_EQUAL(s._mip_map[1].length, 3906);
178 BOOST_CHECK_EQUAL(s._mip_map[1].data_length,
179 LogicDataSnapshot::MipMapDataUnit);
180 BOOST_REQUIRE(s._mip_map[1].data != NULL);
183 for(int i = 0; i < s._mip_map[1].length; i++)
185 BOOST_TEST_MESSAGE("Testing mip_map[1].data[" << i << "]");
187 const uint8_t sample = i;
188 const uint8_t expected = sample ^ prev_sample;
191 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[1].data + i),
195 // Check mip map level 2
196 BOOST_CHECK_EQUAL(s._mip_map[2].length, 244);
197 BOOST_CHECK_EQUAL(s._mip_map[2].data_length,
198 LogicDataSnapshot::MipMapDataUnit);
199 BOOST_REQUIRE(s._mip_map[2].data != NULL);
202 for(int i = 0; i < s._mip_map[2].length; i++)
204 BOOST_TEST_MESSAGE("Testing mip_map[2].data[" << i << "]");
206 const uint8_t sample = i << 4;
207 const uint8_t expected = (sample ^ prev_sample) | 0x0F;
208 prev_sample = sample;
210 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[2].data + i),
214 // Check mip map level 3
215 BOOST_CHECK_EQUAL(s._mip_map[3].length, 15);
216 BOOST_CHECK_EQUAL(s._mip_map[3].data_length,
217 LogicDataSnapshot::MipMapDataUnit);
218 BOOST_REQUIRE(s._mip_map[3].data != NULL);
220 for(int i = 0; i < s._mip_map[3].length; i++)
221 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[3].data + i),
224 // Check the higher levels
225 for(int i = 4; i < LogicDataSnapshot::ScaleStepCount; i++)
227 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
228 BOOST_CHECK_EQUAL(m.length, 0);
229 BOOST_CHECK_EQUAL(m.data_length, 0);
230 BOOST_CHECK(m.data == NULL);
233 //----- Test LogicDataSnapshot::get_subsampled_edges -----//
234 vector<LogicDataSnapshot::EdgePair> edges;
236 s.get_subsampled_edges(edges, 0, Length-1, 1, 7);
237 BOOST_REQUIRE_EQUAL(edges.size(), 32);
239 for(int i = 0; i < 31; i++)
241 BOOST_REQUIRE_EQUAL(edges[i].first, i * 32768);
242 BOOST_REQUIRE_EQUAL(edges[i].second, i & 1);
245 BOOST_REQUIRE_EQUAL(edges[31].first, 999999);
248 BOOST_AUTO_TEST_SUITE_END()