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.hpp>
29 using pv::data::LogicSnapshot
;
33 BOOST_AUTO_TEST_SUITE(LogicSnapshotTest
)
35 void push_logic(LogicSnapshot
&s
, unsigned int length
, uint8_t value
)
37 sr_datafeed_logic logic
;
39 logic
.length
= length
;
40 logic
.data
= new uint8_t[length
];
41 memset(logic
.data
, value
, length
* logic
.unitsize
);
42 s
.append_payload(logic
);
43 delete[] (uint8_t*)logic
.data
;
46 BOOST_AUTO_TEST_CASE(Pow2
)
48 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(0, 0), 0);
49 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(1, 0), 1);
50 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(2, 0), 2);
53 LogicSnapshot::pow2_ceil(INT64_MIN
, 0), INT64_MIN
);
55 LogicSnapshot::pow2_ceil(INT64_MAX
, 0), INT64_MAX
);
57 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(0, 1), 0);
58 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(1, 1), 2);
59 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(2, 1), 2);
60 BOOST_CHECK_EQUAL(LogicSnapshot::pow2_ceil(3, 1), 4);
63 BOOST_AUTO_TEST_CASE(Basic
)
65 // Create an empty LogicSnapshot object
66 sr_datafeed_logic logic
;
71 LogicSnapshot
s(logic
);
73 //----- Test LogicSnapshot::push_logic -----//
75 BOOST_CHECK(s
.get_sample_count() == 0);
76 for (unsigned int i
= 0; i
< LogicSnapshot::ScaleStepCount
; i
++)
78 const LogicSnapshot::MipMapLevel
&m
= s
.mip_map_
[i
];
79 BOOST_CHECK_EQUAL(m
.length
, 0);
80 BOOST_CHECK_EQUAL(m
.data_length
, 0);
81 BOOST_CHECK(m
.data
== NULL
);
84 // Push 8 samples of all zeros
87 BOOST_CHECK(s
.get_sample_count() == 8);
89 // There should not be enough samples to have a single mip map sample
90 for (unsigned int i
= 0; i
< LogicSnapshot::ScaleStepCount
; i
++)
92 const LogicSnapshot::MipMapLevel
&m
= s
.mip_map_
[i
];
93 BOOST_CHECK_EQUAL(m
.length
, 0);
94 BOOST_CHECK_EQUAL(m
.data_length
, 0);
95 BOOST_CHECK(m
.data
== NULL
);
98 // Push 8 samples of 0x11s to bring the total up to 16
99 push_logic(s
, 8, 0x11);
101 // There should now be enough data for exactly one sample
102 // in mip map level 0, and that sample should be 0
103 const LogicSnapshot::MipMapLevel
&m0
= s
.mip_map_
[0];
104 BOOST_CHECK_EQUAL(m0
.length
, 1);
105 BOOST_CHECK_EQUAL(m0
.data_length
, LogicSnapshot::MipMapDataUnit
);
106 BOOST_REQUIRE(m0
.data
!= NULL
);
107 BOOST_CHECK_EQUAL(((uint8_t*)m0
.data
)[0], 0x11);
109 // The higher levels should still be empty
110 for (unsigned int i
= 1; i
< LogicSnapshot::ScaleStepCount
; i
++)
112 const LogicSnapshot::MipMapLevel
&m
= s
.mip_map_
[i
];
113 BOOST_CHECK_EQUAL(m
.length
, 0);
114 BOOST_CHECK_EQUAL(m
.data_length
, 0);
115 BOOST_CHECK(m
.data
== NULL
);
118 // Push 240 samples of all zeros to bring the total up to 256
119 push_logic(s
, 240, 0);
121 BOOST_CHECK_EQUAL(m0
.length
, 16);
122 BOOST_CHECK_EQUAL(m0
.data_length
, LogicSnapshot::MipMapDataUnit
);
124 BOOST_CHECK_EQUAL(((uint8_t*)m0
.data
)[1], 0x11);
125 for (unsigned int i
= 2; i
< m0
.length
; i
++)
126 BOOST_CHECK_EQUAL(((uint8_t*)m0
.data
)[i
], 0);
128 const LogicSnapshot::MipMapLevel
&m1
= s
.mip_map_
[1];
129 BOOST_CHECK_EQUAL(m1
.length
, 1);
130 BOOST_CHECK_EQUAL(m1
.data_length
, LogicSnapshot::MipMapDataUnit
);
131 BOOST_REQUIRE(m1
.data
!= NULL
);
132 BOOST_CHECK_EQUAL(((uint8_t*)m1
.data
)[0], 0x11);
134 //----- Test LogicSnapshot::get_subsampled_edges -----//
136 // Test a full view at full zoom.
137 vector
<LogicSnapshot::EdgePair
> edges
;
138 s
.get_subsampled_edges(edges
, 0, 255, 1, 0);
139 BOOST_REQUIRE_EQUAL(edges
.size(), 4);
141 BOOST_CHECK_EQUAL(edges
[0].first
, 0);
142 BOOST_CHECK_EQUAL(edges
[1].first
, 8);
143 BOOST_CHECK_EQUAL(edges
[2].first
, 16);
144 BOOST_CHECK_EQUAL(edges
[3].first
, 256);
146 // Test a subset at high zoom
148 s
.get_subsampled_edges(edges
, 6, 17, 0.05f
, 0);
149 BOOST_REQUIRE_EQUAL(edges
.size(), 4);
151 BOOST_CHECK_EQUAL(edges
[0].first
, 6);
152 BOOST_CHECK_EQUAL(edges
[1].first
, 8);
153 BOOST_CHECK_EQUAL(edges
[2].first
, 16);
154 BOOST_CHECK_EQUAL(edges
[3].first
, 18);
157 BOOST_AUTO_TEST_CASE(LargeData
)
160 const unsigned int Length
= 1000000;
162 sr_datafeed_logic logic
;
164 logic
.length
= Length
;
165 logic
.data
= new uint8_t[Length
];
166 uint8_t *data
= (uint8_t*)logic
.data
;
168 for (unsigned int i
= 0; i
< Length
; i
++)
169 *data
++ = (uint8_t)(i
>> 8);
171 LogicSnapshot
s(logic
);
172 delete[] (uint8_t*)logic
.data
;
174 BOOST_CHECK(s
.get_sample_count() == Length
);
176 // Check mip map level 0
177 BOOST_CHECK_EQUAL(s
.mip_map_
[0].length
, 62500);
178 BOOST_CHECK_EQUAL(s
.mip_map_
[0].data_length
,
179 LogicSnapshot::MipMapDataUnit
);
180 BOOST_REQUIRE(s
.mip_map_
[0].data
!= NULL
);
183 for (unsigned int i
= 0; i
< s
.mip_map_
[0].length
;)
185 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i
<< "]");
187 const uint8_t sample
= (uint8_t)((i
*16) >> 8);
188 BOOST_CHECK_EQUAL(s
.get_subsample(0, i
++) & 0xFF,
189 prev_sample
^ sample
);
190 prev_sample
= sample
;
192 for (int j
= 1; i
< s
.mip_map_
[0].length
&& j
< 16; j
++)
194 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i
<< "]");
195 BOOST_CHECK_EQUAL(s
.get_subsample(0, i
++) & 0xFF, 0);
199 // Check mip map level 1
200 BOOST_CHECK_EQUAL(s
.mip_map_
[1].length
, 3906);
201 BOOST_CHECK_EQUAL(s
.mip_map_
[1].data_length
,
202 LogicSnapshot::MipMapDataUnit
);
203 BOOST_REQUIRE(s
.mip_map_
[1].data
!= NULL
);
206 for (unsigned int i
= 0; i
< s
.mip_map_
[1].length
; i
++)
208 BOOST_TEST_MESSAGE("Testing mip_map[1].data[" << i
<< "]");
210 const uint8_t sample
= i
;
211 const uint8_t expected
= sample
^ prev_sample
;
214 BOOST_CHECK_EQUAL(s
.get_subsample(1, i
) & 0xFF, expected
);
217 // Check mip map level 2
218 BOOST_CHECK_EQUAL(s
.mip_map_
[2].length
, 244);
219 BOOST_CHECK_EQUAL(s
.mip_map_
[2].data_length
,
220 LogicSnapshot::MipMapDataUnit
);
221 BOOST_REQUIRE(s
.mip_map_
[2].data
!= NULL
);
224 for (unsigned int i
= 0; i
< s
.mip_map_
[2].length
; i
++)
226 BOOST_TEST_MESSAGE("Testing mip_map[2].data[" << i
<< "]");
228 const uint8_t sample
= i
<< 4;
229 const uint8_t expected
= (sample
^ prev_sample
) | 0x0F;
230 prev_sample
= sample
;
232 BOOST_CHECK_EQUAL(s
.get_subsample(2, i
) & 0xFF, expected
);
235 // Check mip map level 3
236 BOOST_CHECK_EQUAL(s
.mip_map_
[3].length
, 15);
237 BOOST_CHECK_EQUAL(s
.mip_map_
[3].data_length
,
238 LogicSnapshot::MipMapDataUnit
);
239 BOOST_REQUIRE(s
.mip_map_
[3].data
!= NULL
);
241 for (unsigned int i
= 0; i
< s
.mip_map_
[3].length
; i
++)
242 BOOST_CHECK_EQUAL(*((uint8_t*)s
.mip_map_
[3].data
+ i
),
245 // Check the higher levels
246 for (unsigned int i
= 4; i
< LogicSnapshot::ScaleStepCount
; i
++)
248 const LogicSnapshot::MipMapLevel
&m
= s
.mip_map_
[i
];
249 BOOST_CHECK_EQUAL(m
.length
, 0);
250 BOOST_CHECK_EQUAL(m
.data_length
, 0);
251 BOOST_CHECK(m
.data
== NULL
);
254 //----- Test LogicSnapshot::get_subsampled_edges -----//
255 // Check in normal case
256 vector
<LogicSnapshot::EdgePair
> edges
;
257 s
.get_subsampled_edges(edges
, 0, Length
-1, 1, 7);
259 BOOST_CHECK_EQUAL(edges
.size(), 32);
261 for (unsigned int i
= 0; i
< edges
.size() - 1; i
++)
263 BOOST_CHECK_EQUAL(edges
[i
].first
, i
* 32768);
264 BOOST_CHECK_EQUAL(edges
[i
].second
, i
& 1);
267 BOOST_CHECK_EQUAL(edges
[31].first
, 1000000);
269 // Check in very low zoom case
271 s
.get_subsampled_edges(edges
, 0, Length
-1, 50e6f
, 7);
273 BOOST_CHECK_EQUAL(edges
.size(), 2);
276 BOOST_AUTO_TEST_CASE(Pulses
)
278 const int Cycles
= 3;
279 const int Period
= 64;
280 const int Length
= Cycles
* Period
;
282 vector
<LogicSnapshot::EdgePair
> edges
;
284 //----- Create a LogicSnapshot -----//
285 sr_datafeed_logic logic
;
287 logic
.length
= Length
;
288 logic
.data
= (uint64_t*)new uint8_t[Length
];
289 uint8_t *p
= (uint8_t*)logic
.data
;
291 for (int i
= 0; i
< Cycles
; i
++) {
293 for (int j
= 1; j
< Period
; j
++)
297 LogicSnapshot
s(logic
);
298 delete[] (uint8_t*)logic
.data
;
300 //----- Check the mip-map -----//
301 // Check mip map level 0
302 BOOST_CHECK_EQUAL(s
.mip_map_
[0].length
, 12);
303 BOOST_CHECK_EQUAL(s
.mip_map_
[0].data_length
,
304 LogicSnapshot::MipMapDataUnit
);
305 BOOST_REQUIRE(s
.mip_map_
[0].data
!= NULL
);
307 for (unsigned int i
= 0; i
< s
.mip_map_
[0].length
;) {
308 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i
<< "]");
309 BOOST_CHECK_EQUAL(s
.get_subsample(0, i
++) & 0xFF, 0xFF);
312 i
< s
.mip_map_
[0].length
&&
313 j
< Period
/LogicSnapshot::MipMapScaleFactor
; j
++) {
315 "Testing mip_map[0].data[" << i
<< "]");
316 BOOST_CHECK_EQUAL(s
.get_subsample(0, i
++) & 0xFF, 0x00);
320 // Check the higher levels are all inactive
321 for (unsigned int i
= 1; i
< LogicSnapshot::ScaleStepCount
; i
++) {
322 const LogicSnapshot::MipMapLevel
&m
= s
.mip_map_
[i
];
323 BOOST_CHECK_EQUAL(m
.length
, 0);
324 BOOST_CHECK_EQUAL(m
.data_length
, 0);
325 BOOST_CHECK(m
.data
== NULL
);
328 //----- Test get_subsampled_edges at reduced scale -----//
329 s
.get_subsampled_edges(edges
, 0, Length
-1, 16.0f
, 2);
330 BOOST_REQUIRE_EQUAL(edges
.size(), Cycles
+ 2);
332 BOOST_CHECK_EQUAL(0, false);
333 for (unsigned int i
= 1; i
< edges
.size(); i
++)
334 BOOST_CHECK_EQUAL(edges
[i
].second
, false);
337 BOOST_AUTO_TEST_CASE(LongPulses
)
339 const int Cycles
= 3;
340 const int Period
= 64;
341 const int PulseWidth
= 16;
342 const int Length
= Cycles
* Period
;
345 vector
<LogicSnapshot::EdgePair
> edges
;
347 //----- Create a LogicSnapshot -----//
348 sr_datafeed_logic logic
;
350 logic
.length
= Length
* 8;
351 logic
.data
= (uint64_t*)new uint64_t[Length
];
352 uint64_t *p
= (uint64_t*)logic
.data
;
354 for (int i
= 0; i
< Cycles
; i
++) {
355 for (j
= 0; j
< PulseWidth
; j
++)
357 for (; j
< Period
; j
++)
361 LogicSnapshot
s(logic
);
362 delete[] (uint64_t*)logic
.data
;
364 //----- Check the mip-map -----//
365 // Check mip map level 0
366 BOOST_CHECK_EQUAL(s
.mip_map_
[0].length
, 12);
367 BOOST_CHECK_EQUAL(s
.mip_map_
[0].data_length
,
368 LogicSnapshot::MipMapDataUnit
);
369 BOOST_REQUIRE(s
.mip_map_
[0].data
!= NULL
);
371 for (unsigned int i
= 0; i
< s
.mip_map_
[0].length
;) {
372 for (j
= 0; i
< s
.mip_map_
[0].length
&& j
< 2; j
++) {
374 "Testing mip_map[0].data[" << i
<< "]");
375 BOOST_CHECK_EQUAL(s
.get_subsample(0, i
++), ~0);
378 for (; i
< s
.mip_map_
[0].length
&&
379 j
< Period
/LogicSnapshot::MipMapScaleFactor
; j
++) {
381 "Testing mip_map[0].data[" << i
<< "]");
382 BOOST_CHECK_EQUAL(s
.get_subsample(0, i
++), 0);
386 // Check the higher levels are all inactive
387 for (unsigned int i
= 1; i
< LogicSnapshot::ScaleStepCount
; i
++) {
388 const LogicSnapshot::MipMapLevel
&m
= s
.mip_map_
[i
];
389 BOOST_CHECK_EQUAL(m
.length
, 0);
390 BOOST_CHECK_EQUAL(m
.data_length
, 0);
391 BOOST_CHECK(m
.data
== NULL
);
394 //----- Test get_subsampled_edges at a full scale -----//
395 s
.get_subsampled_edges(edges
, 0, Length
-1, 16.0f
, 2);
396 BOOST_REQUIRE_EQUAL(edges
.size(), Cycles
* 2 + 1);
398 for (int i
= 0; i
< Cycles
; i
++) {
399 BOOST_CHECK_EQUAL(edges
[i
*2].first
, i
* Period
);
400 BOOST_CHECK_EQUAL(edges
[i
*2].second
, true);
401 BOOST_CHECK_EQUAL(edges
[i
*2+1].first
, i
* Period
+ PulseWidth
);
402 BOOST_CHECK_EQUAL(edges
[i
*2+1].second
, false);
405 BOOST_CHECK_EQUAL(edges
.back().first
, Length
);
406 BOOST_CHECK_EQUAL(edges
.back().second
, false);
408 //----- Test get_subsampled_edges at a simplified scale -----//
410 s
.get_subsampled_edges(edges
, 0, Length
-1, 17.0f
, 2);
412 BOOST_CHECK_EQUAL(edges
[0].first
, 0);
413 BOOST_CHECK_EQUAL(edges
[0].second
, true);
414 BOOST_CHECK_EQUAL(edges
[1].first
, 16);
415 BOOST_CHECK_EQUAL(edges
[1].second
, false);
417 for (int i
= 1; i
< Cycles
; i
++) {
418 BOOST_CHECK_EQUAL(edges
[i
+1].first
, i
* Period
);
419 BOOST_CHECK_EQUAL(edges
[i
+1].second
, false);
422 BOOST_CHECK_EQUAL(edges
.back().first
, Length
);
423 BOOST_CHECK_EQUAL(edges
.back().second
, false);
426 BOOST_AUTO_TEST_CASE(LisaMUsbHid
)
428 /* This test was created from the beginning of the USB_DM signal in
429 * sigrok-dumps-usb/lisa_m_usbhid/lisa_m_usbhid.sr
432 const int Edges
[] = {
433 7028, 7033, 7036, 7041, 7044, 7049, 7053, 7066, 7073, 7079,
434 7086, 7095, 7103, 7108, 7111, 7116, 7119, 7124, 7136, 7141,
437 const int Length
= Edges
[countof(Edges
) - 1];
442 //----- Create a LogicSnapshot -----//
443 sr_datafeed_logic logic
;
445 logic
.length
= Length
;
446 logic
.data
= new uint8_t[Length
];
447 uint8_t *data
= (uint8_t*)logic
.data
;
449 for (unsigned int i
= 0; i
< countof(Edges
); i
++) {
450 const int edgePos
= Edges
[i
];
451 memset(&data
[lastEdgePos
], state
? 0x02 : 0,
452 edgePos
- lastEdgePos
- 1);
454 lastEdgePos
= edgePos
;
458 LogicSnapshot
s(logic
);
459 delete[] (uint64_t*)logic
.data
;
461 vector
<LogicSnapshot::EdgePair
> edges
;
464 /* The trailing edge of the pulse train is falling in the source data.
465 * Check this is always true at different scales
469 s
.get_subsampled_edges(edges
, 0, Length
-1, 33.333332f
, 1);
470 BOOST_CHECK_EQUAL(edges
[edges
.size() - 2].second
, false);
474 * This test checks the rendering of wide data (more than 8 channels)
475 * Probe signals are either all-high, or all-low, but are interleaved such that
476 * they would toggle during every sample if treated like 8 channels.
477 * The packet contains a large number of samples, so the mipmap generation kicks
480 * The signals should not toggle (have exactly two edges: the start and end)
482 BOOST_AUTO_TEST_CASE(WideData
)
484 const int Length
= 512<<10;
485 uint16_t *data
= new uint16_t[Length
];
487 sr_datafeed_logic logic
;
488 logic
.unitsize
= sizeof(data
[0]);
489 logic
.length
= Length
* sizeof(data
[0]);
492 for (int i
= 0; i
< Length
; i
++)
495 LogicSnapshot
s(logic
);
497 vector
<LogicSnapshot::EdgePair
> edges
;
500 s
.get_subsampled_edges(edges
, 0, Length
-1, 1, 0);
501 BOOST_CHECK_EQUAL(edges
.size(), 2);
504 s
.get_subsampled_edges(edges
, 0, Length
-1, 1, 8);
505 BOOST_CHECK_EQUAL(edges
.size(), 2);
512 * This test is a replica of sixteen.sr attached to Bug #33.
514 BOOST_AUTO_TEST_CASE(Sixteen
)
516 const int Length
= 8;
517 uint16_t data
[Length
];
519 sr_datafeed_logic logic
;
520 logic
.unitsize
= sizeof(data
[0]);
521 logic
.length
= Length
* sizeof(data
[0]);
524 for (int i
= 0; i
< Length
; i
++)
527 LogicSnapshot
s(logic
);
529 vector
<LogicSnapshot::EdgePair
> edges
;
530 s
.get_subsampled_edges(edges
, 0, 2, 0.0004, 1);
532 BOOST_CHECK_EQUAL(edges
.size(), 2);
535 BOOST_AUTO_TEST_SUITE_END()