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'.
161 {
162
163
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: ";
177 {
178 output += std::to_string(node->getEntry()) + " ";
179 }
180 return output + "\n";
181 };
182
183
184 std::array<int, 5> intArray = { { 2, 5, 3, 4, 99} };
185 std::array<int, 5> intArray2 = { { 144, 121, 33, 35, 31415} };
186 std::array<int, 5> intArray3 = { { 1440, 1210, 3, 33, 3141529} };
187 std::deque<int> onTheFlyCreatedInts;
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
199 intNetwork.
linkNodes(intArray.at(index - 1), intArray.at(index));
200
201
202 EXPECT_EQ(intArray.at(index), intNetwork.
getInnerEnds().at(0)->getEntry());
203
204
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
211 EXPECT_EQ(1, innerNodes.size());
212 EXPECT_EQ(1, outerNodes.size());
213
214
215 EXPECT_EQ(*(innerNodes.at(0)), *(intNetwork.
getNode(intArray.at(index))));
216 }
219
220
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
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
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
246 intNetwork.
linkNodes(intArray2.at(index - 1), intArray2.at(index));
247
248
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
255 EXPECT_EQ(1, innerNodes.size());
256 EXPECT_EQ(1, outerNodes.size());
257
258
259 EXPECT_TRUE(nodeWasFound(innerNodes, intNetwork.
getNode(intArray2.at(index))));
260 EXPECT_EQ(*(innerNodes.at(0)), *(intNetwork.
getNode(intArray2.at(index))));
261 }
264
265
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
284 intNetwork.
linkNodes(intArray3.at(index - 1), intArray3.at(index));
285
286
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
292 intNetwork.
getNode(intArray3.at(index))));
293
294
295 if (index > 1) continue;
296
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
304 EXPECT_EQ(1, innerNodes.size());
305 EXPECT_EQ(1, outerNodes.size());
306 }
309
310
311 EXPECT_EQ(13, intNetwork.
size());
312
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());
333 std::vector<DirectedNode<int, VoidMetaInfo>*> newOuterEnds = intNetwork.
getOuterEnds();
334 EXPECT_EQ(3, newOuterEnds.size());
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);
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());
358 std::vector<DirectedNode<int, VoidMetaInfo>*> newOuterEnds = intNetwork.
getOuterEnds();
359 EXPECT_EQ(4, newOuterEnds.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());
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
382 EXPECT_EQ(sizeB4, intNetwork.
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();
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();
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483 }
Network of directed nodes of the type EntryType.
Node * getNode(NodeID toBeFound)
Returns pointer to the node carrying the entry which is equal to given parameter.
std::vector< Node * > getInnerEnds()
returns all nodes which have no inner nodes (but outer ones) and therefore are inner ends of the netw...
bool addInnerToLastOuterNode(NodeID innerNodeID)
to the last outerNode added, another innerNode will be attached
unsigned int size() const
Returns number of nodes to be found in the network.
std::vector< Node * > getOuterEnds()
getters:
bool linkNodes(NodeID outerNodeID, NodeID innerNodeID)
takes two entry IDs and weaves them into the network
bool addNode(NodeID nodeID, EntryType &newEntry)
************************* PUBLIC MEMBER FUNCTIONS *************************
std::vector< DirectedNode< EntryType, MetaInfoType > * > & getInnerNodes()
************************* PUBLIC MEMBER FUNCTIONS *************************
EntryType & getEntry()
Allows access to stored entry.
std::vector< DirectedNode< EntryType, MetaInfoType > * > & getOuterNodes()
Returns links to all outer nodes attached to this one.