Skip to content

Commit 2a95aec

Browse files
committed
Added validation and refactored tests
1 parent 1f851c6 commit 2a95aec

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/bmssp.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ class BMSSP {
2929

3030
// Method to calculate shortest paths (placeholder implementation)
3131
calculateShortestPaths(startNode) {
32+
// To clean the state before calculation
33+
this.initializeShortestPaths();
34+
35+
// validate startNode
36+
if (!this.nodeIDs.has(startNode)) {
37+
throw new Error("Start node not found in the graph");
38+
}
39+
3240
// Placeholder logic for shortest path calculation
3341
// This should be replaced with an actual implementation of BMSSP algorithm
34-
this.initializeShortestPaths();
3542
this.shortestPaths.set(startNode, 0);
3643
}
3744
}

test/main.test.mjs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, test, expect } from "@jest/globals";
22
import { BMSSP } from "../index.mjs";
33
import fs from "fs";
44

5+
// Load the roadNet-CA.txt graph and parse it into an array of edges
56
let roadNetCA = (() => {
67
let graph = [];
78
const filePath = new URL("./roadNet-CA.txt", import.meta.url).pathname;
@@ -19,16 +20,17 @@ let roadNetCA = (() => {
1920
return graph;
2021
})();
2122

23+
// Have a initialized BMSSP instance for tests
24+
const myBMSSP = new BMSSP(roadNetCA);
25+
2226
describe("BMSSP constructor", () => {
2327
test("initializes the graph correctly", () => {
24-
const myBMSSP = new BMSSP(roadNetCA);
2528
expect(myBMSSP.graph).toEqual(roadNetCA);
2629
});
2730
});
2831

2932
describe("BMSSP nodeIDs", () => {
3033
test("stores unique node IDs correctly", () => {
31-
const myBMSSP = new BMSSP(roadNetCA);
3234
const uniqueNodeIDs = new Set();
3335
roadNetCA.forEach((edge) => {
3436
uniqueNodeIDs.add(edge[0]);
@@ -40,7 +42,6 @@ describe("BMSSP nodeIDs", () => {
4042

4143
describe("BMSSP shortestPaths", () => {
4244
test("initializes shortest paths with Infinity", () => {
43-
const myBMSSP = new BMSSP(roadNetCA);
4445
const expectedShortestPaths = new Map();
4546
myBMSSP.nodeIDs.forEach((nodeId) => {
4647
expectedShortestPaths.set(nodeId, Infinity);
@@ -51,9 +52,14 @@ describe("BMSSP shortestPaths", () => {
5152

5253
describe("BMSSP initialize calculateShortestPaths", () => {
5354
test("sets the distance to the start node to 0", () => {
54-
const myBMSSP = new BMSSP(roadNetCA);
5555
const startNode = [...myBMSSP.nodeIDs][0];
5656
myBMSSP.calculateShortestPaths(startNode);
5757
expect(myBMSSP.shortestPaths.get(startNode)).toBe(0);
5858
});
59+
test("throws an error if the start node is not in the graph", () => {
60+
const invalidStartNode = -1; // Assuming -1 is not a valid node ID in the graph
61+
expect(() => {
62+
myBMSSP.calculateShortestPaths(invalidStartNode);
63+
}).toThrow("Start node not found in the graph");
64+
});
5965
});

0 commit comments

Comments
 (0)