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
25 #include <boost/test/unit_test.hpp>
27 #include "../../pv/data/logicsnapshot.h"
29 using pv::data::LogicSnapshot;
32 BOOST_AUTO_TEST_SUITE(LogicSnapshotTest)
34 void push_logic(LogicSnapshot &s, unsigned int length, uint8_t value)
36 sr_datafeed_logic logic;
38 logic.length = length;
39 logic.data = new uint8_t[length];
40 memset(logic.data, value, length * logic.unitsize);
41 s.append_payload(logic);
42 delete[] (uint8_t*)logic.data;
45 BOOST_AUTO_TEST_CASE(Pow2)
47 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(0, 0), 0);
48 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(1, 0), 1);
49 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(2, 0), 2);
52 LogicSnapshot::pow2_ceil(INT64_MIN, 0), INT64_MIN);
54 LogicSnapshot::pow2_ceil(INT64_MAX, 0), INT64_MAX);
56 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(0, 1), 0);
57 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(1, 1), 2);
58 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(2, 1), 2);
59 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(3, 1), 4);
62 BOOST_AUTO_TEST_CASE(Basic)
64 // Create an empty LogicSnapshot object
65 sr_datafeed_logic logic;
70 LogicSnapshot s(logic);
72 //----- Test LogicSnapshot::push_logic -----//
74 BOOST_CHECK(s.get_sample_count() == 0);
75 for (unsigned int i = 0; i < LogicSnapshot::ScaleStepCount; i++)
77 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
78 BOOST_CHECK_EQUAL(m.length, 0);
79 BOOST_CHECK_EQUAL(m.data_length, 0);
80 BOOST_CHECK(m.data == NULL);
83 // Push 8 samples of all zeros
86 BOOST_CHECK(s.get_sample_count() == 8);
88 // There should not be enough samples to have a single mip map sample
89 for (unsigned int i = 0; i < LogicSnapshot::ScaleStepCount; i++)
91 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
92 BOOST_CHECK_EQUAL(m.length, 0);
93 BOOST_CHECK_EQUAL(m.data_length, 0);
94 BOOST_CHECK(m.data == NULL);
97 // Push 8 samples of 0x11s to bring the total up to 16
98 push_logic(s, 8, 0x11);
100 // There should now be enough data for exactly one sample
101 // in mip map level 0, and that sample should be 0
102 const LogicSnapshot::MipMapLevel &m0 = s._mip_map[0];
103 BOOST_CHECK_EQUAL(m0.length, 1);
104 BOOST_CHECK_EQUAL(m0.data_length, LogicSnapshot::MipMapDataUnit);
105 BOOST_REQUIRE(m0.data != NULL);
106 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
108 // The higher levels should still be empty
109 for (unsigned int i = 1; i < LogicSnapshot::ScaleStepCount; i++)
111 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
112 BOOST_CHECK_EQUAL(m.length, 0);
113 BOOST_CHECK_EQUAL(m.data_length, 0);
114 BOOST_CHECK(m.data == NULL);
117 // Push 240 samples of all zeros to bring the total up to 256
118 push_logic(s, 240, 0);
120 BOOST_CHECK_EQUAL(m0.length, 16);
121 BOOST_CHECK_EQUAL(m0.data_length, LogicSnapshot::MipMapDataUnit);
123 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
124 for (unsigned int i = 2; i < m0.length; i++)
125 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
127 const LogicSnapshot::MipMapLevel &m1 = s._mip_map[1];
128 BOOST_CHECK_EQUAL(m1.length, 1);
129 BOOST_CHECK_EQUAL(m1.data_length, LogicSnapshot::MipMapDataUnit);
130 BOOST_REQUIRE(m1.data != NULL);
131 BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);
133 //----- Test LogicSnapshot::get_subsampled_edges -----//
135 // Test a full view at full zoom.
136 vector<LogicSnapshot::EdgePair> edges;
137 s.get_subsampled_edges(edges, 0, 255, 1, 0);
138 BOOST_REQUIRE_EQUAL(edges.size(), 4);
140 BOOST_CHECK_EQUAL(edges[0].first, 0);
141 BOOST_CHECK_EQUAL(edges[1].first, 8);
142 BOOST_CHECK_EQUAL(edges[2].first, 16);
143 BOOST_CHECK_EQUAL(edges[3].first, 256);
145 // Test a subset at high zoom
147 s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
148 BOOST_REQUIRE_EQUAL(edges.size(), 4);
150 BOOST_CHECK_EQUAL(edges[0].first, 6);
151 BOOST_CHECK_EQUAL(edges[1].first, 8);
152 BOOST_CHECK_EQUAL(edges[2].first, 16);
153 BOOST_CHECK_EQUAL(edges[3].first, 18);
156 BOOST_AUTO_TEST_CASE(LargeData)
159 const unsigned int Length = 1000000;
161 sr_datafeed_logic logic;
163 logic.length = Length;
164 logic.data = new uint8_t[Length];
165 uint8_t *data = (uint8_t*)logic.data;
167 for (unsigned int i = 0; i < Length; i++)
168 *data++ = (uint8_t)(i >> 8);
170 LogicSnapshot s(logic);
171 delete[] (uint8_t*)logic.data;
173 BOOST_CHECK(s.get_sample_count() == Length);
175 // Check mip map level 0
176 BOOST_CHECK_EQUAL(s._mip_map[0].length, 62500);
177 BOOST_CHECK_EQUAL(s._mip_map[0].data_length,
178 LogicSnapshot::MipMapDataUnit);
179 BOOST_REQUIRE(s._mip_map[0].data != NULL);
182 for (unsigned int i = 0; i < s._mip_map[0].length;)
184 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
186 const uint8_t sample = (uint8_t)((i*16) >> 8);
187 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF,
188 prev_sample ^ sample);
189 prev_sample = sample;
191 for (int j = 1; i < s._mip_map[0].length && j < 16; j++)
193 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
194 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0);
198 // Check mip map level 1
199 BOOST_CHECK_EQUAL(s._mip_map[1].length, 3906);
200 BOOST_CHECK_EQUAL(s._mip_map[1].data_length,
201 LogicSnapshot::MipMapDataUnit);
202 BOOST_REQUIRE(s._mip_map[1].data != NULL);
205 for (unsigned int i = 0; i < s._mip_map[1].length; i++)
207 BOOST_TEST_MESSAGE("Testing mip_map[1].data[" << i << "]");
209 const uint8_t sample = i;
210 const uint8_t expected = sample ^ prev_sample;
213 BOOST_CHECK_EQUAL(s.get_subsample(1, i) & 0xFF, expected);
216 // Check mip map level 2
217 BOOST_CHECK_EQUAL(s._mip_map[2].length, 244);
218 BOOST_CHECK_EQUAL(s._mip_map[2].data_length,
219 LogicSnapshot::MipMapDataUnit);
220 BOOST_REQUIRE(s._mip_map[2].data != NULL);
223 for (unsigned int i = 0; i < s._mip_map[2].length; i++)
225 BOOST_TEST_MESSAGE("Testing mip_map[2].data[" << i << "]");
227 const uint8_t sample = i << 4;
228 const uint8_t expected = (sample ^ prev_sample) | 0x0F;
229 prev_sample = sample;
231 BOOST_CHECK_EQUAL(s.get_subsample(2, i) & 0xFF, expected);
234 // Check mip map level 3
235 BOOST_CHECK_EQUAL(s._mip_map[3].length, 15);
236 BOOST_CHECK_EQUAL(s._mip_map[3].data_length,
237 LogicSnapshot::MipMapDataUnit);
238 BOOST_REQUIRE(s._mip_map[3].data != NULL);
240 for (unsigned int i = 0; i < s._mip_map[3].length; i++)
241 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[3].data + i),
244 // Check the higher levels
245 for (unsigned int i = 4; i < LogicSnapshot::ScaleStepCount; i++)
247 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
248 BOOST_CHECK_EQUAL(m.length, 0);
249 BOOST_CHECK_EQUAL(m.data_length, 0);
250 BOOST_CHECK(m.data == NULL);
253 //----- Test LogicSnapshot::get_subsampled_edges -----//
254 // Check in normal case
255 vector<LogicSnapshot::EdgePair> edges;
256 s.get_subsampled_edges(edges, 0, Length-1, 1, 7);
258 BOOST_CHECK_EQUAL(edges.size(), 32);
260 for (unsigned int i = 0; i < edges.size() - 1; i++)
262 BOOST_CHECK_EQUAL(edges[i].first, i * 32768);
263 BOOST_CHECK_EQUAL(edges[i].second, i & 1);
266 BOOST_CHECK_EQUAL(edges[31].first, 1000000);
268 // Check in very low zoom case
270 s.get_subsampled_edges(edges, 0, Length-1, 50e6f, 7);
272 BOOST_CHECK_EQUAL(edges.size(), 2);
275 BOOST_AUTO_TEST_CASE(Pulses)
277 const int Cycles = 3;
278 const int Period = 64;
279 const int Length = Cycles * Period;
281 vector<LogicSnapshot::EdgePair> edges;
283 //----- Create a LogicSnapshot -----//
284 sr_datafeed_logic logic;
286 logic.length = Length;
287 logic.data = (uint64_t*)new uint8_t[Length];
288 uint8_t *p = (uint8_t*)logic.data;
290 for (int i = 0; i < Cycles; i++) {
292 for (int j = 1; j < Period; j++)
296 LogicSnapshot s(logic);
297 delete[] (uint8_t*)logic.data;
299 //----- Check the mip-map -----//
300 // Check mip map level 0
301 BOOST_CHECK_EQUAL(s._mip_map[0].length, 12);
302 BOOST_CHECK_EQUAL(s._mip_map[0].data_length,
303 LogicSnapshot::MipMapDataUnit);
304 BOOST_REQUIRE(s._mip_map[0].data != NULL);
306 for (unsigned int i = 0; i < s._mip_map[0].length;) {
307 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
308 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0xFF);
311 i < s._mip_map[0].length &&
312 j < Period/LogicSnapshot::MipMapScaleFactor; j++) {
314 "Testing mip_map[0].data[" << i << "]");
315 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0x00);
319 // Check the higher levels are all inactive
320 for (unsigned int i = 1; i < LogicSnapshot::ScaleStepCount; i++) {
321 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
322 BOOST_CHECK_EQUAL(m.length, 0);
323 BOOST_CHECK_EQUAL(m.data_length, 0);
324 BOOST_CHECK(m.data == NULL);
327 //----- Test get_subsampled_edges at reduced scale -----//
328 s.get_subsampled_edges(edges, 0, Length-1, 16.0f, 2);
329 BOOST_REQUIRE_EQUAL(edges.size(), Cycles + 2);
331 BOOST_CHECK_EQUAL(0, false);
332 for (unsigned int i = 1; i < edges.size(); i++)
333 BOOST_CHECK_EQUAL(edges[i].second, false);
336 BOOST_AUTO_TEST_CASE(LongPulses)
338 const int Cycles = 3;
339 const int Period = 64;
340 const int PulseWidth = 16;
341 const int Length = Cycles * Period;
344 vector<LogicSnapshot::EdgePair> edges;
346 //----- Create a LogicSnapshot -----//
347 sr_datafeed_logic logic;
349 logic.length = Length * 8;
350 logic.data = (uint64_t*)new uint64_t[Length];
351 uint64_t *p = (uint64_t*)logic.data;
353 for (int i = 0; i < Cycles; i++) {
354 for (j = 0; j < PulseWidth; j++)
356 for (; j < Period; j++)
360 LogicSnapshot s(logic);
361 delete[] (uint64_t*)logic.data;
363 //----- Check the mip-map -----//
364 // Check mip map level 0
365 BOOST_CHECK_EQUAL(s._mip_map[0].length, 12);
366 BOOST_CHECK_EQUAL(s._mip_map[0].data_length,
367 LogicSnapshot::MipMapDataUnit);
368 BOOST_REQUIRE(s._mip_map[0].data != NULL);
370 for (unsigned int i = 0; i < s._mip_map[0].length;) {
371 for (j = 0; i < s._mip_map[0].length && j < 2; j++) {
373 "Testing mip_map[0].data[" << i << "]");
374 BOOST_CHECK_EQUAL(s.get_subsample(0, i++), ~0);
377 for (; i < s._mip_map[0].length &&
378 j < Period/LogicSnapshot::MipMapScaleFactor; j++) {
380 "Testing mip_map[0].data[" << i << "]");
381 BOOST_CHECK_EQUAL(s.get_subsample(0, i++), 0);
385 // Check the higher levels are all inactive
386 for (unsigned int i = 1; i < LogicSnapshot::ScaleStepCount; i++) {
387 const LogicSnapshot::MipMapLevel &m = s._mip_map[i];
388 BOOST_CHECK_EQUAL(m.length, 0);
389 BOOST_CHECK_EQUAL(m.data_length, 0);
390 BOOST_CHECK(m.data == NULL);
393 //----- Test get_subsampled_edges at a full scale -----//
394 s.get_subsampled_edges(edges, 0, Length-1, 16.0f, 2);
395 BOOST_REQUIRE_EQUAL(edges.size(), Cycles * 2 + 1);
397 for (int i = 0; i < Cycles; i++) {
398 BOOST_CHECK_EQUAL(edges[i*2].first, i * Period);
399 BOOST_CHECK_EQUAL(edges[i*2].second, true);
400 BOOST_CHECK_EQUAL(edges[i*2+1].first, i * Period + PulseWidth);
401 BOOST_CHECK_EQUAL(edges[i*2+1].second, false);
404 BOOST_CHECK_EQUAL(edges.back().first, Length);
405 BOOST_CHECK_EQUAL(edges.back().second, false);
407 //----- Test get_subsampled_edges at a simplified scale -----//
409 s.get_subsampled_edges(edges, 0, Length-1, 17.0f, 2);
411 BOOST_CHECK_EQUAL(edges[0].first, 0);
412 BOOST_CHECK_EQUAL(edges[0].second, true);
413 BOOST_CHECK_EQUAL(edges[1].first, 16);
414 BOOST_CHECK_EQUAL(edges[1].second, false);
416 for (int i = 1; i < Cycles; i++) {
417 BOOST_CHECK_EQUAL(edges[i+1].first, i * Period);
418 BOOST_CHECK_EQUAL(edges[i+1].second, false);
421 BOOST_CHECK_EQUAL(edges.back().first, Length);
422 BOOST_CHECK_EQUAL(edges.back().second, false);
425 BOOST_AUTO_TEST_CASE(LisaMUsbHid)
427 /* This test was created from the beginning of the USB_DM signal in
428 * sigrok-dumps-usb/lisa_m_usbhid/lisa_m_usbhid.sr
431 const int Edges[] = {
432 7028, 7033, 7036, 7041, 7044, 7049, 7053, 7066, 7073, 7079,
433 7086, 7095, 7103, 7108, 7111, 7116, 7119, 7124, 7136, 7141,
436 const int Length = Edges[countof(Edges) - 1];
441 //----- Create a LogicSnapshot -----//
442 sr_datafeed_logic logic;
444 logic.length = Length;
445 logic.data = new uint8_t[Length];
446 uint8_t *data = (uint8_t*)logic.data;
448 for (unsigned int i = 0; i < countof(Edges); i++) {
449 const int edgePos = Edges[i];
450 memset(&data[lastEdgePos], state ? 0x02 : 0,
451 edgePos - lastEdgePos - 1);
453 lastEdgePos = edgePos;
457 LogicSnapshot s(logic);
458 delete[] (uint64_t*)logic.data;
460 vector<LogicSnapshot::EdgePair> edges;
463 /* The trailing edge of the pulse train is falling in the source data.
464 * Check this is always true at different scales
468 s.get_subsampled_edges(edges, 0, Length-1, 33.333332f, 1);
469 BOOST_CHECK_EQUAL(edges[edges.size() - 2].second, false);
473 * This test checks the rendering of wide data (more than 8 probes)
474 * Probe signals are either all-high, or all-low, but are interleaved such that
475 * they would toggle during every sample if treated like 8 probes.
476 * The packet contains a large number of samples, so the mipmap generation kicks
479 * The signals should not toggle (have exactly two edges: the start and end)
481 BOOST_AUTO_TEST_CASE(WideData)
483 const int Length = 512<<10;
484 uint16_t *data = new uint16_t[Length];
486 sr_datafeed_logic logic;
487 logic.unitsize = sizeof(data[0]);
488 logic.length = Length * sizeof(data[0]);
491 for (int i = 0; i < Length; i++)
494 LogicSnapshot s(logic);
496 vector<LogicSnapshot::EdgePair> edges;
499 s.get_subsampled_edges(edges, 0, Length-1, 1, 0);
500 BOOST_CHECK_EQUAL(edges.size(), 2);
503 s.get_subsampled_edges(edges, 0, Length-1, 1, 8);
504 BOOST_CHECK_EQUAL(edges.size(), 2);
511 * This test is a replica of sixteen.sr attached to Bug #33.
513 BOOST_AUTO_TEST_CASE(Sixteen)
515 const int Length = 8;
516 uint16_t data[Length];
518 sr_datafeed_logic logic;
519 logic.unitsize = sizeof(data[0]);
520 logic.length = Length * sizeof(data[0]);
523 for (int i = 0; i < Length; i++)
526 LogicSnapshot s(logic);
528 vector<LogicSnapshot::EdgePair> edges;
529 s.get_subsampled_edges(edges, 0, 2, 0.0004, 1);
531 BOOST_CHECK_EQUAL(edges.size(), 2);
534 BOOST_AUTO_TEST_SUITE_END()