2 * This file is part of the sigrok 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
21 #include <boost/test/unit_test.hpp>
23 #include "../logicdatasnapshot.h"
27 void push_logic(LogicDataSnapshot &s, unsigned int length, uint8_t value)
29 sr_datafeed_logic logic;
31 logic.length = length;
32 logic.data = new uint8_t[length];
33 memset(logic.data, value, length * logic.unitsize);
34 s.append_payload(logic);
35 delete[] (uint8_t*)logic.data;
38 BOOST_AUTO_TEST_CASE(LogicDataSnapshotTest)
40 // Create an empty LogicDataSnapshot object
41 sr_datafeed_logic logic;
46 LogicDataSnapshot s(logic);
48 //----- Test LogicDataSnapshot::push_logic -----//
50 BOOST_CHECK(s.get_sample_count() == 0);
51 for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
53 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
54 BOOST_CHECK_EQUAL(m.length, 0);
55 BOOST_CHECK_EQUAL(m.data_length, 0);
56 BOOST_CHECK(m.data == NULL);
59 // Push 8 samples of all zeros
62 BOOST_CHECK(s.get_sample_count() == 8);
64 // There should not be enough samples to have a single mip map sample
65 for(int i = 0; i < LogicDataSnapshot::ScaleStepCount; i++)
67 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
68 BOOST_CHECK_EQUAL(m.length, 0);
69 BOOST_CHECK_EQUAL(m.data_length, 0);
70 BOOST_CHECK(m.data == NULL);
73 // Push 8 samples of 0x11s to bring the total up to 16
74 push_logic(s, 8, 0x11);
76 // There should now be enough data for exactly one sample
77 // in mip map level 0, and that sample should be 0
78 const LogicDataSnapshot::MipMapLevel &m0 = s._mip_map[0];
79 BOOST_CHECK_EQUAL(m0.length, 1);
80 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
81 BOOST_REQUIRE(m0.data != NULL);
82 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[0], 0x11);
84 // The higher levels should still be empty
85 for(int i = 1; i < LogicDataSnapshot::ScaleStepCount; i++)
87 const LogicDataSnapshot::MipMapLevel &m = s._mip_map[i];
88 BOOST_CHECK_EQUAL(m.length, 0);
89 BOOST_CHECK_EQUAL(m.data_length, 0);
90 BOOST_CHECK(m.data == NULL);
93 // Push 240 samples of all zeros to bring the total up to 256
94 push_logic(s, 240, 0);
96 BOOST_CHECK_EQUAL(m0.length, 16);
97 BOOST_CHECK_EQUAL(m0.data_length, LogicDataSnapshot::MipMapDataUnit);
99 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[1], 0x11);
100 for(int i = 2; i < m0.length; i++)
101 BOOST_CHECK_EQUAL(((uint8_t*)m0.data)[i], 0);
103 const LogicDataSnapshot::MipMapLevel &m1 = s._mip_map[1];
104 BOOST_CHECK_EQUAL(m1.length, 1);
105 BOOST_CHECK_EQUAL(m1.data_length, LogicDataSnapshot::MipMapDataUnit);
106 BOOST_REQUIRE(m1.data != NULL);
107 BOOST_CHECK_EQUAL(((uint8_t*)m1.data)[0], 0x11);
109 //----- Test LogicDataSnapshot::get_subsampled_edges -----//
111 // Test a full view at full zoom.
112 vector<LogicDataSnapshot::EdgePair> edges;
113 s.get_subsampled_edges(edges, 0, 255, 1, 0);
114 BOOST_REQUIRE_EQUAL(edges.size(), 4);
116 BOOST_CHECK_EQUAL(edges[0].first, 0);
117 BOOST_CHECK_EQUAL(edges[1].first, 8);
118 BOOST_CHECK_EQUAL(edges[2].first, 16);
119 BOOST_CHECK_EQUAL(edges[3].first, 255);
121 // Test a subset at high zoom
123 s.get_subsampled_edges(edges, 6, 17, 0.05f, 0);
124 BOOST_REQUIRE_EQUAL(edges.size(), 4);
126 BOOST_CHECK_EQUAL(edges[0].first, 6);
127 BOOST_CHECK_EQUAL(edges[1].first, 8);
128 BOOST_CHECK_EQUAL(edges[2].first, 16);
129 BOOST_CHECK_EQUAL(edges[3].first, 17);