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 {
26 DataStore::Instance().setInitializeActive(
true);
27 evtData.registerInDataStore();
28 profileData.registerInDataStore();
29 relObjData.registerInDataStore();
30 DataStore::Instance().setInitializeActive(
false);
32 for (
int i = 0; i < 10; ++i) {
34 profileData.appendNew();
35 relObjData.appendNew();
40 void TearDown()
override
42 DataStore::Instance().reset();
45 void findRelationsCheckContents();
53 TEST_F(RelationsInternal, RelationCreate)
55 DataStore::Instance().setInitializeActive(
true);
56 evtData.registerRelationTo(profileData);
57 DataStore::Instance().setInitializeActive(
false);
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)
74 DataStore::Instance().setInitializeActive(
true);
75 EXPECT_FALSE(evtData.optionalRelationTo(profileData));
76 EXPECT_FALSE(evtData.requireRelationTo(profileData));
82 DataStore::Instance().setInitializeActive(
false);
85 EXPECT_FALSE(
RelationArray(DataStore::relationName(evtData.getName(), profileData.getName()), DataStore::c_Event));
87 EXPECT_TRUE(
RelationArray(evtData, profileData,
"", DataStore::c_Event));
88 string name = relation.getName();
90 EXPECT_EQ(
"OwnName", evtData2.
getName());
93 RelationArray relationAttachedUsingName(name, DataStore::c_Event);
95 EXPECT_TRUE(relationAttachedUsingName.getFromAccessorParams() == evtData.getAccessorParams());
96 EXPECT_TRUE(relationAttachedUsingName.getToAccessorParams() == profileData.getAccessorParams());
97 EXPECT_TRUE(relationAttachedUsingName);
100 EXPECT_FALSE(
RelationArray(DataStore::relationName(evtData2.
getName(), profileData.getName()), DataStore::c_Event));
101 EXPECT_FALSE(
RelationArray(
"OwnNameToProfileInfos", DataStore::c_Event));
104 EXPECT_TRUE(relation2.getName() ==
"OwnNameToProfileInfos");
108 EXPECT_B2FATAL(evtData.requireRelationTo(profileData));
112 TEST_F(RelationsInternal, RelationWrongDeathTest)
114 DataStore::Instance().setInitializeActive(
true);
116 relation1.registerInDataStore();
117 DataStore::Instance().setInitializeActive(
false);
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));
133 DataStore::Instance().setInitializeActive(
true);
134 relObjData.registerRelationTo(profileData);
135 evtData.registerRelationTo(profileData);
136 DataStore::Instance().setInitializeActive(
false);
139 relObjData[0]->addRelationTo(profileData[4]);
140 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[3], 2.0);
147 TEST_F(RelationsInternal, RelationDefaultConstructed)
149 DataStore::Instance().setInitializeActive(
true);
151 array.registerInDataStore();
152 DataStore::Instance().setInitializeActive(
false);
155 ASSERT_TRUE(DataStore::Instance().createObject(rel,
false, array));
157 EXPECT_FALSE(array.isValid());
164 TEST_F(RelationsInternal, BuildIndex)
166 DataStore::Instance().setInitializeActive(
true);
167 evtData.registerRelationTo(profileData);
168 DataStore::Instance().setInitializeActive(
false);
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)
242 DataStore::Instance().setInitializeActive(
true);
243 evtData.registerRelationTo(profileData);
244 DataStore::Instance().setInitializeActive(
false);
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)
259 DataStore::Instance().setInitializeActive(
true);
260 evtData.registerRelationTo(profileData);
261 DataStore::Instance().setInitializeActive(
false);
265 EXPECT_EQ(index.size(), 0u);
266 EXPECT_EQ(index.getFromAccessorParams().first,
"");
267 EXPECT_EQ(index.getToAccessorParams().first,
"");
271 TEST_F(RelationsInternal, WrongRelationIndexDeathTest)
275 DataStore::Instance().setInitializeActive(
true);
277 relation.registerInDataStore();
279 eventData.registerInDataStore();
281 relation2.registerInDataStore();
282 DataStore::Instance().setInitializeActive(
false);
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)
335 DataStore::Instance().setInitializeActive(
true);
336 evtData.registerRelationTo(profileData);
337 DataStore::Instance().setInitializeActive(
false);
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)
369 DataStore::Instance().setInitializeActive(
true);
370 evtData.registerRelationTo(profileData);
371 DataStore::Instance().setInitializeActive(
false);
373 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[0], 1.0);
374 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[1], 2.0);
375 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[2], -3.0);
377 findRelationsCheckContents();
381 TEST_F(RelationsInternal, GetRelationsWith)
383 DataStore::Instance().setInitializeActive(
true);
384 evtData.registerRelationTo(profileData);
385 DataStore::Instance().setInitializeActive(
false);
387 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[0], 1.0);
388 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[1], 2.0);
389 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[2], 3.0);
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)
410 DataStore::Instance().setInitializeActive(
true);
411 profileData2.registerInDataStore();
412 evtData.registerRelationTo(profileData);
413 evtData.registerRelationTo(profileData2);
414 DataStore::Instance().setInitializeActive(
false);
416 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[0], 1.0);
417 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[1], 2.0);
418 DataStore::Instance().addRelationFromTo((evtData)[0], (profileData)[2], -3.0);
421 profileData2.appendNew();
422 DataStore::Instance().addRelationFromTo((evtData)[0], profileData2[0], 42.0);
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)
467 EXPECT_EQ(0u, DataStore::Instance().getListOfRelatedArrays(profileData2).size());
468 EXPECT_EQ(0u, DataStore::Instance().getListOfRelatedArrays(profileData).size());
469 EXPECT_EQ(0u, DataStore::Instance().getListOfRelatedArrays(evtData).size());
471 DataStore::Instance().setInitializeActive(
true);
472 profileData2.registerInDataStore();
473 evtData.registerRelationTo(profileData);
474 evtData.registerRelationTo(profileData2);
475 DataStore::Instance().setInitializeActive(
false);
477 EXPECT_EQ(1u, DataStore::Instance().getListOfRelatedArrays(profileData2).size());
478 EXPECT_EQ(1u, DataStore::Instance().getListOfRelatedArrays(profileData).size());
479 EXPECT_EQ(2u, DataStore::Instance().getListOfRelatedArrays(evtData).size());
481 EXPECT_EQ(evtData.getName(), DataStore::Instance().getListOfRelatedArrays(profileData2).at(0));
482 EXPECT_EQ(evtData.getName(), DataStore::Instance().getListOfRelatedArrays(profileData).at(0));
485 TEST_F(RelationsInternal, StoreArray_clear_cleans_relations)
487 DataStore::Instance().setInitializeActive(
true);
488 evtData.registerRelationTo(relObjData);
489 relObjData.registerRelationTo(profileData);
490 evtData.registerRelationTo(profileData);
491 DataStore::Instance().setInitializeActive(
false);
493 DataStore::Instance().addRelationFromTo((evtData)[0], (relObjData)[0], 1.0);
494 DataStore::Instance().addRelationFromTo((relObjData)[1], (profileData)[9], 1.0);
495 DataStore::Instance().addRelationFromTo((evtData)[1], (profileData)[1], 2.0);
500 EXPECT_EQ(DataStore::getRelationsWithObj<ProfileInfo>(relObjData[1]).size(), 0u);
501 EXPECT_EQ(DataStore::getRelationsWithObj<EventMetaData>(relObjData[0]).size(), 0u);
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.
size_t size() const
Get the size of the index.
const Element * getFirstElementFrom(const FROM &from) const
Return a pointer to the first relation Element of the given object.
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.
size_t size() const
Get number of relations.
T * object(int index) const
Get object with index.
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.
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.
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.