2 * This file is part of the PulseView project.
4 * Copyright (C) 2016 Soeren Apel <soeren@apelpie.net>
5 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include <boost/test/unit_test.hpp>
27 #include <pv/data/segment.hpp>
29 using pv::data::Segment;
31 BOOST_AUTO_TEST_SUITE(SegmentTest)
34 /* --- For debugging only
35 BOOST_AUTO_TEST_CASE(SmallSize8Single)
37 Segment s(0, 1, sizeof(uint8_t));
38 uint32_t num_samples = 10;
40 //----- Chunk size << pv::data::Segment::MaxChunkSize @ 8bit, added in 1 call ----//
41 uint8_t* const data = new uint8_t[num_samples];
42 for (uint32_t i = 0; i < num_samples; i++)
45 s.append_samples(data, num_samples);
48 BOOST_CHECK(s.get_sample_count() == num_samples);
50 uint8_t *sample_data = new uint8_t[1];
51 for (uint32_t i = 0; i < num_samples; i++) {
52 s.get_raw_samples(i, 1, sample_data);
53 BOOST_CHECK_EQUAL(*sample_data, i);
58 /* --- For debugging only
59 BOOST_AUTO_TEST_CASE(MediumSize8Single)
61 Segment s(0, 1, sizeof(uint8_t));
62 uint32_t num_samples = pv::data::Segment::MaxChunkSize;
64 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 8bit, added in 1 call ----//
65 uint8_t* const data = new uint8_t[num_samples];
66 for (uint32_t i = 0; i < num_samples; i++)
69 s.append_samples(data, num_samples);
72 BOOST_CHECK(s.get_sample_count() == num_samples);
74 uint8_t *sample_data = new uint8_t[1];
75 for (uint32_t i = 0; i < num_samples; i++) {
76 s.get_raw_samples(i, 1, sample_data);
77 BOOST_CHECK_EQUAL(*sample_data, i % 256);
82 /* --- For debugging only
83 BOOST_AUTO_TEST_CASE(MaxSize8Single)
85 Segment s(0, 1, sizeof(uint8_t));
87 // We want to see proper behavior across chunk boundaries
88 uint32_t num_samples = 2*pv::data::Segment::MaxChunkSize;
90 //----- Chunk size >> pv::data::Segment::MaxChunkSize @ 8bit, added in 1 call ----//
91 uint8_t* const data = new uint8_t[num_samples];
92 for (uint32_t i = 0; i < num_samples; i++)
95 s.append_samples(data, num_samples);
98 BOOST_CHECK(s.get_sample_count() == num_samples);
100 uint8_t *sample_data = new uint8_t[1];
101 for (uint32_t i = 0; i < num_samples; i++) {
102 s.get_raw_samples(i, 1, sample_data);
103 BOOST_CHECK_EQUAL(*sample_data, i % 256);
105 delete[] sample_data;
108 /* --- For debugging only
109 BOOST_AUTO_TEST_CASE(MediumSize24Single)
113 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
114 uint32_t num_samples = pv::data::Segment::MaxChunkSize / 3;
116 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 24bit, added in 1 call ----//
117 uint8_t* const data = new uint8_t[num_samples * 3];
118 for (uint32_t i = 0; i < num_samples * 3; i++)
121 s.append_samples(data, num_samples);
124 BOOST_CHECK(s.get_sample_count() == num_samples);
126 uint8_t *sample_data = new uint8_t[3];
127 for (uint32_t i = 0; i < num_samples; i++) {
128 s.get_raw_samples(i, 1, sample_data);
129 BOOST_CHECK_EQUAL(*((uint8_t*)sample_data), 3*i % 256);
130 BOOST_CHECK_EQUAL(*((uint8_t*)(sample_data+1)), (3*i+1) % 256);
131 BOOST_CHECK_EQUAL(*((uint8_t*)(sample_data+2)), (3*i+2) % 256);
133 delete[] sample_data;
136 /* --- For debugging only
137 BOOST_AUTO_TEST_CASE(MediumSize32Single)
139 Segment s(0, 1, sizeof(uint32_t));
141 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
142 uint32_t num_samples = pv::data::Segment::MaxChunkSize / sizeof(uint32_t);
144 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in 1 call ----//
145 uint32_t* const data = new uint32_t[num_samples];
146 for (uint32_t i = 0; i < num_samples; i++)
149 s.append_samples(data, num_samples);
152 BOOST_CHECK(s.get_sample_count() == num_samples);
154 uint8_t *sample_data = new uint8_t[sizeof(uint32_t)];
155 for (uint32_t i = 0; i < num_samples; i++) {
156 s.get_raw_samples(i, 1, sample_data);
157 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
159 delete[] sample_data;
162 /* --- For debugging only
163 BOOST_AUTO_TEST_CASE(MaxSize32Single)
165 Segment s(0, 1, sizeof(uint32_t));
167 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
168 // Also, we want to see proper behavior across chunk boundaries
169 uint32_t num_samples = 2*(pv::data::Segment::MaxChunkSize / sizeof(uint32_t));
171 //----- Chunk size >> pv::data::Segment::MaxChunkSize @ 32bit, added in 1 call ----//
172 uint32_t* const data = new uint32_t[num_samples];
173 for (uint32_t i = 0; i < num_samples; i++)
176 s.append_samples(data, num_samples);
179 BOOST_CHECK(s.get_sample_count() == num_samples);
181 uint8_t *sample_data = new uint8_t[sizeof(uint32_t)];
182 for (uint32_t i = 0; i < num_samples; i++) {
183 s.get_raw_samples(i, 1, sample_data);
184 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
186 delete[] sample_data;
189 /* --- For debugging only
190 BOOST_AUTO_TEST_CASE(MediumSize32Multi)
192 Segment s(0, 1, sizeof(uint32_t));
194 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
195 uint32_t num_samples = pv::data::Segment::MaxChunkSize / sizeof(uint32_t);
197 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in num_samples calls ----//
199 for (uint32_t i = 0; i < num_samples; i++) {
201 s.append_samples(&data, 1);
204 BOOST_CHECK(s.get_sample_count() == num_samples);
206 uint8_t *sample_data = new uint8_t[sizeof(uint32_t)];
207 for (uint32_t i = 0; i < num_samples; i++) {
208 s.get_raw_samples(i, 1, sample_data);
209 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
211 delete[] sample_data;
214 BOOST_AUTO_TEST_CASE(MaxSize32Multi)
216 Segment s(0, 1, sizeof(uint32_t));
218 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
219 uint32_t num_samples = 2*(pv::data::Segment::MaxChunkSize / sizeof(uint32_t));
221 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in num_samples calls ----//
223 for (uint32_t i = 0; i < num_samples; i++) {
225 s.append_samples(&data, 1);
228 BOOST_CHECK(s.get_sample_count() == num_samples);
230 uint8_t *sample_data = new uint8_t[sizeof(uint32_t) * num_samples];
231 for (uint32_t i = 0; i < num_samples; i++) {
232 s.get_raw_samples(i, 1, sample_data);
233 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
236 s.get_raw_samples(0, num_samples, sample_data);
237 for (uint32_t i = 0; i < num_samples; i++) {
238 BOOST_CHECK_EQUAL(*((uint32_t*)(sample_data + i * sizeof(uint32_t))), i);
240 delete[] sample_data;
243 BOOST_AUTO_TEST_CASE(MaxSize32MultiAtOnce)
245 Segment s(0, 1, sizeof(uint32_t));
247 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
248 uint32_t num_samples = 3*(pv::data::Segment::MaxChunkSize / sizeof(uint32_t));
250 //----- Add all samples, requiring multiple chunks, in one call ----//
251 uint32_t *data = new uint32_t[num_samples];
252 for (uint32_t i = 0; i < num_samples; i++)
255 s.append_samples(data, num_samples);
258 BOOST_CHECK(s.get_sample_count() == num_samples);
260 uint8_t *sample_data = new uint8_t[sizeof(uint32_t) * num_samples];
261 for (uint32_t i = 0; i < num_samples; i++) {
262 s.get_raw_samples(i, 1, sample_data);
263 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
266 s.get_raw_samples(0, num_samples, sample_data);
267 for (uint32_t i = 0; i < num_samples; i++) {
268 BOOST_CHECK_EQUAL(*((uint32_t*)(sample_data + i * sizeof(uint32_t))), i);
270 delete[] sample_data;
273 BOOST_AUTO_TEST_CASE(MaxSize32MultiIterated)
275 Segment s(0, 1, sizeof(uint32_t));
277 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
278 uint32_t num_samples = 2*(pv::data::Segment::MaxChunkSize / sizeof(uint32_t));
280 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in num_samples calls ----//
282 for (uint32_t i = 0; i < num_samples; i++) {
284 s.append_samples(&data, 1);
287 BOOST_CHECK(s.get_sample_count() == num_samples);
289 pv::data::SegmentRawDataIterator* it = s.begin_raw_sample_iteration(0);
291 for (uint32_t i = 0; i < num_samples; i++) {
292 uint8_t* sample_data = it->value;
293 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
294 s.continue_raw_sample_iteration(it, 1);
297 s.end_raw_sample_iteration(it);
300 BOOST_AUTO_TEST_SUITE_END()