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