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(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 for (uint32_t i = 0; i < num_samples; i++) {
51 uint8_t* sample_data = s.get_raw_samples(i, 1);
52 BOOST_CHECK_EQUAL(*sample_data, i);
57 /* --- For debugging only
58 BOOST_AUTO_TEST_CASE(MediumSize8Single)
60 Segment s(1, sizeof(uint8_t));
61 uint32_t num_samples = pv::data::Segment::MaxChunkSize;
63 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 8bit, added in 1 call ----//
64 uint8_t* const data = new uint8_t[num_samples];
65 for (uint32_t i = 0; i < num_samples; i++)
68 s.append_samples(data, num_samples);
71 BOOST_CHECK(s.get_sample_count() == num_samples);
73 for (uint32_t i = 0; i < num_samples; i++) {
74 uint8_t* sample_data = s.get_raw_samples(i, 1);
75 BOOST_CHECK_EQUAL(*sample_data, i % 256);
80 /* --- For debugging only
81 BOOST_AUTO_TEST_CASE(MaxSize8Single)
83 Segment s(1, sizeof(uint8_t));
85 // We want to see proper behavior across chunk boundaries
86 uint32_t num_samples = 2*pv::data::Segment::MaxChunkSize;
88 //----- Chunk size >> pv::data::Segment::MaxChunkSize @ 8bit, added in 1 call ----//
89 uint8_t* const data = new uint8_t[num_samples];
90 for (uint32_t i = 0; i < num_samples; i++)
93 s.append_samples(data, num_samples);
96 BOOST_CHECK(s.get_sample_count() == num_samples);
98 for (uint32_t i = 0; i < num_samples; i++) {
99 uint8_t* sample_data = s.get_raw_samples(i, 1);
100 BOOST_CHECK_EQUAL(*sample_data, i % 256);
101 delete[] sample_data;
105 /* --- For debugging only
106 BOOST_AUTO_TEST_CASE(MediumSize24Single)
110 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
111 uint32_t num_samples = pv::data::Segment::MaxChunkSize / 3;
113 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 24bit, added in 1 call ----//
114 uint8_t* const data = new uint8_t[num_samples * 3];
115 for (uint32_t i = 0; i < num_samples * 3; i++)
118 s.append_samples(data, num_samples);
121 BOOST_CHECK(s.get_sample_count() == num_samples);
123 for (uint32_t i = 0; i < num_samples; i++) {
124 uint8_t* sample_data = s.get_raw_samples(i, 1);
125 BOOST_CHECK_EQUAL(*((uint8_t*)sample_data), 3*i % 256);
126 BOOST_CHECK_EQUAL(*((uint8_t*)(sample_data+1)), (3*i+1) % 256);
127 BOOST_CHECK_EQUAL(*((uint8_t*)(sample_data+2)), (3*i+2) % 256);
128 delete[] sample_data;
132 /* --- For debugging only
133 BOOST_AUTO_TEST_CASE(MediumSize32Single)
135 Segment s(1, sizeof(uint32_t));
137 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
138 uint32_t num_samples = pv::data::Segment::MaxChunkSize / sizeof(uint32_t);
140 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in 1 call ----//
141 uint32_t* const data = new uint32_t[num_samples];
142 for (uint32_t i = 0; i < num_samples; i++)
145 s.append_samples(data, num_samples);
148 BOOST_CHECK(s.get_sample_count() == num_samples);
150 for (uint32_t i = 0; i < num_samples; i++) {
151 uint8_t* sample_data = s.get_raw_samples(i, 1);
152 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
153 delete[] sample_data;
157 /* --- For debugging only
158 BOOST_AUTO_TEST_CASE(MaxSize32Single)
160 Segment s(1, sizeof(uint32_t));
162 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
163 // Also, we want to see proper behavior across chunk boundaries
164 uint32_t num_samples = 2*(pv::data::Segment::MaxChunkSize / sizeof(uint32_t));
166 //----- Chunk size >> pv::data::Segment::MaxChunkSize @ 32bit, added in 1 call ----//
167 uint32_t* const data = new uint32_t[num_samples];
168 for (uint32_t i = 0; i < num_samples; i++)
171 s.append_samples(data, num_samples);
174 BOOST_CHECK(s.get_sample_count() == num_samples);
176 for (uint32_t i = 0; i < num_samples; i++) {
177 uint8_t* sample_data = s.get_raw_samples(i, 1);
178 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
179 delete[] sample_data;
183 /* --- For debugging only
184 BOOST_AUTO_TEST_CASE(MediumSize32Multi)
186 Segment s(1, sizeof(uint32_t));
188 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
189 uint32_t num_samples = pv::data::Segment::MaxChunkSize / sizeof(uint32_t);
191 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in num_samples calls ----//
193 for (uint32_t i = 0; i < num_samples; i++) {
195 s.append_samples(&data, 1);
198 BOOST_CHECK(s.get_sample_count() == num_samples);
200 for (uint32_t i = 0; i < num_samples; i++) {
201 uint8_t* sample_data = s.get_raw_samples(i, 1);
202 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
203 delete[] sample_data;
207 BOOST_AUTO_TEST_CASE(MaxSize32Multi)
209 Segment s(1, sizeof(uint32_t));
211 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
212 uint32_t num_samples = 2*(pv::data::Segment::MaxChunkSize / sizeof(uint32_t));
214 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in num_samples calls ----//
216 for (uint32_t i = 0; i < num_samples; i++) {
218 s.append_samples(&data, 1);
221 BOOST_CHECK(s.get_sample_count() == num_samples);
223 for (uint32_t i = 0; i < num_samples; i++) {
224 uint8_t* sample_data = s.get_raw_samples(i, 1);
225 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
226 delete[] sample_data;
229 uint8_t* sample_data = s.get_raw_samples(0, num_samples);
230 for (uint32_t i = 0; i < num_samples; i++) {
231 BOOST_CHECK_EQUAL(*((uint32_t*)(sample_data + i * sizeof(uint32_t))), i);
233 delete[] sample_data;
236 BOOST_AUTO_TEST_CASE(MaxSize32MultiIterated)
238 Segment s(1, sizeof(uint32_t));
240 // Chunk size is num*unit_size, so with pv::data::Segment::MaxChunkSize/unit_size, we reach the maximum size
241 uint32_t num_samples = 2*(pv::data::Segment::MaxChunkSize / sizeof(uint32_t));
243 //----- Chunk size == pv::data::Segment::MaxChunkSize @ 32bit, added in num_samples calls ----//
245 for (uint32_t i = 0; i < num_samples; i++) {
247 s.append_samples(&data, 1);
250 BOOST_CHECK(s.get_sample_count() == num_samples);
252 pv::data::SegmentRawDataIterator* it = s.begin_raw_sample_iteration(0);
254 for (uint32_t i = 0; i < num_samples; i++) {
255 uint8_t* sample_data = it->value;
256 BOOST_CHECK_EQUAL(*((uint32_t*)sample_data), i);
257 s.continue_raw_sample_iteration(it, 1);
260 s.end_raw_sample_iteration(it);
263 BOOST_AUTO_TEST_SUITE_END()