artdaq_core  v3_06_01
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); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
13 
14  BOOST_REQUIRE_EQUAL(f.dataSize(), 1);
15  BOOST_REQUIRE_EQUAL(cf->block_count(), 0);
17  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
18  BOOST_REQUIRE_EQUAL(*reinterpret_cast<const size_t*>(cf->dataBegin()), artdaq::ContainerFragment::CONTAINER_MAGIC); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
19 }
20 
21 BOOST_AUTO_TEST_CASE(AddEmptyFragment)
22 {
23  auto frag = new artdaq::Fragment();
24  frag->setSequenceID(0);
25  frag->setSystemType(artdaq::Fragment::EmptyFragmentType);
26 
27  artdaq::Fragment f(0);
29  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
30  cfl.addFragment(*frag);
31 
32  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), sizeof(artdaq::detail::RawFragmentHeader) + (2 * sizeof(size_t)));
33  BOOST_REQUIRE_EQUAL(cf->block_count(), 1);
35  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
36 
37  delete frag;
38 }
39 
40 BOOST_AUTO_TEST_CASE(AddFragment_Ptr)
41 {
42  std::vector<artdaq::Fragment::value_type> fakeData{1, 2, 3, 4};
45  0,
46  fakeData.begin(),
47  fakeData.end()));
48  tmpFrag->setUserType(artdaq::Fragment::FirstUserFragmentType);
49 
50  artdaq::Fragment f(0);
51  f.setSequenceID(1);
53  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
54  cfl.addFragment(tmpFrag);
55 
56  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + (2 * sizeof(size_t)));
57  BOOST_REQUIRE_EQUAL(f.sizeBytes(), 2 * sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + (2 * sizeof(size_t)) + sizeof(artdaq::ContainerFragment::Metadata));
58  BOOST_REQUIRE_EQUAL(cf->block_count(), 1);
60  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
61  BOOST_REQUIRE_EQUAL(cf->fragSize(0), tmpFrag->sizeBytes());
62 
63  auto outfrag = cf->at(0);
64  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
65  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 0);
66  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
67  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 1);
68  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 2);
69 }
70 
71 BOOST_AUTO_TEST_CASE(AddFragment_Ref)
72 {
73  std::vector<artdaq::Fragment::value_type> fakeData{1, 2, 3, 4};
76  0,
77  fakeData.begin(),
78  fakeData.end()));
79  tmpFrag->setUserType(artdaq::Fragment::FirstUserFragmentType);
80  auto frag = *tmpFrag.get();
81 
82  artdaq::Fragment f(0);
83  f.setSequenceID(1);
85  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
86  cfl.addFragment(frag);
87 
88  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + (2 * sizeof(size_t)));
89  BOOST_REQUIRE_EQUAL(f.sizeBytes(), 2 * sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + (2 * sizeof(size_t)) + sizeof(artdaq::ContainerFragment::Metadata));
90  BOOST_REQUIRE_EQUAL(cf->block_count(), 1);
92  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
93  BOOST_REQUIRE_EQUAL(cf->fragSize(0), tmpFrag->sizeBytes());
94 
95  auto outfrag = cf->at(0);
96  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
97  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 0);
98  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
99  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 1);
100  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 2);
101 }
102 
103 BOOST_AUTO_TEST_CASE(AddFragments)
104 {
105  std::vector<artdaq::Fragment::value_type> fakeData1{1, 2, 3, 4};
106  std::vector<artdaq::Fragment::value_type> fakeData2{5, 6, 7, 8};
108  tmpFrag1(artdaq::Fragment::dataFrag(1,
109  0,
110  fakeData1.begin(),
111  fakeData1.end()));
112  tmpFrag1->setUserType(artdaq::Fragment::FirstUserFragmentType);
114  tmpFrag2(artdaq::Fragment::dataFrag(1,
115  1,
116  fakeData2.begin(),
117  fakeData2.end()));
118  tmpFrag2->setUserType(artdaq::Fragment::FirstUserFragmentType);
119  artdaq::FragmentPtrs frags;
120  frags.push_back(std::move(tmpFrag1));
121  frags.push_back(std::move(tmpFrag2));
122 
123  artdaq::Fragment f(0);
124  f.setSequenceID(1);
126  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
127  cfl.addFragments(frags);
128 
129  BOOST_REQUIRE_EQUAL(f.dataSizeBytes(), 2 * (sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + sizeof(size_t)) + sizeof(size_t));
130  BOOST_REQUIRE_EQUAL(f.sizeBytes(), 2 * (sizeof(artdaq::detail::RawFragmentHeader) + 4 * sizeof(artdaq::Fragment::value_type) + sizeof(size_t)) + sizeof(size_t) + sizeof(artdaq::detail::RawFragmentHeader) + sizeof(artdaq::ContainerFragment::Metadata));
131  BOOST_REQUIRE_EQUAL(cf->block_count(), 2);
133  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
134 
135  auto outfrag = cf->at(0);
136  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
137  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 0);
138  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
139  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 1);
140  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 2);
141  outfrag = cf->at(1);
142  BOOST_REQUIRE_EQUAL(outfrag->sequenceID(), 1);
143  BOOST_REQUIRE_EQUAL(outfrag->fragmentID(), 1);
144  BOOST_REQUIRE_EQUAL(outfrag->dataSize(), 4);
145  BOOST_REQUIRE_EQUAL(*outfrag->dataBegin(), 5);
146  BOOST_REQUIRE_EQUAL(*(outfrag->dataBegin() + 1), 6);
147 }
148 
149 #define PERF_TEST_FRAGMENT_COUNT 1000
150 BOOST_AUTO_TEST_CASE(Performance)
151 {
152  artdaq::FragmentPtrs frags;
153  std::vector<artdaq::Fragment::value_type> fakeData1{1, 2, 3, 4};
154  for (int ii = 0; ii < PERF_TEST_FRAGMENT_COUNT; ++ii)
155  {
157  tmpFrag1(artdaq::Fragment::dataFrag(1,
158  ii,
159  fakeData1.begin(),
160  fakeData1.end()));
161  tmpFrag1->setUserType(artdaq::Fragment::FirstUserFragmentType);
162  frags.push_back(std::move(tmpFrag1));
163  }
164 
165  // Test individual adds
166  artdaq::Fragment f(0);
167  f.setSequenceID(1);
169  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
170  auto start_time = std::chrono::steady_clock::now();
171  for (auto& it : frags)
172  {
173  cfl.addFragment(it);
174  }
175  auto end_time = std::chrono::steady_clock::now();
176 
177  BOOST_REQUIRE_EQUAL(cf->block_count(), PERF_TEST_FRAGMENT_COUNT);
179  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
180  TLOG(TLVL_INFO, "ContainerFragment_t") << "Adding " << PERF_TEST_FRAGMENT_COUNT << " Fragments individually took " << artdaq::TimeUtils::GetElapsedTimeMicroseconds(start_time, end_time) << " us";
181 
182  // Test group add
183  artdaq::Fragment f2(0);
184  f2.setSequenceID(1);
186  cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl2); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
187 
188  start_time = std::chrono::steady_clock::now();
189  cfl2.addFragments(frags);
190  end_time = std::chrono::steady_clock::now();
191 
192  BOOST_REQUIRE_EQUAL(cf->block_count(), PERF_TEST_FRAGMENT_COUNT);
193  BOOST_REQUIRE_EQUAL(cf->fragment_type(), type);
194  TLOG(TLVL_INFO, "ContainerFragment_t") << "Adding " << PERF_TEST_FRAGMENT_COUNT << " Fragments in a group took " << artdaq::TimeUtils::GetElapsedTimeMicroseconds(start_time, end_time) << " us";
195 }
196 
197 BOOST_AUTO_TEST_CASE(Exceptions)
198 {
199  artdaq::Fragment f(0);
200  f.setSequenceID(1);
202  auto cf = reinterpret_cast<artdaq::ContainerFragment*>(&cfl); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
203 
204  // Attempting to access a fragment which is not in the container is an exception
205  BOOST_REQUIRE_EXCEPTION(cf->at(0), cet::exception, [&](cet::exception e) { return e.category() == "ArgumentOutOfRange"; });
206  BOOST_REQUIRE_EXCEPTION(cf->fragSize(0), cet::exception, [&](cet::exception e) { return e.category() == "ArgumentOutOfRange"; });
207  BOOST_REQUIRE_EXCEPTION(cf->fragmentIndex(1), cet::exception, [&](cet::exception e) { return e.category() == "ArgumentOutOfRange"; });
208 
209  // Using an already-initialized Fragment to init a Container Fragment is an exception
210  artdaq::Fragment f2(2);
211  f2.setSequenceID(2);
212  f2.setFragmentID(0);
214  BOOST_REQUIRE_EXCEPTION(artdaq::ContainerFragmentLoader cfl2(f2), cet::exception, [&](cet::exception e) { return e.category() == "InvalidFragment"; });
215 
217  //for (int ii = 0; ii < artdaq::CONTAINER_FRAGMENT_COUNT_MAX; ++ii)
218  //{
219  // cfl.addFragment(f2);
220  //}
221  //BOOST_REQUIRE_EXCEPTION(cfl.addFragment(f2), cet::exception, [&](cet::exception e) { return e.category() == "ContainerFull"; });
222 
223  // Adding a Fragment of different type to a Container is an exception
224  artdaq::Fragment f3(0);
225  f3.setSequenceID(1);
227  cfl3.addFragment(f2);
228  f2.setSystemType(artdaq::Fragment::EmptyFragmentType);
229  BOOST_REQUIRE_EXCEPTION(cfl3.addFragment(f2), cet::exception, [&](cet::exception e) { return e.category() == "WrongFragmentType"; });
230 }
231 
232 BOOST_AUTO_TEST_SUITE_END()
std::unique_ptr< Fragment > FragmentPtr
A std::unique_ptr to a Fragment object.
Definition: Fragment.hh:54
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:155
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:711
constexpr size_t GetElapsedTimeMicroseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Gets the number of microseconds in the given time interval
Definition: TimeUtils.hh:41
static constexpr size_t CONTAINER_MAGIC
Marker word used in index.
QuickVec< RawDataType >::value_type value_type
Alias value_type type from QuickVec&lt;RawDataType&gt;
Definition: Fragment.hh:185
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:154
std::list< FragmentPtr > FragmentPtrs
A std::list of FragmentPtrs.
Definition: Fragment.hh:59
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:85