Macros | Functions
DUNE_MicroSlice_t.cc File Reference
#include "dune-raw-data/Overlays/MicroSliceWriter.hh"
#include <vector>
#include <stdint.h>
#include <memory>
#include "cetlib_except/exception.h"
#include "cetlib/quiet_unit_test.hpp"

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE(MicroSlice_t)
 

Functions

 BOOST_AUTO_TEST_CASE (BaselineTest)
 
 BOOST_AUTO_TEST_CASE (MultipleNanoSliceTest)
 

Macro Definition Documentation

#define BOOST_TEST_MODULE (   MicroSlice_t)

Definition at line 19 of file DUNE_MicroSlice_t.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( BaselineTest  )

Definition at line 24 of file DUNE_MicroSlice_t.cc.

25 {
26  const uint16_t CHANNEL_NUMBER = 123;
27  const uint32_t MS_BUFFER_SIZE = 4096;
28  const uint32_t NS_BUFFER_SIZE = 1024;
29  const uint16_t SAMPLE1 = 0x1234;
30  const uint16_t SAMPLE2 = 0xc3c3;
31  const uint16_t SAMPLE3 = 0xbeef;
32  const uint16_t SAMPLE4 = 0xfe87;
33  const uint16_t SAMPLE5 = 0x5a5a;
34  std::vector<uint8_t> work_buffer(MS_BUFFER_SIZE);
35  std::unique_ptr<dune::NanoSlice> nslice_ptr;
36  std::shared_ptr<dune::NanoSliceWriter> ns_writer_ptr;
37  uint16_t value;
38 
39  // *** Use a MicroSliceWriter to build up a MicroSlice, checking
40  // *** that everything looks good as we go.
41 
42  dune::MicroSliceWriter ms_writer(&work_buffer[0], MS_BUFFER_SIZE);
43  BOOST_REQUIRE_EQUAL(ms_writer.size(), sizeof(dune::MicroSlice::Header));
44  BOOST_REQUIRE_EQUAL(ms_writer.nanoSliceCount(), 0);
45  nslice_ptr = ms_writer.nanoSlice(0);
46  BOOST_REQUIRE(nslice_ptr.get() == 0);
47  nslice_ptr = ms_writer.nanoSlice(999);
48  BOOST_REQUIRE(nslice_ptr.get() == 0);
49 
50  ns_writer_ptr = ms_writer.reserveNanoSlice(NS_BUFFER_SIZE);
51  BOOST_REQUIRE(ns_writer_ptr.get() != 0);
52  BOOST_REQUIRE_EQUAL(ms_writer.size(),
53  sizeof(dune::MicroSlice::Header) + NS_BUFFER_SIZE);
54  BOOST_REQUIRE_EQUAL(ms_writer.nanoSliceCount(), 1);
55  if (ns_writer_ptr.get() != 0) {
56  ns_writer_ptr->setChannelNumber(CHANNEL_NUMBER);
57  ns_writer_ptr->addSample(SAMPLE1);
58  ns_writer_ptr->addSample(SAMPLE2);
59  ns_writer_ptr->addSample(SAMPLE3);
60  ns_writer_ptr->addSample(SAMPLE4);
61  ns_writer_ptr->addSample(SAMPLE5);
62 
63  uint32_t size_diff = ms_writer.finalize();
64  BOOST_REQUIRE_EQUAL(size_diff, MS_BUFFER_SIZE - sizeof(dune::MicroSlice::Header) -
65  sizeof(dune::NanoSlice::Header) - 5*sizeof(uint16_t));
66 
67  nslice_ptr = ms_writer.nanoSlice(1);
68  BOOST_REQUIRE(nslice_ptr.get() == 0);
69  nslice_ptr = ms_writer.nanoSlice(0);
70  BOOST_REQUIRE(nslice_ptr.get() != 0);
71  if (nslice_ptr.get() != 0) {
72  BOOST_REQUIRE_EQUAL(nslice_ptr->size(),
73  sizeof(dune::MicroSlice::Header) + 5*sizeof(uint16_t));
74  BOOST_REQUIRE_EQUAL(nslice_ptr->sampleCount(), 5);
75  BOOST_REQUIRE(nslice_ptr->sampleValue(0, value));
76  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
77  BOOST_REQUIRE(nslice_ptr->sampleValue(1, value));
78  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
79  BOOST_REQUIRE(nslice_ptr->sampleValue(2, value));
80  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
81  BOOST_REQUIRE(nslice_ptr->sampleValue(3, value));
82  BOOST_REQUIRE_EQUAL(value, SAMPLE4);
83  BOOST_REQUIRE(nslice_ptr->sampleValue(4, value));
84  BOOST_REQUIRE_EQUAL(value, SAMPLE5);
85  }
86  }
87 }
BOOST_AUTO_TEST_CASE ( MultipleNanoSliceTest  )

Definition at line 89 of file DUNE_MicroSlice_t.cc.

