]> sigrok.org Git - pulseview.git/blob - test/data/logicsnapshot.cpp
8be915f02544bc6a8f10ade5f770438150c95357
[pulseview.git] / test / data / logicsnapshot.cpp
1 /*
2  * This file is part of the PulseView project.
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
21 #include <extdef.h>
22
23 #define __STDC_LIMIT_MACROS
24 #include <stdint.h>
25
26 #include <boost/test/unit_test.hpp>
27
28 #include "../../pv/data/logicsnapshot.h"
29
30 using pv::data::LogicSnapshot;
31 using std::vector;
32
33 BOOST_AUTO_TEST_SUITE(LogicSnapshotTest)
34
35 void push_logic(LogicSnapshot &s, unsigned int length, uint8_t value)
36 {
37         sr_datafeed_logic logic;
38         logic.unitsize = 1;
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;
44 }
45
46 BOOST_AUTO_TEST_CASE(Pow2)
47 {
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);
51
52         BOOST_CHECK_EQUAL(
53                 LogicSnapshot::pow2_ceil(INT64_MIN, 0), INT64_MIN);
54         BOOST_CHECK_EQUAL(
55                 LogicSnapshot::pow2_ceil(INT64_MAX, 0), INT64_MAX);
56
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);
61 }
62
63 BOOST_AUTO_TEST_CASE(Basic)
64 {
65         // Create an empty LogicSnapshot object
66         sr_datafeed_logic logic;
67         logic.length = 0;
68         logic.unitsize = 1;
69         logic.data = NULL;
70
71         LogicSnapshot s(logic);
72
73         //----- Test LogicSnapshot::push_logic -----//
74
75         BOOST_CHECK(s.get_sample_count() == 0);
76         for (unsigned int i = 0; i < LogicSnapshot::ScaleStepCount; i++)
77         {
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);
82         }
83
84         // Push 8 samples of all zeros
85         push_logic(s, 8, 0);
86
87         BOOST_CHECK(s.get_sample_count() == 8);
88
89         // There should not be enough samples to have a single mip map sample
90         for (unsigned int i = 0; i < LogicSnapshot::ScaleStepCount; i++)
91         {
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);
96         }
97
98         // Push 8 samples of 0x11s to bring the total up to 16
99         push_logic(s, 8, 0x11);
100
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);
108
109         // The higher levels should still be empty
110         for (unsigned int i = 1; i < LogicSnapshot::ScaleStepCount; i++)
111         {
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);
116         }
117
118         // Push 240 samples of all zeros to bring the total up to 256
119         push_logic(s, 240, 0);
120
121         BOOST_CHECK_EQUAL(m0.length, 16);
122         BOOST_CHECK_EQUAL(m0.data_length, LogicSnapshot::MipMapDataUnit);
123
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);
127
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);
133
134         //----- Test LogicSnapshot::get_subsampled_edges -----//
135
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);
140
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, 255);
145
146         // Test a subset at high zoom
147         edges.clear();
148         s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
149         BOOST_REQUIRE_EQUAL(edges.size(), 4);
150
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, 17);
155 }
156
157 BOOST_AUTO_TEST_CASE(LargeData)
158 {
159         uint8_t prev_sample;
160         const unsigned int Length = 1000000;
161
162         sr_datafeed_logic logic;
163         logic.unitsize = 1;
164         logic.length = Length;
165         logic.data = new uint8_t[Length];
166         uint8_t *data = (uint8_t*)logic.data;
167
168         for (unsigned int i = 0; i < Length; i++)
169                 *data++ = (uint8_t)(i >> 8);
170
171         LogicSnapshot s(logic);
172         delete[] (uint8_t*)logic.data;
173
174         BOOST_CHECK(s.get_sample_count() == Length);
175
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);
181
182         prev_sample = 0;
183         for (unsigned int i = 0; i < s._mip_map[0].length;)
184         {
185                 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
186
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;
191
192                 for (int j = 1; i < s._mip_map[0].length && j < 16; j++)
193                 {
194                         BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
195                         BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0);
196                 }
197         }
198
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);
204
205         prev_sample = 0;
206         for (unsigned int i = 0; i < s._mip_map[1].length; i++)
207         {
208                 BOOST_TEST_MESSAGE("Testing mip_map[1].data[" << i << "]");
209
210                 const uint8_t sample = i;
211                 const uint8_t expected = sample ^ prev_sample;
212                 prev_sample = i;
213
214                 BOOST_CHECK_EQUAL(s.get_subsample(1, i) & 0xFF, expected);
215         }
216
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);
222
223         prev_sample = 0;
224         for (unsigned int i = 0; i < s._mip_map[2].length; i++)
225         {
226                 BOOST_TEST_MESSAGE("Testing mip_map[2].data[" << i << "]");
227
228                 const uint8_t sample = i << 4;
229                 const uint8_t expected = (sample ^ prev_sample) | 0x0F;
230                 prev_sample = sample;
231
232                 BOOST_CHECK_EQUAL(s.get_subsample(2, i) & 0xFF, expected);
233         }
234
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);
240
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),
243                         0xFF);
244
245         // Check the higher levels
246         for (unsigned int i = 4; i < LogicSnapshot::ScaleStepCount; i++)
247         {
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);
252         }
253
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);
258
259         BOOST_CHECK_EQUAL(edges.size(), 32);
260
261         for (unsigned int i = 0; i < edges.size() - 1; i++)
262         {
263                 BOOST_CHECK_EQUAL(edges[i].first, i * 32768);
264                 BOOST_CHECK_EQUAL(edges[i].second, i & 1);
265         }
266
267         BOOST_CHECK_EQUAL(edges[31].first, 999999);
268
269         // Check in very low zoom case
270         edges.clear();
271         s.get_subsampled_edges(edges, 0, Length-1, 50e6f, 7);
272
273         BOOST_CHECK_EQUAL(edges.size(), 2);
274 }
275
276 BOOST_AUTO_TEST_CASE(Pulses)
277 {
278         const int Cycles = 3;
279         const int Period = 64;
280         const int Length = Cycles * Period;
281
282         vector<LogicSnapshot::EdgePair> edges;
283
284         //----- Create a LogicSnapshot -----//
285         sr_datafeed_logic logic;
286         logic.unitsize = 1;
287         logic.length = Length;
288         logic.data = (uint64_t*)new uint8_t[Length];
289         uint8_t *p = (uint8_t*)logic.data;
290
291         for (int i = 0; i < Cycles; i++) {
292                 *p++ = 0xFF;
293                 for (int j = 1; j < Period; j++)
294                         *p++ = 0x00;
295         }
296
297         LogicSnapshot s(logic);
298         delete[] (uint8_t*)logic.data;
299
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);
306
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);
310
311                 for (int j = 1;
312                         i < s._mip_map[0].length &&
313                         j < Period/LogicSnapshot::MipMapScaleFactor; j++) {
314                         BOOST_TEST_MESSAGE(
315                                 "Testing mip_map[0].data[" << i << "]");
316                         BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0x00);
317                 }
318         }
319
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);
326         }
327
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);
331
332         BOOST_CHECK_EQUAL(0, false);
333         for (unsigned int i = 1; i < edges.size(); i++)
334                 BOOST_CHECK_EQUAL(edges[i].second, false);
335 }
336
337 BOOST_AUTO_TEST_CASE(LongPulses)
338 {
339         const int Cycles = 3;
340         const int Period = 64;
341         const int PulseWidth = 16;
342         const int Length = Cycles * Period;
343
344         int j;
345         vector<LogicSnapshot::EdgePair> edges;
346
347         //----- Create a LogicSnapshot -----//
348         sr_datafeed_logic logic;
349         logic.unitsize = 8;
350         logic.length = Length * 8;
351         logic.data = (uint64_t*)new uint64_t[Length];
352         uint64_t *p = (uint64_t*)logic.data;
353
354         for (int i = 0; i < Cycles; i++) {
355                 for (j = 0; j < PulseWidth; j++)
356                         *p++ = ~0;
357                 for (; j < Period; j++)
358                         *p++ = 0;
359         }
360
361         LogicSnapshot s(logic);
362         delete[] (uint64_t*)logic.data;
363
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);
370
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++) {
373                         BOOST_TEST_MESSAGE(
374                                 "Testing mip_map[0].data[" << i << "]");
375                         BOOST_CHECK_EQUAL(s.get_subsample(0, i++), ~0);
376                 }
377
378                 for (; i < s._mip_map[0].length &&
379                         j < Period/LogicSnapshot::MipMapScaleFactor; j++) {
380                         BOOST_TEST_MESSAGE(
381                                 "Testing mip_map[0].data[" << i << "]");
382                         BOOST_CHECK_EQUAL(s.get_subsample(0, i++), 0);
383                 }
384         }
385
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);
392         }
393
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);
397
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);
403         }
404
405         BOOST_CHECK_EQUAL(edges.back().first, Length-1);
406         BOOST_CHECK_EQUAL(edges.back().second, false);
407
408         //----- Test get_subsampled_edges at a simplified scale -----//
409         edges.clear();
410         s.get_subsampled_edges(edges, 0, Length-1, 17.0f, 2);
411
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);
416         
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);
420         }
421
422         BOOST_CHECK_EQUAL(edges.back().first, Length-1);
423         BOOST_CHECK_EQUAL(edges.back().second, false);
424 }
425
426 BOOST_AUTO_TEST_CASE(LisaMUsbHid)
427 {
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
430          */
431
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,
435                 7148, 7162, 7500
436         };
437         const int Length = Edges[countof(Edges) - 1];
438
439         bool state = false;
440         int lastEdgePos = 0;
441
442         //----- Create a LogicSnapshot -----//
443         sr_datafeed_logic logic;
444         logic.unitsize = 1;
445         logic.length = Length;
446         logic.data = new uint8_t[Length];
447         uint8_t *data = (uint8_t*)logic.data;
448
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);
453
454                 lastEdgePos = edgePos;
455                 state = !state;
456         }
457
458         LogicSnapshot s(logic);
459         delete[] (uint64_t*)logic.data;
460
461         vector<LogicSnapshot::EdgePair> edges;
462
463
464         /* The trailing edge of the pulse train is falling in the source data.
465          * Check this is always true at different scales
466          */
467
468         edges.clear();
469         s.get_subsampled_edges(edges, 0, Length-1, 33.333332f, 1);
470         BOOST_CHECK_EQUAL(edges[edges.size() - 2].second, false);
471 }
472
473 /*
474  * This test checks the rendering of wide data (more than 8 probes)
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 probes.
477  * The packet contains a large number of samples, so the mipmap generation kicks
478  * in.
479  *
480  * The signals should not toggle (have exactly two edges: the start and end)
481  */
482 BOOST_AUTO_TEST_CASE(WideData)
483 {
484         const int Length = 512<<10;
485         uint16_t *data = new uint16_t[Length];
486
487         sr_datafeed_logic logic;
488         logic.unitsize = sizeof(data[0]);
489         logic.length = Length * sizeof(data[0]);
490         logic.data = data;
491
492         for (int i = 0; i < Length; i++)
493                 data[i] = 0x0FF0;
494
495         LogicSnapshot s(logic);
496
497         vector<LogicSnapshot::EdgePair> edges;
498
499         edges.clear();
500         s.get_subsampled_edges(edges, 0, Length-1, 1, 0);
501         BOOST_CHECK_EQUAL(edges.size(), 2);
502
503         edges.clear();
504         s.get_subsampled_edges(edges, 0, Length-1, 1, 8);
505         BOOST_CHECK_EQUAL(edges.size(), 2);
506
507         // Cleanup
508         delete [] data;
509 }
510
511 /*
512  * This test is a replica of sixteen.sr attached to Bug #33.
513  */
514 BOOST_AUTO_TEST_CASE(Sixteen)
515 {
516         const int Length = 8;
517         uint16_t data[Length];
518
519         sr_datafeed_logic logic;
520         logic.unitsize = sizeof(data[0]);
521         logic.length = Length * sizeof(data[0]);
522         logic.data = data;
523
524         for (int i = 0; i < Length; i++)
525                 data[i] = 0xFFFE;
526
527         LogicSnapshot s(logic);
528
529         vector<LogicSnapshot::EdgePair> edges;
530         s.get_subsampled_edges(edges, 0, 2, 0.0004, 1);
531
532         BOOST_CHECK_EQUAL(edges.size(), 2);
533 }
534
535 BOOST_AUTO_TEST_SUITE_END()