Skip to content

Commit e397427

Browse files
committed
test: add tests for findShortestPathAsync
1 parent e4fd99b commit e397427

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/graph/src/workers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const dijkstraAsync = (
6969
createWorker<{
7070
length: number;
7171
path: any;
72+
allPaths: any;
7273
}>(ALGORITHM.dijkstra)(...[graphData, source, directed, weightPropertyName]);
7374

7475
/**
@@ -99,6 +100,7 @@ const findShortestPathAsync = (
99100
createWorker<{
100101
length: number;
101102
path: any;
103+
allPaths: any;
102104
}>(ALGORITHM.findShortestPath)(...[graphData, start, end, directed, weightPropertyName]);
103105

104106
/**

packages/graph/tests/unit/find-path-async-spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ describe('(Async) Shortest Path from source to target on graph', () => {
7979
expect(path).toStrictEqual(['A', 'B', 'C']);
8080
});
8181

82+
it('find all shortest paths', async () => {
83+
const { findShortestPathAsync } = await getAlgorithm();
84+
const { length, allPath } = await findShortestPathAsync(data, 'A', 'F');
85+
expect(length).toBe(2);
86+
expect(allPath[0]).toStrictEqual(['A', 'E', 'F']);
87+
expect(allPath[1]).toStrictEqual(['A', 'D', 'F']);
88+
89+
const {
90+
length: directedLenght,
91+
path: directedPath,
92+
allPath: directedAllPath,
93+
} = await findShortestPathAsync(data, 'A', 'F', true);
94+
expect(directedLenght).toBe(2);
95+
expect(directedAllPath[0]).toStrictEqual(['A', 'E', 'F']);
96+
expect(directedPath).toStrictEqual(['A', 'E', 'F']);
97+
});
98+
8299
it('find all paths', async () => {
83100
const { findAllPathAsync } = await getAlgorithm();
84101
const allPaths = await findAllPathAsync(data, 'A', 'E');
@@ -95,4 +112,16 @@ describe('(Async) Shortest Path from source to target on graph', () => {
95112
expect(allPaths[0]).toStrictEqual(['A', 'D', 'E']);
96113
expect(allPaths[1]).toStrictEqual(['A', 'E']);
97114
});
115+
116+
it('find all shortest paths in weighted graph', async () => {
117+
const { findShortestPathAsync } = await getAlgorithm();
118+
data.edges.forEach((edge, i) => {
119+
edge.weight = ((i % 2) + 1) * 2;
120+
if (edge.source === 'F' && edge.target === 'D') edge.weight = 10;
121+
});
122+
const { length, path, allPath } = await findShortestPathAsync(data, 'A', 'F', false, 'weight');
123+
expect(length).toBe(6);
124+
expect(allPath[0]).toStrictEqual(['A', 'E', 'F']);
125+
expect(path).toStrictEqual(['A', 'E', 'F']);
126+
});
98127
});

0 commit comments

Comments
 (0)