generated from HugoMendes98/Nx-NestJS-Angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(graph): add tests for cycle detection
- Loading branch information
1 parent
1577641
commit 590204d
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
libs/common/src/app/graph/algorithms/graph.has-cycle.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { graphHasCycle } from "./graph.has-cycle"; | ||
import { AdjacencyListTransformationParams, getAdjacencyList } from "../transformations"; | ||
|
||
describe("graphHasCycle", () => { | ||
it("should not have a cycle", () => { | ||
const graphs: AdjacencyListTransformationParams[] = [ | ||
// "Empty" graphs | ||
{ arcs: [], nodes: [] }, | ||
{ | ||
arcs: [], | ||
nodes: [ | ||
{ inputs: [], outputs: [] }, | ||
{ inputs: [], outputs: [] } | ||
] | ||
}, | ||
{ | ||
arcs: [], | ||
nodes: [ | ||
{ inputs: [{ _id: 1 }], outputs: [{ _id: 2 }] }, | ||
{ inputs: [{ _id: 3 }], outputs: [{ _id: 4 }] } | ||
] | ||
}, | ||
|
||
{ | ||
arcs: [{ __from: 1, __to: 2 }], | ||
nodes: [ | ||
{ inputs: [], outputs: [{ _id: 1 }] }, | ||
{ inputs: [{ _id: 2 }], outputs: [] } | ||
] | ||
}, | ||
{ | ||
arcs: [ | ||
{ __from: 11, __to: 20 }, | ||
{ __from: 11, __to: 21 }, | ||
{ __from: 11, __to: 30 }, | ||
{ __from: 22, __to: 31 } | ||
], | ||
nodes: [ | ||
{ inputs: [{ _id: 10 }], outputs: [{ _id: 11 }] }, | ||
{ inputs: [{ _id: 20 }, { _id: 21 }], outputs: [{ _id: 22 }] }, | ||
{ inputs: [{ _id: 30 }, { _id: 31 }], outputs: [] } | ||
] | ||
} | ||
]; | ||
|
||
for (const graph of graphs) { | ||
expect(graphHasCycle(getAdjacencyList(graph))).toBeFalse(); | ||
} | ||
}); | ||
|
||
it("should have a cycle", () => { | ||
const graphs: AdjacencyListTransformationParams[] = [ | ||
{ | ||
arcs: [{ __from: 2, __to: 1 }], | ||
nodes: [{ inputs: [{ _id: 1 }], outputs: [{ _id: 2 }] }] | ||
}, | ||
{ | ||
arcs: [ | ||
{ __from: 11, __to: 20 }, | ||
{ __from: 21, __to: 10 } | ||
], | ||
nodes: [ | ||
{ inputs: [{ _id: 10 }], outputs: [{ _id: 11 }] }, | ||
{ inputs: [{ _id: 20 }], outputs: [{ _id: 21 }] } | ||
] | ||
}, | ||
{ | ||
arcs: [ | ||
{ __from: 11, __to: 20 }, | ||
{ __from: 11, __to: 30 }, | ||
{ __from: 22, __to: 30 }, | ||
{ __from: 22, __to: 31 }, | ||
{ __from: 32, __to: 10 } | ||
], | ||
nodes: [ | ||
{ inputs: [{ _id: 10 }], outputs: [{ _id: 11 }] }, | ||
{ inputs: [{ _id: 20 }], outputs: [{ _id: 22 }] }, | ||
{ inputs: [{ _id: 30 }, { _id: 31 }], outputs: [{ _id: 32 }] } | ||
] | ||
} | ||
]; | ||
|
||
for (const graph of graphs) { | ||
expect(graphHasCycle(getAdjacencyList(graph))).toBeTrue(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { AdjacencyList } from "../transformations"; | ||
|
||
/** | ||
* Detects if the given graph has a cycle. | ||
* | ||
* The cycle is determined by the explored nodes. | ||
* | ||
* @param graph the graph by its adjacency list | ||
* @returns true if the graphs contains at least one cycle | ||
*/ | ||
export function graphHasCycle(graph: AdjacencyList): boolean { | ||
throw new Error("TODO"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./graph.has-cycle"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters