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