Belle II Software  release-08-01-10
DirectedNodeNetworkTests Namespace Reference

These tests cover the functionality of the classes: DirectedNode, DirectedNodeNetwork. More...

Classes

class  DirectedNodeNetworkTest
 Test class demonstrating the behavior of TCNetworkContainer. More...
 

Functions

VXD::SensorInfoBase provideSensorInfo (VxdID aVxdID, double globalX=0., double globalY=0., double globalZ=-0.)
 helper function: create a sensorInfo to be used
 
PXDCluster providePXDCluster (double u, double v, VxdID aVxdID, double uError=0.1, double vError=0.1)
 helper function: returns a pxdCluster with given sensorID and local coordinates
 
 TEST_F (DirectedNodeNetworkTest, MockupSanityCheck)
 basic sanity checks: checks mockup for working properly
 
 TEST_F (DirectedNodeNetworkTest, CreateNetworkIntAndThoroughlyTestIntendedBehavior)
 testing basic functionality of the DirectedNodeNetwork when filled with a basic type - int. More...
 

Detailed Description

These tests cover the functionality of the classes: DirectedNode, DirectedNodeNetwork.

Function Documentation

◆ TEST_F()

DirectedNodeNetworkTests::TEST_F ( DirectedNodeNetworkTest  ,
CreateNetworkIntAndThoroughlyTestIntendedBehavior   
)

testing basic functionality of the DirectedNodeNetwork when filled with a basic type - int.

This test is a bit overcrowded and therefore not an ideal test for learning the intended usage of the network. This test tries to cover all cases relevant. The correct usage of the network is better described in the test 'CreateRealisticNetwork'.

small lambda function for checking existence of a given node in a given vector

small lambda function for printing all the nodes for debugging

Definition at line 160 of file directedNodeNetwork.cc.

