8 #include <framework/dataobjects/EventMetaData.h>
9 #include <framework/dataobjects/ProfileInfo.h>
10 #include <framework/datastore/StoreArray.h>
11 #include <framework/datastore/StoreObjPtr.h>
12 #include <framework/datastore/RelationsObject.h>
13 #include <framework/utilities/TestHelpers.h>
15 #include <gtest/gtest.h>
24 class DataStoreTest :
public ::testing::Test {
36 DataStore::Instance().setInitializeActive(
true);
39 evtDataDifferentName.registerInDataStore();
40 evtDataEmpty.registerInDataStore();
41 evtDataDifferentDurability.registerInDataStore();
43 DataStore::Instance().setInitializeActive(
false);
50 for (
int i = 0; i < 10; ++i) {
58 evtDataDifferentName.appendNew(newObj2);
61 newobj = evtDataDifferentDurability.appendNew(30 + i);
62 ASSERT_TRUE(newobj !=
nullptr);
70 void TearDown()
override
72 DataStore::Instance().reset();
76 static void verifyContents()
80 EXPECT_EQ(evtPtr->getEvent(), (
unsigned long)42);
87 EXPECT_EQ(evtDataDifferentName.getEntries(), 10);
88 EXPECT_EQ(evtDataDifferentDurability.getEntries(), 10);
90 for (
int i = 0; i < 10; ++i) {
91 EXPECT_EQ((
int)evtData[i]->getEvent(), 10 + i);
92 EXPECT_EQ((
int)evtDataDifferentName[i]->getEvent(), 20 + i);
93 EXPECT_EQ((
int)evtDataDifferentDurability[i]->getEvent(), 30 + i);
95 EXPECT_EQ(profileInfo[i]->getVirtualMemory(), 128u);
96 EXPECT_FLOAT_EQ(profileInfo[i]->getTimeInSec(), 60.0);
102 TEST_F(DataStoreTest, EntryNames)
104 EXPECT_EQ(
"JustSomeStuff", DataStore::defaultObjectName(
"JustSomeStuff"));
105 EXPECT_EQ(
"JustSomeStuff", DataStore::defaultObjectName(
"Belle2::Foo::JustSomeStuff"));
106 EXPECT_EQ(
"JustSomeStuffs", DataStore::defaultArrayName(
"Belle2::Foo::JustSomeStuff"));
108 EXPECT_EQ(
"MyOwnName", DataStore::arrayName<TObject>(
"MyOwnName"));
109 EXPECT_EQ(
"MyOwnName", DataStore::arrayName(TObject::Class(),
"MyOwnName"));
110 EXPECT_EQ(
"TObjects", DataStore::arrayName<TObject>(
""));
111 EXPECT_EQ(
"TObjects", DataStore::arrayName(TObject::Class(),
""));
113 EXPECT_EQ(
"MyOwnName", DataStore::objectName<TObject>(
"MyOwnName"));
114 EXPECT_EQ(
"MyOwnName", DataStore::objectName(TObject::Class(),
"MyOwnName"));
116 EXPECT_EQ(
"TObject", DataStore::objectName<TObject>(
""));
117 EXPECT_EQ(
"TObject", DataStore::objectName(TObject::Class(),
""));
119 EXPECT_EQ(
"EventMetaDatas", DataStore::arrayName<EventMetaData>(
""));
120 EXPECT_EQ(
"GF2Track", DataStore::defaultObjectName(
"genfit::Track"));
122 EXPECT_EQ(
"AToB", DataStore::relationName(
"A",
"B"));
123 auto relname = DataStore::defaultRelationName<EventMetaData, ProfileInfo>();
124 EXPECT_EQ(
"EventMetaDatasToProfileInfos", relname);
127 TEST_F(DataStoreTest, GetTClass)
129 EXPECT_EQ(Belle2::EventMetaData::Class(), DataStore::getTClassFromDefaultObjectName(
"Belle2::EventMetaData"));
130 EXPECT_EQ(Belle2::EventMetaData::Class(), DataStore::getTClassFromDefaultObjectName(
"EventMetaData"));
132 EXPECT_EQ(Belle2::EventMetaData::Class(), DataStore::getTClassFromDefaultArrayName(
"Belle2::EventMetaDatas"));
133 EXPECT_EQ(Belle2::EventMetaData::Class(), DataStore::getTClassFromDefaultArrayName(
"EventMetaDatas"));
138 TEST_F(DataStoreTest, AttachTest)
144 EXPECT_TRUE(evtData);
146 EXPECT_TRUE(evtDataDifferentName);
148 EXPECT_TRUE(evtDataDifferentDurability);
150 EXPECT_TRUE(profileInfo);
154 TEST_F(DataStoreTest, TypeTest)
169 EXPECT_TRUE(emd.isValid());
170 EXPECT_EQ(std::string(
"Belle2::EventMetaData"), std::string(emd->GetName()));
174 TEST_F(DataStoreTest, MetaDataTest)
178 EXPECT_TRUE(evtPtr.
getName() ==
"EventMetaData");
179 EXPECT_FALSE(evtPtr.
isArray());
180 EXPECT_TRUE(evtPtr.
getClass() == EventMetaData::Class());
186 EXPECT_TRUE(evtData.
getName() ==
"EventMetaDatas");
187 EXPECT_TRUE(evtData.
isArray());
188 EXPECT_TRUE(evtData.
getClass() == EventMetaData::Class());
190 EXPECT_TRUE(evtData == evtData2);
191 EXPECT_FALSE(evtData != evtData2);
192 EXPECT_FALSE(evtData == evtDataDifferentName);
193 EXPECT_FALSE(evtData == evtDataDifferentDurability);
197 EXPECT_FALSE(profileInfo == evtData);
198 EXPECT_TRUE(profileInfo != evtData);
204 TEST_F(DataStoreTest, ReadOnlyAttach)
208 EXPECT_EQ(a->getEvent(), (
unsigned long)42);
222 TEST_F(DataStoreTest, VerifyContents)
229 EXPECT_TRUE(evtData.
isValid());
235 EXPECT_EQ(evtPtr->getEvent(), 1);
237 TEST_F(DataStoreTest, InvalidAccessor)
240 EXPECT_FALSE(none.isValid());
241 EXPECT_THROW(none.getPtr(), std::runtime_error);
242 EXPECT_THROW(none.appendNew(), std::runtime_error);
244 EXPECT_THROW(none.getPtr(), std::runtime_error);
245 EXPECT_EQ(0, none.getEntries());
248 EXPECT_FALSE(noobj.isValid());
249 EXPECT_THROW(*noobj, std::runtime_error);
250 EXPECT_THROW(noobj->getEvent(), std::runtime_error);
253 TEST_F(DataStoreTest, RawAccess)
256 EXPECT_TRUE(evtData.
getPtr() !=
nullptr);
260 TEST_F(DataStoreTest, ArrayConsistency)
266 EXPECT_EQ((
int)evtData.
getPtr()->GetEntries(), 10);
267 EXPECT_EQ((
int)evtDataDifferentName.getPtr()->GetEntries(), 10);
268 EXPECT_EQ((
int)evtDataDifferentDurability.getPtr()->GetEntries(), 10);
269 EXPECT_EQ((
int)profileInfo.
getPtr()->GetEntries(), 10);
273 TEST_F(DataStoreTest, RangeCheck)
276 EXPECT_TRUE(evtData[0] !=
nullptr);
277 EXPECT_THROW(evtData[-1], std::out_of_range);
278 EXPECT_THROW(evtData[10], std::out_of_range);
282 TEST_F(DataStoreTest, ClearMaps)
285 EXPECT_EQ(evtPtr->getEvent(), (
unsigned long)42);
288 DataStore::Instance().invalidateData(DataStore::c_Event);
301 EXPECT_TRUE(*a == *b);
306 EXPECT_EQ(a->getEvent(), (
unsigned long)42);
309 EXPECT_NE(a->getEvent(), (
unsigned long)42);
317 EXPECT_EQ(evtDataDifferentName.getEntries(), 0);
322 EXPECT_EQ(evtDataDifferentDurability.getEntries(), 10);
326 TEST_F(DataStoreTest, RequireObjects)
328 DataStore::Instance().setInitializeActive(
true);
344 DataStore::Instance().setInitializeActive(
false);
350 TEST_F(DataStoreTest, StoreArrayIteration)
355 for (
int i = 0; i < evtData.
getEntries(); i++) {
356 EXPECT_TRUE(evtData[i] !=
nullptr);
367 EXPECT_TRUE(it == it2);
368 EXPECT_FALSE(it != it2);
371 EXPECT_TRUE((it++) == it2);
375 EXPECT_TRUE(it != (++it2));
387 EXPECT_TRUE(ev == *it2);
388 EXPECT_TRUE(it2 != it);
389 EXPECT_TRUE(ev != *it);
395 EXPECT_TRUE(ev == *it3);
401 EXPECT_EQ((
int)evtDataNonConst[0]->getEvent(), 10);
402 EXPECT_EQ((
int)evtDataDifferentName[0]->getEvent(), 20);
404 std::swap_ranges(evtDataNonConst.
begin(), evtDataNonConst.
end(), evtDataDifferentName.begin());
405 EXPECT_EQ((
int)evtDataDifferentName[9]->getEvent(), 19);
406 EXPECT_EQ((
int)evtDataNonConst[9]->getEvent(), 29);
409 std::swap_ranges(evtDataDifferentName.begin(), evtDataDifferentName.end(), evtDataNonConst.
begin());
410 EXPECT_EQ((
int)evtDataNonConst[9]->getEvent(), 19);
411 EXPECT_EQ((
int)evtDataDifferentName[9]->getEvent(), 29);
416 EXPECT_TRUE(&(*it) == evtData[i]);
417 EXPECT_EQ((
int)it->getEvent(), 10 + i);
425 EXPECT_TRUE(&emd == evtData[i]);
433 EXPECT_TRUE(&emd == evtData[i]);
436 EXPECT_EQ(i, evtData.getEntries());
457 TEST_F(DataStoreTest, DataStoreRegistration)
460 DataStore::Instance().setInitializeActive(
true);
467 EXPECT_FALSE(evtArray.isOptional());
469 EXPECT_FALSE(evtArray.isRequired());
470 EXPECT_FALSE(evtPtr.
isValid());
471 EXPECT_FALSE(evtArray.isValid());
476 EXPECT_TRUE(evtArray.registerInDataStore(DataStore::c_DontWriteOut));
480 EXPECT_TRUE(evtArray.registerInDataStore(DataStore::c_DontWriteOut));
483 EXPECT_B2ERROR(EXPECT_FALSE(evtPtr.
registerInDataStore(DataStore::c_ErrorIfAlreadyRegistered)));
484 EXPECT_B2ERROR(EXPECT_FALSE(evtArray.registerInDataStore(DataStore::c_DontWriteOut | DataStore::c_ErrorIfAlreadyRegistered)));
488 EXPECT_TRUE(evtArray.isOptional());
490 EXPECT_TRUE(evtArray.isRequired());
491 EXPECT_FALSE(evtPtr.
isValid());
492 EXPECT_TRUE(evtArray.isValid());
495 DataStore::Instance().setInitializeActive(
false);
498 TEST_F(DataStoreTest, RegistrationOutsideOfInitializeShouldFail)
507 EXPECT_FALSE(someothernewname.isValid());
510 TEST_F(DataStoreTest, ConstructedBeforeInitializeButWithNonDefaultName)
518 DataStore::Instance().setInitializeActive(
true);
520 EXPECT_TRUE(events.registerInDataStore(
"ThisBeInterestingNameForEvents"));
521 EXPECT_TRUE(profile.registerInDataStore(
"MyProfileInfoName", DataStore::c_DontWriteOut));
524 EXPECT_TRUE(eventsMetaDatas2.
isOptional(
"EventMetaDatas_2"));
525 EXPECT_TRUE(eventsMetaDatas2.
isRequired(
"EventMetaDatas_2"));
527 DataStore::Instance().setInitializeActive(
false);
530 EXPECT_EQ(
"ThisBeInterestingNameForEvents", events.getName());
531 EXPECT_TRUE(profile.getName() ==
"MyProfileInfoName");
532 EXPECT_TRUE(profile.notWrittenOut());
533 EXPECT_TRUE(eventsMetaDatas2.
getName() ==
"EventMetaDatas_2");
536 EXPECT_EQ(0, events.getEntries());
537 EXPECT_FALSE(profile.isValid());
542 EXPECT_TRUE(profile.isValid());
544 EXPECT_TRUE(profileAttachAgain.isValid());
547 EXPECT_EQ(1, events.getEntries());
549 EXPECT_EQ(1, eventsAttachAgain.getEntries());
552 TEST_F(DataStoreTest, ArrayList)
555 std::vector<std::string> exparrayList = {
"Empty",
"EventMetaDatas",
"EventMetaDatas_2"};
556 EXPECT_EQ(exparrayList, arrayList);
559 TEST_F(DataStoreTest, ReplaceData)
565 DataStore::Instance().setInitializeActive(
true);
566 evtPtrB.registerInDataStore(DataStore::c_DontWriteOut);
567 evtDataB.registerInDataStore(DataStore::c_DontWriteOut);
568 DataStore::Instance().setInitializeActive(
false);
572 EXPECT_EQ(42, evtPtr->getEvent());
573 EXPECT_FALSE(evtPtrB.isValid());
575 EXPECT_TRUE(evtPtrB.notWrittenOut());
577 DataStore::Instance().replaceData(evtPtr, evtPtrB);
578 EXPECT_EQ(42, evtPtrB->getEvent());
579 EXPECT_FALSE(evtPtr.
isValid());
583 EXPECT_TRUE(evtPtrB.notWrittenOut());
586 DataStore::Instance().replaceData(evtPtr, evtPtrB);
587 EXPECT_FALSE(evtPtr.
isValid());
588 EXPECT_FALSE(evtPtrB.isValid());
591 DataStore::Instance().replaceData(evtPtr, evtPtrB);
592 EXPECT_FALSE(evtPtr.
isValid());
593 EXPECT_FALSE(evtPtrB.isValid());
599 EXPECT_EQ(0, evtDataB.getEntries());
601 DataStore::Instance().replaceData(evtData, evtDataB);
603 EXPECT_EQ(10, evtDataB.getEntries());
605 DataStore::Instance().replaceData(evtData, evtDataB);
607 EXPECT_EQ(0, evtDataB.getEntries());
609 DataStore::Instance().replaceData(evtData, evtDataB);
611 EXPECT_EQ(0, evtDataB.getEntries());
615 TEST_F(DataStoreTest, SwitchDataStore)
617 EXPECT_TRUE(
"" == DataStore::Instance().currentID());
619 EXPECT_THROW(DataStore::Instance().copyContentsTo(
"foo"), std::out_of_range);
622 DataStore::Instance().createNewDataStoreID(
"foo");
623 EXPECT_TRUE(
"" == DataStore::Instance().currentID());
624 DataStore::Instance().copyContentsTo(
"foo");
625 EXPECT_TRUE(
"" == DataStore::Instance().currentID());
627 DataStore::Instance().switchID(
"foo");
628 EXPECT_TRUE(
"foo" == DataStore::Instance().currentID());
633 a->setEvent(1234567);
636 DataStore::Instance().switchID(
"");
637 EXPECT_TRUE(
"" == DataStore::Instance().currentID());
643 DataStore::Instance().setInitializeActive(
true);
644 evtDataB.registerInDataStore();
645 DataStore::Instance().copyContentsTo(
"foo");
648 TEST_F(DataStoreTest, FindStoreEntry)
657 EXPECT_TRUE(DataStore::Instance().findStoreEntry(evtData[5], entry, index));
662 EXPECT_TRUE(DataStore::Instance().findStoreEntry(evtData[5], entry, index));
667 EXPECT_TRUE(DataStore::Instance().findStoreEntry(evtDataDifferentName[7], entry, index));
669 EXPECT_EQ(entry->
name, evtDataDifferentName.getName());
671 entry =
nullptr; index = -1;
672 EXPECT_TRUE(DataStore::Instance().findStoreEntry(evtDataDifferentName[7], entry, index));
674 EXPECT_EQ(entry->
name, evtDataDifferentName.getName());
676 entry =
nullptr; index = -1;
678 EXPECT_FALSE(DataStore::Instance().findStoreEntry(&localObj, entry, index));
679 EXPECT_EQ(index, -1);
680 EXPECT_EQ(entry,
nullptr);
686 DataStore::Instance().setInitializeActive(
true);
688 relObjs2.registerInDataStore();
689 DataStore::Instance().setInitializeActive(
false);
690 for (
int i = 0; i < 6; i++) {
692 relObjs2.appendNew();
695 entry =
nullptr; index = -1;
696 EXPECT_TRUE(DataStore::Instance().findStoreEntry(relObjs[5], entry, index));
701 entry =
nullptr; index = -1;
702 EXPECT_TRUE(DataStore::Instance().findStoreEntry(relObjs[5], entry, index));
706 entry =
nullptr; index = -1;
707 EXPECT_TRUE(DataStore::Instance().findStoreEntry(relObjs2[2], entry, index));
709 EXPECT_EQ(entry->
name, relObjs2.getName());
712 entry =
nullptr; index = -1;
713 EXPECT_TRUE(DataStore::Instance().findStoreEntry(relObjs2[2], entry, index));
715 EXPECT_EQ(entry->
name, relObjs2.getName());
719 entry =
nullptr; index = -1;
720 EXPECT_FALSE(DataStore::Instance().findStoreEntry(&(*evtPtr), entry, index));
721 EXPECT_EQ(index, -1);
722 EXPECT_EQ(entry,
nullptr);
725 EXPECT_FALSE(DataStore::Instance().findStoreEntry(
nullptr, entry, index));
728 TEST_F(DataStoreTest, ListEntries)
731 EXPECT_EQ(0, DataStore::Instance().getListOfRelatedArrays(evtData).size());
733 DataStore::Instance().setInitializeActive(
true);
736 DataStore::Instance().setInitializeActive(
false);
738 EXPECT_TRUE(DataStore::Instance().hasRelation(evtData, profileInfo, DataStore::c_Event,
""));
739 EXPECT_FALSE(DataStore::Instance().hasRelation(profileInfo, evtData, DataStore::c_Event,
""));
740 EXPECT_FALSE(DataStore::Instance().hasRelation(evtData, profileInfo, DataStore::c_Event,
"SOMENONSENSE"));
741 EXPECT_FALSE(DataStore::Instance().hasRelation(evtData, profileInfo, DataStore::c_Persistent,
""));
742 EXPECT_FALSE(DataStore::Instance().hasRelation(profileInfo, evtData, DataStore::c_Persistent,
""));
744 EXPECT_EQ(1, DataStore::Instance().getListOfRelatedArrays(evtData).size());
745 EXPECT_EQ(1, DataStore::Instance().getListOfRelatedArrays(profileInfo).size());
747 EXPECT_EQ(1, DataStore::Instance().getListOfArrays(ProfileInfo::Class(), DataStore::c_Event).size());
748 EXPECT_EQ(0, DataStore::Instance().getListOfArrays(ProfileInfo::Class(), DataStore::c_Persistent).size());
749 EXPECT_EQ(1, DataStore::Instance().getListOfArrays(EventMetaData::Class(), DataStore::c_Persistent).size());
750 EXPECT_EQ(3, DataStore::Instance().getListOfArrays(EventMetaData::Class(), DataStore::c_Event).size());
752 EXPECT_EQ(1, DataStore::Instance().getListOfArrays(TObject::Class(), DataStore::c_Persistent).size());
753 EXPECT_EQ(4, DataStore::Instance().getListOfArrays(TObject::Class(), DataStore::c_Event).size());
755 EXPECT_EQ(1, DataStore::Instance().getListOfObjects(EventMetaData::Class(), DataStore::c_Event).size());
756 EXPECT_EQ(0, DataStore::Instance().getListOfObjects(EventMetaData::Class(), DataStore::c_Persistent).size());
757 EXPECT_EQ(1, DataStore::Instance().getListOfObjects(TObject::Class(), DataStore::c_Event).size());
758 EXPECT_EQ(0, DataStore::Instance().getListOfObjects(TObject::Class(), DataStore::c_Persistent).size());
761 TEST_F(DataStoreTest, Assign)
767 EXPECT_TRUE(evtData.
isValid());
775 EXPECT_EQ(evtPtr->getEvent(), 42);
776 EXPECT_TRUE(evtPtr.
assign(newobj,
true));
777 EXPECT_EQ(evtPtr->getEvent(), 123);
Optimizes class to iterate over TObjArray and classes inheriting from it.
Store execution time and memory usage.
DataStore::EDurability getDurability() const
Return durability with which the object is saved in the DataStore.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool notWrittenOut() const
Returns true if this object/array should not be saved by output modules.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
virtual void clear()
Clear contents of this object.
bool assign(TObject *object, bool replace=false)
Assign 'object' to this accessor.
TClass * getClass() const
The underlying object's type.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
bool create(bool replace=false)
Create a default object in the data store.
bool isArray() const
Is this an accessor for an array?
Accessor to arrays stored in the data store.
T * appendNew()
Construct a new T object at the end of the array.
bool isValid() const
Check wether the array was registered.
int getEntries() const
Get the number of objects in the array.
iterator end()
Return iterator to last entry +1.
iterator begin()
Return iterator to first entry.
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
void clear() override
Delete all entries in this array.
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Type-safe access to single objects in the data store.
bool isValid() const
Check whether the object was created.
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
bool isValid(EForwardBackward eForwardBackward)
Check whether the given enum instance is one of the valid values.
Abstract base class for different kinds of events.
Wraps a stored array/object, stored under unique (name, durability) key.
std::string name
Name of the entry.