]> sigrok.org Git - pulseview.git/blame - test/logicdatasnapshot.cpp
Add struct sr_context * to sr_driver_init() calls
[pulseview.git] / test / logicdatasnapshot.cpp
CommitLineData
4ceab49a 1/*
b3f22de0 2 * This file is part of the PulseView project.
4ceab49a
JH
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
cef18fc6
JH
21#include <extdef.h>
22
8743e7cb
JH
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
25
4ceab49a
JH
26#include <boost/test/unit_test.hpp>
27
51e77110 28#include "../pv/logicdatasnapshot.h"
4ceab49a 29
0b02e057
JH
30using namespace std;
31
51e77110
JH
32using pv::LogicDataSnapshot;
33
ecda6ca9
JH
34BOOST_AUTO_TEST_SUITE(LogicDataSnapshotTest)
35
4ceab49a
JH
36void push_logic(LogicDataSnapshot &s, unsigned int length, uint8_t value)
37{
38 sr_datafeed_logic logic;
39 logic.unitsize = 1;
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;
45}
46
8743e7cb
JH
47BOOST_AUTO_TEST_CASE(Pow2)
48{
8743e7cb
JH
49 BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(0, 0), 0);
50 BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(1, 0), 1);
51 BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(2, 0), 2);
52
53 BOOST_CHECK_EQUAL(
54 LogicDataSnapshot::pow2_ceil(INT64_MIN, 0), INT64_MIN);
55 BOOST_CHECK_EQUAL(
56 LogicDataSnapshot::pow2_ceil(INT64_MAX, 0), INT64_MAX);
57
8743e7cb
JH
58 BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(0, 1), 0);
59 BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(1, 1), 2);
60 BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(2, 1), 2);
61 BOOST_CHECK_EQUAL(LogicDataSnapshot::pow2_ceil(3, 1), 4);
62}
63
ecda6ca9 64BOOST_AUTO_TEST_CASE(Basic)
4ceab49a
JH
65{
66 // Create an empty LogicDataSnapshot object
67 sr_datafeed_logic logic;
68 logic.length = 0;
69 logic.unitsize = 1;
70 logic.data = NULL;
71
72 LogicDataSnapshot s(logic);
73
0b02e057
JH
74 //----- Test LogicDataSnapshot::push_logic -----//
75
4ceab49a 76 BOOST_CHECK(s.get_sample_count() == 0);
38f5609e 77 for(unsigned int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
4ceab49a
JH
78 {
79 const LogicDataSnapshot::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);
83 }
84
85 // Push 8 samples of all zeros
86 push_logic(s, 8, 0);
87
88 BOOST_CHECK(s.get_sample_count() == 8);
89
90 // There should not be enough samples to have a single mip map sample
38f5609e 91 for(unsigned int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
4ceab49a
JH
92 {
93 const LogicDataSnapshot::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);
97 }
98
99 // Push 8 samples of 0x11s to bring the total up to 16
100 push_logic(s, 8, 0x11);
101
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 LogicDataSnapshot::MipMapLevel &m0 = s._mip_map[0];
105 BOOST_CHECK_EQUAL(m0.length, 1);
106 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
107 BOOST_REQUIRE(m0.data != NULL);
108 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
109
110 // The higher levels should still be empty
38f5609e 111 for(unsigned int i = 1; i < LogicDataSnapshot::ScaleStepCount; i++)
4ceab49a
JH
112 {
113 const LogicDataSnapshot::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);
117 }
118
119 // Push 240 samples of all zeros to bring the total up to 256
120 push_logic(s, 240, 0);
121
122 BOOST_CHECK_EQUAL(m0.length, 16);
123 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
124
125 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
38f5609e 126 for(unsigned int i = 2; i < m0.length; i++)
4ceab49a
JH
127 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
128
129 const LogicDataSnapshot::MipMapLevel &m1 = s._mip_map[1];
130 BOOST_CHECK_EQUAL(m1.length, 1);
131 BOOST_CHECK_EQUAL(m1.data_length, LogicDataSnapshot::MipMapDataUnit);
132 BOOST_REQUIRE(m1.data != NULL);
133 BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);
0b02e057
JH
134
135 //----- Test LogicDataSnapshot::get_subsampled_edges -----//
136
137 // Test a full view at full zoom.
138 vector<LogicDataSnapshot::EdgePair> edges;
139 s.get_subsampled_edges(edges, 0, 255, 1, 0);
140 BOOST_REQUIRE_EQUAL(edges.size(), 4);
141
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);
146
147 // Test a subset at high zoom
148 edges.clear();
149 s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
150 BOOST_REQUIRE_EQUAL(edges.size(), 4);
151
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);
4ceab49a 156}
ecda6ca9
JH
157
158BOOST_AUTO_TEST_CASE(LargeData)
159{
160 uint8_t prev_sample;
38f5609e 161 const unsigned int Length = 1000000;
ecda6ca9
JH
162
163 sr_datafeed_logic logic;
164 logic.unitsize = 1;
165 logic.length = Length;
166 logic.data = new uint8_t[Length];
167 uint8_t *data = (uint8_t*)logic.data;
168
38f5609e 169 for(unsigned int i = 0; i < Length; i++)
ecda6ca9
JH
170 *data++ = (uint8_t)(i >> 8);
171
172 LogicDataSnapshot s(logic);
173 delete[] (uint8_t*)logic.data;
174
175 BOOST_CHECK(s.get_sample_count() == Length);
176
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 LogicDataSnapshot::MipMapDataUnit);
181 BOOST_REQUIRE(s._mip_map[0].data != NULL);
182
183 prev_sample = 0;
38f5609e 184 for(unsigned int i = 0; i < s._mip_map[0].length;)
ecda6ca9
JH
185 {
186 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
187
188 const uint8_t sample = (uint8_t)((i*16) >> 8);
b2bcbe51 189 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF,
ecda6ca9
JH
190 prev_sample ^ sample);
191 prev_sample = sample;
192
193 for(int j = 1; i < s._mip_map[0].length && j < 16; j++)
194 {
195 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
b2bcbe51 196 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0);
ecda6ca9
JH
197 }
198 }
199
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 LogicDataSnapshot::MipMapDataUnit);
204 BOOST_REQUIRE(s._mip_map[1].data != NULL);
205
206 prev_sample = 0;
38f5609e 207 for(unsigned int i = 0; i < s._mip_map[1].length; i++)
ecda6ca9
JH
208 {
209 BOOST_TEST_MESSAGE("Testing mip_map[1].data[" << i << "]");
210
211 const uint8_t sample = i;
212 const uint8_t expected = sample ^ prev_sample;
213 prev_sample = i;
214
b2bcbe51 215 BOOST_CHECK_EQUAL(s.get_subsample(1, i) & 0xFF, expected);
ecda6ca9
JH
216 }
217
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 LogicDataSnapshot::MipMapDataUnit);
222 BOOST_REQUIRE(s._mip_map[2].data != NULL);
223
224 prev_sample = 0;
38f5609e 225 for(unsigned int i = 0; i < s._mip_map[2].length; i++)
ecda6ca9
JH
226 {
227 BOOST_TEST_MESSAGE("Testing mip_map[2].data[" << i << "]");
228
229 const uint8_t sample = i << 4;
230 const uint8_t expected = (sample ^ prev_sample) | 0x0F;
231 prev_sample = sample;
232
b2bcbe51 233 BOOST_CHECK_EQUAL(s.get_subsample(2, i) & 0xFF, expected);
ecda6ca9
JH
234 }
235
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 LogicDataSnapshot::MipMapDataUnit);
240 BOOST_REQUIRE(s._mip_map[3].data != NULL);
241
38f5609e 242 for(unsigned int i = 0; i < s._mip_map[3].length; i++)
ecda6ca9
JH
243 BOOST_CHECK_EQUAL(*((uint8_t*)s._mip_map[3].data + i),
244 0xFF);
245
246 // Check the higher levels
38f5609e 247 for(unsigned int i = 4; i < LogicDataSnapshot::ScaleStepCount; i++)
ecda6ca9
JH
248 {
249 const LogicDataSnapshot::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);
253 }
254
255 //----- Test LogicDataSnapshot::get_subsampled_edges -----//
ac7aa636 256 // Check in normal case
ecda6ca9 257 vector<LogicDataSnapshot::EdgePair> edges;
ecda6ca9 258 s.get_subsampled_edges(edges, 0, Length-1, 1, 7);
ecda6ca9 259
39f6e56e
JH
260 BOOST_CHECK_EQUAL(edges.size(), 32);
261
38f5609e 262 for(unsigned int i = 0; i < edges.size() - 1; i++)
ecda6ca9 263 {
39f6e56e
JH
264 BOOST_CHECK_EQUAL(edges[i].first, i * 32768);
265 BOOST_CHECK_EQUAL(edges[i].second, i & 1);
ecda6ca9
JH
266 }
267
39f6e56e 268 BOOST_CHECK_EQUAL(edges[31].first, 999999);
ac7aa636
JH
269
270 // Check in very low zoom case
271 edges.clear();
272 s.get_subsampled_edges(edges, 0, Length-1, 50e6f, 7);
273
274 BOOST_CHECK_EQUAL(edges.size(), 2);
ecda6ca9
JH
275}
276
3db297c4
JH
277BOOST_AUTO_TEST_CASE(Pulses)
278{
279 const int Cycles = 3;
280 const int Period = 64;
281 const int Length = Cycles * Period;
282
283 vector<LogicDataSnapshot::EdgePair> edges;
284
285 //----- Create a LogicDataSnapshot -----//
286 sr_datafeed_logic logic;
287 logic.unitsize = 1;
288 logic.length = Length;
289 logic.data = (uint64_t*)new uint8_t[Length];
290 uint8_t *p = (uint8_t*)logic.data;
291
292 for(int i = 0; i < Cycles; i++) {
293 *p++ = 0xFF;
294 for(int j = 1; j < Period; j++)
295 *p++ = 0x00;
296 }
297
298 LogicDataSnapshot s(logic);
821f23af 299 delete[] (uint8_t*)logic.data;
3db297c4
JH
300
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 LogicDataSnapshot::MipMapDataUnit);
306 BOOST_REQUIRE(s._mip_map[0].data != NULL);
307
38f5609e 308 for(unsigned int i = 0; i < s._mip_map[0].length;) {
3db297c4
JH
309 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
310 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0xFF);
311
312 for(int j = 1;
313 i < s._mip_map[0].length &&
314 j < Period/LogicDataSnapshot::MipMapScaleFactor; j++) {
315 BOOST_TEST_MESSAGE(
316 "Testing mip_map[0].data[" << i << "]");
317 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0x00);
318 }
319 }
320
321 // Check the higher levels are all inactive
38f5609e 322 for(unsigned int i = 1; i < LogicDataSnapshot::ScaleStepCount; i++) {
3db297c4
JH
323 const LogicDataSnapshot::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);
327 }
328
329 //----- Test get_subsampled_edges at reduced scale -----//
330 s.get_subsampled_edges(edges, 0, Length-1, 16.0f, 2);
7d0d64f9 331 BOOST_REQUIRE_EQUAL(edges.size(), Cycles + 2);
3db297c4 332
7d0d64f9 333 BOOST_CHECK_EQUAL(0, false);
38f5609e 334 for(unsigned int i = 1; i < edges.size(); i++)
3db297c4
JH
335 BOOST_CHECK_EQUAL(edges[i].second, false);
336}
337
910b16ec
JH
338BOOST_AUTO_TEST_CASE(LongPulses)
339{
340 const int Cycles = 3;
341 const int Period = 64;
342 const int PulseWidth = 16;
343 const int Length = Cycles * Period;
344
345 int j;
346 vector<LogicDataSnapshot::EdgePair> edges;
347
348 //----- Create a LogicDataSnapshot -----//
349 sr_datafeed_logic logic;
4f767cf7 350 logic.unitsize = 8;
910b16ec 351 logic.length = Length;
4f767cf7
JH
352 logic.data = (uint64_t*)new uint64_t[Length];
353 uint64_t *p = (uint64_t*)logic.data;
910b16ec
JH
354
355 for(int i = 0; i < Cycles; i++) {
356 for(j = 0; j < PulseWidth; j++)
4f767cf7 357 *p++ = ~0;
fe48aaeb 358 for(; j < Period; j++)
4f767cf7 359 *p++ = 0;
910b16ec
JH
360 }
361
362 LogicDataSnapshot s(logic);
ba3c4dae 363 delete[] (uint64_t*)logic.data;
910b16ec
JH
364
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 LogicDataSnapshot::MipMapDataUnit);
370 BOOST_REQUIRE(s._mip_map[0].data != NULL);
371
38f5609e 372 for(unsigned int i = 0; i < s._mip_map[0].length;) {
910b16ec
JH
373 for(j = 0; i < s._mip_map[0].length && j < 2; j++) {
374 BOOST_TEST_MESSAGE(
375 "Testing mip_map[0].data[" << i << "]");
4f767cf7 376 BOOST_CHECK_EQUAL(s.get_subsample(0, i++), ~0);
910b16ec
JH
377 }
378
fe48aaeb 379 for(; i < s._mip_map[0].length &&
910b16ec
JH
380 j < Period/LogicDataSnapshot::MipMapScaleFactor; j++) {
381 BOOST_TEST_MESSAGE(
382 "Testing mip_map[0].data[" << i << "]");
4f767cf7 383 BOOST_CHECK_EQUAL(s.get_subsample(0, i++), 0);
910b16ec
JH
384 }
385 }
386
387 // Check the higher levels are all inactive
38f5609e 388 for(unsigned int i = 1; i < LogicDataSnapshot::ScaleStepCount; i++) {
910b16ec
JH
389 const LogicDataSnapshot::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);
393 }
394
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);
398
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);
404 }
405
406 BOOST_CHECK_EQUAL(edges.back().first, Length-1);
407 BOOST_CHECK_EQUAL(edges.back().second, false);
408
409 //----- Test get_subsampled_edges at a simplified scale -----//
410 edges.clear();
411 s.get_subsampled_edges(edges, 0, Length-1, 17.0f, 2);
412
7d0d64f9
JH
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);
417
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);
910b16ec
JH
421 }
422
423 BOOST_CHECK_EQUAL(edges.back().first, Length-1);
424 BOOST_CHECK_EQUAL(edges.back().second, false);
425}
426
a126c277
JH
427BOOST_AUTO_TEST_CASE(LisaMUsbHid)
428{
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
431 */
432
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,
436 7148, 7162, 7500
437 };
438 const int Length = Edges[countof(Edges) - 1];
439
440 bool state = false;
441 int lastEdgePos = 0;
442
443 //----- Create a LogicDataSnapshot -----//
444 sr_datafeed_logic logic;
445 logic.unitsize = 1;
446 logic.length = Length;
447 logic.data = new uint8_t[Length];
448 uint8_t *data = (uint8_t*)logic.data;
449
38f5609e 450 for(unsigned int i = 0; i < countof(Edges); i++) {
a126c277
JH
451 const int edgePos = Edges[i];
452 memset(&data[lastEdgePos], state ? 0x02 : 0,
453 edgePos - lastEdgePos - 1);
454
455 lastEdgePos = edgePos;
456 state = !state;
457 }
458
459 LogicDataSnapshot s(logic);
460 delete[] (uint64_t*)logic.data;
461
462 vector<LogicDataSnapshot::EdgePair> edges;
463
464
465 /* The trailing edge of the pulse train is falling in the source data.
466 * Check this is always true at different scales
467 */
468
469 edges.clear();
470 s.get_subsampled_edges(edges, 0, Length-1, 33.333332f, 1);
471 BOOST_CHECK_EQUAL(edges[edges.size() - 2].second, false);
472}
473
474
ecda6ca9 475BOOST_AUTO_TEST_SUITE_END()