iteration.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var test = require('tap').test,
  2. createGraph = require('..');
  3. test('forEachLinkedNode respects orientation', function(t) {
  4. t.plan(3);
  5. var graph = createGraph();
  6. graph.addLink(1, 2);
  7. graph.addLink(2, 3);
  8. var oriented = true;
  9. graph.forEachLinkedNode(2, function(node, link) {
  10. t.ok(link.toId === 3, 'Only 3 is connected to node 2, when traversal is oriented');
  11. }, oriented);
  12. graph.forEachLinkedNode(2, function(node, link) {
  13. t.ok(link.toId === 3 || link.toId === 2, 'both incoming and outgoing links are visited');
  14. }, !oriented);
  15. t.end();
  16. });
  17. test('forEachLinkedNode handles self-loops', function(t) {
  18. t.plan(1);
  19. var graph = createGraph();
  20. graph.addLink(1, 1);
  21. // we should visit exactly one time
  22. graph.forEachLinkedNode(1, function(node, link) {
  23. t.ok(link.fromId === 1 && link.toId === 1, 'Link 1 is visited once');
  24. });
  25. t.end();
  26. });
  27. test('forEachLinkedNode will not crash on invalid node id', function(t) {
  28. t.plan(0);
  29. var graph = createGraph();
  30. graph.addLink(1, 2);
  31. graph.forEachLinkedNode(3, function() {
  32. t.notOk(true, 'This code will never be executed');
  33. });
  34. });
  35. test('forEachLinkedNode can quit fast for oriented graphs', function(t) {
  36. t.plan(1);
  37. var graph = createGraph();
  38. var oriented = true;
  39. graph.addLink(1, 2);
  40. graph.addLink(1, 3);
  41. graph.forEachLinkedNode(1, function() {
  42. t.ok(true, 'Visited first link');
  43. return true; // We want to stop right now!
  44. }, oriented);
  45. });
  46. test('forEachLinkedNode can quit fast for non-oriented graphs', function(t) {
  47. t.plan(1);
  48. var graph = createGraph();
  49. var oriented = false;
  50. graph.addLink(1, 2);
  51. graph.addLink(1, 3);
  52. graph.forEachLinkedNode(1, function() {
  53. t.ok(true, 'Visited first link');
  54. return true; // We want to stop right now!
  55. }, oriented);
  56. });
  57. test('forEachLinkedNode returns quitFast flag', function(t) {
  58. var graph = createGraph();
  59. graph.addLink(1, 2);
  60. graph.addLink(1, 3);
  61. var fastQuit = graph.forEachLinkedNode(1, function() {
  62. return true; // Stop right now.
  63. });
  64. t.ok(fastQuit, 'Fast quit triggered when callback opted out');
  65. var notSoFast = graph.forEachLinkedNode(1, function() { });
  66. t.notOk(notSoFast, 'Fast quit is not triggered when all elements visited');
  67. t.end();
  68. });
  69. test('forEachLink visits each link', function(t) {
  70. t.plan(1);
  71. var graph = createGraph();
  72. graph.addLink(1, 2);
  73. graph.forEachLink(function(link) {
  74. t.ok(link.fromId === 1 && link.toId === 2, 'Link is here');
  75. });
  76. t.end();
  77. });
  78. test('forEachLink will not crash on empty callback', function(t) {
  79. var graph = createGraph();
  80. graph.addLink(1, 2);
  81. graph.forEachLink(); // didn't pass callback, no worries.
  82. t.end();
  83. });
  84. test('forEachNode will stop when requested', function(t) {
  85. t.plan(1);
  86. var graph = createGraph();
  87. graph.addLink(1, 2);
  88. graph.forEachNode(function(node) {
  89. t.equals(node.id, 1, 'We visit only one node');
  90. return true; // we want to stop now!
  91. });
  92. t.end();
  93. });
  94. test('forEachNode returns fastQuit', function(t) {
  95. var graph = createGraph();
  96. graph.addLink(1, 2);
  97. var fastQuit = graph.forEachNode(function(node) {
  98. t.equals(node.id, 1, 'We visit only one node');
  99. return true; // we want to stop now!
  100. }); // no callback? No worries
  101. t.ok(fastQuit, 'fastQuit is set when callback opted out');
  102. var notSoFast = graph.forEachNode(function() { });
  103. t.notOk(notSoFast, 'fastQuit is not set when all nodes visited');
  104. t.end();
  105. });