90 {
91  const uint16_t CHANNEL_NUMBER = 123;
92  const uint32_t MS_BUFFER_SIZE = 4096;
93  const uint32_t NS_BUFFER_SIZE = 1024;
94  const uint16_t SAMPLE1 = 0x1234;
95  const uint16_t SAMPLE2 = 0xc3c3;
96  const uint16_t SAMPLE3 = 0xbeef;
97  const uint16_t SAMPLE4 = 0xfe87;
98  const uint16_t SAMPLE5 = 0x5a5a;
99  std::vector<uint8_t> work_buffer(MS_BUFFER_SIZE);
100  std::unique_ptr<dune::NanoSlice> nslice_ptr;
101  std::shared_ptr<dune::NanoSliceWriter> ns_writer_ptr;
102  uint16_t value;
103 
104  dune::MicroSliceWriter ms_writer(&work_buffer[0], MS_BUFFER_SIZE);
105  BOOST_REQUIRE_EQUAL(ms_writer.size(), sizeof(dune::MicroSlice::Header));
106  BOOST_REQUIRE_EQUAL(ms_writer.nanoSliceCount(), 0);
107 
108  ns_writer_ptr = ms_writer.reserveNanoSlice(NS_BUFFER_SIZE);
109  BOOST_REQUIRE(ns_writer_ptr.get() != 0);
110  BOOST_REQUIRE_EQUAL(ms_writer.size(),
111  sizeof(dune::MicroSlice::Header) + NS_BUFFER_SIZE);
112  BOOST_REQUIRE_EQUAL(ms_writer.nanoSliceCount(), 1);
113  if (ns_writer_ptr.get() != 0) {
114  ns_writer_ptr->setChannelNumber(CHANNEL_NUMBER);
115  ns_writer_ptr->addSample(SAMPLE1);
116  ns_writer_ptr->addSample(SAMPLE2);
117  ns_writer_ptr->addSample(SAMPLE3);
118  ns_writer_ptr->addSample(SAMPLE4);
119  ns_writer_ptr->addSample(SAMPLE5);
120 
121  nslice_ptr = ms_writer.nanoSlice(1);
122  BOOST_REQUIRE(nslice_ptr.get() == 0);
123  nslice_ptr = ms_writer.nanoSlice(0);
124  BOOST_REQUIRE(nslice_ptr.get() != 0);
125  }
126 
127  // *** Add a second NanoSlice
128 
129  ns_writer_ptr = ms_writer.reserveNanoSlice(NS_BUFFER_SIZE);
130  BOOST_REQUIRE(ns_writer_ptr.get() != 0);
131  BOOST_REQUIRE_EQUAL(ms_writer.size(), sizeof(dune::MicroSlice::Header) +
132  sizeof(dune::NanoSlice::Header) + 5*sizeof(uint16_t) +
133  NS_BUFFER_SIZE);
134  BOOST_REQUIRE_EQUAL(ms_writer.nanoSliceCount(), 2);
135  if (ns_writer_ptr.get() != 0) {
136  ns_writer_ptr->setChannelNumber(CHANNEL_NUMBER+1);
137  ns_writer_ptr->addSample(SAMPLE5);
138  ns_writer_ptr->addSample(SAMPLE3);
139  ns_writer_ptr->addSample(SAMPLE1);
140 
141  nslice_ptr = ms_writer.nanoSlice(2);
142  BOOST_REQUIRE(nslice_ptr.get() == 0);
143  nslice_ptr = ms_writer.nanoSlice(99);
144  BOOST_REQUIRE(nslice_ptr.get() == 0);
145  nslice_ptr = ms_writer.nanoSlice(0);
146  BOOST_REQUIRE(nslice_ptr.get() != 0);
147  nslice_ptr = ms_writer.nanoSlice(1);
148  BOOST_REQUIRE(nslice_ptr.get() != 0);
149  }
150 
151  // *** Add a third NanoSlice
152 
153  ns_writer_ptr = ms_writer.reserveNanoSlice(NS_BUFFER_SIZE);
154  BOOST_REQUIRE(ns_writer_ptr.get() != 0);
155  BOOST_REQUIRE_EQUAL(ms_writer.size(), sizeof(dune::MicroSlice::Header) +
156  2*sizeof(dune::NanoSlice::Header) + 8*sizeof(uint16_t) +
157  NS_BUFFER_SIZE);
158  BOOST_REQUIRE_EQUAL(ms_writer.nanoSliceCount(), 3);
159  if (ns_writer_ptr.get() != 0) {
160  ns_writer_ptr->setChannelNumber(CHANNEL_NUMBER+2);
161  ns_writer_ptr->addSample(SAMPLE4);
162  ns_writer_ptr->addSample(SAMPLE5);
163 
164  nslice_ptr = ms_writer.nanoSlice(3);
165  BOOST_REQUIRE(nslice_ptr.get() == 0);
166  nslice_ptr = ms_writer.nanoSlice(99);
167  BOOST_REQUIRE(nslice_ptr.get() == 0);
168  nslice_ptr = ms_writer.nanoSlice(0);
169  BOOST_REQUIRE(nslice_ptr.get() != 0);
170  nslice_ptr = ms_writer.nanoSlice(1);
171  BOOST_REQUIRE(nslice_ptr.get() != 0);
172  nslice_ptr = ms_writer.nanoSlice(2);
173  BOOST_REQUIRE(nslice_ptr.get() != 0);
174  }
175 
176  // *** Finish off the creation of the MicroSlice and verify that we can't
177  // *** add any more NanoSlices after it is finalized
178 
179  int32_t size_diff = ms_writer.finalize();
180  BOOST_REQUIRE_EQUAL(size_diff, MS_BUFFER_SIZE - sizeof(dune::MicroSlice::Header) -
181  3*sizeof(dune::NanoSlice::Header) - 10*sizeof(uint16_t));
182 
183  {
184  bool threw_exception = false;
185 
186  try {
187  ms_writer.reserveNanoSlice(NS_BUFFER_SIZE);
188  } catch (const cet::exception& ) {
189  threw_exception = true;
190  }
191 
192  BOOST_REQUIRE_EQUAL(threw_exception, true);
193  }
194 
195  // *** Now we construct an instance of a read-only MicroSlice from
196  // *** the work buffer and verify that everything still looks good
197 
198  dune::MicroSlice mslice(&work_buffer[0]);
199  BOOST_REQUIRE_EQUAL(mslice.size(), sizeof(dune::MicroSlice::Header) +
200  3*sizeof(dune::NanoSlice::Header) + 10*sizeof(uint16_t));
201  BOOST_REQUIRE_EQUAL(mslice.nanoSliceCount(), 3);
202 
203  nslice_ptr = ms_writer.nanoSlice(0);
204  BOOST_REQUIRE(nslice_ptr.get() != 0);
205  BOOST_REQUIRE_EQUAL(nslice_ptr->size(), sizeof(dune::NanoSlice::Header) +
206  5*sizeof(uint16_t));
207  BOOST_REQUIRE_EQUAL(nslice_ptr->channelNumber(), CHANNEL_NUMBER);
208  BOOST_REQUIRE_EQUAL(nslice_ptr->sampleCount(), 5);
209  BOOST_REQUIRE(nslice_ptr->sampleValue(0, value));
210  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
211  BOOST_REQUIRE(nslice_ptr->sampleValue(1, value));
212  BOOST_REQUIRE_EQUAL(value, SAMPLE2);
213  BOOST_REQUIRE(nslice_ptr->sampleValue(2, value));
214  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
215  BOOST_REQUIRE(nslice_ptr->sampleValue(3, value));
216  BOOST_REQUIRE_EQUAL(value, SAMPLE4);
217  BOOST_REQUIRE(nslice_ptr->sampleValue(4, value));
218  BOOST_REQUIRE_EQUAL(value, SAMPLE5);
219  BOOST_REQUIRE(! nslice_ptr->sampleValue(5, value));
220  BOOST_REQUIRE(! nslice_ptr->sampleValue(567, value));
221 
222  nslice_ptr = ms_writer.nanoSlice(1);
223  BOOST_REQUIRE(nslice_ptr.get() != 0);
224  BOOST_REQUIRE_EQUAL(nslice_ptr->size(), sizeof(dune::NanoSlice::Header) +
225  3*sizeof(uint16_t));
226  BOOST_REQUIRE_EQUAL(nslice_ptr->channelNumber(), CHANNEL_NUMBER+1);
227  BOOST_REQUIRE_EQUAL(nslice_ptr->sampleCount(), 3);
228  BOOST_REQUIRE(nslice_ptr->sampleValue(0, value));
229  BOOST_REQUIRE_EQUAL(value, SAMPLE5);
230  BOOST_REQUIRE(nslice_ptr->sampleValue(1, value));
231  BOOST_REQUIRE_EQUAL(value, SAMPLE3);
232  BOOST_REQUIRE(nslice_ptr->sampleValue(2, value));
233  BOOST_REQUIRE_EQUAL(value, SAMPLE1);
234  BOOST_REQUIRE(! nslice_ptr->sampleValue(3, value));
235 
236  nslice_ptr = ms_writer.nanoSlice(2);
237  BOOST_REQUIRE(nslice_ptr.get() != 0);
238  BOOST_REQUIRE_EQUAL(nslice_ptr->size(), sizeof(dune::NanoSlice::Header) +
239  2*sizeof(uint16_t));
240  BOOST_REQUIRE_EQUAL(nslice_ptr->channelNumber(), CHANNEL_NUMBER+2);
241  BOOST_REQUIRE_EQUAL(nslice_ptr->sampleCount(), 2);
242  BOOST_REQUIRE(nslice_ptr->sampleValue(0, value));
243  BOOST_REQUIRE_EQUAL(value, SAMPLE4);
244  BOOST_REQUIRE(nslice_ptr->sampleValue(1, value));
245  BOOST_REQUIRE_EQUAL(value, SAMPLE5);
246  BOOST_REQUIRE(! nslice_ptr->sampleValue(2, value));
247 }
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33