artdaq_core  v3_01_05
ContainerFragment_t.cc
1 #include "artdaq-core/Data/ContainerFragmentLoader.hh"
2 
3 #define BOOST_TEST_MODULE(ContainerFragment_t)
4 #include "cetlib/quiet_unit_test.hpp"
5 
6 BOOST_AUTO_TEST_SUITE(ContainerFragment_test)
7 
8 BOOST_AUTO_TEST_CASE(Construct)
9 {
10  artdaq::Fragment f(0);
12  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl);
13 
14  BOOST_REQUIRE_EQUAL(f.dataSize(), 0);
15  BOOST_REQUIRE_EQUAL(cf->block_count(), 0);
17  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
18 }
19 
20 BOOST_AUTO_TEST_CASE(AddEmptyFragment)
21 {
22  auto frag = new artdaq::Fragment();
23  frag->setSequenceID(0);
24  frag->setSystemType(artdaq::Fragment::EmptyFragmentType);
25 
26  artdaq::Fragment f(0);
28  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl);
29  cfl.addFragment(*frag);
30 
31  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), sizeof(artdaq::detail::RawFragmentHeader));
32  BOOST_REQUIRE_EQUAL(cf->block_count(), 1);
34  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
35 }
36 
37 BOOST_AUTO_TEST_CASE(AddFragment_Ptr)
38 {
39  std::vector<artdaq::Fragment::value_type> fakeData{ 1, 2, 3, 4 };
42  0,
43  fakeData.begin(),
44  fakeData.end()));
45  tmpFrag->setUserType(artdaq::Fragment::FirstUserFragmentType);
46 
47  artdaq::Fragment f(0);
48  f.setSequenceID(1);
50  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl);
51  cfl.addFragment(tmpFrag);
52 
53  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type));
54  BOOST_REQUIRE_EQUAL(f.sizeBytes(), 2 * sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + sizeof(artdaq::ContainerFragment::Metadata));
55  BOOST_REQUIRE_EQUAL(cf->block_count(), 1);
57  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
58  BOOST_REQUIRE_EQUAL(cf->fragSize(0), tmpFrag->sizeBytes());
59 
60  auto outfrag = cf->at(0);
61  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
62  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 0);
63  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
64  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 1);
65  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 2);
66 }
67 
68 BOOST_AUTO_TEST_CASE(AddFragment_Ref)
69 {
70  std::vector<artdaq::Fragment::value_type> fakeData{ 1, 2, 3, 4 };
73  0,
74  fakeData.begin(),
75  fakeData.end()));
76  tmpFrag->setUserType(artdaq::Fragment::FirstUserFragmentType);
77  auto frag = *tmpFrag.get();
78 
79  artdaq::Fragment f(0);
80  f.setSequenceID(1);
82  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl);
83  cfl.addFragment(frag);
84 
85  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type));
86  BOOST_REQUIRE_EQUAL(f.sizeBytes(), 2 * sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + sizeof(artdaq::ContainerFragment::Metadata));
87  BOOST_REQUIRE_EQUAL(cf->block_count(), 1);
89  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
90  BOOST_REQUIRE_EQUAL(cf->fragSize(0), tmpFrag->sizeBytes());
91 
92  auto outfrag = cf->at(0);
93  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
94  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 0);
95  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
96  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 1);
97  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 2);
98 }
99 
100 
101 BOOST_AUTO_TEST_CASE(AddFragments)
102 {
103  std::vector<artdaq::Fragment::value_type> fakeData1{ 1, 2, 3, 4 };
104  std::vector<artdaq::Fragment::value_type> fakeData2{ 5, 6, 7, 8 };
106  tmpFrag1(artdaq::Fragment::dataFrag(1,
107  0,
108  fakeData1.begin(),
109  fakeData1.end()));
110  tmpFrag1->setUserType(artdaq::Fragment::FirstUserFragmentType);
112  tmpFrag2(artdaq::Fragment::dataFrag(1,
113  1,
114  fakeData2.begin(),
115  fakeData2.end()));
116  tmpFrag2->setUserType(artdaq::Fragment::FirstUserFragmentType);
117  artdaq::FragmentPtrs frags;
118  frags.push_back(std::move(tmpFrag1));
119  frags.push_back(std::move(tmpFrag2));
120 
121  artdaq::Fragment f(0);
122  f.setSequenceID(1);
124  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl);
125  cfl.addFragments(frags);
126 
127  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), 2 * (sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type)));
128  BOOST_REQUIRE_EQUAL(f.sizeBytes(), 2 * (sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type)) + sizeof(artdaq::detail::RawFragmentHeader) + sizeof(artdaq::ContainerFragment::Metadata));
129  BOOST_REQUIRE_EQUAL(cf->block_count(), 2);
131  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
132 
133  auto outfrag = cf->at(0);
134  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
135  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 0);
136  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
137  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 1);
138  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 2);
139  outfrag = cf->at(1);
140  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
141  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 1);
142  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
143  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 5);
144  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 6);
145 }
146 
147 BOOST_AUTO_TEST_CASE(Exceptions)
148 {
149  artdaq::Fragment f(0);
150  f.setSequenceID(1);
152  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl);
153 
154  // Attempting to access a fragment which is not in the container is an exception
155  BOOST_REQUIRE_EXCEPTION(cf->at(0), cet::exception, [&](cet::exception e) { return e.category() == "ArgumentOutOfRange"; });
156  BOOST_REQUIRE_EXCEPTION(cf->fragSize(0), cet::exception, [&](cet::exception e) { return e.category() == "ArgumentOutOfRange"; });
157  BOOST_REQUIRE_EXCEPTION(cf->fragmentIndex(1), cet::exception, [&](cet::exception e) { return e.category() == "ArgumentOutOfRange"; });
158 
159  // Using an already-initialized Fragment to init a Container Fragment is an exception
160  artdaq::Fragment f2(2);
161  f2.setSequenceID(2);
162  f2.setFragmentID(0);
164  BOOST_REQUIRE_EXCEPTION(artdaq::ContainerFragmentLoader cfl2(f2), cet::exception, [&](cet::exception e) { return e.category() == "InvalidFragment"; });
165 
166  // Adding a Fragment to a full Container is an exception
167  for (int ii = 0; ii < artdaq::CONTAINER_FRAGMENT_COUNT_MAX; ++ii)
168  {
169  cfl.addFragment(f2);
170  }
171  BOOST_REQUIRE_EXCEPTION(cfl.addFragment(f2), cet::exception, [&](cet::exception e) { return e.category() == "ContainerFull"; });
172 
173  // Adding a Fragment of different type to a Container is an exception
174  artdaq::Fragment f3(0);
175  f3.setSequenceID(1);
177  cfl3.addFragment(f2);
178  f2.setSystemType(artdaq::Fragment::EmptyFragmentType);
179  BOOST_REQUIRE_EXCEPTION(cfl3.addFragment(f2), cet::exception, [&](cet::exception e) { return e.category() == "WrongFragmentType"; });
180 
181 }
182 
183 BOOST_AUTO_TEST_SUITE_END()
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
Definition: Fragment.hh:53
The artdaq::ContainerFragment class represents a Fragment which contains other Fragments.
A Read-Write version of the ContainerFragment, used for filling ContainerFragment objects with other ...
static constexpr type_t EmptyFragmentType
Copy EmptyFragmentType from RawFragmentHeader.
Definition: Fragment.hh:154
The RawFragmentHeader class contains the basic fields used by artdaq for routing Fragment objects thr...
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...
Definition: Fragment.hh:669
static const int CONTAINER_FRAGMENT_COUNT_MAX
The maximum capacity of the ContainerFragment (in fragments)
QuickVec< RawDataType >::value_type value_type
Alias value_type type from QuickVec&lt;RawDataType&gt;
Definition: Fragment.hh:183
Contains the information necessary for retrieving Fragment objects from the ContainerFragment.
static constexpr type_t FirstUserFragmentType
Copy FIRST_USER_TYPE from RawFragmentHeader.
Definition: Fragment.hh:153
std::list< FragmentPtr > FragmentPtrs
A std::list of FragmentPtrs.
Definition: Fragment.hh:58
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:84