2 * This file is part of the PulseView 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
23 #define __STDC_LIMIT_MACROS
26 #include <boost/test/unit_test.hpp>
28 #include "../../pv/data/logicsnapshot.h"
32 using pv::data::LogicSnapshot;
34 BOOST_AUTO_TEST_SUITE(LogicSnapshotTest)
36 void push_logic(LogicSnapshot &s, unsigned int length, uint8_t value)
38 sr_datafeed_logic logic;
40 logic.length = length;
41 logic.data = new uint8_t[length];
42 memset(logic.data, value, length * logic.unitsize);
43 s.append_payload(logic);
44 delete[] (uint8_t*)logic.data;
47 BOOST_AUTO_TEST_CASE(Pow2)
49 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(0, 0), 0);
50 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(1, 0), 1);
51 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(2, 0), 2);
54 LogicSnapshot::pow2_ceil(INT64_MIN, 0), INT64_MIN);
56 LogicSnapshot::pow2_ceil(INT64_MAX, 0), INT64_MAX);
58 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(0, 1), 0);
59 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(1, 1), 2);
60 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(2, 1), 2);
61 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(3, 1), 4);
64 BOOST_AUTO_TEST_CASE(Basic)
66 // Create an empty LogicSnapshot object
67 sr_datafeed_logic logic;
72 LogicSnapshot s(logic);
74 //----- Test LogicSnapshot::push_logic -----//
76 BOOST_CHECK(s.get_sample_count() == 0);
77 for (unsigned int i = 0; i < LogicSnapshot::ScaleStepCount; i++)
79 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
80 BOOST_CHECK_EQUAL(m.length, 0);
81 BOOST_CHECK_EQUAL(m.data_length, 0);
82 BOOST_CHECK(m.data == NULL);
85 // Push 8 samples of all zeros
88 BOOST_CHECK(s.get_sample_count() == 8);
90 // There should not be enough samples to have a single mip map sample
91 for (unsigned int i = 0; i < LogicSnapshot::ScaleStepCount; i++)
93 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
94 BOOST_CHECK_EQUAL(m.length, 0);
95 BOOST_CHECK_EQUAL(m.data_length, 0);
96 BOOST_CHECK(m.data == NULL);
99 // Push 8 samples of 0x11s to bring the total up to 16
100 push_logic(s, 8, 0x11);
102 // There should now be enough data for exactly one sample
103 // in mip map level 0, and that sample should be 0
104 const LogicSnapshot::MipMapLevel &m0 = s._mip_map[0];
105 BOOST_CHECK_EQUAL(m0.length, 1);
106 BOOST_CHECK_EQUAL(m0.data_length, LogicSnapshot::MipMapDataUnit);
107 BOOST_REQUIRE(m0.data != NULL);
108 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
110 // The higher levels should still be empty
111 for (unsigned int i = 1; i < LogicSnapshot::ScaleStepCount; i++)
113 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
114 BOOST_CHECK_EQUAL(m.length, 0);
115 BOOST_CHECK_EQUAL(m.data_length, 0);
116 BOOST_CHECK(m.data == NULL);
119 // Push 240 samples of all zeros to bring the total up to 256
120 push_logic(s, 240, 0);
122 BOOST_CHECK_EQUAL(m0.length, 16);
123 BOOST_CHECK_EQUAL(m0.data_length, LogicSnapshot::MipMapDataUnit);
125 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
126 for (unsigned int i = 2; i < m0.length; i++)
127 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
129 const LogicSnapshot::MipMapLevel &m1 = s._mip_map[1];
130 BOOST_CHECK_EQUAL(m1.length, 1);
131 BOOST_CHECK_EQUAL(m1.data_length, LogicSnapshot::MipMapDataUnit);
132 BOOST_REQUIRE(m1.data != NULL);
133 BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);
135 //----- Test LogicSnapshot::get_subsampled_edges -----//
137 // Test a full view at full zoom.
138 vector<LogicSnapshot::EdgePair> edges;
139 s.get_subsampled_edges(edges, 0, 255, 1, 0);
140 BOOST_REQUIRE_EQUAL(edges.size(), 4);
142 BOOST_CHECK_EQUAL(edges[0].first, 0);
143 BOOST_CHECK_EQUAL(edges[1].first, 8);
144 BOOST_CHECK_EQUAL(edges[2].first, 16);
145 BOOST_CHECK_EQUAL(edges[3].first, 255);
147 // Test a subset at high zoom
149 s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
150 BOOST_REQUIRE_EQUAL(edges.size(), 4);
152 BOOST_CHECK_EQUAL(edges[0].first, 6);
153 BOOST_CHECK_EQUAL(edges[1].first, 8);
154 BOOST_CHECK_EQUAL(edges[2].first, 16);
155 BOOST_CHECK_EQUAL(edges[3].first, 17);
158 BOOST_AUTO_TEST_CASE(LargeData)
161 const unsigned int Length = 1000000;
163 sr_datafeed_logic logic;
165 logic.length = Length;
166 logic.data = new uint8_t[Length];
167 uint8_t *data = (uint8_t*)logic.data;
169 for (unsigned int i = 0; i < Length; i++)
170 *data++ = (uint8_t)(i >> 8);
172 LogicSnapshot s(logic);
173 delete[] (uint8_t*)logic.data;
175 BOOST_CHECK(s.get_sample_count() == Length);
177 // Check mip map level 0
178 BOOST_CHECK_EQUAL(s._mip_map[0].length, 62500);
179 BOOST_CHECK_EQUAL(s._mip_map[0].data_length,
180 LogicSnapshot::MipMapDataUnit);
181 BOOST_REQUIRE(s._mip_map[0].data != NULL);
184 for (unsigned int i = 0; i < s._mip_map[0].length;)
186 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
188 const uint8_t sample = (uint8_t)((i*16) >> 8);
189 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF,
190 prev_sample ^ sample);
191 prev_sample = sample;
193 for (int j = 1; i < s._mip_map[0].length && j < 16; j++)
195 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
196 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0);
200 // Check mip map level 1
201 BOOST_CHECK_EQUAL(s._mip_map[1].length, 3906);
202 BOOST_CHECK_EQUAL(s._mip_map[1].data_length,
203 LogicSnapshot::MipMapDataUnit);
204 BOOST_REQUIRE(s._mip_map[1].data != NULL);
207 for (unsigned int i = 0; i < s._mip_map[1].length; i++)
209 BOOST_TEST_MESSAGE("Testing mip_map[1].data[" << i << "]");
211 const uint8_t sample = i;
212 const uint8_t expected = sample ^ prev_sample;
215 BOOST_CHECK_EQUAL(s.get_subsample(1, i) & 0xFF, expected);
218 // Check mip map level 2
219 BOOST_CHECK_EQUAL(s._mip_map[2].length, 244);
220 BOOST_CHECK_EQUAL(s._mip_map[2].data_length,
221 LogicSnapshot::MipMapDataUnit);
222 BOOST_REQUIRE(s._mip_map[2].data != NULL);
225 for (unsigned int i = 0; i < s._mip_map[2].length; i++)
227 BOOST_TEST_MESSAGE("Testing mip_map[2].data[" << i << "]");
229 const uint8_t sample = i << 4;
230 const uint8_t expected = (sample ^ prev_sample) | 0x0F;
231 prev_sample = sample;
233 BOOST_CHECK_EQUAL(s.get_subsample(2, i) & 0xFF, expected);
236 // Check mip map level 3
237 BOOST_CHECK_EQUAL(s._mip_map[3].length, 15);
238 BOOST_CHECK_EQUAL(s._mip_map[3].data_length,
239 LogicSnapshot::MipMapDataUnit);
240 BOOST_REQUIRE(s._mip_map[3].data != NULL);
242 for (unsigned int i = 0; i < s._mip_map[3].length; i++)
243 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[3].data + i),
246 // Check the higher levels
247 for (unsigned int i = 4; i < LogicSnapshot::ScaleStepCount; i++)
249 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
250 BOOST_CHECK_EQUAL(m.length, 0);
251 BOOST_CHECK_EQUAL(m.data_length, 0);
252 BOOST_CHECK(m.data == NULL);
255 //----- Test LogicSnapshot::get_subsampled_edges -----//
256 // Check in normal case
257 vector<LogicSnapshot::EdgePair> edges;
258 s.get_subsampled_edges(edges, 0, Length-1, 1, 7);
260 BOOST_CHECK_EQUAL(edges.size(), 32);
262 for (unsigned int i = 0; i < edges.size() - 1; i++)
264 BOOST_CHECK_EQUAL(edges[i].first, i * 32768);
265 BOOST_CHECK_EQUAL(edges[i].second, i & 1);
268 BOOST_CHECK_EQUAL(edges[31].first, 999999);
270 // Check in very low zoom case
272 s.get_subsampled_edges(edges, 0, Length-1, 50e6f, 7);
274 BOOST_CHECK_EQUAL(edges.size(), 2);
277 BOOST_AUTO_TEST_CASE(Pulses)
279 const int Cycles = 3;
280 const int Period = 64;
281 const int Length = Cycles * Period;
283 vector<LogicSnapshot::EdgePair> edges;
285 //----- Create a LogicSnapshot -----//
286 sr_datafeed_logic logic;
288 logic.length = Length;
289 logic.data = (uint64_t*)new uint8_t[Length];
290 uint8_t *p = (uint8_t*)logic.data;
292 for (int i = 0; i < Cycles; i++) {
294 for (int j = 1; j < Period; j++)
298 LogicSnapshot s(logic);
299 delete[] (uint8_t*)logic.data;
301 //----- Check the mip-map -----//
302 // Check mip map level 0
303 BOOST_CHECK_EQUAL(s._mip_map[0].length, 12);
304 BOOST_CHECK_EQUAL(s._mip_map[0].data_length,
305 LogicSnapshot::MipMapDataUnit);
306 BOOST_REQUIRE(s._mip_map[0].data != NULL);
308 for (unsigned int i = 0; i < s._mip_map[0].length;) {
309 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
310 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0xFF);
313 i < s._mip_map[0].length &&
314 j < Period/LogicSnapshot::MipMapScaleFactor; j++) {
316 "Testing mip_map[0].data[" << i << "]");
317 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0x00);
321 // Check the higher levels are all inactive
322 for (unsigned int i = 1; i < LogicSnapshot::ScaleStepCount; i++) {
323 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
324 BOOST_CHECK_EQUAL(m.length, 0);
325 BOOST_CHECK_EQUAL(m.data_length, 0);
326 BOOST_CHECK(m.data == NULL);
329 //----- Test get_subsampled_edges at reduced scale -----//
330 s.get_subsampled_edges(edges, 0, Length-1, 16.0f, 2);
331 BOOST_REQUIRE_EQUAL(edges.size(), Cycles + 2);
333 BOOST_CHECK_EQUAL(0, false);
334 for (unsigned int i = 1; i < edges.size(); i++)
335 BOOST_CHECK_EQUAL(edges[i].second, false);
338 BOOST_AUTO_TEST_CASE(LongPulses)
340 const int Cycles = 3;
341 const int Period = 64;
342 const int PulseWidth = 16;
343 const int Length = Cycles * Period;
346 vector<LogicSnapshot::EdgePair> edges;
348 //----- Create a LogicSnapshot -----//
349 sr_datafeed_logic logic;
351 logic.length = Length * 8;
352 logic.data = (uint64_t*)new uint64_t[Length];
353 uint64_t *p = (uint64_t*)logic.data;
355 for (int i = 0; i < Cycles; i++) {
356 for (j = 0; j < PulseWidth; j++)
358 for (; j < Period; j++)
362 LogicSnapshot s(logic);
363 delete[] (uint64_t*)logic.data;
365 //----- Check the mip-map -----//
366 // Check mip map level 0
367 BOOST_CHECK_EQUAL(s._mip_map[0].length, 12);
368 BOOST_CHECK_EQUAL(s._mip_map[0].data_length,
369 LogicSnapshot::MipMapDataUnit);
370 BOOST_REQUIRE(s._mip_map[0].data != NULL);
372 for (unsigned int i = 0; i < s._mip_map[0].length;) {
373 for (j = 0; i < s._mip_map[0].length && j < 2; j++) {
375 "Testing mip_map[0].data[" << i << "]");
376 BOOST_CHECK_EQUAL(s.get_subsample(0, i++), ~0);
379 for (; i < s._mip_map[0].length &&
380 j < Period/LogicSnapshot::MipMapScaleFactor; j++) {
382 "Testing mip_map[0].data[" << i << "]");
383 BOOST_CHECK_EQUAL(s.get_subsample(0, i++), 0);
387 // Check the higher levels are all inactive
388 for (unsigned int i = 1; i < LogicSnapshot::ScaleStepCount; i++) {
389 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
390 BOOST_CHECK_EQUAL(m.length, 0);
391 BOOST_CHECK_EQUAL(m.data_length, 0);
392 BOOST_CHECK(m.data == NULL);
395 //----- Test get_subsampled_edges at a full scale -----//
396 s.get_subsampled_edges(edges, 0, Length-1, 16.0f, 2);
397 BOOST_REQUIRE_EQUAL(edges.size(), Cycles * 2 + 1);
399 for (int i = 0; i < Cycles; i++) {
400 BOOST_CHECK_EQUAL(edges[i*2].first, i * Period);
401 BOOST_CHECK_EQUAL(edges[i*2].second, true);
402 BOOST_CHECK_EQUAL(edges[i*2+1].first, i * Period + PulseWidth);
403 BOOST_CHECK_EQUAL(edges[i*2+1].second, false);
406 BOOST_CHECK_EQUAL(edges.back().first, Length-1);
407 BOOST_CHECK_EQUAL(edges.back().second, false);
409 //----- Test get_subsampled_edges at a simplified scale -----//
411 s.get_subsampled_edges(edges, 0, Length-1, 17.0f, 2);
413 BOOST_CHECK_EQUAL(edges[0].first, 0);
414 BOOST_CHECK_EQUAL(edges[0].second, true);
415 BOOST_CHECK_EQUAL(edges[1].first, 16);
416 BOOST_CHECK_EQUAL(edges[1].second, false);
418 for (int i = 1; i < Cycles; i++) {
419 BOOST_CHECK_EQUAL(edges[i+1].first, i * Period);
420 BOOST_CHECK_EQUAL(edges[i+1].second, false);
423 BOOST_CHECK_EQUAL(edges.back().first, Length-1);
424 BOOST_CHECK_EQUAL(edges.back().second, false);
427 BOOST_AUTO_TEST_CASE(LisaMUsbHid)
429 /* This test was created from the beginning of the USB_DM signal in
430 * sigrok-dumps-usb/lisa_m_usbhid/lisa_m_usbhid.sr
433 const int Edges[] = {
434 7028, 7033, 7036, 7041, 7044, 7049, 7053, 7066, 7073, 7079,
435 7086, 7095, 7103, 7108, 7111, 7116, 7119, 7124, 7136, 7141,
438 const int Length = Edges[countof(Edges) - 1];
443 //----- Create a LogicSnapshot -----//
444 sr_datafeed_logic logic;
446 logic.length = Length;
447 logic.data = new uint8_t[Length];
448 uint8_t *data = (uint8_t*)logic.data;
450 for (unsigned int i = 0; i < countof(Edges); i++) {
451 const int edgePos = Edges[i];
452 memset(&data[lastEdgePos], state ? 0x02 : 0,
453 edgePos - lastEdgePos - 1);
455 lastEdgePos = edgePos;
459 LogicSnapshot s(logic);
460 delete[] (uint64_t*)logic.data;
462 vector<LogicSnapshot::EdgePair> edges;
465 /* The trailing edge of the pulse train is falling in the source data.
466 * Check this is always true at different scales
470 s.get_subsampled_edges(edges, 0, Length-1, 33.333332f, 1);
471 BOOST_CHECK_EQUAL(edges[edges.size() - 2].second, false);
475 * This test checks the rendering of wide data (more than 8 probes)
476 * Probe signals are either all-high, or all-low, but are interleaved such that
477 * they would toggle during every sample if treated like 8 probes.
478 * The packet contains a large number of samples, so the mipmap generation kicks
481 * The signals should not toggle (have exactly two edges: the start and end)
483 BOOST_AUTO_TEST_CASE(WideData)
485 const int Length = 512<<10;
486 uint16_t *data = new uint16_t[Length];
488 sr_datafeed_logic logic;
489 logic.unitsize = sizeof(data[0]);
490 logic.length = Length * sizeof(data[0]);
493 for (int i = 0; i < Length; i++)
496 LogicSnapshot s(logic);
498 vector<LogicSnapshot::EdgePair> edges;
501 s.get_subsampled_edges(edges, 0, Length-1, 1, 0);
502 BOOST_CHECK_EQUAL(edges.size(), 2);
505 s.get_subsampled_edges(edges, 0, Length-1, 1, 8);
506 BOOST_CHECK_EQUAL(edges.size(), 2);
513 * This test is a replica of sixteen.sr attached to Bug #33.
515 BOOST_AUTO_TEST_CASE(Sixteen)
517 const int Length = 8;
518 uint16_t data[Length];
520 sr_datafeed_logic logic;
521 logic.unitsize = sizeof(data[0]);
522 logic.length = Length * sizeof(data[0]);
525 for (int i = 0; i < Length; i++)
528 LogicSnapshot s(logic);
530 vector<LogicSnapshot::EdgePair> edges;
531 s.get_subsampled_edges(edges, 0, 2, 0.0004, 1);
533 BOOST_CHECK_EQUAL(edges.size(), 2);
536 BOOST_AUTO_TEST_SUITE_END()