dfs.test.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const dfs_1 = require("./dfs");
  4. const assert_1 = require("./assert");
  5. const _ = require("lodash");
  6. const graph = new Map([
  7. [0, new Map([['a', 1], ['b', 2]])],
  8. [1, new Map([['c', 0]])],
  9. [2, new Map([['d', 3]])],
  10. ]);
  11. function getNeighbors(node) {
  12. const outgoing = graph.get(node);
  13. if (outgoing === undefined) {
  14. return [];
  15. }
  16. return [...outgoing.entries()];
  17. }
  18. describe("DFS", () => {
  19. it("Find node one hop", () => {
  20. const path = (0, dfs_1.findDFS)(0, 1, getNeighbors);
  21. (0, assert_1.assert)(_.isEqual(path, ['a']), "Expected node to be found.");
  22. });
  23. it("Find node two hops", () => {
  24. const path = (0, dfs_1.findDFS)(0, 3, getNeighbors);
  25. // There is more than one path from 0 to 3 (in fact, there are infinitely many, if we allow loops),
  26. // but we don't allow visiting the same element more than once, so only one path is possible:
  27. (0, assert_1.assert)(_.isEqual(path, ['b', 'd']), "Expected node to be found.");
  28. });
  29. it("Find node zero hops", () => {
  30. const path = (0, dfs_1.findDFS)(0, 0, getNeighbors);
  31. (0, assert_1.assert)(_.isEqual(path, []), "Expected node to be found.");
  32. });
  33. it("Find unreachable node", () => {
  34. const path = (0, dfs_1.findDFS)(3, 0, getNeighbors);
  35. (0, assert_1.assert)(path === undefined, "Expected node to be unreachable.");
  36. });
  37. });
  38. //# sourceMappingURL=dfs.test.js.map