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);
71 std::vector<artdaq::RawDataType> d{1, 2, 3};
73 BOOST_REQUIRE_EQUAL(f4->dataSize(), (size_t)3);
79 BOOST_REQUIRE_EQUAL(f4->hasMetadata(),
false);
101 TLOG(TLVL_INFO) <<
"Example Fragment: " << f1;
105 BOOST_AUTO_TEST_CASE(FragmentType)
110 BOOST_REQUIRE_THROW(frag.setUserType(0), cet::exception);
111 BOOST_REQUIRE_THROW(frag.setUserType(225), cet::exception);
112 BOOST_REQUIRE_THROW(frag.setUserType(255), cet::exception);
122 frag.setUserType(100);
123 frag.setUserType(200);
124 frag.setUserType(224);
127 BOOST_REQUIRE_THROW(frag.setSystemType(0), cet::exception);
128 BOOST_REQUIRE_THROW(frag.setSystemType(1), cet::exception);
129 BOOST_REQUIRE_THROW(frag.setSystemType(224), cet::exception);
136 frag.setSystemType(225);
137 frag.setSystemType(230);
138 frag.setSystemType(240);
139 frag.setSystemType(250);
140 frag.setSystemType(255);
143 BOOST_REQUIRE(map.size() > 0);
145 BOOST_REQUIRE(map.size() > 0);
148 BOOST_AUTO_TEST_CASE(SequenceID)
152 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)0);
154 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)1);
156 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)0xffff);
158 BOOST_REQUIRE_EQUAL(f1.
sequenceID(), (uint64_t)0x0000ffffffffffff);
161 BOOST_REQUIRE_EQUAL(f2.sequenceID(), (uint64_t)0x12345);
164 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint64_t)0x0000567812345678);
167 BOOST_AUTO_TEST_CASE(FragmentID)
171 BOOST_REQUIRE_EQUAL(f1.
fragmentID(), (uint16_t)0);
173 BOOST_REQUIRE_EQUAL(f1.
fragmentID(), (uint16_t)1);
175 BOOST_REQUIRE_EQUAL(f1.
fragmentID(), (uint16_t)0xffff);
178 BOOST_REQUIRE_EQUAL(f2.fragmentID(), (uint16_t)0xab);
181 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xffff);
184 BOOST_AUTO_TEST_CASE(Resize)
189 BOOST_REQUIRE_EQUAL(f1.
dataSize(), (size_t)1234);
190 BOOST_REQUIRE_EQUAL(f1.
size(), (size_t)1234 +
197 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)129);
198 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)129 + 2 +
202 BOOST_AUTO_TEST_CASE(Empty)
205 BOOST_REQUIRE_EQUAL(f1.
empty(),
true);
207 BOOST_REQUIRE_EQUAL(f1.
empty(),
false);
211 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
213 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
215 BOOST_REQUIRE_EQUAL(f2.empty(),
true);
216 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)0);
217 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)2 +
221 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
223 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
226 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
227 f4.setMetadata(mdOneA);
228 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
231 BOOST_AUTO_TEST_CASE(Clear)
234 BOOST_REQUIRE_EQUAL(f1.
empty(),
true);
236 BOOST_REQUIRE_EQUAL(f1.
empty(),
false);
238 BOOST_REQUIRE_EQUAL(f1.
empty(),
true);
242 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
244 BOOST_REQUIRE_EQUAL(f2.empty(),
false);
246 BOOST_REQUIRE_EQUAL(f2.empty(),
true);
247 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)0);
248 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)2 +
252 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
253 BOOST_REQUIRE_EQUAL(f3.hasMetadata(),
false);
255 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
256 BOOST_REQUIRE_EQUAL(f3.hasMetadata(),
true);
258 BOOST_REQUIRE_EQUAL(f3.empty(),
true);
259 BOOST_REQUIRE_EQUAL(f3.hasMetadata(),
true);
262 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
263 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
false);
264 f4.setMetadata(mdOneA);
265 BOOST_REQUIRE_EQUAL(f4.empty(),
false);
266 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
true);
268 BOOST_REQUIRE_EQUAL(f4.empty(),
true);
269 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
true);
272 BOOST_AUTO_TEST_CASE(Addresses)
276 BOOST_REQUIRE_EQUAL(f1.
dataSize(), (size_t)200);
277 BOOST_REQUIRE_EQUAL(f1.
size(), (size_t)200 +
281 BOOST_REQUIRE_EQUAL(daddr,
286 BOOST_REQUIRE_EQUAL(haddr, &(*(f1.
headerBegin())));
287 BOOST_REQUIRE_EQUAL(daddr, &(*(f1.
dataBegin())));
288 BOOST_REQUIRE_EQUAL(daddr + 200, &(*(f1.
dataEnd())));
292 BOOST_REQUIRE_EQUAL(cf1.dataSize(), (size_t)200);
293 BOOST_REQUIRE_EQUAL(cf1.size(), (size_t)200 +
297 BOOST_REQUIRE_EQUAL(cdaddr,
300 BOOST_REQUIRE_EQUAL(cdaddr + 200, &(*(cf1.dataEnd())));
305 BOOST_REQUIRE_EQUAL(f2.dataSize(), (size_t)135);
306 BOOST_REQUIRE_EQUAL(f2.size(), (size_t)135 + 2 +
308 haddr = f2.headerAddress();
309 daddr = f2.dataAddress();
311 BOOST_REQUIRE_EQUAL(maddr, haddr +
313 BOOST_REQUIRE_EQUAL(daddr, maddr + 2);
314 BOOST_REQUIRE_EQUAL(haddr, &(*(f2.headerBegin())));
315 BOOST_REQUIRE_EQUAL(daddr, &(*(f2.dataBegin())));
316 BOOST_REQUIRE_EQUAL(daddr + 135, &(*(f2.dataEnd())));
321 BOOST_REQUIRE_EQUAL(f3.dataSize(), (size_t)77);
322 BOOST_REQUIRE_EQUAL(f3.size(), (size_t)77 + 4 +
324 haddr = f3.headerAddress();
325 daddr = f3.dataAddress();
326 maddr = f3.metadataAddress();
327 BOOST_REQUIRE_EQUAL(maddr, haddr +
329 BOOST_REQUIRE_EQUAL(daddr, maddr + 4);
330 BOOST_REQUIRE_EQUAL(haddr, &(*(f3.headerBegin())));
331 BOOST_REQUIRE_EQUAL(daddr, &(*(f3.dataBegin())));
332 BOOST_REQUIRE_EQUAL(daddr + 77, &(*(f3.dataEnd())));
335 BOOST_AUTO_TEST_CASE(Metadata)
351 BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t)5);
352 BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)10);
353 BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)15);
356 BOOST_REQUIRE_THROW(f1.
setMetadata(mdOneB), cet::exception);
371 BOOST_REQUIRE_EQUAL(mdTwoPtr->field1, (uint64_t)10);
372 BOOST_REQUIRE_EQUAL(mdTwoPtr->field2, (uint32_t)20);
373 BOOST_REQUIRE_EQUAL(mdTwoPtr->field3, (uint32_t)30);
374 BOOST_REQUIRE_EQUAL(mdTwoPtr->field4, (uint32_t)40);
375 BOOST_REQUIRE_EQUAL(mdTwoPtr->field5, (uint32_t)50);
378 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
379 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
380 BOOST_REQUIRE_EQUAL(f3.type(), (uint16_t)123);
382 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
383 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
384 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123);
386 dataPtr[0] = 0x12345678;
388 dataPtr[2] = 0x456789ab;
389 dataPtr[3] = 0x3c3c3c3c;
390 dataPtr[4] = 0x5a5a5a5a;
391 BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678);
392 BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd);
393 BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab);
394 BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c);
395 BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a);
396 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
397 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
398 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123);
403 f3.setMetadata(mdOneC);
405 BOOST_REQUIRE_EQUAL(mdOnePtr->field1, (uint64_t)505);
406 BOOST_REQUIRE_EQUAL(mdOnePtr->field2, (uint32_t)510);
407 BOOST_REQUIRE_EQUAL(mdOnePtr->field3, (uint32_t)515);
408 BOOST_REQUIRE_EQUAL(f3.sequenceID(), (uint32_t)0xabcdef);
409 BOOST_REQUIRE_EQUAL(f3.fragmentID(), (uint16_t)0xc3a5);
410 BOOST_REQUIRE_EQUAL(f3.type(), (uint8_t)123);
411 dataPtr = f3.dataAddress();
412 dataPtr[0] = 0x12345678;
414 dataPtr[2] = 0x456789ab;
415 dataPtr[3] = 0x3c3c3c3c;
416 dataPtr[4] = 0x5a5a5a5a;
417 BOOST_REQUIRE_EQUAL(dataPtr[0], (uint64_t)0x12345678);
418 BOOST_REQUIRE_EQUAL(dataPtr[1], (uint64_t)0xabcd);
419 BOOST_REQUIRE_EQUAL(dataPtr[2], (uint64_t)0x456789ab);
420 BOOST_REQUIRE_EQUAL(dataPtr[3], (uint64_t)0x3c3c3c3c);
421 BOOST_REQUIRE_EQUAL(dataPtr[4], (uint64_t)0x5a5a5a5a);
425 BOOST_REQUIRE_EQUAL(f4.hasMetadata(),
false);
427 BOOST_REQUIRE_THROW(f4.setMetadata(mdHuge), cet::exception);
429 BOOST_REQUIRE_THROW(
artdaq::Fragment f5(127, 1, 2, 3, mdHuge), cet::exception);
435 BOOST_AUTO_TEST_CASE(Bytes)
437 std::size_t payload_size = 5;
454 Metadata theMetadata;
470 BOOST_REQUIRE(f1->
size() == f1_factory->size());
471 BOOST_REQUIRE(f1->
sizeBytes() == f1_factory->sizeBytes());
473 std::unique_ptr<artdaq::Fragment> f2(
new artdaq::Fragment(payload_size, seqID, fragID, type, theMetadata));
479 BOOST_REQUIRE(f2->size() == f2_factory->size());
480 BOOST_REQUIRE(f2->sizeBytes() == f2_factory->sizeBytes());
485 std::size_t offset = 3;
491 BOOST_REQUIRE(f3_factory->size() == f2->size());
492 BOOST_REQUIRE(f3_factory->sizeBytes() == f2->sizeBytes());
499 &*f3_factory->headerBegin());
500 BOOST_REQUIRE_EQUAL(&*f3_factory->headerBeginBytes(), hdrptr);
503 &*f3_factory->dataBegin());
509 BOOST_REQUIRE_EQUAL(ptr1, ptr2);
510 BOOST_REQUIRE_EQUAL(ptr2, ptr3);
513 &*f3_factory->dataEnd());
514 BOOST_REQUIRE_EQUAL(&*f3_factory->dataEndBytes(), dataEndPtr);
519 BOOST_REQUIRE_EQUAL(&*f3_copy.headerBeginBytes(), chdrptr);
521 &*f3_copy.dataBegin());
525 BOOST_REQUIRE_EQUAL(cptr1, cptr2);
528 &*f3_copy.dataEnd());
529 BOOST_REQUIRE_EQUAL(&*f3_copy.dataEndBytes(), cdataEndPtr);
533 std::size_t
const metadata_size =
535 BOOST_REQUIRE(metadata_size ==
539 BOOST_REQUIRE(static_cast<std::size_t>(f3_factory->dataEndBytes() - f3_factory->dataBeginBytes()) == f3_factory->dataSizeBytes());
543 BOOST_REQUIRE_EQUAL(f4.dataSize(), payload_size);
545 f4.resize(payload_size + 1);
546 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 1));
547 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 1) *
sizeof(
artdaq::RawDataType)));
548 f4.resizeBytes(f4.dataSizeBytes() + 2);
549 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 2));
550 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 2) *
sizeof(
artdaq::RawDataType)));
551 f4.resizeBytes(f4.dataSizeBytes() + 1);
552 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 3));
553 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 3) *
sizeof(
artdaq::RawDataType)));
554 f4.resizeBytes(f4.dataSizeBytes() + 1);
555 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 4));
556 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 4) *
sizeof(
artdaq::RawDataType)));
560 f4.resizeBytes(targetSize);
561 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
562 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
564 f4.resizeBytes(targetSize);
565 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
566 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
568 f4.resizeBytes(targetSize);
569 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
570 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
572 f4.resizeBytes(targetSize);
573 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
574 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
576 f4.resizeBytes(targetSize);
577 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
578 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
580 f4.resizeBytes(targetSize);
581 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
582 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
584 f4.resizeBytes(targetSize);
585 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
586 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
588 f4.resizeBytes(targetSize);
589 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 5));
590 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 5) *
sizeof(
artdaq::RawDataType)));
592 f4.resizeBytes(targetSize);
593 BOOST_REQUIRE_EQUAL(f4.dataSize(), (payload_size + 6));
594 BOOST_REQUIRE_EQUAL(f4.dataSizeBytes(), ((payload_size + 6) *
sizeof(
artdaq::RawDataType)));
600 f5.setMetadata(theMetadata);
601 BOOST_REQUIRE_EQUAL(f5.dataSize(), payload_size);
607 BOOST_AUTO_TEST_CASE(Upgrade_V0)
624 memcpy(f.headerBeginBytes(), &hdr0,
sizeof(hdr0));
629 memcpy(f.headerBegin() + ii, &(++counter),
sizeof(counter));
632 BOOST_REQUIRE_EQUAL(f.version(), 0);
633 BOOST_REQUIRE_EQUAL(f.type(), 0xFE);
634 BOOST_REQUIRE_EQUAL(f.hasMetadata(),
false);
636 BOOST_REQUIRE_EQUAL(f.sequenceID(), 0xFEEDDEADBEEF);
637 BOOST_REQUIRE_EQUAL(f.fragmentID(), 0xBEE7);
638 BOOST_REQUIRE_EQUAL(f.timestamp(), 0xCAFEFECA);
640 for (
size_t jj = 0; jj < f.dataSize(); ++jj)
642 BOOST_REQUIRE_EQUAL(*(f.dataBegin() + jj), jj + 1);
646 BOOST_AUTO_TEST_CASE(Upgrade_V1)
660 memcpy(f.headerBeginBytes(), &hdr1,
sizeof(hdr1));
665 memcpy(f.headerBegin() + ii, &(++counter),
sizeof(counter));
668 BOOST_REQUIRE_EQUAL(f.version(), 1);
669 BOOST_REQUIRE_EQUAL(f.type(), 0xFE);
670 BOOST_REQUIRE_EQUAL(f.hasMetadata(),
false);
672 BOOST_REQUIRE_EQUAL(f.sequenceID(), 0xFEEDDEADBEEF);
673 BOOST_REQUIRE_EQUAL(f.fragmentID(), 0xBEE7);
674 BOOST_REQUIRE_EQUAL(f.timestamp(), 0xCAFEFECAAAAABBBB);
676 for (
size_t jj = 0; jj < f.dataSize(); ++jj)
678 BOOST_REQUIRE_EQUAL(*(f.dataBegin() + jj), jj + 1);
682 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
static FragmentPtr dataFrag(sequence_id_t sequenceID, fragment_id_t fragID, InputIterator i, InputIterator e)
Creates a Fragment, copying data from given location. 12-Apr-2013, KAB - this method is deprecated...
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...
std::string typeString() const
Print the type of the Fragment.
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.