artdaq_core  v1_07_02
 All Classes Namespaces Functions
Fragment_t.cc
1 #include "artdaq-core/Data/Fragment.hh"
2 
3 #define BOOST_TEST_MODULE(Fragment_t)
4 #include "cetlib/quiet_unit_test.hpp"
5 
7  uint64_t field1;
8  uint32_t field2;
9  uint32_t field3;
10 };
11 
13  uint64_t field1;
14  uint32_t field2;
15  uint32_t field3;
16  uint64_t field4;
17  uint16_t field5;
18 };
19 
21  uint64_t fields[300];
22 };
23 
24 BOOST_AUTO_TEST_SUITE(Fragment_test)
25 
26 BOOST_AUTO_TEST_CASE(Construct)
27 {
28  // 01-Mar-2013, KAB - I'm constructing these tests based on the
29  // constructors that I already see in the class, but I have to
30  // wonder if these truly correspond to the behavior that we want.
32  BOOST_REQUIRE_EQUAL(f1.dataSize(), (size_t) 0);
33  BOOST_REQUIRE_EQUAL(f1.size(), (size_t) 3);
34  BOOST_REQUIRE_EQUAL(f1.version(), (artdaq::Fragment::version_t) 0);
35  BOOST_REQUIRE_EQUAL(f1.type(), (artdaq::Fragment::type_t) 0);
36  BOOST_REQUIRE_EQUAL(f1.sequenceID(), (artdaq::Fragment::sequence_id_t) 0);
37  BOOST_REQUIRE_EQUAL(f1.fragmentID(), (artdaq::Fragment::fragment_id_t) 0);
38  BOOST_REQUIRE_EQUAL(f1.hasMetadata(),false);
39 
40  artdaq::Fragment f2(7);
41  BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t) 7);
42  BOOST_REQUIRE_EQUAL(f2.size(), (size_t) 10);
43  BOOST_REQUIRE_EQUAL(f2.version(), (artdaq::Fragment::version_t) 0);
44  BOOST_REQUIRE(f2.type() == artdaq::Fragment::InvalidFragmentType);
45  BOOST_REQUIRE(f2.sequenceID() == artdaq::Fragment::InvalidSequenceID);
46  BOOST_REQUIRE(f2.fragmentID() == artdaq::Fragment::InvalidFragmentID);
47  BOOST_REQUIRE_EQUAL(f2.hasMetadata(),false);
48 
49  artdaq::Fragment f3(101, 202);
50  BOOST_REQUIRE_EQUAL(f3.dataSize(), (size_t) 0);
51  BOOST_REQUIRE_EQUAL(f3.size(), (size_t) 3);
52  BOOST_REQUIRE_EQUAL(f3.version(), (artdaq::Fragment::version_t) 0);
53  BOOST_REQUIRE(f3.type() == artdaq::Fragment::DataFragmentType);
54  BOOST_REQUIRE_EQUAL(f3.sequenceID(), (artdaq::Fragment::sequence_id_t) 101);
55  BOOST_REQUIRE_EQUAL(f3.fragmentID(), (artdaq::Fragment::fragment_id_t) 202);
56  BOOST_REQUIRE_EQUAL(f3.hasMetadata(),false);
57 
58  // Verify that only "user" fragment types may be specified
59  // in the constructor
60  try {
61  artdaq::Fragment frag(101, 202, 0);
62  BOOST_REQUIRE(0 && "Should have thrown exception");
63  }
64  catch (cet::exception const & excpt) {
65  }
66  catch (...) {
67  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
68  }
69 
70  try {
71  artdaq::Fragment frag(101, 202, 225);
72  BOOST_REQUIRE(0 && "Should have thrown exception");
73  }
74  catch (cet::exception const & excpt) {
75  }
76  catch (...) {
77  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
78  }
79 
80  try {
81  artdaq::Fragment frag(101, 202, 255);
82  BOOST_REQUIRE(0 && "Should have thrown exception");
83  }
84  catch (cet::exception const & excpt) {
85  }
86  catch (...) {
87  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
88  }
89 
90  try {
91  artdaq::Fragment frag(101, 202, artdaq::Fragment::InvalidFragmentType);
92  BOOST_REQUIRE(0 && "Should have thrown exception");
93  }
94  catch (cet::exception const & excpt) {
95  }
96  catch (...) {
97  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
98  }
99 
100  try {
102  frag(101, 202, artdaq::detail::RawFragmentHeader::FIRST_SYSTEM_TYPE);
103  BOOST_REQUIRE(0 && "Should have thrown exception");
104  }
105  catch (cet::exception const & excpt) {
106  }
107  catch (...) {
108  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
109  }
110 
111  try {
113  frag(101, 202, artdaq::detail::RawFragmentHeader::LAST_SYSTEM_TYPE);
114  BOOST_REQUIRE(0 && "Should have thrown exception");
115  }
116  catch (cet::exception const & excpt) {
117  }
118  catch (...) {
119  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
120  }
121 
122  try {
124  fragA(101, 202, artdaq::detail::RawFragmentHeader::FIRST_USER_TYPE);
126  fragB(101, 202, artdaq::detail::RawFragmentHeader::LAST_USER_TYPE);
127  artdaq::Fragment fragC(101, 202, 1);
128  artdaq::Fragment fragD(101, 202, 2);
129  artdaq::Fragment fragE(101, 202, 3);
130  artdaq::Fragment fragF(101, 202, 100);
131  artdaq::Fragment fragG(101, 202, 200);
132  artdaq::Fragment fragH(101, 202, 224);
133  }
134  catch (...) {
135  BOOST_REQUIRE(0 && "Should not have thrown exception");
136  }
137 }
138 
139 BOOST_AUTO_TEST_CASE(FragmentType)
140 {
141  artdaq::Fragment frag(15);
142 
143  // test "user" fragment types
144  try {
145  frag.setUserType(0);
146  BOOST_REQUIRE(0 && "Should have thrown exception");
147  }
148  catch (cet::exception const & excpt) {
149  }
150  catch (...) {
151  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
152  }
153 
154  try {
155  frag.setUserType(225);
156  BOOST_REQUIRE(0 && "Should have thrown exception");
157  }
158  catch (cet::exception const & excpt) {
159  }
160  catch (...) {
161  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
162  }
163 
164  try {
165  frag.setUserType(255);
166  BOOST_REQUIRE(0 && "Should have thrown exception");
167  }
168  catch (cet::exception const & excpt) {
169  }
170  catch (...) {
171  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
172  }
173 
174  try {
175  frag.setUserType(artdaq::Fragment::InvalidFragmentType);
176  BOOST_REQUIRE(0 && "Should have thrown exception");
177  }
178  catch (cet::exception const & excpt) {
179  }
180  catch (...) {
181  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
182  }
183 
184  try {
185  frag.setUserType(artdaq::detail::RawFragmentHeader::FIRST_SYSTEM_TYPE);
186  BOOST_REQUIRE(0 && "Should have thrown exception");
187  }
188  catch (cet::exception const & excpt) {
189  }
190  catch (...) {
191  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
192  }
193 
194  try {
195  frag.setUserType(artdaq::detail::RawFragmentHeader::LAST_SYSTEM_TYPE);
196  BOOST_REQUIRE(0 && "Should have thrown exception");
197  }
198  catch (cet::exception const & excpt) {
199  }
200  catch (...) {
201  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
202  }
203 
204  try {
205  frag.setUserType(artdaq::detail::RawFragmentHeader::FIRST_USER_TYPE);
206  frag.setUserType(artdaq::detail::RawFragmentHeader::LAST_USER_TYPE);
207  frag.setUserType( 1);
208  frag.setUserType( 2);
209  frag.setUserType( 3);
210  frag.setUserType(100);
211  frag.setUserType(200);
212  frag.setUserType(224);
213  }
214  catch (...) {
215  BOOST_REQUIRE(0 && "Should not have thrown exception");
216  }
217 
218  // test "system" fragment types
219  try {
220  frag.setSystemType(0);
221  BOOST_REQUIRE(0 && "Should have thrown exception");
222  }
223  catch (cet::exception const & excpt) {
224  }
225  catch (...) {
226  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
227  }
228 
229  try {
230  frag.setSystemType(1);
231  BOOST_REQUIRE(0 && "Should have thrown exception");
232  }
233  catch (cet::exception const & excpt) {
234  }
235  catch (...) {
236  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
237  }
238 
239  try {
240  frag.setSystemType(224);
241  BOOST_REQUIRE(0 && "Should have thrown exception");
242  }
243  catch (cet::exception const & excpt) {
244  }
245  catch (...) {
246  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
247  }
248 
249  try {
250  frag.setSystemType(artdaq::Fragment::InvalidFragmentType);
251  BOOST_REQUIRE(0 && "Should have thrown exception");
252  }
253  catch (cet::exception const & excpt) {
254  }
255  catch (...) {
256  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
257  }
258 
259  try {
260  frag.setSystemType(artdaq::detail::RawFragmentHeader::FIRST_USER_TYPE);
261  BOOST_REQUIRE(0 && "Should have thrown exception");
262  }
263  catch (cet::exception const & excpt) {
264  }
265  catch (...) {
266  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
267  }
268 
269  try {
270  frag.setSystemType(artdaq::detail::RawFragmentHeader::LAST_USER_TYPE);
271  BOOST_REQUIRE(0 && "Should have thrown exception");
272  }
273  catch (cet::exception const & excpt) {
274  }
275  catch (...) {
276  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
277  }
278 
279  try {
280  frag.setSystemType(artdaq::detail::RawFragmentHeader::FIRST_SYSTEM_TYPE);
281  frag.setSystemType(artdaq::detail::RawFragmentHeader::LAST_SYSTEM_TYPE);
282  frag.setSystemType(225);
283  frag.setSystemType(230);
284  frag.setSystemType(240);
285  frag.setSystemType(250);
286  frag.setSystemType(255);
287  }
288  catch (...) {
289  BOOST_REQUIRE(0 && "Should not have thrown exception");
290  }
291 }
292 
293 BOOST_AUTO_TEST_CASE(SequenceID)
294 {
295  artdaq::Fragment f1;
296  f1.setSequenceID(0);
297  BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)0);
298  f1.setSequenceID(1);
299  BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)1);
300  f1.setSequenceID(0xffff);
301  BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)0xffff);
302  f1.setSequenceID(0x0000ffffffffffff);
303  BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)0x0000ffffffffffff);
304 
305  artdaq::Fragment f2(0x12345, 0xab);
306  BOOST_REQUIRE_EQUAL(f2.sequenceID(), (uint64_t)0x12345);
307 
308  artdaq::Fragment f3(0x0000567812345678, 0xab);
309  BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint64_t)0x0000567812345678);
310 }
311 
312 BOOST_AUTO_TEST_CASE(FragmentID)
313 {
314  artdaq::Fragment f1;
315  f1.setFragmentID(0);
316  BOOST_REQUIRE_EQUAL(f1.fragmentID(), (uint16_t)0);
317  f1.setFragmentID(1);
318  BOOST_REQUIRE_EQUAL(f1.fragmentID(), (uint16_t)1);
319  f1.setFragmentID(0xffff);
320  BOOST_REQUIRE_EQUAL(f1.fragmentID(), (uint16_t)0xffff);
321 
322  artdaq::Fragment f2(0x12345, 0xab);
323  BOOST_REQUIRE_EQUAL(f2.fragmentID(), (uint16_t)0xab);
324 
325  artdaq::Fragment f3(0x0000567812345678, 0xffff);
326  BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xffff);
327 }
328 
329 BOOST_AUTO_TEST_CASE(Resize)
330 {
331  // basic fragment
332  artdaq::Fragment f1;
333  f1.resize(1234);
334  BOOST_REQUIRE_EQUAL(f1.dataSize(), (size_t) 1234);
335  BOOST_REQUIRE_EQUAL(f1.size(), (size_t) 1234 +
336  artdaq::detail::RawFragmentHeader::num_words());
337 
338  // fragment with metadata
339  MetadataTypeOne mdOneA;
340  artdaq::Fragment f2(1, 123, 3, 5, mdOneA);
341  f2.resize(129);
342  BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t) 129);
343  BOOST_REQUIRE_EQUAL(f2.size(), (size_t) 129 + 2 +
344  artdaq::detail::RawFragmentHeader::num_words());
345 }
346 
347 BOOST_AUTO_TEST_CASE(Empty)
348 {
349  artdaq::Fragment f1;
350  BOOST_REQUIRE_EQUAL(f1.empty(), true);
351  f1.resize(1234);
352  BOOST_REQUIRE_EQUAL(f1.empty(), false);
353 
354  MetadataTypeOne mdOneA;
355  artdaq::Fragment f2(1, 123, 3, 5, mdOneA);
356  BOOST_REQUIRE_EQUAL(f2.empty(), false);
357  f2.resize(129);
358  BOOST_REQUIRE_EQUAL(f2.empty(), false);
359  f2.resize(0);
360  BOOST_REQUIRE_EQUAL(f2.empty(), true);
361  BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t) 0);
362  BOOST_REQUIRE_EQUAL(f2.size(), (size_t) 2 +
363  artdaq::detail::RawFragmentHeader::num_words());
364 
365  artdaq::Fragment f3;
366  BOOST_REQUIRE_EQUAL(f3.empty(), true);
367  f3.setMetadata(mdOneA);
368  BOOST_REQUIRE_EQUAL(f3.empty(), true);
369 
370  artdaq::Fragment f4(14);
371  BOOST_REQUIRE_EQUAL(f4.empty(), false);
372  f4.setMetadata(mdOneA);
373  BOOST_REQUIRE_EQUAL(f4.empty(), false);
374 }
375 
376 BOOST_AUTO_TEST_CASE(Clear)
377 {
378  artdaq::Fragment f1;
379  BOOST_REQUIRE_EQUAL(f1.empty(), true);
380  f1.resize(1234);
381  BOOST_REQUIRE_EQUAL(f1.empty(), false);
382  f1.clear();
383  BOOST_REQUIRE_EQUAL(f1.empty(), true);
384 
385  MetadataTypeOne mdOneA;
386  artdaq::Fragment f2(1, 123, 3, 5, mdOneA);
387  BOOST_REQUIRE_EQUAL(f2.empty(), false);
388  f2.resize(129);
389  BOOST_REQUIRE_EQUAL(f2.empty(), false);
390  f2.clear();
391  BOOST_REQUIRE_EQUAL(f2.empty(), true);
392  BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t) 0);
393  BOOST_REQUIRE_EQUAL(f2.size(), (size_t) 2 +
394  artdaq::detail::RawFragmentHeader::num_words());
395 
396  artdaq::Fragment f3;
397  BOOST_REQUIRE_EQUAL(f3.empty(), true);
398  BOOST_REQUIRE_EQUAL(f3.hasMetadata(), false);
399  f3.setMetadata(mdOneA);
400  BOOST_REQUIRE_EQUAL(f3.empty(), true);
401  BOOST_REQUIRE_EQUAL(f3.hasMetadata(), true);
402  f3.clear();
403  BOOST_REQUIRE_EQUAL(f3.empty(), true);
404  BOOST_REQUIRE_EQUAL(f3.hasMetadata(), true);
405 
406  artdaq::Fragment f4(14);
407  BOOST_REQUIRE_EQUAL(f4.empty(), false);
408  BOOST_REQUIRE_EQUAL(f4.hasMetadata(), false);
409  f4.setMetadata(mdOneA);
410  BOOST_REQUIRE_EQUAL(f4.empty(), false);
411  BOOST_REQUIRE_EQUAL(f4.hasMetadata(), true);
412  f4.clear();
413  BOOST_REQUIRE_EQUAL(f4.empty(), true);
414  BOOST_REQUIRE_EQUAL(f4.hasMetadata(), true);
415 }
416 
417 BOOST_AUTO_TEST_CASE(Addresses)
418 {
419  // no metadata
420  artdaq::Fragment f1(200);
421  BOOST_REQUIRE_EQUAL(f1.dataSize(), (size_t) 200);
422  BOOST_REQUIRE_EQUAL(f1.size(), (size_t) 200 +
423  artdaq::detail::RawFragmentHeader::num_words());
424  artdaq::RawDataType* haddr = f1.headerAddress();
425  artdaq::RawDataType* daddr = f1.dataAddress();
426  BOOST_REQUIRE_EQUAL(daddr,
427  (haddr + artdaq::detail::RawFragmentHeader::num_words()));
428  try {
429  f1.metadataAddress();
430  BOOST_REQUIRE(0 && "Should have thrown exception");
431  }
432  catch (cet::exception const & excpt) {
433  }
434  catch (...) {
435  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
436  }
437  BOOST_REQUIRE_EQUAL(haddr, &(*(f1.headerBegin())));
438  BOOST_REQUIRE_EQUAL(daddr, &(*(f1.dataBegin())));
439  BOOST_REQUIRE_EQUAL(daddr+200, &(*(f1.dataEnd())));
440 
441  // metadata with integer number of longwords
442  MetadataTypeOne mdOneA;
443  artdaq::Fragment f2(135, 101, 0, 3, mdOneA);
444  BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t) 135);
445  BOOST_REQUIRE_EQUAL(f2.size(), (size_t) 135 + 2 +
446  artdaq::detail::RawFragmentHeader::num_words());
447  haddr = f2.headerAddress();
448  daddr = f2.dataAddress();
449  artdaq::RawDataType* maddr = f2.metadataAddress();
450  BOOST_REQUIRE_EQUAL(maddr, haddr +
451  artdaq::detail::RawFragmentHeader::num_words());
452  BOOST_REQUIRE_EQUAL(daddr, maddr + 2);
453  BOOST_REQUIRE_EQUAL(haddr, &(*(f2.headerBegin())));
454  BOOST_REQUIRE_EQUAL(daddr, &(*(f2.dataBegin())));
455  BOOST_REQUIRE_EQUAL(daddr+135, &(*(f2.dataEnd())));
456 
457  // metadata with fractional number of longwords
458  MetadataTypeTwo mdTwoA;
459  artdaq::Fragment f3(77, 101, 0, 3, mdTwoA);
460  BOOST_REQUIRE_EQUAL(f3.dataSize(), (size_t) 77);
461  BOOST_REQUIRE_EQUAL(f3.size(), (size_t) 77 + 4 +
462  artdaq::detail::RawFragmentHeader::num_words());
463  haddr = f3.headerAddress();
464  daddr = f3.dataAddress();
465  maddr = f3.metadataAddress();
466  BOOST_REQUIRE_EQUAL(maddr, haddr +
467  artdaq::detail::RawFragmentHeader::num_words());
468  BOOST_REQUIRE_EQUAL(daddr, maddr + 4);
469  BOOST_REQUIRE_EQUAL(haddr, &(*(f3.headerBegin())));
470  BOOST_REQUIRE_EQUAL(daddr, &(*(f3.dataBegin())));
471  BOOST_REQUIRE_EQUAL(daddr+77, &(*(f3.dataEnd())));
472 }
473 
474 BOOST_AUTO_TEST_CASE(Metadata)
475 {
476  artdaq::Fragment f1(42);
477  BOOST_REQUIRE_EQUAL(f1.hasMetadata(),false);
478  try {
479  f1.metadata<MetadataTypeOne>();
480  BOOST_REQUIRE(0 && "Should have thrown exception");
481  }
482  catch (cet::exception const & excpt) {
483  }
484  catch (...) {
485  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
486  }
487 
488  MetadataTypeOne mdOneA;
489  mdOneA.field1 = 5;
490  mdOneA.field2 = 10;
491  mdOneA.field3 = 15;
492 
493  try {
494  f1.updateMetadata(mdOneA);
495  BOOST_REQUIRE(0 && "Should have thrown exception");
496  }
497  catch (cet::exception const & excpt) {
498  }
499  catch (...) {
500  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
501  }
502 
503 
504  f1.setMetadata(mdOneA);
505  MetadataTypeOne* mdOnePtr = f1.metadata<MetadataTypeOne>();
506  BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t) 5);
507  BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)10);
508  BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)15);
509 
510  try {
511  MetadataTypeOne mdOneB;
512  f1.setMetadata(mdOneB);
513  BOOST_REQUIRE(0 && "Should have thrown exception");
514  }
515  catch (cet::exception const & excpt) {
516  }
517  catch (...) {
518  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
519  }
520 
521  f1.updateMetadata( *mdOnePtr );
522 
523  MetadataTypeTwo mdTwoA;
524  mdTwoA.field1 = 10;
525  mdTwoA.field2 = 20;
526  mdTwoA.field3 = 30;
527  mdTwoA.field4 = 40;
528  mdTwoA.field5 = 50;
529 
530  try {
531  f1.updateMetadata(mdTwoA);
532  BOOST_REQUIRE(0 && "Should have thrown exception");
533  }
534  catch (cet::exception const & excpt) {
535  }
536  catch (...) {
537  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
538  }
539 
540  artdaq::Fragment f2(10, 1, 2, 3, mdTwoA);
541  MetadataTypeTwo* mdTwoPtr = f2.metadata<MetadataTypeTwo>();
542  BOOST_REQUIRE_EQUAL(mdTwoPtr->field1, (uint64_t)10);
543  BOOST_REQUIRE_EQUAL(mdTwoPtr->field2, (uint32_t)20);
544  BOOST_REQUIRE_EQUAL(mdTwoPtr->field3, (uint32_t)30);
545  BOOST_REQUIRE_EQUAL(mdTwoPtr->field4, (uint32_t)40);
546  BOOST_REQUIRE_EQUAL(mdTwoPtr->field5, (uint32_t)50);
547 
548  artdaq::Fragment f3(0xabcdef, 0xc3a5, 123);
549  BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
550  BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
551  BOOST_REQUIRE_EQUAL(f3.type(), (uint16_t)123);
552  f3.resize(5);
553  BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
554  BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
555  BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t) 123);
556  artdaq::RawDataType * dataPtr = f3.dataAddress();
557  dataPtr[0] = 0x12345678;
558  dataPtr[1] = 0xabcd;
559  dataPtr[2] = 0x456789ab;
560  dataPtr[3] = 0x3c3c3c3c;
561  dataPtr[4] = 0x5a5a5a5a;
562  BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678);
563  BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd);
564  BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab);
565  BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c);
566  BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a);
567  BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
568  BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
569  BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t) 123);
570  MetadataTypeOne mdOneC;
571  mdOneC.field1 = 505;
572  mdOneC.field2 = 510;
573  mdOneC.field3 = 515;
574  f3.setMetadata(mdOneC);
575  mdOnePtr = f3.metadata<MetadataTypeOne>();
576  BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t)505);
577  BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)510);
578  BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)515);
579  BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
580  BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
581  BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t) 123);
582  dataPtr = f3.dataAddress();
583  dataPtr[0] = 0x12345678;
584  dataPtr[1] = 0xabcd;
585  dataPtr[2] = 0x456789ab;
586  dataPtr[3] = 0x3c3c3c3c;
587  dataPtr[4] = 0x5a5a5a5a;
588  BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678);
589  BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd);
590  BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab);
591  BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c);
592  BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a);
593 
594  MetadataTypeHuge mdHuge;
595  artdaq::Fragment f4(19);
596  BOOST_REQUIRE_EQUAL(f4.hasMetadata(),false);
597  try {
598  f4.setMetadata(mdHuge);
599  BOOST_REQUIRE(0 && "Should have thrown exception");
600  }
601  catch (cet::exception const & excpt) {
602  }
603  catch (...) {
604  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
605  }
606 
607  try {
608  artdaq::Fragment f5(127, 1, 2, 3, mdHuge);
609  BOOST_REQUIRE(0 && "Should have thrown exception");
610  }
611  catch (cet::exception const & excpt) {
612  }
613  catch (...) {
614  BOOST_REQUIRE(0 && "Should have thrown cet::exception");
615  }
616 }
617 
618 
619 // JCF, 4/15/14 -- perform a set of tests concerning the new
620 // byte-by-byte interface functions added to artdaq::Fragment
621 
622 BOOST_AUTO_TEST_CASE(Bytes) {
623 
624  std::size_t payload_size = 5;
625 
626  // seqID, fragID, type are all random
627  artdaq::Fragment::sequence_id_t seqID = 1;
628  artdaq::Fragment::fragment_id_t fragID = 1;
629  artdaq::Fragment::type_t type = 3;
630 
631  // No explicit constructor necessary for Metadata -- all we care
632  // about is its size in the artdaq::Fragment, not its values
633 
634  struct Metadata {
635  uint8_t byteOne;
636  uint8_t byteTwo;
637  uint8_t byteThree;
638  };
639 
640  Metadata theMetadata;
641 
642  BOOST_REQUIRE( sizeof( artdaq::Fragment::byte_t) == 1);
643 
644  // Assumption in some of the arithmetic below is that RawDataType is 8 bytes
645  BOOST_REQUIRE( sizeof( artdaq::RawDataType) == 8);
646 
647  // Check that the factory function and the explicit constructor
648  // methods of creating a fragment yield identical results IF the
649  // number of bytes passed to FragmentBytes() is a multiple of the
650  // size of the RawDataType
651 
652  std::unique_ptr<artdaq::Fragment> f1( new artdaq::Fragment(payload_size) );
653  std::unique_ptr<artdaq::Fragment> f1_factory(artdaq::Fragment::FragmentBytes(
654  payload_size * sizeof( artdaq::RawDataType ) ) );
655 
656  BOOST_REQUIRE( f1->size() == f1_factory->size() );
657  BOOST_REQUIRE( f1->sizeBytes() == f1_factory->sizeBytes() );
658 
659  std::unique_ptr<artdaq::Fragment> f2( new artdaq::Fragment( payload_size, seqID, fragID, type, theMetadata) );
660  std::unique_ptr<artdaq::Fragment> f2_factory( artdaq::Fragment::FragmentBytes(
661  payload_size * sizeof( artdaq::RawDataType ),
662  seqID, fragID,
663  type, theMetadata) );
664 
665  BOOST_REQUIRE( f2->size() == f2_factory->size() );
666  BOOST_REQUIRE( f2->sizeBytes() == f2_factory->sizeBytes() );
667 
668  // Now let's make sure that data gets aligned as expected (i.e.,
669  // along boundaries separated by sizeof(RawDataType) bytes)
670 
671  std::size_t offset = 3;
672  std::unique_ptr<artdaq::Fragment> f3_factory( artdaq::Fragment::FragmentBytes(
673  payload_size * sizeof( artdaq::RawDataType ) - offset,
674  seqID, fragID,
675  type, theMetadata) );
676 
677  BOOST_REQUIRE( f3_factory->size() == f2->size() );
678  BOOST_REQUIRE( f3_factory->sizeBytes() == f2->sizeBytes() );
679 
680  // Make certain dataBegin(), dataBeginBytes() and the
681  // (now-deprecated, but still in legacy code) dataAddress() point to
682  // the same region in memory, i.e., the start of the payload
683 
684  artdaq::Fragment::byte_t* ptr1 = reinterpret_cast<artdaq::Fragment::byte_t*>(
685  &*f3_factory->dataBegin());
686 
687  artdaq::Fragment::byte_t* ptr2 = f3_factory->dataBeginBytes();
688 
689  artdaq::Fragment::byte_t* ptr3 = reinterpret_cast<artdaq::Fragment::byte_t*>( f3_factory->dataAddress() );
690 
691  BOOST_REQUIRE_EQUAL( ptr1, ptr2 );
692  BOOST_REQUIRE_EQUAL( ptr2, ptr3 );
693 
694  // Make sure metadata struct gets aligned
695  // header == 3 RawDataTypes, metadata is 3 bytes (rounds up to 1 RawDataType)
696  BOOST_REQUIRE( f3_factory->dataBeginBytes() -
697  reinterpret_cast<artdaq::Fragment::byte_t*>(
698  &*f3_factory->headerBegin() )
699  == 4 * sizeof(artdaq::RawDataType) );
700 
701  // Sanity check for the payload size
702  BOOST_REQUIRE( static_cast<std::size_t>( f3_factory->dataEndBytes() - f3_factory->dataBeginBytes() ) == f3_factory->dataSizeBytes() );
703 
704  // Check resizing
705  artdaq::Fragment f4( payload_size );
706  BOOST_REQUIRE_EQUAL( f4.dataSize(), payload_size );
707  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), (payload_size * sizeof(artdaq::RawDataType)) );
708  f4.resize( payload_size + 1 );
709  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 1) );
710  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 1) * sizeof(artdaq::RawDataType)) );
711  f4.resizeBytes( f4.dataSizeBytes() + 2 );
712  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 2) );
713  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 2) * sizeof(artdaq::RawDataType)) );
714  f4.resizeBytes( f4.dataSizeBytes() + 1 );
715  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 3) );
716  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 3) * sizeof(artdaq::RawDataType)) );
717  f4.resizeBytes( f4.dataSizeBytes() + 1 );
718  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 4) );
719  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 4) * sizeof(artdaq::RawDataType)) );
720 
721  size_t targetSize = (payload_size + 4) * sizeof(artdaq::RawDataType);
722  ++targetSize;
723  f4.resizeBytes( targetSize );
724  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
725  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
726  ++targetSize;
727  f4.resizeBytes( targetSize );
728  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
729  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
730  ++targetSize;
731  f4.resizeBytes( targetSize );
732  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
733  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
734  ++targetSize;
735  f4.resizeBytes( targetSize );
736  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
737  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
738  ++targetSize;
739  f4.resizeBytes( targetSize );
740  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
741  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
742  ++targetSize;
743  f4.resizeBytes( targetSize );
744  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
745  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
746  ++targetSize;
747  f4.resizeBytes( targetSize );
748  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
749  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
750  ++targetSize;
751  f4.resizeBytes( targetSize );
752  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 5) );
753  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType)) );
754  ++targetSize;
755  f4.resizeBytes( targetSize );
756  BOOST_REQUIRE_EQUAL( f4.dataSize(), (payload_size + 6) );
757  BOOST_REQUIRE_EQUAL( f4.dataSizeBytes(), ((payload_size + 6) * sizeof(artdaq::RawDataType)) );
758 
759  // Check adding metadata after construction
760  artdaq::Fragment f5( payload_size );
761  BOOST_REQUIRE_EQUAL( f5.size(), (payload_size + artdaq::detail::RawFragmentHeader::num_words()) );
762  BOOST_REQUIRE_EQUAL( f5.sizeBytes(), ((payload_size + artdaq::detail::RawFragmentHeader::num_words()) * sizeof(artdaq::RawDataType)));
763  f5.setMetadata(theMetadata);
764  BOOST_REQUIRE_EQUAL( f5.dataSize(), payload_size );
765  BOOST_REQUIRE_EQUAL( f5.dataSizeBytes(), (payload_size * sizeof(artdaq::RawDataType)) );
766  BOOST_REQUIRE_EQUAL( f5.size(), (payload_size + 1 + artdaq::detail::RawFragmentHeader::num_words()) );
767  BOOST_REQUIRE_EQUAL( f5.sizeBytes(), ((payload_size + 1 + artdaq::detail::RawFragmentHeader::num_words()) * sizeof(artdaq::RawDataType)));
768 }
769 
770 BOOST_AUTO_TEST_SUITE_END()