@@ -2,6 +2,7 @@ import { describe, test, expect } from "@jest/globals";
22import { BMSSP } from "../index.mjs" ;
33import fs from "fs" ;
44
5+ // Load the roadNet-CA.txt graph and parse it into an array of edges
56let 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 an initialized BMSSP instance for tests
24+ const myBMSSP = new BMSSP ( roadNetCA ) ;
25+
2226describe ( "BMSSP constructor" , ( ) => {
2327 test ( "initializes the graph correctly" , ( ) => {
24- const myBMSSP = new BMSSP ( roadNetCA ) ;
2528 expect ( myBMSSP . graph ) . toEqual ( roadNetCA ) ;
2629 } ) ;
2730} ) ;
2831
2932describe ( "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,11 +42,24 @@ describe("BMSSP nodeIDs", () => {
4042
4143describe ( "BMSSP shortestPaths" , ( ) => {
4244 test ( "initializes shortest paths with Infinity" , ( ) => {
43- const myBMSSP = new BMSSP ( roadNetCA ) ;
44- const expectedShortestPaths = [ ] ;
45+ const expectedShortestPaths = new Map ( ) ;
4546 myBMSSP . nodeIDs . forEach ( ( nodeId ) => {
46- expectedShortestPaths . push ( [ nodeId , Infinity ] ) ;
47+ expectedShortestPaths . set ( nodeId , Infinity ) ;
4748 } ) ;
4849 expect ( myBMSSP . shortestPaths ) . toEqual ( expectedShortestPaths ) ;
4950 } ) ;
5051} ) ;
52+
53+ describe ( "BMSSP initialize calculateShortestPaths" , ( ) => {
54+ test ( "sets the distance to the start node to 0" , ( ) => {
55+ const startNode = [ ...myBMSSP . nodeIDs ] [ 0 ] ;
56+ myBMSSP . calculateShortestPaths ( startNode ) ;
57+ expect ( myBMSSP . shortestPaths . get ( startNode ) ) . toBe ( 0 ) ;
58+ } ) ;
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+ } ) ;
65+ } ) ;
0 commit comments