8#include <framework/datastore/RelationIndex.h>
9#include <framework/dataobjects/EventMetaData.h>
10#include <framework/dataobjects/ProfileInfo.h>
11#include <framework/datastore/RelationsObject.h>
12#include <framework/utilities/TestHelpers.h>
14#include <gtest/gtest.h>
21 class RelationsInternal :
public ::testing::Test {
27 evtData.registerInDataStore();
28 profileData.registerInDataStore();
29 relObjData.registerInDataStore();
32 for (
int i = 0; i < 10; ++i) {
34 profileData.appendNew();
35 relObjData.appendNew();
40 void TearDown()
override
45 void findRelationsCheckContents();
53 TEST_F(RelationsInternal, RelationCreate)
56 evtData.registerRelationTo(profileData);
60 EXPECT_FALSE(relation);
62 EXPECT_TRUE(relation.getFromAccessorParams() == evtData.getAccessorParams());
63 EXPECT_TRUE(relation.getToAccessorParams() == profileData.getAccessorParams());
66 EXPECT_TRUE(relation);
67 EXPECT_TRUE(relation.getFromAccessorParams() == evtData.getAccessorParams());
68 EXPECT_TRUE(relation.getToAccessorParams() == profileData.getAccessorParams());
72 TEST_F(RelationsInternal, RelationFind)
75 EXPECT_FALSE(evtData.optionalRelationTo(profileData));
76 EXPECT_FALSE(evtData.requireRelationTo(profileData));
88 string name = relation.getName();
90 EXPECT_EQ(
"OwnName", evtData2.
getName());
95 EXPECT_TRUE(relationAttachedUsingName.getFromAccessorParams() == evtData.getAccessorParams());
96 EXPECT_TRUE(relationAttachedUsingName.getToAccessorParams() == profileData.getAccessorParams());
97 EXPECT_TRUE(relationAttachedUsingName);
104 EXPECT_TRUE(relation2.getName() ==
"OwnNameToProfileInfos");
108 EXPECT_B2FATAL(evtData.requireRelationTo(profileData));
112 TEST_F(RelationsInternal, RelationWrongDeathTest)
116 relation1.registerInDataStore();
121 EXPECT_B2FATAL(
RelationArray(profileData, evtData,
"test").add(0, 0, 1.0));
122 EXPECT_B2FATAL(
RelationArray(profileData, evtData,
"test")[0]);
123 EXPECT_B2FATAL(
RelationArray(profileData, evtData,
"test").getModified());
127 TEST_F(RelationsInternal, RegistrationWithDefaultNames)
130 EXPECT_B2FATAL(relObjData[0]->addRelationTo(profileData[3]));
131 EXPECT_B2FATAL(
DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[1], 2.0));
134 relObjData.registerRelationTo(profileData);
135 evtData.registerRelationTo(profileData);
139 relObjData[0]->addRelationTo(profileData[4]);
147 TEST_F(RelationsInternal, RelationDefaultConstructed)
151 array.registerInDataStore();
157 EXPECT_FALSE(array.isValid());
164 TEST_F(RelationsInternal, BuildIndex)
167 evtData.registerRelationTo(profileData);
171 relation.add(0, 0, 1.0);
172 relation.add(0, 1, 2.0);
173 relation.add(0, 2, 3.0);
174 relation.consolidate();
175 EXPECT_EQ(relation.getEntries(), 1);
178 EXPECT_EQ(relIndex.
size(), 3u);
180 relation.add(1, 0, 1.0);
182 EXPECT_EQ(relIndex2.
size(), 4u);
185 EXPECT_EQ(relIndex.
size(), 4u);
209 double allweights(0);
212 allweights += e.weight;
215 EXPECT_DOUBLE_EQ(allweights, 6.0);
219 double allweights(0);
222 allweights += e.weight;
225 EXPECT_DOUBLE_EQ(allweights, 2.0);
229 double allweights(0);
230 for (
const auto& e : relIndex.
getElementsTo((profileData)[4])) {
232 allweights += e.weight;
235 EXPECT_DOUBLE_EQ(allweights, 0.0);
240 TEST_F(RelationsInternal, InconsistentIndexDeathTest)
243 evtData.registerRelationTo(profileData);
247 relation.add(0, 10, 1.0);
249 EXPECT_B2FATAL(rel_t relIndex);
251 relation.add(10, 0, 1.0);
252 EXPECT_B2FATAL(rel_t relIndex);
257 TEST_F(RelationsInternal, EmptyIndex)
260 evtData.registerRelationTo(profileData);
265 EXPECT_EQ(index.size(), 0u);
266 EXPECT_EQ(index.getFromAccessorParams().first,
"");
267 EXPECT_EQ(index.getToAccessorParams().first,
"");
271 TEST_F(RelationsInternal, WrongRelationIndexDeathTest)
277 relation.registerInDataStore();
279 eventData.registerInDataStore();
281 relation2.registerInDataStore();
286 EXPECT_B2FATAL(rel_t(evtData, profileData,
"test"));
287 EXPECT_B2FATAL(rel_t(
"test"));
290 EXPECT_B2FATAL(rel_t(eventData, profileData,
"test2"));
295 EXPECT_TRUE(rel_t(
"test2"));
299 void RelationsInternal::findRelationsCheckContents()
303 EXPECT_EQ(toRels.
size(), 3u);
305 EXPECT_DOUBLE_EQ(toRels.
weight(0), 1.0);
306 EXPECT_DOUBLE_EQ(toRels.
weight(1), 2.0);
307 EXPECT_DOUBLE_EQ(toRels.
weight(2), -3.0);
309 EXPECT_TRUE(toRels.
object(0) == (profileData)[0]);
310 EXPECT_TRUE(toRels.
object(1) == (profileData)[1]);
311 EXPECT_TRUE(toRels.
object(2) == (profileData)[2]);
323 EXPECT_EQ(fromRels.
size(), 1u);
324 EXPECT_DOUBLE_EQ(fromRels.
weight(0), -3.0);
325 EXPECT_TRUE(fromRels.
object(0) == fromObj);
326 EXPECT_TRUE(fromRels[0] == fromObj);
329 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>(fromObj).size(), 0u);
333 TEST_F(RelationsInternal, FindRelations)
336 evtData.registerRelationTo(profileData);
342 EXPECT_EQ(toRels.
size(), 0u);
345 EXPECT_EQ(fromRels.
size(), 0u);
349 EXPECT_EQ(toRels2.
size(), 0u);
351 EXPECT_EQ(fromRels2.
size(), 0u);
354 relation.add(0, 0, 1.0);
355 relation.add(0, 1, 2.0);
356 relation.add(0, 2, -3.0);
358 findRelationsCheckContents();
362 relation.consolidate();
363 findRelationsCheckContents();
367 TEST_F(RelationsInternal, AddRelations)
370 evtData.registerRelationTo(profileData);
377 findRelationsCheckContents();
381 TEST_F(RelationsInternal, GetRelationsWith)
384 evtData.registerRelationTo(profileData);
392 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>((evtData)[0]).size(), 0u);
393 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>((profileData)[3]).size(), 0u);
396 EXPECT_EQ(profRels.
size(), 3u);
397 EXPECT_EQ(profRels.
weight(0), 1.0);
400 EXPECT_EQ(eventRels.
size(), 1u);
401 EXPECT_EQ(eventRels.
weight(0), 1.0);
405 TEST_F(RelationsInternal, SearchAll)
411 profileData2.registerInDataStore();
412 evtData.registerRelationTo(profileData);
413 evtData.registerRelationTo(profileData2);
421 profileData2.appendNew();
425 findRelationsCheckContents();
430 EXPECT_EQ(toRels.
size(), 4u);
433 for (
int i = 0; i < (int)toRels.
size(); i++) {
436 EXPECT_DOUBLE_EQ(sum, 42.0 + 1 + 2 - 3);
439 EXPECT_EQ(DataStore::getRelationsWithObj<ProfileInfo>(fromObj, profileData.getName()).size(), 3u);
441 EXPECT_EQ(DataStore::getRelationsWithObj<ProfileInfo>(fromObj, profileData2.getName()).size(), 1u);
443 EXPECT_EQ(DataStore::getRelationsWithObj<ProfileInfo>(fromObj,
"DoesntExist").size(), 0u);
447 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>(toObj).size(), 1u);
449 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>(toObj,
"ALL").size(), 1u);
451 EXPECT_EQ(DataStore::getRelationsWithObj<TObject>(toObj,
"ALL").size(), 1u);
454 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>(fromObj,
"ALL").size(), 0u);
456 EXPECT_EQ(DataStore::getRelationsWithObj<TObject>(fromObj,
"ALL").size(), 4u);
461 TEST_F(RelationsInternal, ListOfRelatedArrays)
472 profileData2.registerInDataStore();
473 evtData.registerRelationTo(profileData);
474 evtData.registerRelationTo(profileData2);
485 TEST_F(RelationsInternal, StoreArray_clear_cleans_relations)
488 evtData.registerRelationTo(relObjData);
489 relObjData.registerRelationTo(profileData);
490 evtData.registerRelationTo(profileData);
500 EXPECT_EQ(DataStore::getRelationsWithObj<ProfileInfo>(relObjData[1]).size(), 0u);
501 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>(relObjData[0]).size(), 0u);
static void addRelationFromTo(const TObject *fromObject, const TObject *toObject, float weight=1.0, const std::string &namedRelation="")
Add a relation from an object in a store array to another object in a store array.
std::vector< std::string > getListOfRelatedArrays(const StoreAccessorBase &array) const
Returns a list of names of arrays which have registered relations that point to or from 'array'.
@ 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.
Store execution time and memory usage.
Low-level class to create/modify relations between StoreArrays.
Class to store relations between StoreArrays in the DataStore.
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
const Element * getFirstElementFrom(const FROM &from) const
Return a pointer to the first relation Element of the given object.
size_t size() const
Get the size of the index.
range_from getElementsFrom(const FROM *from) const
Return a range of all elements pointing from the given object.
const Element * getFirstElementTo(const TO &to) const
Return a pointer to the first relation Element of the given object.
range_to getElementsTo(const TO *to) const
Return a range of all elements pointing to the given object.
Class for type safe access to objects that are referred to in relations.
T * object(int index) const
Get object with index.
size_t size() const
Get number of relations.
float weight(int index) const
Get weight with index.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Accessor to arrays stored in the data store.
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.
bool isValid(EForwardBackward eForwardBackward)
Check whether the given enum instance is one of the valid values.
Abstract base class for different kinds of events.
const FROM * from
pointer of the element from which the relation points.
const TO * to
pointer of the element to which the relation points.
RelationElement::weight_type weight
weight of the relation.