1 #include "artdaq-core/Data/Fragment.hh"
2 #include "artdaq-core/Data/detail/RawFragmentHeader.hh"
4 #define BOOST_TEST_MODULE(Fragment_t)
5 #include <cetlib/quiet_unit_test.hpp>
37 BOOST_AUTO_TEST_SUITE(Fragment_test)
39 BOOST_AUTO_TEST_CASE(Construct)
45 BOOST_REQUIRE_EQUAL(f1.
dataSize(), (size_t)0);
54 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)7);
60 BOOST_REQUIRE_EQUAL(f2.hasMetadata(),
false);
63 BOOST_REQUIRE_EQUAL(f3.dataSize(), (size_t)0);
69 BOOST_REQUIRE_EQUAL(f3.hasMetadata(),
false);
92 BOOST_AUTO_TEST_CASE(FragmentType)
97 BOOST_REQUIRE_THROW(frag.setUserType(0), cet::exception);
98 BOOST_REQUIRE_THROW(frag.setUserType(225), cet::exception);
99 BOOST_REQUIRE_THROW(frag.setUserType(255), cet::exception);
109 frag.setUserType(100);
110 frag.setUserType(200);
111 frag.setUserType(224);
114 BOOST_REQUIRE_THROW(frag.setSystemType(0), cet::exception);
115 BOOST_REQUIRE_THROW(frag.setSystemType(1), cet::exception);
116 BOOST_REQUIRE_THROW(frag.setSystemType(224), cet::exception);
123 frag.setSystemType(225);
124 frag.setSystemType(230);
125 frag.setSystemType(240);
126 frag.setSystemType(250);
127 frag.setSystemType(255);
130 BOOST_REQUIRE(map.size() > 0);
132 BOOST_REQUIRE(map.size() > 0);
135 BOOST_AUTO_TEST_CASE(SequenceID)
139 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)0);
141 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)1);
143 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)0xffff);
145 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)0x0000ffffffffffff);
148 BOOST_REQUIRE_EQUAL(f2.sequenceID(), (uint64_t)0x12345);
151 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint64_t)0x0000567812345678);
154 BOOST_AUTO_TEST_CASE(FragmentID)
158 BOOST_REQUIRE_EQUAL(f1.
fragmentID(), (uint16_t)0);
160 BOOST_REQUIRE_EQUAL(f1.
fragmentID(), (uint16_t)1);
162 BOOST_REQUIRE_EQUAL(f1.
fragmentID(), (uint16_t)0xffff);
165 BOOST_REQUIRE_EQUAL(f2.fragmentID(), (uint16_t)0xab);
168 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xffff);
171 BOOST_AUTO_TEST_CASE(Resize)
176 BOOST_REQUIRE_EQUAL(f1.
dataSize(), (size_t)1234);
177 BOOST_REQUIRE_EQUAL(f1.
size(), (size_t)1234 +
184 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)129);
185 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)129 + 2 +
189 BOOST_AUTO_TEST_CASE(Empty)
192 BOOST_REQUIRE_EQUAL(f1.
empty(),
true);
194 BOOST_REQUIRE_EQUAL(f1.
empty(),
false);
198 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
200 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
202 BOOST_REQUIRE_EQUAL(f2.empty(),
true);
203 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)0);
204 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)2 +
208 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
210 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
213 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
214 f4.setMetadata(mdOneA);
215 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
218 BOOST_AUTO_TEST_CASE(Clear)
221 BOOST_REQUIRE_EQUAL(f1.
empty(),
true);
223 BOOST_REQUIRE_EQUAL(f1.
empty(),
false);
225 BOOST_REQUIRE_EQUAL(f1.
empty(),
true);
229 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
231 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
233 BOOST_REQUIRE_EQUAL(f2.empty(),
true);
234 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)0);
235 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)2 +
239 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
240 BOOST_REQUIRE_EQUAL(f3.hasMetadata(),
false);
242 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
243 BOOST_REQUIRE_EQUAL(f3.hasMetadata(),
true);
245 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
246 BOOST_REQUIRE_EQUAL(f3.hasMetadata(),
true);
249 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
250 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
false);
251 f4.setMetadata(mdOneA);
252 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
253 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
true);
255 BOOST_REQUIRE_EQUAL(f4.empty(),
true);
256 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
true);
259 BOOST_AUTO_TEST_CASE(Addresses)
263 BOOST_REQUIRE_EQUAL(f1.
dataSize(), (size_t)200);
264 BOOST_REQUIRE_EQUAL(f1.
size(), (size_t)200 +
268 BOOST_REQUIRE_EQUAL(daddr,
273 BOOST_REQUIRE_EQUAL(haddr, &(*(f1.
headerBegin())));
274 BOOST_REQUIRE_EQUAL(daddr, &(*(f1.
dataBegin())));
275 BOOST_REQUIRE_EQUAL(daddr + 200, &(*(f1.
dataEnd())));
280 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)135);
281 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)135 + 2 +
283 haddr = f2.headerAddress();
284 daddr = f2.dataAddress();
286 BOOST_REQUIRE_EQUAL(maddr, haddr +
288 BOOST_REQUIRE_EQUAL(daddr, maddr + 2);
289 BOOST_REQUIRE_EQUAL(haddr, &(*(f2.headerBegin())));
290 BOOST_REQUIRE_EQUAL(daddr, &(*(f2.dataBegin())));
291 BOOST_REQUIRE_EQUAL(daddr + 135, &(*(f2.dataEnd())));
296 BOOST_REQUIRE_EQUAL(f3.dataSize(), (size_t)77);
297 BOOST_REQUIRE_EQUAL(f3.size(), (size_t)77 + 4 +
299 haddr = f3.headerAddress();
300 daddr = f3.dataAddress();
301 maddr = f3.metadataAddress();
302 BOOST_REQUIRE_EQUAL(maddr, haddr +
304 BOOST_REQUIRE_EQUAL(daddr, maddr + 4);
305 BOOST_REQUIRE_EQUAL(haddr, &(*(f3.headerBegin())));
306 BOOST_REQUIRE_EQUAL(daddr, &(*(f3.dataBegin())));
307 BOOST_REQUIRE_EQUAL(daddr + 77, &(*(f3.dataEnd())));
310 BOOST_AUTO_TEST_CASE(Metadata)
326 BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t)5);
327 BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)10);
328 BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)15);
331 BOOST_REQUIRE_THROW(f1.
setMetadata(mdOneB), cet::exception);
346 BOOST_REQUIRE_EQUAL(mdTwoPtr->field1, (uint64_t)10);
347 BOOST_REQUIRE_EQUAL(mdTwoPtr->field2, (uint32_t)20);
348 BOOST_REQUIRE_EQUAL(mdTwoPtr->field3, (uint32_t)30);
349 BOOST_REQUIRE_EQUAL(mdTwoPtr->field4, (uint32_t)40);
350 BOOST_REQUIRE_EQUAL(mdTwoPtr->field5, (uint32_t)50);
353 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
354 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
355 BOOST_REQUIRE_EQUAL(f3.type(), (uint16_t)123);
357 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
358 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
359 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123);
361 dataPtr[0] = 0x12345678;
363 dataPtr[2] = 0x456789ab;
364 dataPtr[3] = 0x3c3c3c3c;
365 dataPtr[4] = 0x5a5a5a5a;
366 BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678);
367 BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd);
368 BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab);
369 BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c);
370 BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a);
371 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
372 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
373 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123);
378 f3.setMetadata(mdOneC);
380 BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t)505);
381 BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)510);
382 BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)515);
383 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
384 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
385 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123);
386 dataPtr = f3.dataAddress();
387 dataPtr[0] = 0x12345678;
389 dataPtr[2] = 0x456789ab;
390 dataPtr[3] = 0x3c3c3c3c;
391 dataPtr[4] = 0x5a5a5a5a;
392 BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678);
393 BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd);
394 BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab);
395 BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c);
396 BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a);
400 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
false);
402 BOOST_REQUIRE_THROW(f4.setMetadata(mdHuge), cet::exception);
404 BOOST_REQUIRE_THROW(
artdaq::Fragment f5(127, 1, 2, 3, mdHuge), cet::exception);
410 BOOST_AUTO_TEST_CASE(Bytes)
412 std::size_t payload_size = 5;
429 Metadata theMetadata;
445 BOOST_REQUIRE(f1->
size() == f1_factory->size());
446 BOOST_REQUIRE(f1->
sizeBytes() == f1_factory->sizeBytes());
448 std::unique_ptr<artdaq::Fragment> f2(
new artdaq::Fragment(payload_size, seqID, fragID, type, theMetadata));
454 BOOST_REQUIRE(f2->size() == f2_factory->size());
455 BOOST_REQUIRE(f2->sizeBytes() == f2_factory->sizeBytes());
460 std::size_t offset = 3;
466 BOOST_REQUIRE(f3_factory->size() == f2->size());
467 BOOST_REQUIRE(f3_factory->sizeBytes() == f2->sizeBytes());
474 &*f3_factory->dataBegin());
480 BOOST_REQUIRE_EQUAL(ptr1, ptr2);
481 BOOST_REQUIRE_EQUAL(ptr2, ptr3);
485 std::size_t
const metadata_size =
487 BOOST_REQUIRE(metadata_size ==
491 BOOST_REQUIRE(static_cast<std::size_t>(f3_factory->dataEndBytes() - f3_factory->dataBeginBytes()) == f3_factory->dataSizeBytes());
495 BOOST_REQUIRE_EQUAL(f4.dataSize(), payload_size);
497 f4.resize(payload_size + 1);
498 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 1));
499 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 1) *
sizeof(
artdaq::RawDataType)));
500 f4.resizeBytes(f4.dataSizeBytes() + 2);
501 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 2));
502 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 2) *
sizeof(
artdaq::RawDataType)));
503 f4.resizeBytes(f4.dataSizeBytes() + 1);
504 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 3));
505 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 3) *
sizeof(
artdaq::RawDataType)));
506 f4.resizeBytes(f4.dataSizeBytes() + 1);
507 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 4));
508 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 4) *
sizeof(
artdaq::RawDataType)));
512 f4.resizeBytes(targetSize);
513 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
514 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
516 f4.resizeBytes(targetSize);
517 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
518 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
520 f4.resizeBytes(targetSize);
521 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
522 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
524 f4.resizeBytes(targetSize);
525 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
526 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
528 f4.resizeBytes(targetSize);
529 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
530 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
532 f4.resizeBytes(targetSize);
533 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
534 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
536 f4.resizeBytes(targetSize);
537 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
538 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
540 f4.resizeBytes(targetSize);
541 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
542 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
544 f4.resizeBytes(targetSize);
545 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 6));
546 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 6) *
sizeof(
artdaq::RawDataType)));
552 f5.setMetadata(theMetadata);
553 BOOST_REQUIRE_EQUAL(f5.dataSize(), payload_size);
559 BOOST_AUTO_TEST_CASE(Upgrade_V0)
576 memcpy(f.headerBeginBytes(), &hdr0,
sizeof(hdr0));
581 memcpy(f.headerBegin() + ii, &(++counter),
sizeof(counter));
584 BOOST_REQUIRE_EQUAL(f.version(), 0);
585 BOOST_REQUIRE_EQUAL(f.type(), 0xFE);
586 BOOST_REQUIRE_EQUAL(f.hasMetadata(),
false);
588 BOOST_REQUIRE_EQUAL(f.sequenceID(), 0xFEEDDEADBEEF);
589 BOOST_REQUIRE_EQUAL(f.fragmentID(), 0xBEE7);
590 BOOST_REQUIRE_EQUAL(f.timestamp(), 0xCAFEFECA);
592 for (
size_t jj = 0; jj < f.dataSize(); ++jj)
594 BOOST_REQUIRE_EQUAL(*(f.dataBegin() + jj), jj + 1);
598 BOOST_AUTO_TEST_CASE(Upgrade_V1)
612 memcpy(f.headerBeginBytes(), &hdr1,
sizeof(hdr1));
617 memcpy(f.headerBegin() + ii, &(++counter),
sizeof(counter));
620 BOOST_REQUIRE_EQUAL(f.version(), 1);
621 BOOST_REQUIRE_EQUAL(f.type(), 0xFE);
622 BOOST_REQUIRE_EQUAL(f.hasMetadata(),
false);
624 BOOST_REQUIRE_EQUAL(f.sequenceID(), 0xFEEDDEADBEEF);
625 BOOST_REQUIRE_EQUAL(f.fragmentID(), 0xBEE7);
626 BOOST_REQUIRE_EQUAL(f.timestamp(), 0xCAFEFECAAAAABBBB);
628 for (
size_t jj = 0; jj < f.dataSize(); ++jj)
630 BOOST_REQUIRE_EQUAL(*(f.dataBegin() + jj), jj + 1);
634 BOOST_AUTO_TEST_SUITE_END()
static constexpr type_t InvalidFragmentType
Copy InvalidFragmentType from RawFragmentHeader.
void setSequenceID(sequence_id_t sequence_id)
Sets the Sequence ID of the Fragment.
std::size_t size() const
Gets the size of the Fragment, from the Fragment header.
detail::RawFragmentHeader::fragment_id_t fragment_id_t
typedef for fragment_id_t from RawFragmentHeader
std::size_t sizeBytes() const
Size of vals_ vector ( header + (optional) metadata + payload) in bytes.
detail::RawFragmentHeader::type_t type_t
typedef for type_t from RawFragmentHeader
sequence_id_t sequenceID() const
Sequence ID of the Fragment, from the Fragment header.
static constexpr type_t DataFragmentType
Copy DataFragmentType from RawFragmentHeader.
uint8_t byte_t
For byte representation.
void resize(std::size_t sz)
Resize the data payload to hold sz RawDataType words.
static constexpr fragment_id_t InvalidFragmentID
Copy InvalidFragmentID from RawFragmentHeader.
void setMetadata(const T &metadata)
Set the metadata in the Fragment to the contents of the specified structure. This throws an exception...
detail::RawFragmentHeader::version_t version_t
typedef for version_t from RawFragmentHeader
iterator headerBegin()
Return an iterator to the beginning of the header (should be used for serialization only: use setters...
iterator dataBegin()
Return an iterator to the beginning of the data payload (after header and metadata) ...
bool empty()
Determines if the Fragment contains no data.
static constexpr sequence_id_t InvalidSequenceID
Copy InvalidSequenceID from RawFragmentHeader.
void updateMetadata(const T &metadata)
Updates existing metadata with a new metadata object.
type_t type() const
Type of the Fragment, from the Fragment header.
RawDataType * dataAddress()
Returns a RawDataType pointer to the beginning of the payload.
T * metadata()
Return a pointer to the metadata. This throws an exception if the Fragment contains no metadata...
iterator dataEnd()
Return an iterator to the end of the data payload.
static FragmentPtr FragmentBytes(std::size_t nbytes)
Create a Fragment using a static factory function rather than a constructor to allow for the function...
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
RawDataType * metadataAddress()
Get the address of the metadata. For internal use only, use metadata() instead.
void clear()
Removes all elements from the payload section of the Fragment.
bool hasMetadata() const
Test whether this Fragment has metadata.
void setFragmentID(fragment_id_t fragment_id)
Sets the Fragment ID of the Fragment.
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
std::size_t dataSize() const
Return the number of RawDataType words in the data payload. This does not include the number of words...
detail::RawFragmentHeader::sequence_id_t sequence_id_t
typedef for sequence_id_t from RawFragmentHeader
fragment_id_t fragmentID() const
Fragment ID of the Fragment, from the Fragment header.
RawDataType * headerAddress()
Gets the address of the header.
version_t version() const
Version of the Fragment, from the Fragment header.