161  {
162  // here some tool-definitions first:
163 
165  auto nodeWasFound = [&](std::vector<DirectedNode<int, VoidMetaInfo>*>& nodes, DirectedNode<int, VoidMetaInfo>* node) -> bool {
166  for (DirectedNode<int, VoidMetaInfo>* otherNode : nodes)
167  {
168  if (node->getEntry() == otherNode->getEntry()) { return true; }
169  }
170  return false;
171  };
172 
174  auto printNodeEntries = [&](std::vector<DirectedNode<int, VoidMetaInfo>*>& nodes) -> std::string {
175  std::string output = "Nodes got the following entries: ";
176  for (DirectedNode<int, VoidMetaInfo>* node : nodes)
177  {
178  output += std::to_string(node->getEntry()) + " ";
179  }
180  return output + "\n";
181  };
182 
183  // just some input for testing:
184  std::array<int, 5> intArray = { { 2, 5, 3, 4, 99} };
185  std::array<int, 5> intArray2 = { { 144, 121, 33, 35, 31415} }; // these entries are independent of the first intArray-entries
186  std::array<int, 5> intArray3 = { { 1440, 1210, 3, 33, 3141529} }; // entry 2 crosses intArray, entry 3 crosses intArray2
187  std::deque<int> onTheFlyCreatedInts; // the user has to take care about the lifetime of the objects to be linked in the network!
188  EXPECT_EQ(5, intArray.size());
189 
191  EXPECT_EQ(0, intNetwork.size());
192  B2INFO("tests case when both nodes are new and when inner one is new, but outer one not");
193  for (unsigned int index = 1 ; index < 5; index++) {
194  B2INFO("intArray-index " << index << " of array has entry: " << intArray.at(index));
195 
196  intNetwork.addNode(intArray.at(index - 1), intArray.at(index - 1));
197  intNetwork.addNode(intArray.at(index), intArray.at(index));
198  // correct order: outerEntry, innerEntry:
199  intNetwork.linkNodes(intArray.at(index - 1), intArray.at(index));
200 
201  // innerEnd has been updated:
202  EXPECT_EQ(intArray.at(index), intNetwork.getInnerEnds().at(0)->getEntry());
203 
204  // entries are now in network:
205  EXPECT_EQ(intArray.at(index - 1), *intNetwork.getNode(intArray.at(index - 1)));
206  EXPECT_EQ(intArray.at(index), *intNetwork.getNode(intArray.at(index)));
207 
208  // get all nodes of outer node, expected: 1 inner and no outerNodes:
209  auto& innerNodes = intNetwork.getNode(intArray.at(index - 1))->getInnerNodes();
210  auto& outerNodes = intNetwork.getNode(intArray.at(index))->getOuterNodes();
211  EXPECT_EQ(1, innerNodes.size());
212  EXPECT_EQ(1, outerNodes.size());
213 
214  // array[index] is now linked as inner node of array[index-1]:
215  EXPECT_EQ(*(innerNodes.at(0)), *(intNetwork.getNode(intArray.at(index))));
216  }
217  EXPECT_EQ(0, intNetwork.getNode(intArray.at(0))->getOuterNodes().size()); // the outermost node has no outerNodes
218  EXPECT_EQ(0, intNetwork.getNode(intArray.at(4))->getInnerNodes().size()); // the innermost node has no innerNodes
219 
220  // some extra sanity checks, are inner- and outerEnds as expected?
221  EXPECT_EQ(intArray.size(), intNetwork.size());
222  std::vector<DirectedNode<int, VoidMetaInfo>*> outerEnds_before = intNetwork.getOuterEnds();
223  std::vector<DirectedNode<int, VoidMetaInfo>*> innerEnds_before = intNetwork.getInnerEnds();
224  EXPECT_EQ(1, outerEnds_before.size());
225  EXPECT_EQ(1, innerEnds_before.size());
226 
227  DirectedNode<int, VoidMetaInfo>* outermostNode = outerEnds_before.at(0);
228  EXPECT_EQ(intArray.at(0), outermostNode->getEntry());
229  EXPECT_EQ(outermostNode->getEntry(), *outermostNode);
230  EXPECT_EQ(intArray.at(1), *(outermostNode->getInnerNodes().at(0)));
231 
232  DirectedNode<int, VoidMetaInfo>* innermostNode = innerEnds_before.at(0);
233  EXPECT_EQ(intArray.at(4), innermostNode->getEntry());
234  EXPECT_EQ(innermostNode->getEntry(), *innermostNode);
235  EXPECT_EQ(intArray.at(3), *(innermostNode->getOuterNodes().at(0)));
236 
237 
238  {
239  B2INFO("building another path, which is independent of the first one:");
240  for (unsigned int index = 1 ; index < 5; index++) {
241  B2INFO("intArray2-index " << index << " of array has entry: " << intArray2.at(index));
242 
243  intNetwork.addNode(intArray2.at(index - 1), intArray2.at(index - 1));
244  intNetwork.addNode(intArray2.at(index), intArray2.at(index));
245  // correct order: outerEntry, innerEntry:
246  intNetwork.linkNodes(intArray2.at(index - 1), intArray2.at(index));
247 
248  // entries are now in network:
249  EXPECT_EQ(intArray2.at(index - 1), *intNetwork.getNode(intArray2.at(index - 1)));
250  EXPECT_EQ(intArray2.at(index), *intNetwork.getNode(intArray2.at(index)));
251 
252  // get all nodes of outer node, expected: 1 inner and no outerNodes:
253  auto& innerNodes = intNetwork.getNode(intArray2.at(index - 1))->getInnerNodes();
254  auto& outerNodes = intNetwork.getNode(intArray2.at(index))->getOuterNodes();
255  EXPECT_EQ(1, innerNodes.size());
256  EXPECT_EQ(1, outerNodes.size());
257 
258  // array[index] is now linked as inner node of array[index-1]:
259  EXPECT_TRUE(nodeWasFound(innerNodes, intNetwork.getNode(intArray2.at(index))));
260  EXPECT_EQ(*(innerNodes.at(0)), *(intNetwork.getNode(intArray2.at(index))));
261  }
262  EXPECT_EQ(0, intNetwork.getNode(intArray2.at(0))->getOuterNodes().size()); // the outermost node has no outerNodes
263  EXPECT_EQ(0, intNetwork.getNode(intArray2.at(4))->getInnerNodes().size()); // the innermost node has no innerNodes
264 
265  // some extra sanity checks, are inner- and outerEnds as expected?
266  EXPECT_EQ(10, intNetwork.size());
267  std::vector<DirectedNode<int, VoidMetaInfo>*> outerEnds = intNetwork.getOuterEnds();
268  std::vector<DirectedNode<int, VoidMetaInfo>*> innerEnds = intNetwork.getInnerEnds();
269  EXPECT_EQ(2, outerEnds.size());
270  EXPECT_EQ(2, innerEnds.size());
271  }
272 
273 
274  B2INFO("building another path into the network which crosses the other paths:"); {
275  B2INFO("tests cases: both new, outer new but inner not, inner new but outer not, both already existing:");
276  for (unsigned int index = 1 ; index < 5; index++) {
277  B2INFO("intArray3-indices " << index - 1 << "/" << index <<
278  " of array have entries: " << intArray3.at(index - 1) << "/" << intArray3.at(index) << "\n"
279  );
280 
281  intNetwork.addNode(intArray3.at(index - 1), intArray3.at(index - 1));
282  intNetwork.addNode(intArray3.at(index), intArray3.at(index));
283  // correct order: outerEntry, innerEntry:
284  intNetwork.linkNodes(intArray3.at(index - 1), intArray3.at(index));
285 
286  // entries are now in network:
287  EXPECT_EQ(intArray3.at(index - 1), *intNetwork.getNode(intArray3.at(index - 1)));
288  EXPECT_EQ(intArray3.at(index), *intNetwork.getNode(intArray3.at(index)));
289 
290  // array[index] is now linked as an inner node of array[index-1]:
291  EXPECT_TRUE(nodeWasFound(intNetwork.getNode(intArray3.at(index - 1))->getInnerNodes(),
292  intNetwork.getNode(intArray3.at(index))));
293 
294 
295  if (index > 1) continue; // the following tests do not work for paths crossing each other, therefore tests would fail
296  // innerEnd has been updated:
297  std::vector<DirectedNode<int, VoidMetaInfo>*> innerEnds = intNetwork.getInnerEnds();
298  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray3.at(index))));
299  B2INFO("innerEnds after indices " << index - 1 << "/" << index << " are: " << printNodeEntries(innerEnds));
300 
301  // get all nodes of outer node, expected: 1 inner and no outerNodes:
302  auto& innerNodes = intNetwork.getNode(intArray3.at(index - 1))->getInnerNodes();
303  auto& outerNodes = intNetwork.getNode(intArray3.at(index))->getOuterNodes();
304  EXPECT_EQ(1, innerNodes.size());
305  EXPECT_EQ(1, outerNodes.size());
306  }
307  EXPECT_EQ(0, intNetwork.getNode(intArray3.at(0))->getOuterNodes().size()); // the outermost node has no outerNodes
308  EXPECT_EQ(0, intNetwork.getNode(intArray3.at(4))->getInnerNodes().size()); // the innermost node has no innerNodes
309 
310  // some extra sanity checks, are inner- and outerEnds as expected?
311  EXPECT_EQ(13, intNetwork.size());
312  //B2INFO(" theWholeNetwork: \n" << printNodeEntries(intNetwork.getNodes()));
313  std::vector<DirectedNode<int, VoidMetaInfo>*> outerEnds = intNetwork.getOuterEnds();
314  std::vector<DirectedNode<int, VoidMetaInfo>*> innerEnds = intNetwork.getInnerEnds();
315  EXPECT_EQ(3, outerEnds.size());
316  EXPECT_EQ(3, innerEnds.size());
317  B2INFO("outerEnds are: \n" << printNodeEntries(outerEnds));
318  B2INFO("innerEnds are: \n" << printNodeEntries(innerEnds));
319  }
320 
321  B2INFO("case: when outer node is new, but inner one not: "); {
322  B2INFO(" case: inner one was outer end before: ");
323  auto* oldOuterMostNode = intNetwork.getOuterEnds().at(0);
324  int oldOuterInt = oldOuterMostNode->getEntry();
325  onTheFlyCreatedInts.push_back(42);
326  int& newInnerInt = onTheFlyCreatedInts.back();
327  EXPECT_TRUE(nullptr == intNetwork.getNode(newInnerInt));
328  intNetwork.addNode(newInnerInt, newInnerInt);
329  intNetwork.linkNodes(newInnerInt, oldOuterInt);
330  EXPECT_FALSE(nullptr == intNetwork.getNode(newInnerInt));
331  EXPECT_EQ(14, intNetwork.size());
332  EXPECT_EQ(3, intNetwork.getInnerEnds().size());
333  std::vector<DirectedNode<int, VoidMetaInfo>*> newOuterEnds = intNetwork.getOuterEnds();
334  EXPECT_EQ(3, newOuterEnds.size()); // did not change, new outermost node replaced old one
335  EXPECT_EQ(1, oldOuterMostNode->getOuterNodes().size());
336  EXPECT_EQ(newInnerInt, *(oldOuterMostNode->getOuterNodes().at(0)));
337  }
338 
339  {
340  B2INFO(" case: inner one was no outer end before: ");
341  onTheFlyCreatedInts.push_back(23);
342  int& newOuterInt = onTheFlyCreatedInts.back();
343  int& existingInt = intArray.at(1); // neither an outer nor an inner end before.
344  std::vector<DirectedNode<int, VoidMetaInfo>*> oldOuterEnds = intNetwork.getOuterEnds();
345  std::vector<DirectedNode<int, VoidMetaInfo>*> oldInnerEnds = intNetwork.getInnerEnds();
346 
347  EXPECT_EQ(3, oldOuterEnds.size());
348  EXPECT_TRUE(nullptr == intNetwork.getNode(newOuterInt));
349  unsigned int sizeB4 = intNetwork.size();
350  intNetwork.addNode(newOuterInt, newOuterInt);
351  intNetwork.linkNodes(newOuterInt, existingInt);
352  EXPECT_FALSE(nullptr == intNetwork.getNode(newOuterInt));
353  EXPECT_EQ(sizeB4 + 1, intNetwork.size());
354  EXPECT_EQ(3, intNetwork.getInnerEnds().size());
355  DirectedNode<int, VoidMetaInfo>* existingNode = intNetwork.getNode(existingInt);
356  EXPECT_EQ(1, existingNode->getInnerNodes().size());
357  EXPECT_EQ(2, existingNode->getOuterNodes().size());
358  std::vector<DirectedNode<int, VoidMetaInfo>*> newOuterEnds = intNetwork.getOuterEnds();
359  EXPECT_EQ(4, newOuterEnds.size()); // did change, new outermost node co-existing with old outermost one
360  EXPECT_EQ(2, existingNode->getOuterNodes().size());
361 
362  }
363 
364 
365  {
366  B2INFO("case: when both were there, but not linked yet: ");
367  unsigned int sizeB4 = intNetwork.size();
368  intNetwork.linkNodes(intArray.at(0), intArray.at(2));
369  EXPECT_EQ(sizeB4, intNetwork.size()); // size of network does not change
370  EXPECT_EQ(3, intNetwork.getInnerEnds().size()); // this can not change here
371  EXPECT_EQ(4, intNetwork.getOuterEnds().size());
372  std::vector<DirectedNode<int, VoidMetaInfo>*> innerEnds = intNetwork.getNode(intArray.at(0))->getInnerNodes();
373  EXPECT_EQ(2, innerEnds.size());
374  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(1))));
375  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(2))));
376  EXPECT_TRUE(nodeWasFound(innerEnds, innerEnds.at(1)));
377 
378 
379  B2INFO("case: when outer both were there and already linked: ");
380  EXPECT_FALSE(intNetwork.linkNodes(intArray.at(0), intArray.at(2)));
381  // nothing was added, everything the same as last case:
382  EXPECT_EQ(sizeB4, intNetwork.size());
383  EXPECT_EQ(3, intNetwork.getInnerEnds().size());
384  EXPECT_EQ(4, intNetwork.getOuterEnds().size());
385  std::vector<DirectedNode<int, VoidMetaInfo>*> moreInnerEnds = intNetwork.getNode(intArray.at(0))->getInnerNodes();
386  EXPECT_EQ(innerEnds.size(), moreInnerEnds.size());
387  EXPECT_TRUE(nodeWasFound(innerEnds, moreInnerEnds.at(0)));
388  EXPECT_TRUE(nodeWasFound(innerEnds, moreInnerEnds.at(1)));
389  }
390 
391 
392  B2INFO("testing members for filling, when (at least) one entry was already there:"); {
393  B2INFO("case: addInnerToLastOuterNode: both were there, but not linked yet: ");
394  unsigned int networkSizeB4 = intNetwork.size();
395  unsigned int nInnerEndsB4 = intNetwork.getInnerEnds().size();
396  intNetwork.addInnerToLastOuterNode(intArray.at(3));
397  EXPECT_EQ(networkSizeB4, intNetwork.size());
398  EXPECT_EQ(nInnerEndsB4, intNetwork.getInnerEnds().size());
399  std::vector<DirectedNode<int, VoidMetaInfo>*> innerEnds = intNetwork.getNode(intArray.at(0))->getInnerNodes();
400  EXPECT_EQ(3, innerEnds.size());
401 
402  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(1))));
403  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(2))));
404  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(3))));
405  }
406 
407 
408  {
409  B2INFO("case: addInnerToLastOuterNode: both were there, but already linked (same results as before, but with an error for unintended behavior):");
410  unsigned int networkSizeB4 = intNetwork.size();
411  unsigned int nInnerEndsB4 = intNetwork.getInnerEnds().size();
412  intNetwork.addInnerToLastOuterNode(intArray.at(3));
413  EXPECT_EQ(networkSizeB4, intNetwork.size());
414  EXPECT_EQ(nInnerEndsB4, intNetwork.getInnerEnds().size());
415  std::vector<DirectedNode<int, VoidMetaInfo>*> innerEnds = intNetwork.getNode(intArray.at(0))->getInnerNodes();
416  EXPECT_EQ(3, innerEnds.size());
417 
418  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(1))));
419  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(2))));
420  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(intArray.at(3))));
421  }
422  /*
423  {
424  B2INFO("case: addInnerToLastOuterNode: inner was not there yet (innerEndsUpdate):");
425  onTheFlyCreatedInts.push_back(31);
426  int& lastOuterNodeInt = intNetwork.getLastOuterNode()->getEntry();
427  int& newInnerInt = onTheFlyCreatedInts.back();
428  unsigned int networkSizeB4 = intNetwork.size();
429  unsigned int nInnerEndsB4 = intNetwork.getInnerEnds().size();
430  unsigned int nInnerNodesB4 = intNetwork.getNode(lastOuterNodeInt)->getInnerNodes().size();
431  intNetwork.addInnerToLastOuterNode(newInnerInt);
432  EXPECT_EQ(networkSizeB4 + 1, intNetwork.size());
433  EXPECT_EQ(nInnerEndsB4 + 1, intNetwork.getInnerEnds().size());
434  EXPECT_EQ(nInnerNodesB4 + 1, intNetwork.getNode(lastOuterNodeInt)->getInnerNodes().size());
435 
436  EXPECT_TRUE(nodeWasFound(intNetwork.getNode(lastOuterNodeInt)->getInnerNodes(), intNetwork.getNode(newInnerInt)));
437  auto innerEnds = intNetwork.getInnerEnds();
438  EXPECT_TRUE(nodeWasFound(innerEnds, intNetwork.getNode(newInnerInt)));
439  }
440 
441  {
442  B2INFO("case: addOuterToLastInnerNode: both were there, but not linked yet:");
443  int& lastInnerNodeInt = intNetwork.getLastInnerNode()->getEntry();
444  unsigned int networkSizeB4 = intNetwork.size();
445  unsigned int nOuterEndsB4 = intNetwork.getOuterEnds().size();
446  intNetwork.addOuterToLastInnerNode(intArray2.at(1));
447  EXPECT_EQ(networkSizeB4, intNetwork.size());
448  EXPECT_EQ(nOuterEndsB4, intNetwork.getOuterEnds().size());
449  std::vector<DirectedNode<int, VoidMetaInfo>*> outerNodes = intNetwork.getNode(lastInnerNodeInt)->getOuterNodes();
450  EXPECT_EQ(2, outerNodes.size());
451  }
452 
453  {
454  B2INFO("case: addOuterToLastInnerNode: both were there, but already linked (same results as before, but with an error for unintended behavior):");
455  int& lastInnerNodeInt = intNetwork.getLastInnerNode()->getEntry();
456  unsigned int networkSizeB4 = intNetwork.size();
457  unsigned int nOuterEndsB4 = intNetwork.getOuterEnds().size();
458  intNetwork.addOuterToLastInnerNode(intArray2.at(1));
459  EXPECT_EQ(networkSizeB4, intNetwork.size());
460  EXPECT_EQ(nOuterEndsB4, intNetwork.getOuterEnds().size());
461  std::vector<DirectedNode<int, VoidMetaInfo>*> outerNodes = intNetwork.getNode(lastInnerNodeInt)->getOuterNodes();
462  EXPECT_EQ(2, outerNodes.size());
463  }
464 
465  {
466  B2INFO("case: addOuterToLastInnerNode: outer was not there yet (outerEndsUpdate):");
467  onTheFlyCreatedInts.push_back(66);
468  int& lastInnerNodeInt = intNetwork.getLastInnerNode()->getEntry();
469  int& newOuterInt = onTheFlyCreatedInts.back();
470  unsigned int networkSizeB4 = intNetwork.size();
471  unsigned int nOuterEndsB4 = intNetwork.getOuterEnds().size();
472  unsigned int nOuterNodesB4 = intNetwork.getNode(lastInnerNodeInt)->getOuterNodes().size();
473  intNetwork.addOuterToLastInnerNode(newOuterInt);
474  EXPECT_EQ(networkSizeB4 + 1, intNetwork.size());
475  EXPECT_EQ(nOuterEndsB4 + 1, intNetwork.getOuterEnds().size());
476  EXPECT_EQ(nOuterNodesB4 + 1, intNetwork.getNode(lastInnerNodeInt)->getOuterNodes().size());
477 
478  EXPECT_TRUE(nodeWasFound(intNetwork.getNode(lastInnerNodeInt)->getOuterNodes(), intNetwork.getNode(newOuterInt)));
479  auto outerEnds = intNetwork.getOuterEnds();
480  EXPECT_TRUE(nodeWasFound(outerEnds, intNetwork.getNode(newOuterInt)));
481  }
482  */
483  }
Network of directed nodes of the type EntryType.
bool addInnerToLastOuterNode(NodeID innerNodeID)
to the last outerNode added, another innerNode will be attached
Node * getNode(NodeID toBeFound)
Returns pointer to the node carrying the entry which is equal to given parameter.
std::vector< Node * > getOuterEnds()
getters:
unsigned int size() const
Returns number of nodes to be found in the network.
bool linkNodes(NodeID outerNodeID, NodeID innerNodeID)
takes two entry IDs and weaves them into the network
std::vector< Node * > getInnerEnds()
returns all nodes which have no inner nodes (but outer ones) and therefore are inner ends of the netw...
bool addNode(NodeID nodeID, EntryType &newEntry)
************************* PUBLIC MEMBER FUNCTIONS *************************
The Node-Class.
Definition: DirectedNode.h:31
std::vector< DirectedNode< EntryType, MetaInfoType > * > & getOuterNodes()
Returns links to all outer nodes attached to this one.
Definition: DirectedNode.h:89
std::vector< DirectedNode< EntryType, MetaInfoType > * > & getInnerNodes()
************************* PUBLIC MEMBER FUNCTIONS *************************
Definition: DirectedNode.h:86
EntryType & getEntry()
Allows access to stored entry.
Definition: DirectedNode.h:92