DUNE_NanoSlice_t.cc
Go to the documentation of this file.
2 #include <vector>
3 #include <stdint.h>
4 
5 
6 // JCF, 9/29/14
7 
8 // A new wrinkle with gcc 4.9.1 is that it gives an error if a signed
9 // and unsigned are compared as happens in Boosts's test_tools.hpp
10 // file; this error is therefore explicitly disabled for the duration
11 // of the file
12 
13 
14 #pragma GCC diagnostic push
15 #pragma GCC diagnostic ignored "-Wsign-compare"
16 
17 
18 #define BOOST_TEST_MODULE(NanoSlice_t)
20 
21 
22 BOOST_AUTO_TEST_SUITE(NanoSlice_test)
23 
24 BOOST_AUTO_TEST_CASE(BaselineTest)
25 {
26  const uint16_t CHANNEL_NUMBER = 123;
27  const uint32_t BUFFER_SIZE = 4096;
28  const uint16_t SAMPLE1 = 0x1234;
29  const uint16_t SAMPLE2 = 0xc3c3;
30  const uint16_t SAMPLE3 = 0xbeef;
31  const uint16_t SAMPLE4 = 0xfe87;
32  const uint16_t SAMPLE5 = 0x5a5a;
33  std::vector<uint8_t> work_buffer(BUFFER_SIZE);
34  uint16_t value;
35 
36  // *** Use a NanoSliceWriter to build up a NanoSlice, checking
37  // *** that everything looks good as we go.
38 
39  dune::NanoSliceWriter ns_writer(&work_buffer[0], BUFFER_SIZE, 0);
40  BOOST_REQUIRE_EQUAL(ns_writer.size(), sizeof(dune::NanoSlice::Header));
41  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 0);
42  BOOST_REQUIRE_EQUAL(ns_writer.channelNumber(), 0);
43  BOOST_REQUIRE(! ns_writer.sampleValue(0, value));
44  BOOST_REQUIRE(! ns_writer.sampleValue(999, value));
45 
46  ns_writer.setChannelNumber(CHANNEL_NUMBER);
47  BOOST_REQUIRE_EQUAL(ns_writer.channelNumber(), CHANNEL_NUMBER);
48 
49  BOOST_REQUIRE(ns_writer.addSample(SAMPLE1));
50  BOOST_REQUIRE_EQUAL(ns_writer.size(),
51  sizeof(dune::NanoSlice::Header) + sizeof(uint16_t));
52  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 1);
53  BOOST_REQUIRE(ns_writer.sampleValue(0, value));
54  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
55  BOOST_REQUIRE(! ns_writer.sampleValue(1, value));
56  BOOST_REQUIRE(! ns_writer.sampleValue(999, value));
57 
58  BOOST_REQUIRE(ns_writer.addSample(SAMPLE2));
59  BOOST_REQUIRE_EQUAL(ns_writer.size(),
60  sizeof(dune::NanoSlice::Header) + 2*sizeof(uint16_t));
61  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 2);
62  BOOST_REQUIRE(ns_writer.sampleValue(0, value));
63  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
64  BOOST_REQUIRE(ns_writer.sampleValue(1, value));
65  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
66  BOOST_REQUIRE(! ns_writer.sampleValue(2, value));
67  BOOST_REQUIRE(! ns_writer.sampleValue(101, value));
68 
69  BOOST_REQUIRE(ns_writer.addSample(SAMPLE3));
70  BOOST_REQUIRE_EQUAL(ns_writer.size(),
71  sizeof(dune::NanoSlice::Header) + 3*sizeof(uint16_t));
72  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 3);
73  BOOST_REQUIRE(ns_writer.sampleValue(0, value));
74  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
75  BOOST_REQUIRE(ns_writer.sampleValue(1, value));
76  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
77  BOOST_REQUIRE(ns_writer.sampleValue(2, value));
78  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
79  BOOST_REQUIRE(! ns_writer.sampleValue(3, value));
80  BOOST_REQUIRE(! ns_writer.sampleValue(101, value));
81 
82  BOOST_REQUIRE(ns_writer.addSample(SAMPLE4));
83  BOOST_REQUIRE_EQUAL(ns_writer.size(),
84  sizeof(dune::NanoSlice::Header) + 4*sizeof(uint16_t));
85  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 4);
86  BOOST_REQUIRE(ns_writer.sampleValue(0, value));
87  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
88  BOOST_REQUIRE(ns_writer.sampleValue(1, value));
89  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
90  BOOST_REQUIRE(ns_writer.sampleValue(2, value));
91  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
92  BOOST_REQUIRE(ns_writer.sampleValue(3, value));
93  BOOST_REQUIRE_EQUAL(value, SAMPLE4);
94  BOOST_REQUIRE(! ns_writer.sampleValue(4, value));
95  BOOST_REQUIRE(! ns_writer.sampleValue(101, value));
96 
97  // *** Finish off the creation of the NanoSlice and verify that we can't
98  // *** add any more samples after it is finalized
99 
100  int32_t size_diff = ns_writer.finalize();
101  BOOST_REQUIRE_EQUAL(size_diff, BUFFER_SIZE -
102  sizeof(dune::NanoSlice::Header) - 4*sizeof(uint16_t));
103  BOOST_REQUIRE(! ns_writer.addSample(SAMPLE5));
104 
105  // *** Now let's construct an instance of a read-only NanoSlice from
106  // *** the work buffer and verify that everything still looks good
107 
108  dune::NanoSlice nslice(&work_buffer[0]);
109  BOOST_REQUIRE_EQUAL(nslice.size(),
110  sizeof(dune::NanoSlice::Header) + 4*sizeof(uint16_t));
111  BOOST_REQUIRE_EQUAL(nslice.channelNumber(), CHANNEL_NUMBER);
112  BOOST_REQUIRE_EQUAL(nslice.sampleCount(), 4);
113  BOOST_REQUIRE(nslice.sampleValue(0, value));
114  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
115  BOOST_REQUIRE(nslice.sampleValue(1, value));
116  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
117  BOOST_REQUIRE(nslice.sampleValue(2, value));
118  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
119  BOOST_REQUIRE(nslice.sampleValue(3, value));
120  BOOST_REQUIRE_EQUAL(value, SAMPLE4);
121  BOOST_REQUIRE(! nslice.sampleValue(4, value));
122  BOOST_REQUIRE(! nslice.sampleValue(567, value));
123 }
124 
125 BOOST_AUTO_TEST_CASE(TinyBufferTest)
126 {
127  const uint16_t CHANNEL_NUMBER = 456;
128  const uint32_t BUFFER_SIZE = 4096;
129  const uint32_t SMALL_SIZE1 = 1;
130  const uint16_t SAMPLE1 = 0x1234;
131  std::vector<uint8_t> work_buffer(BUFFER_SIZE);
132  uint16_t value;
133 
134  // *** Test a buffer that is too small for even the header
135 
136  dune::NanoSliceWriter ns_writer(&work_buffer[0], SMALL_SIZE1, CHANNEL_NUMBER);
137  BOOST_REQUIRE_EQUAL(ns_writer.size(), sizeof(dune::NanoSlice::Header));
138  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 0);
139  BOOST_REQUIRE_EQUAL(ns_writer.channelNumber(), CHANNEL_NUMBER);
140  BOOST_REQUIRE(! ns_writer.sampleValue(0, value));
141  BOOST_REQUIRE(! ns_writer.sampleValue(999, value));
142 
143  BOOST_REQUIRE(! ns_writer.addSample(SAMPLE1));
144  BOOST_REQUIRE_EQUAL(ns_writer.size(), sizeof(dune::NanoSlice::Header));
145  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 0);
146  BOOST_REQUIRE_EQUAL(ns_writer.channelNumber(), CHANNEL_NUMBER);
147  BOOST_REQUIRE(! ns_writer.sampleValue(0, value));
148  BOOST_REQUIRE(! ns_writer.sampleValue(1, value));
149 
150  int32_t size_diff = ns_writer.finalize();
151  BOOST_REQUIRE_EQUAL(size_diff, 0);
152 }
153 
154 BOOST_AUTO_TEST_CASE(SmallBufferTest)
155 {
156  const uint16_t CHANNEL_NUMBER = 456;
157  const uint32_t BUFFER_SIZE = 4096;
158  const uint32_t SMALL_SIZE2 = sizeof(dune::NanoSlice::Header) + sizeof(uint16_t) + 1;
159  const uint16_t SAMPLE1 = 0x1234;
160  const uint16_t SAMPLE2 = 0xc3c3;
161  std::vector<uint8_t> work_buffer(BUFFER_SIZE);
162  uint16_t value;
163 
164  // *** Test a buffer that is too small for two values
165 
166  dune::NanoSliceWriter ns_writer(&work_buffer[0], SMALL_SIZE2, CHANNEL_NUMBER);
167  BOOST_REQUIRE_EQUAL(ns_writer.size(), sizeof(dune::NanoSlice::Header));
168  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 0);
169  BOOST_REQUIRE_EQUAL(ns_writer.channelNumber(), CHANNEL_NUMBER);
170  BOOST_REQUIRE(! ns_writer.sampleValue(0, value));
171  BOOST_REQUIRE(! ns_writer.sampleValue(999, value));
172 
173  BOOST_REQUIRE(ns_writer.addSample(SAMPLE1));
174  BOOST_REQUIRE_EQUAL(ns_writer.size(),
175  sizeof(dune::NanoSlice::Header) + sizeof(uint16_t));
176  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 1);
177  BOOST_REQUIRE(ns_writer.sampleValue(0, value));
178  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
179  BOOST_REQUIRE(! ns_writer.sampleValue(1, value));
180  BOOST_REQUIRE(! ns_writer.sampleValue(999, value));
181 
182  BOOST_REQUIRE(! ns_writer.addSample(SAMPLE2));
183  BOOST_REQUIRE_EQUAL(ns_writer.size(),
184  sizeof(dune::NanoSlice::Header) + sizeof(uint16_t));
185  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 1);
186  BOOST_REQUIRE(ns_writer.sampleValue(0, value));
187  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
188  BOOST_REQUIRE(! ns_writer.sampleValue(1, value));
189  BOOST_REQUIRE(! ns_writer.sampleValue(2, value));
190 
191  int32_t size_diff = ns_writer.finalize();
192  BOOST_REQUIRE_EQUAL(size_diff, 1);
193 }
194 
196 {
197  const uint16_t CHANNEL_NUMBER = 123;
198  const uint32_t BUFFER_SIZE = 4096;
199  const uint16_t SAMPLE1 = 0x1234;
200  const uint16_t SAMPLE2 = 0xc3c3;
201  const uint16_t SAMPLE3 = 0xbeef;
202  const uint16_t SAMPLE4 = 0xfe87;
203  const uint16_t SAMPLE5 = 0x5a5a;
204  std::vector<uint8_t> work_buffer(BUFFER_SIZE);
205  uint16_t value;
206 
207  dune::NanoSliceWriter ns_writer(&work_buffer[0], BUFFER_SIZE, CHANNEL_NUMBER);
208  BOOST_REQUIRE_EQUAL(ns_writer.size(), sizeof(dune::NanoSlice::Header));
209  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 0);
210  BOOST_REQUIRE_EQUAL(ns_writer.channelNumber(), CHANNEL_NUMBER);
211  BOOST_REQUIRE(ns_writer.addSample(SAMPLE1));
212  BOOST_REQUIRE(ns_writer.addSample(SAMPLE2));
213  BOOST_REQUIRE(ns_writer.addSample(SAMPLE3));
214  BOOST_REQUIRE(ns_writer.addSample(SAMPLE4));
215  BOOST_REQUIRE(ns_writer.addSample(SAMPLE5));
216 
217  int32_t size_diff = ns_writer.finalize();
218  BOOST_REQUIRE_EQUAL(size_diff, BUFFER_SIZE -
219  sizeof(dune::NanoSlice::Header) - 5*sizeof(uint16_t));
220 
221  dune::NanoSliceWriter ns_writer_copy = ns_writer;
222  BOOST_REQUIRE_EQUAL(ns_writer_copy.sampleCount(), 5);
223  BOOST_REQUIRE(ns_writer_copy.sampleValue(0, value));
224  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
225  BOOST_REQUIRE(ns_writer_copy.sampleValue(1, value));
226  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
227  BOOST_REQUIRE(ns_writer_copy.sampleValue(2, value));
228  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
229  BOOST_REQUIRE(ns_writer_copy.sampleValue(3, value));
230  BOOST_REQUIRE_EQUAL(value, SAMPLE4);
231  BOOST_REQUIRE(ns_writer_copy.sampleValue(4, value));
232  BOOST_REQUIRE_EQUAL(value, SAMPLE5);
233  BOOST_REQUIRE(! ns_writer.addSample(SAMPLE5));
234 
235  dune::NanoSlice nslice(&work_buffer[0]);
236  dune::NanoSlice nslice_copy = nslice;
237  BOOST_REQUIRE_EQUAL(nslice_copy.size(),
238  sizeof(dune::NanoSlice::Header) + 5*sizeof(uint16_t));
239  BOOST_REQUIRE_EQUAL(nslice_copy.channelNumber(), CHANNEL_NUMBER);
240  BOOST_REQUIRE_EQUAL(nslice_copy.sampleCount(), 5);
241  BOOST_REQUIRE(nslice_copy.sampleValue(0, value));
242  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
243  BOOST_REQUIRE(nslice_copy.sampleValue(1, value));
244  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
245  BOOST_REQUIRE(nslice_copy.sampleValue(2, value));
246  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
247  BOOST_REQUIRE(nslice_copy.sampleValue(3, value));
248  BOOST_REQUIRE_EQUAL(value, SAMPLE4);
249  BOOST_REQUIRE(nslice_copy.sampleValue(4, value));
250  BOOST_REQUIRE_EQUAL(value, SAMPLE5);
251  BOOST_REQUIRE(! nslice_copy.sampleValue(6, value));
252  BOOST_REQUIRE(! nslice_copy.sampleValue(567, value));
253 }
254 
255 BOOST_AUTO_TEST_CASE(BufferReuseTest)
256 {
257  const uint16_t CHANNEL_NUMBER = 123;
258  const uint32_t BUFFER_SIZE = 4096;
259  const uint16_t SAMPLE1 = 0x1234;
260  const uint16_t SAMPLE2 = 0xc3c3;
261  const uint16_t SAMPLE3 = 0xbeef;
262  std::vector<uint8_t> work_buffer(BUFFER_SIZE);
263  uint16_t value;
264 
265  dune::NanoSliceWriter ns_writer(&work_buffer[0], BUFFER_SIZE, CHANNEL_NUMBER);
266  BOOST_REQUIRE_EQUAL(ns_writer.size(), sizeof(dune::NanoSlice::Header));
267  BOOST_REQUIRE_EQUAL(ns_writer.sampleCount(), 0);
268  BOOST_REQUIRE_EQUAL(ns_writer.channelNumber(), CHANNEL_NUMBER);
269  BOOST_REQUIRE(ns_writer.addSample(SAMPLE1));
270  BOOST_REQUIRE(ns_writer.addSample(SAMPLE2));
271  BOOST_REQUIRE(ns_writer.addSample(SAMPLE3));
272 
273  int32_t size_diff = ns_writer.finalize();
274  BOOST_REQUIRE_EQUAL(size_diff, BUFFER_SIZE -
275  sizeof(dune::NanoSlice::Header) - 3*sizeof(uint16_t));
276 
277  // *** If we reuse a buffer, then all history of the earlier NanoSlice
278  // *** should be gone
279 
280  dune::NanoSliceWriter ns_writer2(&work_buffer[0], BUFFER_SIZE, 0);
281  BOOST_REQUIRE_EQUAL(ns_writer2.size(), sizeof(dune::NanoSlice::Header));
282  BOOST_REQUIRE_EQUAL(ns_writer2.sampleCount(), 0);
283  BOOST_REQUIRE_EQUAL(ns_writer2.channelNumber(), 0);
284  BOOST_REQUIRE(ns_writer2.addSample(SAMPLE3));
285  BOOST_REQUIRE_EQUAL(ns_writer2.sampleCount(), 1);
286  BOOST_REQUIRE(ns_writer2.sampleValue(0, value));
287  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
288  BOOST_REQUIRE(! ns_writer2.sampleValue(1, value));
289  BOOST_REQUIRE(! ns_writer2.sampleValue(2, value));
290 
291  // *** And, if we try to use the earlier Writer after the buffer has
292  // *** been reused, that shouldn't work either
293 
294  BOOST_REQUIRE(! ns_writer.sampleValue(1, value));
295  BOOST_REQUIRE(! ns_writer.sampleValue(2, value));
296 }
297 
298 BOOST_AUTO_TEST_SUITE_END()
299 
300 #pragma GCC diagnostic pop
bool sampleValue(uint32_t index, uint16_t &value) const
Definition: NanoSlice.cc:20
Header::sample_count_t sampleCount() const
Definition: NanoSlice.cc:15
BOOST_AUTO_TEST_CASE(BaselineTest)
void setChannelNumber(uint16_t channel)
bool addSample(uint16_t value)
Header::nanoslice_size_t size() const
Definition: NanoSlice.cc:5
Header::channel_number_t channelNumber() const
Definition: NanoSlice.cc:10