]> sigrok.org Git - pulseview.git/blob - test/data/logicsegment.cpp
b634257c17eaa278d261e560f7fb6a5c776c5cc6
[pulseview.git] / test / data / logicsegment.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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <extdef.h>
21
22 #include <cstdint>
23
24 #include <boost/test/unit_test.hpp>
25
26 #include <pv/data/logicsegment.hpp>
27
28 using pv::data::LogicSegment;
29 using std::vector;
30
31 // Dummy, remove again when unit tests are fixed.
32 BOOST_AUTO_TEST_SUITE(DummyTestSuite)
33 BOOST_AUTO_TEST_CASE(DummyTestCase)
34 {
35         BOOST_CHECK_EQUAL(1, 1);
36 }
37 BOOST_AUTO_TEST_SUITE_END()
38
39 #if 0
40 BOOST_AUTO_TEST_SUITE(LogicSegmentTest)
41
42 void push_logic(LogicSegment &s, unsigned int length, uint8_t value)
43 {
44         sr_datafeed_logic logic;
45         logic.unitsize = 1;
46         logic.length = length;
47         logic.data = new uint8_t[length];
48         memset(logic.data, value, length * logic.unitsize);
49         s.append_payload(logic);
50         delete[] (uint8_t*)logic.data;
51 }
52
53 BOOST_AUTO_TEST_CASE(Pow2)
54 {
55         BOOST_CHECK_EQUAL(LogicSegment::pow2_ceil(0, 0), 0);
56         BOOST_CHECK_EQUAL(LogicSegment::pow2_ceil(1, 0), 1);
57         BOOST_CHECK_EQUAL(LogicSegment::pow2_ceil(2, 0), 2);
58
59         BOOST_CHECK_EQUAL(
60                 LogicSegment::pow2_ceil(INT64_MIN, 0), INT64_MIN);
61         BOOST_CHECK_EQUAL(
62                 LogicSegment::pow2_ceil(INT64_MAX, 0), INT64_MAX);
63
64         BOOST_CHECK_EQUAL(LogicSegment::pow2_ceil(0, 1), 0);
65         BOOST_CHECK_EQUAL(LogicSegment::pow2_ceil(1, 1), 2);
66         BOOST_CHECK_EQUAL(LogicSegment::pow2_ceil(2, 1), 2);
67         BOOST_CHECK_EQUAL(LogicSegment::pow2_ceil(3, 1), 4);
68 }
69
70 BOOST_AUTO_TEST_CASE(Basic)
71 {
72         // Create an empty LogicSegment object
73         sr_datafeed_logic logic;
74         logic.length = 0;
75         logic.unitsize = 1;
76         logic.data = nullptr;
77
78         LogicSegment s(logic);
79
80         //----- Test LogicSegment::push_logic -----//
81
82         BOOST_CHECK(s.get_sample_count() == 0);
83         for (unsigned int i = 0; i < LogicSegment::ScaleStepCount; i++)
84         {
85                 const LogicSegment::MipMapLevel &m = s.mip_map_[i];
86                 BOOST_CHECK_EQUAL(m.length, 0);
87                 BOOST_CHECK_EQUAL(m.data_length, 0);
88                 BOOST_CHECK(m.data == nullptr);
89         }
90
91         // Push 8 samples of all zeros
92         push_logic(s, 8, 0);
93
94         BOOST_CHECK(s.get_sample_count() == 8);
95
96         // There should not be enough samples to have a single mip map sample
97         for (unsigned int i = 0; i < LogicSegment::ScaleStepCount; i++)
98         {
99                 const LogicSegment::MipMapLevel &m = s.mip_map_[i];
100                 BOOST_CHECK_EQUAL(m.length, 0);
101                 BOOST_CHECK_EQUAL(m.data_length, 0);
102                 BOOST_CHECK(m.data == nullptr);
103         }
104
105         // Push 8 samples of 0x11s to bring the total up to 16
106         push_logic(s, 8, 0x11);
107
108         // There should now be enough data for exactly one sample
109         // in mip map level 0, and that sample should be 0
110         const LogicSegment::MipMapLevel &m0 = s.mip_map_[0];
111         BOOST_CHECK_EQUAL(m0.length, 1);
112         BOOST_CHECK_EQUAL(m0.data_length, LogicSegment::MipMapDataUnit);
113         BOOST_REQUIRE(m0.data != nullptr);
114         BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
115
116         // The higher levels should still be empty
117         for (unsigned int i = 1; i < LogicSegment::ScaleStepCount; i++)
118         {
119                 const LogicSegment::MipMapLevel &m = s.mip_map_[i];
120                 BOOST_CHECK_EQUAL(m.length, 0);
121                 BOOST_CHECK_EQUAL(m.data_length, 0);
122                 BOOST_CHECK(m.data == nullptr);
123         }
124
125         // Push 240 samples of all zeros to bring the total up to 256
126         push_logic(s, 240, 0);
127
128         BOOST_CHECK_EQUAL(m0.length, 16);
129         BOOST_CHECK_EQUAL(m0.data_length, LogicSegment::MipMapDataUnit);
130
131         BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
132         for (unsigned int i = 2; i < m0.length; i++)
133                 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
134
135         const LogicSegment::MipMapLevel &m1 = s.mip_map_[1];
136         BOOST_CHECK_EQUAL(m1.length, 1);
137         BOOST_CHECK_EQUAL(m1.data_length, LogicSegment::MipMapDataUnit);
138         BOOST_REQUIRE(m1.data != nullptr);
139         BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);
140
141         //----- Test LogicSegment::get_subsampled_edges -----//
142
143         // Test a full view at full zoom.
144         vector<LogicSegment::EdgePair> edges;
145         s.get_subsampled_edges(edges, 0, 255, 1, 0);
146         BOOST_REQUIRE_EQUAL(edges.size(), 4);
147
148         BOOST_CHECK_EQUAL(edges[0].first, 0);
149         BOOST_CHECK_EQUAL(edges[1].first, 8);
150         BOOST_CHECK_EQUAL(edges[2].first, 16);
151         BOOST_CHECK_EQUAL(edges[3].first, 256);
152
153         // Test a subset at high zoom
154         edges.clear();
155         s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
156         BOOST_REQUIRE_EQUAL(edges.size(), 4);
157
158         BOOST_CHECK_EQUAL(edges[0].first, 6);
159         BOOST_CHECK_EQUAL(edges[1].first, 8);
160         BOOST_CHECK_EQUAL(edges[2].first, 16);
161         BOOST_CHECK_EQUAL(edges[3].first, 18);
162 }
163
164 BOOST_AUTO_TEST_CASE(LargeData)
165 {
166         uint8_t prev_sample;
167         const unsigned int Length = 1000000;
168
169         sr_datafeed_logic logic;
170         logic.unitsize = 1;
171         logic.length = Length;
172         logic.data = new uint8_t[Length];
173         uint8_t *data = (uint8_t*)logic.data;
174
175         for (unsigned int i = 0; i < Length; i++)
176                 *data++ = (uint8_t)(i >> 8);
177
178         LogicSegment s(logic);
179         delete[] (uint8_t*)logic.data;
180
181         BOOST_CHECK(s.get_sample_count() == Length);
182
183         // Check mip map level 0
184         BOOST_CHECK_EQUAL(s.mip_map_[0].length, 62500);
185         BOOST_CHECK_EQUAL(s.mip_map_[0].data_length,
186                 LogicSegment::MipMapDataUnit);
187         BOOST_REQUIRE(s.mip_map_[0].data != nullptr);
188
189         prev_sample = 0;
190         for (unsigned int i = 0; i < s.mip_map_[0].length;)
191         {
192                 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
193
194                 const uint8_t sample = (uint8_t)((i*16) >> 8);
195                 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF,
196                         prev_sample ^ sample);
197                 prev_sample = sample;
198
199                 for (int j = 1; i < s.mip_map_[0].length && j < 16; j++)
200                 {
201                         BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
202                         BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0);
203                 }
204         }
205
206         // Check mip map level 1
207         BOOST_CHECK_EQUAL(s.mip_map_[1].length, 3906);
208         BOOST_CHECK_EQUAL(s.mip_map_[1].data_length,
209                 LogicSegment::MipMapDataUnit);
210         BOOST_REQUIRE(s.mip_map_[1].data != nullptr);
211
212         prev_sample = 0;
213         for (unsigned int i = 0; i < s.mip_map_[1].length; i++)
214         {
215                 BOOST_TEST_MESSAGE("Testing mip_map[1].data[" << i << "]");
216
217                 const uint8_t sample = i;
218                 const uint8_t expected = sample ^ prev_sample;
219                 prev_sample = i;
220
221                 BOOST_CHECK_EQUAL(s.get_subsample(1, i) & 0xFF, expected);
222         }
223
224         // Check mip map level 2
225         BOOST_CHECK_EQUAL(s.mip_map_[2].length, 244);
226         BOOST_CHECK_EQUAL(s.mip_map_[2].data_length,
227                 LogicSegment::MipMapDataUnit);
228         BOOST_REQUIRE(s.mip_map_[2].data != nullptr);
229
230         prev_sample = 0;
231         for (unsigned int i = 0; i < s.mip_map_[2].length; i++)
232         {
233                 BOOST_TEST_MESSAGE("Testing mip_map[2].data[" << i << "]");
234
235                 const uint8_t sample = i << 4;
236                 const uint8_t expected = (sample ^ prev_sample) | 0x0F;
237                 prev_sample = sample;
238
239                 BOOST_CHECK_EQUAL(s.get_subsample(2, i) & 0xFF, expected);
240         }
241
242         // Check mip map level 3
243         BOOST_CHECK_EQUAL(s.mip_map_[3].length, 15);
244         BOOST_CHECK_EQUAL(s.mip_map_[3].data_length,
245                 LogicSegment::MipMapDataUnit);
246         BOOST_REQUIRE(s.mip_map_[3].data != nullptr);
247
248         for (unsigned int i = 0; i < s.mip_map_[3].length; i++)
249                 BOOST_CHECK_EQUAL(*((uint8_t*)s.mip_map_[3].data + i),
250                         0xFF);
251
252         // Check the higher levels
253         for (unsigned int i = 4; i < LogicSegment::ScaleStepCount; i++)
254         {
255                 const LogicSegment::MipMapLevel &m = s.mip_map_[i];
256                 BOOST_CHECK_EQUAL(m.length, 0);
257                 BOOST_CHECK_EQUAL(m.data_length, 0);
258                 BOOST_CHECK(m.data == nullptr);
259         }
260
261         //----- Test LogicSegment::get_subsampled_edges -----//
262         // Check in normal case
263         vector<LogicSegment::EdgePair> edges;
264         s.get_subsampled_edges(edges, 0, Length-1, 1, 7);
265
266         BOOST_CHECK_EQUAL(edges.size(), 32);
267
268         for (unsigned int i = 0; i < edges.size() - 1; i++)
269         {
270                 BOOST_CHECK_EQUAL(edges[i].first, i * 32768);
271                 BOOST_CHECK_EQUAL(edges[i].second, i & 1);
272         }
273
274         BOOST_CHECK_EQUAL(edges[31].first, 1000000);
275
276         // Check in very low zoom case
277         edges.clear();
278         s.get_subsampled_edges(edges, 0, Length-1, 50e6f, 7);
279
280         BOOST_CHECK_EQUAL(edges.size(), 2);
281 }
282
283 BOOST_AUTO_TEST_CASE(Pulses)
284 {
285         const int Cycles = 3;
286         const int Period = 64;
287         const int Length = Cycles * Period;
288
289         vector<LogicSegment::EdgePair> edges;
290
291         //----- Create a LogicSegment -----//
292         sr_datafeed_logic logic;
293         logic.unitsize = 1;
294         logic.length = Length;
295         logic.data = (uint64_t*)new uint8_t[Length];
296         uint8_t *p = (uint8_t*)logic.data;
297
298         for (int i = 0; i < Cycles; i++) {
299                 *p++ = 0xFF;
300                 for (int j = 1; j < Period; j++)
301                         *p++ = 0x00;
302         }
303
304         LogicSegment s(logic);
305         delete[] (uint8_t*)logic.data;
306
307         //----- Check the mip-map -----//
308         // Check mip map level 0
309         BOOST_CHECK_EQUAL(s.mip_map_[0].length, 12);
310         BOOST_CHECK_EQUAL(s.mip_map_[0].data_length,
311                 LogicSegment::MipMapDataUnit);
312         BOOST_REQUIRE(s.mip_map_[0].data != nullptr);
313
314         for (unsigned int i = 0; i < s.mip_map_[0].length;) {
315                 BOOST_TEST_MESSAGE("Testing mip_map[0].data[" << i << "]");
316                 BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0xFF);
317
318                 for (int j = 1;
319                         i < s.mip_map_[0].length &&
320                         j < Period/LogicSegment::MipMapScaleFactor; j++) {
321                         BOOST_TEST_MESSAGE(
322                                 "Testing mip_map[0].data[" << i << "]");
323                         BOOST_CHECK_EQUAL(s.get_subsample(0, i++) & 0xFF, 0x00);
324                 }
325         }
326
327         // Check the higher levels are all inactive
328         for (unsigned int i = 1; i < LogicSegment::ScaleStepCount; i++) {
329                 const LogicSegment::MipMapLevel &m = s.mip_map_[i];
330                 BOOST_CHECK_EQUAL(m.length, 0);
331                 BOOST_CHECK_EQUAL(m.data_length, 0);
332                 BOOST_CHECK(m.data == nullptr);
333         }
334
335         //----- Test get_subsampled_edges at reduced scale -----//
336         s.get_subsampled_edges(edges, 0, Length-1, 16.0f, 2);
337         BOOST_REQUIRE_EQUAL(edges.size(), Cycles + 2);
338
339         BOOST_CHECK_EQUAL(0, false);
340         for (unsigned int i = 1; i < edges.size(); i++)
341                 BOOST_CHECK_EQUAL(edges[i].second, false);
342 }
343
344 BOOST_AUTO_TEST_CASE(LongPulses)
345 {
346         const int Cycles = 3;
347         const int Period = 64;
348         const int PulseWidth = 16;
349         const int Length = Cycles * Period;
350
351         int j;
352         vector<LogicSegment::EdgePair> edges;
353
354         //----- Create a LogicSegment -----//
355         sr_datafeed_logic logic;
356         logic.unitsize = 8;
357         logic.length = Length * 8;
358         logic.data = (uint64_t*)new uint64_t[Length];
359         uint64_t *p = (uint64_t*)logic.data;
360
361         for (int i = 0; i < Cycles; i++) {
362                 for (j = 0; j < PulseWidth; j++)
363                         *p++ = ~0;
364                 for (; j < Period; j++)
365                         *p++ = 0;
366         }
367
368         LogicSegment s(logic);
369         delete[] (uint64_t*)logic.data;
370
371         //----- Check the mip-map -----//
372         // Check mip map level 0
373         BOOST_CHECK_EQUAL(s.mip_map_[0].length, 12);
374         BOOST_CHECK_EQUAL(s.mip_map_[0].data_length,
375                 LogicSegment::MipMapDataUnit);
376         BOOST_REQUIRE(s.mip_map_[0].data != nullptr);
377
378         for (unsigned int i = 0; i < s.mip_map_[0].length;) {
379                 for (j = 0; i < s.mip_map_[0].length && j < 2; 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                 for (; i < s.mip_map_[0].length &&
386                         j < Period/LogicSegment::MipMapScaleFactor; j++) {
387                         BOOST_TEST_MESSAGE(
388                                 "Testing mip_map[0].data[" << i << "]");
389                         BOOST_CHECK_EQUAL(s.get_subsample(0, i++), 0);
390                 }
391         }
392
393         // Check the higher levels are all inactive
394         for (unsigned int i = 1; i < LogicSegment::ScaleStepCount; i++) {
395                 const LogicSegment::MipMapLevel &m = s.mip_map_[i];
396                 BOOST_CHECK_EQUAL(m.length, 0);
397                 BOOST_CHECK_EQUAL(m.data_length, 0);
398                 BOOST_CHECK(m.data == nullptr);
399         }
400
401         //----- Test get_subsampled_edges at a full scale -----//
402         s.get_subsampled_edges(edges, 0, Length-1, 16.0f, 2);
403         BOOST_REQUIRE_EQUAL(edges.size(), Cycles * 2 + 1);
404
405         for (int i = 0; i < Cycles; i++) {
406                 BOOST_CHECK_EQUAL(edges[i*2].first, i * Period);
407                 BOOST_CHECK_EQUAL(edges[i*2].second, true);
408                 BOOST_CHECK_EQUAL(edges[i*2+1].first, i * Period + PulseWidth);
409                 BOOST_CHECK_EQUAL(edges[i*2+1].second, false);
410         }
411
412         BOOST_CHECK_EQUAL(edges.back().first, Length);
413         BOOST_CHECK_EQUAL(edges.back().second, false);
414
415         //----- Test get_subsampled_edges at a simplified scale -----//
416         edges.clear();
417         s.get_subsampled_edges(edges, 0, Length-1, 17.0f, 2);
418
419         BOOST_CHECK_EQUAL(edges[0].first, 0);
420         BOOST_CHECK_EQUAL(edges[0].second, true);
421         BOOST_CHECK_EQUAL(edges[1].first, 16);
422         BOOST_CHECK_EQUAL(edges[1].second, false);
423         
424         for (int i = 1; i < Cycles; i++) {
425                 BOOST_CHECK_EQUAL(edges[i+1].first, i * Period);
426                 BOOST_CHECK_EQUAL(edges[i+1].second, false);
427         }
428
429         BOOST_CHECK_EQUAL(edges.back().first, Length);
430         BOOST_CHECK_EQUAL(edges.back().second, false);
431 }
432
433 BOOST_AUTO_TEST_CASE(LisaMUsbHid)
434 {
435         /* This test was created from the beginning of the USB_DM signal in
436          * sigrok-dumps-usb/lisa_m_usbhid/lisa_m_usbhid.sr
437          */
438
439         const int Edges[] = {
440                 7028, 7033, 7036, 7041, 7044, 7049, 7053, 7066, 7073, 7079,
441                 7086, 7095, 7103, 7108, 7111, 7116, 7119, 7124, 7136, 7141,
442                 7148, 7162, 7500
443         };
444         const int Length = Edges[countof(Edges) - 1];
445
446         bool state = false;
447         int lastEdgePos = 0;
448
449         //----- Create a LogicSegment -----//
450         sr_datafeed_logic logic;
451         logic.unitsize = 1;
452         logic.length = Length;
453         logic.data = new uint8_t[Length];
454         uint8_t *data = (uint8_t*)logic.data;
455
456         for (unsigned int i = 0; i < countof(Edges); i++) {
457                 const int edgePos = Edges[i];
458                 memset(&data[lastEdgePos], state ? 0x02 : 0,
459                         edgePos - lastEdgePos - 1);
460
461                 lastEdgePos = edgePos;
462                 state = !state;
463         }
464
465         LogicSegment s(logic);
466         delete[] (uint64_t*)logic.data;
467
468         vector<LogicSegment::EdgePair> edges;
469
470
471         /* The trailing edge of the pulse train is falling in the source data.
472          * Check this is always true at different scales
473          */
474
475         edges.clear();
476         s.get_subsampled_edges(edges, 0, Length-1, 33.333332f, 1);
477         BOOST_CHECK_EQUAL(edges[edges.size() - 2].second, false);
478 }
479
480 /*
481  * This test checks the rendering of wide data (more than 8 channels)
482  * Probe signals are either all-high, or all-low, but are interleaved such that
483  * they would toggle during every sample if treated like 8 channels.
484  * The packet contains a large number of samples, so the mipmap generation kicks
485  * in.
486  *
487  * The signals should not toggle (have exactly two edges: the start and end)
488  */
489 BOOST_AUTO_TEST_CASE(WideData)
490 {
491         const int Length = 512<<10;
492         uint16_t *data = new uint16_t[Length];
493
494         sr_datafeed_logic logic;
495         logic.unitsize = sizeof(data[0]);
496         logic.length = Length * sizeof(data[0]);
497         logic.data = data;
498
499         for (int i = 0; i < Length; i++)
500                 data[i] = 0x0FF0;
501
502         LogicSegment s(logic);
503
504         vector<LogicSegment::EdgePair> edges;
505
506         edges.clear();
507         s.get_subsampled_edges(edges, 0, Length-1, 1, 0);
508         BOOST_CHECK_EQUAL(edges.size(), 2);
509
510         edges.clear();
511         s.get_subsampled_edges(edges, 0, Length-1, 1, 8);
512         BOOST_CHECK_EQUAL(edges.size(), 2);
513
514         // Cleanup
515         delete [] data;
516 }
517
518 /*
519  * This test is a replica of sixteen.sr attached to Bug #33.
520  */
521 BOOST_AUTO_TEST_CASE(Sixteen)
522 {
523         const int Length = 8;
524         uint16_t data[Length];
525
526         sr_datafeed_logic logic;
527         logic.unitsize = sizeof(data[0]);
528         logic.length = Length * sizeof(data[0]);
529         logic.data = data;
530
531         for (int i = 0; i < Length; i++)
532                 data[i] = 0xFFFE;
533
534         LogicSegment s(logic);
535
536         vector<LogicSegment::EdgePair> edges;
537         s.get_subsampled_edges(edges, 0, 2, 0.0004, 1);
538
539         BOOST_CHECK_EQUAL(edges.size(), 2);
540 }
541
542 BOOST_AUTO_TEST_SUITE_END()
543 #endif