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 {
39 evtDataDifferentName.registerInDataStore();
40 evtDataEmpty.registerInDataStore();
41 evtDataDifferentDurability.registerInDataStore();
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
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)
108 EXPECT_EQ(
"MyOwnName", DataStore::arrayName<TObject>(
"MyOwnName"));
110 EXPECT_EQ(
"TObjects", DataStore::arrayName<TObject>(
""));
113 EXPECT_EQ(
"MyOwnName", DataStore::objectName<TObject>(
"MyOwnName"));
116 EXPECT_EQ(
"TObject", DataStore::objectName<TObject>(
""));
119 EXPECT_EQ(
"EventMetaDatas", DataStore::arrayName<EventMetaData>(
""));
123 auto relname = DataStore::defaultRelationName<EventMetaData, ProfileInfo>();
124 EXPECT_EQ(
"EventMetaDatasToProfileInfos", relname);
127 TEST_F(DataStoreTest, GetTClass)
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);
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)
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)
467 EXPECT_FALSE(evtArray.isOptional());
469 EXPECT_FALSE(evtArray.isRequired());
470 EXPECT_FALSE(evtPtr.
isValid());
471 EXPECT_FALSE(evtArray.isValid());
488 EXPECT_TRUE(evtArray.isOptional());
490 EXPECT_TRUE(evtArray.isRequired());
491 EXPECT_FALSE(evtPtr.
isValid());
492 EXPECT_TRUE(evtArray.isValid());
498 TEST_F(DataStoreTest, RegistrationOutsideOfInitializeShouldFail)
507 EXPECT_FALSE(someothernewname.isValid());
510 TEST_F(DataStoreTest, ConstructedBeforeInitializeButWithNonDefaultName)
524 EXPECT_TRUE(eventsMetaDatas2.
isOptional(
"EventMetaDatas_2"));
525 EXPECT_TRUE(eventsMetaDatas2.
isRequired(
"EventMetaDatas_2"));
530 EXPECT_EQ(
"ThisBeInterestingNameForEvents", events.
getName());
531 EXPECT_TRUE(profile.
getName() ==
"MyProfileInfoName");
533 EXPECT_TRUE(eventsMetaDatas2.
getName() ==
"EventMetaDatas_2");
537 EXPECT_FALSE(profile.
isValid());
542 EXPECT_TRUE(profile.
isValid());
544 EXPECT_TRUE(profileAttachAgain.isValid());
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)
572 EXPECT_EQ(42, evtPtr->getEvent());
573 EXPECT_FALSE(evtPtrB.isValid());
575 EXPECT_TRUE(evtPtrB.notWrittenOut());
578 EXPECT_EQ(42, evtPtrB->getEvent());
579 EXPECT_FALSE(evtPtr.
isValid());
583 EXPECT_TRUE(evtPtrB.notWrittenOut());
587 EXPECT_FALSE(evtPtr.
isValid());
588 EXPECT_FALSE(evtPtrB.isValid());
592 EXPECT_FALSE(evtPtr.
isValid());
593 EXPECT_FALSE(evtPtrB.isValid());
599 EXPECT_EQ(0, evtDataB.getEntries());
603 EXPECT_EQ(10, evtDataB.getEntries());
607 EXPECT_EQ(0, evtDataB.getEntries());
611 EXPECT_EQ(0, evtDataB.getEntries());
615 TEST_F(DataStoreTest, SwitchDataStore)
633 a->setEvent(1234567);
644 evtDataB.registerInDataStore();
648 TEST_F(DataStoreTest, FindStoreEntry)
669 EXPECT_EQ(entry->
name, evtDataDifferentName.getName());
671 entry =
nullptr; index = -1;
674 EXPECT_EQ(entry->
name, evtDataDifferentName.getName());
676 entry =
nullptr; index = -1;
679 EXPECT_EQ(index, -1);
680 EXPECT_EQ(entry,
nullptr);
688 relObjs2.registerInDataStore();
690 for (
int i = 0; i < 6; i++) {
692 relObjs2.appendNew();
695 entry =
nullptr; index = -1;
701 entry =
nullptr; index = -1;
706 entry =
nullptr; index = -1;
709 EXPECT_EQ(entry->
name, relObjs2.getName());
712 entry =
nullptr; index = -1;
715 EXPECT_EQ(entry->
name, relObjs2.getName());
719 entry =
nullptr; index = -1;
721 EXPECT_EQ(index, -1);
722 EXPECT_EQ(entry,
nullptr);
728 TEST_F(DataStoreTest, ListEntries)
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);
static std::string objectName(const TClass *t, const std::string &name)
Return the storage name for an object of the given TClass and name.
void createNewDataStoreID(const std::string &id)
creates new datastore with given id, copying the registered objects/arrays from the current one.
static std::string defaultObjectName()
Return the default storage name for an object of the given type.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
void switchID(const std::string &id)
switch to DataStore with given ID.
static TClass * getTClassFromDefaultArrayName(const std::string &arrayName)
Tries to deduce the TClass from a default array name, which is generally the name of the C++ class wi...
static TClass * getTClassFromDefaultObjectName(const std::string &objectName)
Tries to deduce the TClass from a default object name, which is generally the name of the C++ class.
static std::string defaultArrayName()
Return the default storage name for an array of the given type.
void copyContentsTo(const std::string &id, const std::vector< std::string > &entrylist_event={})
copy contents (actual array / object contents) of current DataStore to the DataStore with given ID.
@ c_Persistent
Object is available during entire execution time.
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
static DataStore & Instance()
Instance of singleton Store.
void setInitializeActive(bool active)
Setter for m_initializeActive.
static std::string relationName(const std::string &fromName, const std::string &toName, std::string const &namedRelation="")
Return storage name for a relation between two arrays of the given names.
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
void invalidateData(EDurability durability)
Clears all registered StoreEntry objects of a specified durability, invalidating all objects.
static std::string arrayName(const TClass *t, const std::string &name)
Return the storage name for an object of the given TClass and name.
void replaceData(const StoreAccessorBase &from, const StoreAccessorBase &to)
For two StoreAccessors of same type, move all data in 'from' into 'to', discarding previous contents ...
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.
const std::string & getName() const
Return name under 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.
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.
static std::vector< std::string > getArrayList(DataStore::EDurability durability=DataStore::c_Event)
Return list of array names with matching type.
T * appendNew()
Construct a new T object at the end of the array.
bool isValid() const
Check wether the array was registered.
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
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.
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.
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.