Skip to content

Commit

Permalink
test(enrich): adds a few test cases for cycle detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Dec 29, 2023
1 parent 1a5d994 commit daa8a1e
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
111 changes: 111 additions & 0 deletions test/graph-utl/__mocks__/cycle-input-graphs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,115 @@ export default {
],
},
],
MICRO_EIGHT: [
{
source: "a",
dependencies: [
{
resolved: "aa",
},
{
resolved: "ba",
},
],
},
{
source: "aa",
dependencies: [
{
resolved: "a",
},
],
},
{
source: "ba",
dependencies: [
{
resolved: "a",
},
],
},
],
SHORT_EIGHT: [
{
source: "a",
dependencies: [
{
resolved: "aa",
},
{
resolved: "ba",
},
],
},
{
source: "aa",
dependencies: [
{
resolved: "a",
},
],
},
{
source: "ba",
dependencies: [
{
resolved: "bb",
},
],
},
{
source: "bb",
dependencies: [
{
resolved: "a",
},
],
},
],
SLIGHTLY_LESS_SHORT_EIGHT: [
{
source: "a",
dependencies: [
{
resolved: "aa",
},
{
resolved: "ba",
},
],
},
{
source: "aa",
dependencies: [
{
resolved: "a",
},
],
},
{
source: "ba",
dependencies: [
{
resolved: "bb",
},
],
},
{
source: "bb",
dependencies: [
{
resolved: "bc",
},
],
},
{
source: "bc",
dependencies: [
{
resolved: "a",
},
],
},
],
};
79 changes: 79 additions & 0 deletions test/graph-utl/indexed-module-graph.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,85 @@ describe("[U] graph-utl/indexed-module-graph - getCycle (algorithm check)", () =
it("if the 'to' node is not in the graph, it returns []", () => {
deepEqual(getCycle(cycleInputGraphs.D_E_D, "d", "not-in-graph"), []);
});
it("a minimal 'eight' - all cycles", () => {
/*
aa◄───┐
│ │
└────►a◄────┐
│ │
└───►ba
*/
deepEqual(getCycle(cycleInputGraphs.MICRO_EIGHT, "a", "aa"), ["aa", "a"]);
deepEqual(getCycle(cycleInputGraphs.MICRO_EIGHT, "a", "ba"), ["ba", "a"]);
deepEqual(getCycle(cycleInputGraphs.MICRO_EIGHT, "aa", "a"), ["a", "aa"]);
deepEqual(getCycle(cycleInputGraphs.MICRO_EIGHT, "ba", "a"), ["a", "ba"]);
});

it("an 'eight' with a slightly fatter belly - all cycles", () => {
/*
aa◄────┐
│ │
└─────►a◄──────────bc
│ ▲
│ │
└───►ba───►bb
*/
deepEqual(getCycle(cycleInputGraphs.SHORT_EIGHT, "a", "aa"), ["aa", "a"]);
deepEqual(getCycle(cycleInputGraphs.SHORT_EIGHT, "aa", "a"), ["a", "aa"]);
deepEqual(getCycle(cycleInputGraphs.SHORT_EIGHT, "a", "ba"), [
"ba",
"bb",
"a",
]);
deepEqual(getCycle(cycleInputGraphs.SHORT_EIGHT, "ba", "bb"), [
"bb",
"a",
"ba",
]);
deepEqual(getCycle(cycleInputGraphs.SHORT_EIGHT, "bb", "a"), [
"a",
"ba",
"bb",
]);
});
it("an 'eight' with an even fatter belly - all cycles", () => {
/*
aa◄────┐
│ │
└─────►a◄──────────bc
│ ▲
│ │
└───►ba───►bb
*/
deepEqual(getCycle(cycleInputGraphs.SLIGHTLY_LESS_SHORT_EIGHT, "a", "aa"), [
"aa",
"a",
]);
deepEqual(getCycle(cycleInputGraphs.SLIGHTLY_LESS_SHORT_EIGHT, "aa", "a"), [
"a",
"aa",
]);
deepEqual(getCycle(cycleInputGraphs.SLIGHTLY_LESS_SHORT_EIGHT, "a", "ba"), [
"ba",
"bb",
"bc",
"a",
]);
deepEqual(
getCycle(cycleInputGraphs.SLIGHTLY_LESS_SHORT_EIGHT, "ba", "bb"),
["bb", "bc", "a", "ba"],
);
deepEqual(
getCycle(cycleInputGraphs.SLIGHTLY_LESS_SHORT_EIGHT, "bb", "bc"),
["bc", "a", "ba", "bb"],
);
// deepEqual(getCycle(cycleInputGraphs.SLIGHTLY_LESS_SHORT_EIGHT, "bc", "a"), [
// "a",
// "ba",
// "bb",
// "bc",
// ]);
});
});

function getCycleRich(pGraph, pFrom, pToDep) {
Expand Down

0 comments on commit daa8a1e

Please sign in to comment.