$treeview $search $mathjax $extrastylesheet
artdaq_core
v3_06_01
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "artdaq-core/Data/Fragment.hh" 00002 #include "artdaq-core/Data/detail/RawFragmentHeader.hh" 00003 00004 #define BOOST_TEST_MODULE(Fragment_t) 00005 #include <cetlib/quiet_unit_test.hpp> 00006 00010 struct MetadataTypeOne 00011 { 00012 uint64_t field1; 00013 uint32_t field2; 00014 uint32_t field3; 00015 }; 00016 00020 struct MetadataTypeTwo 00021 { 00022 uint64_t field1; 00023 uint32_t field2; 00024 uint32_t field3; 00025 uint64_t field4; 00026 uint16_t field5; 00027 }; 00028 00032 struct MetadataTypeHuge 00033 { 00034 uint64_t fields[300]; 00035 }; 00036 00037 BOOST_AUTO_TEST_SUITE(Fragment_test) 00038 00039 BOOST_AUTO_TEST_CASE(Construct) 00040 { 00041 // 01-Mar-2013, KAB - I'm constructing these tests based on the 00042 // constructors that I already see in the class, but I have to 00043 // wonder if these truly correspond to the behavior that we want. 00044 artdaq::Fragment f1; 00045 BOOST_REQUIRE_EQUAL(f1.dataSize(), (size_t)0); 00046 BOOST_REQUIRE_EQUAL(f1.size(), (size_t)artdaq::detail::RawFragmentHeader::num_words()); 00047 BOOST_REQUIRE_EQUAL(f1.version(), (artdaq::Fragment::version_t)artdaq::detail::RawFragmentHeader::CurrentVersion); 00048 BOOST_REQUIRE_EQUAL(f1.type(), (artdaq::Fragment::type_t)artdaq::Fragment::InvalidFragmentType); 00049 BOOST_REQUIRE_EQUAL(f1.sequenceID(), (artdaq::Fragment::sequence_id_t)artdaq::Fragment::InvalidSequenceID); 00050 BOOST_REQUIRE_EQUAL(f1.fragmentID(), (artdaq::Fragment::fragment_id_t)artdaq::Fragment::InvalidFragmentID); 00051 BOOST_REQUIRE_EQUAL(f1.hasMetadata(), false); 00052 00053 artdaq::Fragment f2(7); 00054 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)7); 00055 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)artdaq::detail::RawFragmentHeader::num_words() + 7); 00056 BOOST_REQUIRE_EQUAL(f2.version(), (artdaq::Fragment::version_t)artdaq::detail::RawFragmentHeader::CurrentVersion); 00057 BOOST_REQUIRE(f2.type() == artdaq::Fragment::InvalidFragmentType); 00058 BOOST_REQUIRE(f2.sequenceID() == artdaq::Fragment::InvalidSequenceID); 00059 BOOST_REQUIRE(f2.fragmentID() == artdaq::Fragment::InvalidFragmentID); 00060 BOOST_REQUIRE_EQUAL(f2.hasMetadata(), false); 00061 00062 artdaq::Fragment f3(101, 202); 00063 BOOST_REQUIRE_EQUAL(f3.dataSize(), (size_t)0); 00064 BOOST_REQUIRE_EQUAL(f3.size(), (size_t)artdaq::detail::RawFragmentHeader::num_words()); 00065 BOOST_REQUIRE_EQUAL(f3.version(), (artdaq::Fragment::version_t)artdaq::detail::RawFragmentHeader::CurrentVersion); 00066 BOOST_REQUIRE(f3.type() == artdaq::Fragment::DataFragmentType); 00067 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (artdaq::Fragment::sequence_id_t)101); 00068 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (artdaq::Fragment::fragment_id_t)202); 00069 BOOST_REQUIRE_EQUAL(f3.hasMetadata(), false); 00070 00071 // Verify that only "user" fragment types may be specified 00072 // in the constructor 00073 try 00074 { 00075 artdaq::Fragment frag(101, 202, 0); 00076 BOOST_REQUIRE(0 && "Should have thrown exception"); 00077 } 00078 catch (cet::exception const& excpt) 00079 {} 00080 catch (...) 00081 { 00082 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00083 } 00084 00085 try 00086 { 00087 artdaq::Fragment frag(101, 202, 225); 00088 BOOST_REQUIRE(0 && "Should have thrown exception"); 00089 } 00090 catch (cet::exception const& excpt) 00091 {} 00092 catch (...) 00093 { 00094 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00095 } 00096 00097 try 00098 { 00099 artdaq::Fragment frag(101, 202, 255); 00100 BOOST_REQUIRE(0 && "Should have thrown exception"); 00101 } 00102 catch (cet::exception const& excpt) 00103 {} 00104 catch (...) 00105 { 00106 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00107 } 00108 00109 try 00110 { 00111 artdaq::Fragment frag(101, 202, artdaq::Fragment::InvalidFragmentType); 00112 BOOST_REQUIRE(0 && "Should have thrown exception"); 00113 } 00114 catch (cet::exception const& excpt) 00115 {} 00116 catch (...) 00117 { 00118 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00119 } 00120 00121 try 00122 { 00123 artdaq::Fragment 00124 frag(101, 202, artdaq::detail::RawFragmentHeader::FIRST_SYSTEM_TYPE); 00125 BOOST_REQUIRE(0 && "Should have thrown exception"); 00126 } 00127 catch (cet::exception const& excpt) 00128 {} 00129 catch (...) 00130 { 00131 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00132 } 00133 00134 try 00135 { 00136 artdaq::Fragment 00137 frag(101, 202, artdaq::detail::RawFragmentHeader::LAST_SYSTEM_TYPE); 00138 BOOST_REQUIRE(0 && "Should have thrown exception"); 00139 } 00140 catch (cet::exception const& excpt) 00141 {} 00142 catch (...) 00143 { 00144 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00145 } 00146 00147 try 00148 { 00149 artdaq::Fragment 00150 fragA(101, 202, artdaq::detail::RawFragmentHeader::FIRST_USER_TYPE); 00151 artdaq::Fragment 00152 fragB(101, 202, artdaq::detail::RawFragmentHeader::LAST_USER_TYPE); 00153 artdaq::Fragment fragC(101, 202, 1); 00154 artdaq::Fragment fragD(101, 202, 2); 00155 artdaq::Fragment fragE(101, 202, 3); 00156 artdaq::Fragment fragF(101, 202, 100); 00157 artdaq::Fragment fragG(101, 202, 200); 00158 artdaq::Fragment fragH(101, 202, 224); 00159 } 00160 catch (...) 00161 { 00162 BOOST_REQUIRE(0 && "Should not have thrown exception"); 00163 } 00164 } 00165 00166 BOOST_AUTO_TEST_CASE(FragmentType) 00167 { 00168 artdaq::Fragment frag(15); 00169 00170 // test "user" fragment types 00171 try 00172 { 00173 frag.setUserType(0); 00174 BOOST_REQUIRE(0 && "Should have thrown exception"); 00175 } 00176 catch (cet::exception const& excpt) 00177 {} 00178 catch (...) 00179 { 00180 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00181 } 00182 00183 try 00184 { 00185 frag.setUserType(225); 00186 BOOST_REQUIRE(0 && "Should have thrown exception"); 00187 } 00188 catch (cet::exception const& excpt) 00189 {} 00190 catch (...) 00191 { 00192 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00193 } 00194 00195 try 00196 { 00197 frag.setUserType(255); 00198 BOOST_REQUIRE(0 && "Should have thrown exception"); 00199 } 00200 catch (cet::exception const& excpt) 00201 {} 00202 catch (...) 00203 { 00204 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00205 } 00206 00207 try 00208 { 00209 frag.setUserType(artdaq::Fragment::InvalidFragmentType); 00210 BOOST_REQUIRE(0 && "Should have thrown exception"); 00211 } 00212 catch (cet::exception const& excpt) 00213 {} 00214 catch (...) 00215 { 00216 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00217 } 00218 00219 try 00220 { 00221 frag.setUserType(artdaq::detail::RawFragmentHeader::FIRST_SYSTEM_TYPE); 00222 BOOST_REQUIRE(0 && "Should have thrown exception"); 00223 } 00224 catch (cet::exception const& excpt) 00225 {} 00226 catch (...) 00227 { 00228 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00229 } 00230 00231 try 00232 { 00233 frag.setUserType(artdaq::detail::RawFragmentHeader::LAST_SYSTEM_TYPE); 00234 BOOST_REQUIRE(0 && "Should have thrown exception"); 00235 } 00236 catch (cet::exception const& excpt) 00237 {} 00238 catch (...) 00239 { 00240 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00241 } 00242 00243 try 00244 { 00245 frag.setUserType(artdaq::detail::RawFragmentHeader::FIRST_USER_TYPE); 00246 frag.setUserType(artdaq::detail::RawFragmentHeader::LAST_USER_TYPE); 00247 frag.setUserType(1); 00248 frag.setUserType(2); 00249 frag.setUserType(3); 00250 frag.setUserType(100); 00251 frag.setUserType(200); 00252 frag.setUserType(224); 00253 } 00254 catch (...) 00255 { 00256 BOOST_REQUIRE(0 && "Should not have thrown exception"); 00257 } 00258 00259 // test "system" fragment types 00260 try 00261 { 00262 frag.setSystemType(0); 00263 BOOST_REQUIRE(0 && "Should have thrown exception"); 00264 } 00265 catch (cet::exception const& excpt) 00266 {} 00267 catch (...) 00268 { 00269 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00270 } 00271 00272 try 00273 { 00274 frag.setSystemType(1); 00275 BOOST_REQUIRE(0 && "Should have thrown exception"); 00276 } 00277 catch (cet::exception const& excpt) 00278 {} 00279 catch (...) 00280 { 00281 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00282 } 00283 00284 try 00285 { 00286 frag.setSystemType(224); 00287 BOOST_REQUIRE(0 && "Should have thrown exception"); 00288 } 00289 catch (cet::exception const& excpt) 00290 {} 00291 catch (...) 00292 { 00293 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00294 } 00295 00296 try 00297 { 00298 frag.setSystemType(artdaq::Fragment::InvalidFragmentType); 00299 BOOST_REQUIRE(0 && "Should have thrown exception"); 00300 } 00301 catch (cet::exception const& excpt) 00302 {} 00303 catch (...) 00304 { 00305 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00306 } 00307 00308 try 00309 { 00310 frag.setSystemType(artdaq::detail::RawFragmentHeader::FIRST_USER_TYPE); 00311 BOOST_REQUIRE(0 && "Should have thrown exception"); 00312 } 00313 catch (cet::exception const& excpt) 00314 {} 00315 catch (...) 00316 { 00317 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00318 } 00319 00320 try 00321 { 00322 frag.setSystemType(artdaq::detail::RawFragmentHeader::LAST_USER_TYPE); 00323 BOOST_REQUIRE(0 && "Should have thrown exception"); 00324 } 00325 catch (cet::exception const& excpt) 00326 {} 00327 catch (...) 00328 { 00329 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00330 } 00331 00332 try 00333 { 00334 frag.setSystemType(artdaq::detail::RawFragmentHeader::FIRST_SYSTEM_TYPE); 00335 frag.setSystemType(artdaq::detail::RawFragmentHeader::LAST_SYSTEM_TYPE); 00336 frag.setSystemType(225); 00337 frag.setSystemType(230); 00338 frag.setSystemType(240); 00339 frag.setSystemType(250); 00340 frag.setSystemType(255); 00341 } 00342 catch (...) 00343 { 00344 BOOST_REQUIRE(0 && "Should not have thrown exception"); 00345 } 00346 } 00347 00348 BOOST_AUTO_TEST_CASE(SequenceID) 00349 { 00350 artdaq::Fragment f1; 00351 f1.setSequenceID(0); 00352 BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)0); 00353 f1.setSequenceID(1); 00354 BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)1); 00355 f1.setSequenceID(0xffff); 00356 BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)0xffff); 00357 f1.setSequenceID(0x0000ffffffffffff); 00358 BOOST_REQUIRE_EQUAL(f1.sequenceID(), (uint64_t)0x0000ffffffffffff); 00359 00360 artdaq::Fragment f2(0x12345, 0xab); 00361 BOOST_REQUIRE_EQUAL(f2.sequenceID(), (uint64_t)0x12345); 00362 00363 artdaq::Fragment f3(0x0000567812345678, 0xab); 00364 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint64_t)0x0000567812345678); 00365 } 00366 00367 BOOST_AUTO_TEST_CASE(FragmentID) 00368 { 00369 artdaq::Fragment f1; 00370 f1.setFragmentID(0); 00371 BOOST_REQUIRE_EQUAL(f1.fragmentID(), (uint16_t)0); 00372 f1.setFragmentID(1); 00373 BOOST_REQUIRE_EQUAL(f1.fragmentID(), (uint16_t)1); 00374 f1.setFragmentID(0xffff); 00375 BOOST_REQUIRE_EQUAL(f1.fragmentID(), (uint16_t)0xffff); 00376 00377 artdaq::Fragment f2(0x12345, 0xab); 00378 BOOST_REQUIRE_EQUAL(f2.fragmentID(), (uint16_t)0xab); 00379 00380 artdaq::Fragment f3(0x0000567812345678, 0xffff); 00381 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xffff); 00382 } 00383 00384 BOOST_AUTO_TEST_CASE(Resize) 00385 { 00386 // basic fragment 00387 artdaq::Fragment f1; 00388 f1.resize(1234); 00389 BOOST_REQUIRE_EQUAL(f1.dataSize(), (size_t)1234); 00390 BOOST_REQUIRE_EQUAL(f1.size(), (size_t)1234 + 00391 artdaq::detail::RawFragmentHeader::num_words()); 00392 00393 // fragment with metadata 00394 MetadataTypeOne mdOneA; 00395 artdaq::Fragment f2(1, 123, 3, 5, mdOneA); 00396 f2.resize(129); 00397 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)129); 00398 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)129 + 2 + 00399 artdaq::detail::RawFragmentHeader::num_words()); 00400 } 00401 00402 BOOST_AUTO_TEST_CASE(Empty) 00403 { 00404 artdaq::Fragment f1; 00405 BOOST_REQUIRE_EQUAL(f1.empty(), true); 00406 f1.resize(1234); 00407 BOOST_REQUIRE_EQUAL(f1.empty(), false); 00408 00409 MetadataTypeOne mdOneA; 00410 artdaq::Fragment f2(1, 123, 3, 5, mdOneA); 00411 BOOST_REQUIRE_EQUAL(f2.empty(), false); 00412 f2.resize(129); 00413 BOOST_REQUIRE_EQUAL(f2.empty(), false); 00414 f2.resize(0); 00415 BOOST_REQUIRE_EQUAL(f2.empty(), true); 00416 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)0); 00417 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)2 + 00418 artdaq::detail::RawFragmentHeader::num_words()); 00419 00420 artdaq::Fragment f3; 00421 BOOST_REQUIRE_EQUAL(f3.empty(), true); 00422 f3.setMetadata(mdOneA); 00423 BOOST_REQUIRE_EQUAL(f3.empty(), true); 00424 00425 artdaq::Fragment f4(14); 00426 BOOST_REQUIRE_EQUAL(f4.empty(), false); 00427 f4.setMetadata(mdOneA); 00428 BOOST_REQUIRE_EQUAL(f4.empty(), false); 00429 } 00430 00431 BOOST_AUTO_TEST_CASE(Clear) 00432 { 00433 artdaq::Fragment f1; 00434 BOOST_REQUIRE_EQUAL(f1.empty(), true); 00435 f1.resize(1234); 00436 BOOST_REQUIRE_EQUAL(f1.empty(), false); 00437 f1.clear(); 00438 BOOST_REQUIRE_EQUAL(f1.empty(), true); 00439 00440 MetadataTypeOne mdOneA; 00441 artdaq::Fragment f2(1, 123, 3, 5, mdOneA); 00442 BOOST_REQUIRE_EQUAL(f2.empty(), false); 00443 f2.resize(129); 00444 BOOST_REQUIRE_EQUAL(f2.empty(), false); 00445 f2.clear(); 00446 BOOST_REQUIRE_EQUAL(f2.empty(), true); 00447 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)0); 00448 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)2 + 00449 artdaq::detail::RawFragmentHeader::num_words()); 00450 00451 artdaq::Fragment f3; 00452 BOOST_REQUIRE_EQUAL(f3.empty(), true); 00453 BOOST_REQUIRE_EQUAL(f3.hasMetadata(), false); 00454 f3.setMetadata(mdOneA); 00455 BOOST_REQUIRE_EQUAL(f3.empty(), true); 00456 BOOST_REQUIRE_EQUAL(f3.hasMetadata(), true); 00457 f3.clear(); 00458 BOOST_REQUIRE_EQUAL(f3.empty(), true); 00459 BOOST_REQUIRE_EQUAL(f3.hasMetadata(), true); 00460 00461 artdaq::Fragment f4(14); 00462 BOOST_REQUIRE_EQUAL(f4.empty(), false); 00463 BOOST_REQUIRE_EQUAL(f4.hasMetadata(), false); 00464 f4.setMetadata(mdOneA); 00465 BOOST_REQUIRE_EQUAL(f4.empty(), false); 00466 BOOST_REQUIRE_EQUAL(f4.hasMetadata(), true); 00467 f4.clear(); 00468 BOOST_REQUIRE_EQUAL(f4.empty(), true); 00469 BOOST_REQUIRE_EQUAL(f4.hasMetadata(), true); 00470 } 00471 00472 BOOST_AUTO_TEST_CASE(Addresses) 00473 { 00474 // no metadata 00475 artdaq::Fragment f1(200); 00476 BOOST_REQUIRE_EQUAL(f1.dataSize(), (size_t)200); 00477 BOOST_REQUIRE_EQUAL(f1.size(), (size_t)200 + 00478 artdaq::detail::RawFragmentHeader::num_words()); 00479 artdaq::RawDataType* haddr = f1.headerAddress(); 00480 artdaq::RawDataType* daddr = f1.dataAddress(); 00481 BOOST_REQUIRE_EQUAL(daddr, 00482 (haddr + artdaq::detail::RawFragmentHeader::num_words())); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00483 try 00484 { 00485 f1.metadataAddress(); 00486 BOOST_REQUIRE(0 && "Should have thrown exception"); 00487 } 00488 catch (cet::exception const& excpt) 00489 {} 00490 catch (...) 00491 { 00492 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00493 } 00494 BOOST_REQUIRE_EQUAL(haddr, &(*(f1.headerBegin()))); 00495 BOOST_REQUIRE_EQUAL(daddr, &(*(f1.dataBegin()))); 00496 BOOST_REQUIRE_EQUAL(daddr + 200, &(*(f1.dataEnd()))); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00497 00498 // metadata with integer number of longwords 00499 MetadataTypeOne mdOneA; 00500 artdaq::Fragment f2(135, 101, 0, 3, mdOneA); 00501 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)135); 00502 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)135 + 2 + 00503 artdaq::detail::RawFragmentHeader::num_words()); 00504 haddr = f2.headerAddress(); 00505 daddr = f2.dataAddress(); 00506 artdaq::RawDataType* maddr = f2.metadataAddress(); 00507 BOOST_REQUIRE_EQUAL(maddr, haddr + // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00508 artdaq::detail::RawFragmentHeader::num_words()); 00509 BOOST_REQUIRE_EQUAL(daddr, maddr + 2); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00510 BOOST_REQUIRE_EQUAL(haddr, &(*(f2.headerBegin()))); 00511 BOOST_REQUIRE_EQUAL(daddr, &(*(f2.dataBegin()))); 00512 BOOST_REQUIRE_EQUAL(daddr + 135, &(*(f2.dataEnd()))); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00513 00514 // metadata with fractional number of longwords 00515 MetadataTypeTwo mdTwoA; 00516 artdaq::Fragment f3(77, 101, 0, 3, mdTwoA); 00517 BOOST_REQUIRE_EQUAL(f3.dataSize(), (size_t)77); 00518 BOOST_REQUIRE_EQUAL(f3.size(), (size_t)77 + 4 + // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00519 artdaq::detail::RawFragmentHeader::num_words()); 00520 haddr = f3.headerAddress(); 00521 daddr = f3.dataAddress(); 00522 maddr = f3.metadataAddress(); 00523 BOOST_REQUIRE_EQUAL(maddr, haddr + // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00524 artdaq::detail::RawFragmentHeader::num_words()); 00525 BOOST_REQUIRE_EQUAL(daddr, maddr + 4); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00526 BOOST_REQUIRE_EQUAL(haddr, &(*(f3.headerBegin()))); 00527 BOOST_REQUIRE_EQUAL(daddr, &(*(f3.dataBegin()))); 00528 BOOST_REQUIRE_EQUAL(daddr + 77, &(*(f3.dataEnd()))); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00529 } 00530 00531 BOOST_AUTO_TEST_CASE(Metadata) 00532 { 00533 artdaq::Fragment f1(42); 00534 BOOST_REQUIRE_EQUAL(f1.hasMetadata(), false); 00535 try 00536 { 00537 f1.metadata<MetadataTypeOne>(); 00538 BOOST_REQUIRE(0 && "Should have thrown exception"); 00539 } 00540 catch (cet::exception const& excpt) 00541 {} 00542 catch (...) 00543 { 00544 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00545 } 00546 00547 MetadataTypeOne mdOneA; 00548 mdOneA.field1 = 5; 00549 mdOneA.field2 = 10; 00550 mdOneA.field3 = 15; 00551 00552 try 00553 { 00554 f1.updateMetadata(mdOneA); 00555 BOOST_REQUIRE(0 && "Should have thrown exception"); 00556 } 00557 catch (cet::exception const& excpt) 00558 {} 00559 catch (...) 00560 { 00561 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00562 } 00563 00564 f1.setMetadata(mdOneA); 00565 auto* mdOnePtr = f1.metadata<MetadataTypeOne>(); 00566 BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t)5); 00567 BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)10); 00568 BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)15); 00569 00570 try 00571 { 00572 MetadataTypeOne mdOneB; 00573 f1.setMetadata(mdOneB); 00574 BOOST_REQUIRE(0 && "Should have thrown exception"); 00575 } 00576 catch (cet::exception const& excpt) 00577 {} 00578 catch (...) 00579 { 00580 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00581 } 00582 00583 f1.updateMetadata(*mdOnePtr); 00584 00585 MetadataTypeTwo mdTwoA; 00586 mdTwoA.field1 = 10; 00587 mdTwoA.field2 = 20; 00588 mdTwoA.field3 = 30; 00589 mdTwoA.field4 = 40; 00590 mdTwoA.field5 = 50; 00591 00592 try 00593 { 00594 f1.updateMetadata(mdTwoA); 00595 BOOST_REQUIRE(0 && "Should have thrown exception"); 00596 } 00597 catch (cet::exception const& excpt) 00598 {} 00599 catch (...) 00600 { 00601 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00602 } 00603 00604 artdaq::Fragment f2(10, 1, 2, 3, mdTwoA); 00605 auto* mdTwoPtr = f2.metadata<MetadataTypeTwo>(); 00606 BOOST_REQUIRE_EQUAL(mdTwoPtr->field1, (uint64_t)10); 00607 BOOST_REQUIRE_EQUAL(mdTwoPtr->field2, (uint32_t)20); 00608 BOOST_REQUIRE_EQUAL(mdTwoPtr->field3, (uint32_t)30); 00609 BOOST_REQUIRE_EQUAL(mdTwoPtr->field4, (uint32_t)40); 00610 BOOST_REQUIRE_EQUAL(mdTwoPtr->field5, (uint32_t)50); 00611 00612 artdaq::Fragment f3(0xabcdef, 0xc3a5, 123); 00613 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef); 00614 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5); 00615 BOOST_REQUIRE_EQUAL(f3.type(), (uint16_t)123); 00616 f3.resize(5); 00617 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef); 00618 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5); 00619 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123); 00620 artdaq::RawDataType* dataPtr = f3.dataAddress(); 00621 dataPtr[0] = 0x12345678; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00622 dataPtr[1] = 0xabcd; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00623 dataPtr[2] = 0x456789ab; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00624 dataPtr[3] = 0x3c3c3c3c; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00625 dataPtr[4] = 0x5a5a5a5a; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00626 BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00627 BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00628 BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00629 BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00630 BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00631 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef); 00632 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5); 00633 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123); 00634 MetadataTypeOne mdOneC; 00635 mdOneC.field1 = 505; 00636 mdOneC.field2 = 510; 00637 mdOneC.field3 = 515; 00638 f3.setMetadata(mdOneC); 00639 mdOnePtr = f3.metadata<MetadataTypeOne>(); 00640 BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t)505); 00641 BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)510); 00642 BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)515); 00643 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef); 00644 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5); 00645 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123); 00646 dataPtr = f3.dataAddress(); 00647 dataPtr[0] = 0x12345678; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00648 dataPtr[1] = 0xabcd; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00649 dataPtr[2] = 0x456789ab; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00650 dataPtr[3] = 0x3c3c3c3c; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00651 dataPtr[4] = 0x5a5a5a5a; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00652 BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00653 BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00654 BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00655 BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00656 BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00657 00658 MetadataTypeHuge mdHuge; 00659 artdaq::Fragment f4(19); 00660 BOOST_REQUIRE_EQUAL(f4.hasMetadata(), false); 00661 try 00662 { 00663 f4.setMetadata(mdHuge); 00664 BOOST_REQUIRE(0 && "Should have thrown exception"); 00665 } 00666 catch (cet::exception const& excpt) 00667 {} 00668 catch (...) 00669 { 00670 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00671 } 00672 00673 try 00674 { 00675 artdaq::Fragment f5(127, 1, 2, 3, mdHuge); 00676 BOOST_REQUIRE(0 && "Should have thrown exception"); 00677 } 00678 catch (cet::exception const& excpt) 00679 {} 00680 catch (...) 00681 { 00682 BOOST_REQUIRE(0 && "Should have thrown cet::exception"); 00683 } 00684 } 00685 00686 // JCF, 4/15/14 -- perform a set of tests concerning the new 00687 // byte-by-byte interface functions added to artdaq::Fragment 00688 00689 BOOST_AUTO_TEST_CASE(Bytes) 00690 { 00691 std::size_t payload_size = 5; 00692 00693 // seqID, fragID, type are all random 00694 artdaq::Fragment::sequence_id_t seqID = 1; 00695 artdaq::Fragment::fragment_id_t fragID = 1; 00696 artdaq::Fragment::type_t type = 3; 00697 00698 // No explicit constructor necessary for Metadata -- all we care 00699 // about is its size in the artdaq::Fragment, not its values 00700 00701 struct Metadata 00702 { 00703 uint8_t byteOne; 00704 uint8_t byteTwo; 00705 uint8_t byteThree; 00706 }; 00707 00708 Metadata theMetadata; 00709 00710 BOOST_REQUIRE(sizeof(artdaq::Fragment::byte_t) == 1); 00711 00712 // Assumption in some of the arithmetic below is that RawDataType is 8 bytes 00713 BOOST_REQUIRE(sizeof(artdaq::RawDataType) == 8); 00714 00715 // Check that the factory function and the explicit constructor 00716 // methods of creating a fragment yield identical results IF the 00717 // number of bytes passed to FragmentBytes() is a multiple of the 00718 // size of the RawDataType 00719 00720 std::unique_ptr<artdaq::Fragment> f1(new artdaq::Fragment(payload_size)); 00721 std::unique_ptr<artdaq::Fragment> f1_factory(artdaq::Fragment::FragmentBytes( 00722 payload_size * sizeof(artdaq::RawDataType))); 00723 00724 BOOST_REQUIRE(f1->size() == f1_factory->size()); 00725 BOOST_REQUIRE(f1->sizeBytes() == f1_factory->sizeBytes()); 00726 00727 std::unique_ptr<artdaq::Fragment> f2(new artdaq::Fragment(payload_size, seqID, fragID, type, theMetadata)); 00728 std::unique_ptr<artdaq::Fragment> f2_factory(artdaq::Fragment::FragmentBytes( 00729 payload_size * sizeof(artdaq::RawDataType), 00730 seqID, fragID, 00731 type, theMetadata)); 00732 00733 BOOST_REQUIRE(f2->size() == f2_factory->size()); 00734 BOOST_REQUIRE(f2->sizeBytes() == f2_factory->sizeBytes()); 00735 00736 // Now let's make sure that data gets aligned as expected (i.e., 00737 // along boundaries separated by sizeof(RawDataType) bytes) 00738 00739 std::size_t offset = 3; 00740 std::unique_ptr<artdaq::Fragment> f3_factory(artdaq::Fragment::FragmentBytes( 00741 payload_size * sizeof(artdaq::RawDataType) - offset, 00742 seqID, fragID, 00743 type, theMetadata)); 00744 00745 BOOST_REQUIRE(f3_factory->size() == f2->size()); 00746 BOOST_REQUIRE(f3_factory->sizeBytes() == f2->sizeBytes()); 00747 00748 // Make certain dataBegin(), dataBeginBytes() and the 00749 // (now-deprecated, but still in legacy code) dataAddress() point to 00750 // the same region in memory, i.e., the start of the payload 00751 00752 auto* ptr1 = reinterpret_cast<artdaq::Fragment::byte_t*>( // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) 00753 &*f3_factory->dataBegin()); 00754 00755 artdaq::Fragment::byte_t* ptr2 = f3_factory->dataBeginBytes(); 00756 00757 auto* ptr3 = reinterpret_cast<artdaq::Fragment::byte_t*>(f3_factory->dataAddress()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) 00758 00759 BOOST_REQUIRE_EQUAL(ptr1, ptr2); 00760 BOOST_REQUIRE_EQUAL(ptr2, ptr3); 00761 00762 // Make sure metadata struct gets aligned 00763 // header == 3 RawDataTypes, metadata is 3 bytes (rounds up to 1 RawDataType) 00764 std::size_t const metadata_size = 00765 f3_factory->dataBeginBytes() - reinterpret_cast<artdaq::Fragment::byte_t*>(&*f3_factory->headerBegin()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) 00766 BOOST_REQUIRE(metadata_size == 00767 (1 + artdaq::detail::RawFragmentHeader::num_words()) * sizeof(artdaq::RawDataType)); 00768 00769 // Sanity check for the payload size 00770 BOOST_REQUIRE(static_cast<std::size_t>(f3_factory->dataEndBytes() - f3_factory->dataBeginBytes()) == f3_factory->dataSizeBytes()); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00771 00772 // Check resizing 00773 artdaq::Fragment f4(payload_size); 00774 BOOST_REQUIRE_EQUAL(f4.dataSize(), payload_size); 00775 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), (payload_size * sizeof(artdaq::RawDataType))); 00776 f4.resize(payload_size + 1); 00777 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 1)); 00778 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 1) * sizeof(artdaq::RawDataType))); 00779 f4.resizeBytes(f4.dataSizeBytes() + 2); 00780 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 2)); 00781 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 2) * sizeof(artdaq::RawDataType))); 00782 f4.resizeBytes(f4.dataSizeBytes() + 1); 00783 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 3)); 00784 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 3) * sizeof(artdaq::RawDataType))); 00785 f4.resizeBytes(f4.dataSizeBytes() + 1); 00786 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 4)); 00787 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 4) * sizeof(artdaq::RawDataType))); 00788 00789 size_t targetSize = (payload_size + 4) * sizeof(artdaq::RawDataType); 00790 ++targetSize; 00791 f4.resizeBytes(targetSize); 00792 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00793 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00794 ++targetSize; 00795 f4.resizeBytes(targetSize); 00796 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00797 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00798 ++targetSize; 00799 f4.resizeBytes(targetSize); 00800 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00801 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00802 ++targetSize; 00803 f4.resizeBytes(targetSize); 00804 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00805 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00806 ++targetSize; 00807 f4.resizeBytes(targetSize); 00808 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00809 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00810 ++targetSize; 00811 f4.resizeBytes(targetSize); 00812 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00813 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00814 ++targetSize; 00815 f4.resizeBytes(targetSize); 00816 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00817 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00818 ++targetSize; 00819 f4.resizeBytes(targetSize); 00820 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5)); 00821 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) * sizeof(artdaq::RawDataType))); 00822 ++targetSize; 00823 f4.resizeBytes(targetSize); 00824 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 6)); 00825 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 6) * sizeof(artdaq::RawDataType))); 00826 00827 // Check adding metadata after construction 00828 artdaq::Fragment f5(payload_size); 00829 BOOST_REQUIRE_EQUAL(f5.size(), (payload_size + artdaq::detail::RawFragmentHeader::num_words())); 00830 BOOST_REQUIRE_EQUAL(f5.sizeBytes(), ((payload_size + artdaq::detail::RawFragmentHeader::num_words()) * sizeof(artdaq::RawDataType))); 00831 f5.setMetadata(theMetadata); 00832 BOOST_REQUIRE_EQUAL(f5.dataSize(), payload_size); 00833 BOOST_REQUIRE_EQUAL(f5.dataSizeBytes(), (payload_size * sizeof(artdaq::RawDataType))); 00834 BOOST_REQUIRE_EQUAL(f5.size(), (payload_size + 1 + artdaq::detail::RawFragmentHeader::num_words())); 00835 BOOST_REQUIRE_EQUAL(f5.sizeBytes(), ((payload_size + 1 + artdaq::detail::RawFragmentHeader::num_words()) * sizeof(artdaq::RawDataType))); 00836 } 00837 00838 BOOST_AUTO_TEST_CASE(Upgrade_V0) 00839 { 00840 artdaq::Fragment f(7); 00841 artdaq::detail::RawFragmentHeaderV0 hdr0; 00842 00843 hdr0.word_count = artdaq::detail::RawFragmentHeader::num_words() + 7; 00844 hdr0.version = 0; 00845 hdr0.type = 0xFE; 00846 hdr0.metadata_word_count = 0; 00847 00848 hdr0.sequence_id = 0xFEEDDEADBEEF; 00849 hdr0.fragment_id = 0xBEE7; 00850 hdr0.timestamp = 0xCAFEFECA; 00851 00852 hdr0.unused1 = 0xF0F0; 00853 hdr0.unused2 = 0xC5C5; 00854 00855 memcpy(f.headerBeginBytes(), &hdr0, sizeof(hdr0)); 00856 00857 artdaq::detail::RawFragmentHeader::RawDataType counter = 0; 00858 for (size_t ii = artdaq::detail::RawFragmentHeaderV0::num_words(); ii < artdaq::detail::RawFragmentHeader::num_words() + 7; ++ii) 00859 { 00860 memcpy(f.headerBegin() + ii, &(++counter), sizeof(counter)); 00861 } 00862 00863 BOOST_REQUIRE_EQUAL(f.version(), 0); 00864 BOOST_REQUIRE_EQUAL(f.type(), 0xFE); 00865 BOOST_REQUIRE_EQUAL(f.hasMetadata(), false); 00866 00867 BOOST_REQUIRE_EQUAL(f.sequenceID(), 0xFEEDDEADBEEF); 00868 BOOST_REQUIRE_EQUAL(f.fragmentID(), 0xBEE7); 00869 BOOST_REQUIRE_EQUAL(f.timestamp(), 0xCAFEFECA); 00870 00871 for (size_t jj = 0; jj < f.dataSize(); ++jj) 00872 { 00873 BOOST_REQUIRE_EQUAL(*(f.dataBegin() + jj), jj + 1); 00874 } 00875 } 00876 00877 BOOST_AUTO_TEST_CASE(Upgrade_V1) 00878 { 00879 artdaq::Fragment f(7); 00880 artdaq::detail::RawFragmentHeaderV1 hdr1; 00881 00882 hdr1.word_count = artdaq::detail::RawFragmentHeader::num_words() + 7; 00883 hdr1.version = 1; 00884 hdr1.type = 0xFE; 00885 hdr1.metadata_word_count = 0; 00886 00887 hdr1.sequence_id = 0xFEEDDEADBEEF; 00888 hdr1.fragment_id = 0xBEE7; 00889 hdr1.timestamp = 0xCAFEFECAAAAABBBB; 00890 00891 memcpy(f.headerBeginBytes(), &hdr1, sizeof(hdr1)); 00892 00893 artdaq::detail::RawFragmentHeader::RawDataType counter = 0; 00894 for (size_t ii = artdaq::detail::RawFragmentHeaderV1::num_words(); ii < artdaq::detail::RawFragmentHeader::num_words() + 7; ++ii) 00895 { 00896 memcpy(f.headerBegin() + ii, &(++counter), sizeof(counter)); 00897 } 00898 00899 BOOST_REQUIRE_EQUAL(f.version(), 1); 00900 BOOST_REQUIRE_EQUAL(f.type(), 0xFE); 00901 BOOST_REQUIRE_EQUAL(f.hasMetadata(), false); 00902 00903 BOOST_REQUIRE_EQUAL(f.sequenceID(), 0xFEEDDEADBEEF); 00904 BOOST_REQUIRE_EQUAL(f.fragmentID(), 0xBEE7); 00905 BOOST_REQUIRE_EQUAL(f.timestamp(), 0xCAFEFECAAAAABBBB); 00906 00907 for (size_t jj = 0; jj < f.dataSize(); ++jj) 00908 { 00909 BOOST_REQUIRE_EQUAL(*(f.dataBegin() + jj), jj + 1); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 00910 } 00911 } 00912 00913 BOOST_AUTO_TEST_SUITE_END()