2017-03-01 12:27:57 -05:00
var OSRM = require ( '../../' ) ;
var test = require ( 'tape' ) ;
2017-03-29 09:28:45 -04:00
var data _path = require ( './constants' ) . data _path ;
2017-08-16 17:35:02 -04:00
var mld _data _path = require ( './constants' ) . mld _data _path ;
2017-03-29 09:28:45 -04:00
var three _test _coordinates = require ( './constants' ) . three _test _coordinates ;
var two _test _coordinates = require ( './constants' ) . two _test _coordinates ;
2022-08-29 16:01:26 -04:00
const flatbuffers = require ( '../../features/support/flatbuffers' ) . flatbuffers ;
const FBResult = require ( '../../features/support/fbresult_generated' ) . osrm . engine . api . fbresult . FBResult ;
test ( 'table: flatbuffer format' , function ( assert ) {
assert . plan ( 3 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ] ,
format : 'flatbuffers'
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( table instanceof Buffer ) ;
const fb = FBResult . getRootAsFBResult ( new flatbuffers . ByteBuffer ( table ) ) ;
assert . ok ( fb . table ( ) ) ;
} ) ;
} ) ;
2017-03-01 12:27:57 -05:00
2018-04-24 11:05:35 -04:00
test ( 'table: test annotations paramater combination' , function ( assert ) {
assert . plan ( 12 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ] ,
annotations : [ 'distance' ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( table [ 'distances' ] , 'distances table result should exist' ) ;
assert . notOk ( table [ 'durations' ] , 'durations table result should not exist' ) ;
} ) ;
options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ] ,
annotations : [ 'duration' ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( table [ 'durations' ] , 'durations table result should exist' ) ;
assert . notOk ( table [ 'distances' ] , 'distances table result should not exist' ) ;
} ) ;
options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ] ,
annotations : [ 'duration' , 'distance' ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( table [ 'durations' ] , 'durations table result should exist' ) ;
assert . ok ( table [ 'distances' ] , 'distances table result should exist' ) ;
} ) ;
options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( table [ 'durations' ] , 'durations table result should exist' ) ;
assert . notOk ( table [ 'distances' ] , 'distances table result should not exist' ) ;
} ) ;
} ) ;
2017-03-29 09:28:45 -04:00
2018-09-02 01:22:37 -04:00
test ( 'table: returns buffer' , function ( assert ) {
assert . plan ( 3 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ] ,
} ;
osrm . table ( options , { format : 'json_buffer' } , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( table instanceof Buffer ) ;
table = JSON . parse ( table ) ;
assert . ok ( table [ 'durations' ] , 'distances table result should exist' ) ;
} ) ;
} ) ;
2018-04-20 18:18:55 -04:00
var tables = [ 'distances' , 'durations' ] ;
tables . forEach ( function ( annotation ) {
test ( 'table: ' + annotation + ' table in Monaco' , function ( assert ) {
assert . plan ( 11 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ] ,
annotations : [ annotation . slice ( 0 , - 1 ) ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( Array . isArray ( table [ annotation ] ) , 'result must be an array' ) ;
var row _count = table [ annotation ] . length ;
for ( var i = 0 ; i < row _count ; ++ i ) {
var column = table [ annotation ] [ i ] ;
var column _count = column . length ;
assert . equal ( row _count , column _count ) ;
for ( var j = 0 ; j < column _count ; ++ j ) {
if ( i == j ) {
// check that diagonal is zero
assert . equal ( 0 , column [ j ] , 'diagonal must be zero' ) ;
} else {
// everything else is non-zero
assert . notEqual ( 0 , column [ j ] , 'other entries must be non-zero' ) ;
// and finite (not nan, inf etc.)
assert . ok ( Number . isFinite ( column [ j ] ) , 'distance is finite number' ) ;
}
2017-03-01 12:27:57 -05:00
}
}
2018-04-20 18:18:55 -04:00
assert . equal ( options . coordinates . length , row _count ) ;
} ) ;
2017-03-01 12:27:57 -05:00
} ) ;
2018-04-20 18:18:55 -04:00
test ( 'table: ' + annotation + ' table in Monaco with sources/destinations' , function ( assert ) {
assert . plan ( 7 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : [ three _test _coordinates [ 0 ] , three _test _coordinates [ 1 ] ] ,
sources : [ 0 ] ,
destinations : [ 0 , 1 ] ,
annotations : [ annotation . slice ( 0 , - 1 ) ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
assert . ok ( Array . isArray ( table [ annotation ] ) , 'result must be an array' ) ;
var row _count = table [ annotation ] . length ;
for ( var i = 0 ; i < row _count ; ++ i ) {
var column = table [ annotation ] [ i ] ;
var column _count = column . length ;
assert . equal ( options . destinations . length , column _count ) ;
for ( var j = 0 ; j < column _count ; ++ j ) {
if ( i == j ) {
// check that diagonal is zero
assert . equal ( 0 , column [ j ] , 'diagonal must be zero' ) ;
} else {
// everything else is non-zero
assert . notEqual ( 0 , column [ j ] , 'other entries must be non-zero' ) ;
// and finite (not nan, inf etc.)
assert . ok ( Number . isFinite ( column [ j ] ) , 'distance is finite number' ) ;
}
2017-03-01 12:27:57 -05:00
}
}
2018-04-20 18:18:55 -04:00
assert . equal ( options . sources . length , row _count ) ;
} ) ;
2017-03-01 12:27:57 -05:00
} ) ;
2018-04-20 18:18:55 -04:00
test ( 'table: ' + annotation + ' throws on invalid arguments' , function ( assert ) {
2022-08-22 15:47:47 -04:00
assert . plan ( 18 ) ;
2018-04-20 18:18:55 -04:00
var osrm = new OSRM ( data _path ) ;
var options = { annotations : [ annotation . slice ( 0 , - 1 ) ] } ;
assert . throws ( function ( ) { osrm . table ( options ) ; } ,
/Two arguments required/ ) ;
options . coordinates = null ;
assert . throws ( function ( ) { osrm . table ( options , function ( ) { } ) ; } ,
/Coordinates must be an array of \(lon\/lat\) pairs/ ) ;
options . coordinates = [ three _test _coordinates [ 0 ] ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/At least two coordinates must be provided/ ) ;
options . coordinates = three _test _coordinates [ 0 ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Coordinates must be an array of \(lon\/lat\) pairs/ ) ;
options . coordinates = [ three _test _coordinates [ 0 ] [ 0 ] , three _test _coordinates [ 0 ] [ 1 ] ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Coordinates must be an array of \(lon\/lat\) pairs/ ) ;
options . coordinates = two _test _coordinates ;
2018-09-02 01:22:37 -04:00
assert . throws ( function ( ) { osrm . table ( options , { format : 'invalid' } , function ( err , response ) { } ) } ,
/format must be a string:/ ) ;
2018-04-20 18:18:55 -04:00
options . sources = true ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Sources must be an array of indices \(or undefined\)/ ) ;
options . sources = [ 0 , 4 ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
2021-01-28 09:02:01 -05:00
/Source indices must be less than the number of coordinates/ ) ;
2018-04-20 18:18:55 -04:00
options . sources = [ 0.3 , 1.1 ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Source must be an integer/ ) ;
2021-01-28 09:02:01 -05:00
options . sources = [ 0 , 1 , 2 ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Source indices must be less than the number of coordinates/ ) ;
2018-04-20 18:18:55 -04:00
options . destinations = true ;
delete options . sources ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Destinations must be an array of indices \(or undefined\)/ ) ;
options . destinations = [ 0 , 4 ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
2021-01-28 09:02:01 -05:00
/Destination indices must be less than the number of coordinates/ ) ;
2018-04-20 18:18:55 -04:00
options . destinations = [ 0.3 , 1.1 ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Destination must be an integer/ ) ;
2021-01-28 09:02:01 -05:00
options . destinations = [ 0 , 4 ] ;
assert . throws ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Destination indices must be less than the number of coordinates/ ) ;
2018-04-20 18:18:55 -04:00
// does not throw: the following two have been changed in OSRM v5
options . sources = [ 0 , 1 ] ;
delete options . destinations ;
assert . doesNotThrow ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/Both sources and destinations need to be specified/ ) ;
options . destinations = [ 0 , 1 ] ;
assert . doesNotThrow ( function ( ) { osrm . table ( options , function ( err , response ) { } ) } ,
/You can either specify sources and destinations, or coordinates/ ) ;
assert . throws ( function ( ) { osrm . route ( { coordinates : two _test _coordinates , generate _hints : null } , function ( err , route ) { } ) } ,
/generate_hints must be of type Boolean/ ) ;
2022-08-22 15:47:47 -04:00
assert . throws ( function ( ) { osrm . route ( { coordinates : two _test _coordinates , skip _waypoints : null } , function ( err , route ) { } ) } ,
/skip_waypoints must be of type Boolean/ ) ;
2018-04-20 18:18:55 -04:00
} ) ;
2017-03-01 12:27:57 -05:00
2018-04-20 18:18:55 -04:00
test ( 'table: throws on invalid arguments' , function ( assert ) {
assert . plan ( 1 ) ;
var osrm = new OSRM ( data _path ) ;
assert . throws ( function ( ) { osrm . table ( null , function ( ) { } ) ; } ,
/First arg must be an object/ ) ;
} ) ;
2017-03-01 12:27:57 -05:00
2018-04-20 18:18:55 -04:00
test ( 'table: ' + annotation + ' table in Monaco with hints' , function ( assert ) {
assert . plan ( 5 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : two _test _coordinates ,
generate _hints : true , // true is default but be explicit here
annotations : [ annotation . slice ( 0 , - 1 ) ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
function assertHasHints ( waypoint ) {
assert . notStrictEqual ( waypoint . hint , undefined ) ;
}
table . sources . map ( assertHasHints ) ;
table . destinations . map ( assertHasHints ) ;
} ) ;
2017-03-01 12:27:57 -05:00
} ) ;
2018-04-20 18:18:55 -04:00
test ( 'table: ' + annotation + ' table in Monaco without hints' , function ( assert ) {
assert . plan ( 5 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : two _test _coordinates ,
generate _hints : false , // true is default
annotations : [ annotation . slice ( 0 , - 1 ) ]
} ;
osrm . table ( options , function ( err , table ) {
assert . ifError ( err ) ;
function assertHasNoHints ( waypoint ) {
assert . strictEqual ( waypoint . hint , undefined ) ;
}
table . sources . map ( assertHasNoHints ) ;
table . destinations . map ( assertHasNoHints ) ;
} ) ;
2017-03-01 12:27:57 -05:00
} ) ;
2017-08-16 17:35:02 -04:00
2022-08-22 15:47:47 -04:00
test ( 'table: ' + annotation + ' table in Monaco without waypoints' , function ( assert ) {
assert . plan ( 2 ) ;
var osrm = new OSRM ( data _path ) ;
var options = {
coordinates : two _test _coordinates ,
skip _waypoints : true , // false is default
annotations : [ annotation . slice ( 0 , - 1 ) ]
} ;
osrm . table ( options , function ( err , table ) {
assert . strictEqual ( table . sources , undefined ) ;
assert . strictEqual ( table . destinations , undefined ) ;
} ) ;
} ) ;
2018-04-20 18:18:55 -04:00
test ( 'table: ' + annotation + ' table in Monaco without motorways' , function ( assert ) {
2018-12-11 12:21:57 -05:00
assert . plan ( 2 ) ;
2018-04-20 18:18:55 -04:00
var osrm = new OSRM ( { path : mld _data _path , algorithm : 'MLD' } ) ;
var options = {
coordinates : two _test _coordinates ,
exclude : [ 'motorway' ] ,
annotations : [ annotation . slice ( 0 , - 1 ) ]
} ;
osrm . table ( options , function ( err , response ) {
2018-04-07 22:20:59 -04:00
assert . equal ( response [ annotation ] . length , 2 ) ;
2018-12-11 12:21:57 -05:00
assert . strictEqual ( response . fallback _speed _cells , undefined ) ;
2018-04-20 18:18:55 -04:00
} ) ;
2017-08-16 17:35:02 -04:00
} ) ;
2018-11-01 01:02:54 -04:00
test ( 'table: ' + annotation + ' table in Monaco with fallback speeds' , function ( assert ) {
2018-12-11 12:21:57 -05:00
assert . plan ( 2 ) ;
2018-11-01 01:02:54 -04:00
var osrm = new OSRM ( { path : mld _data _path , algorithm : 'MLD' } ) ;
var options = {
coordinates : two _test _coordinates ,
annotations : [ annotation . slice ( 0 , - 1 ) ] ,
fallback _speed : 1 ,
fallback _coordinate : 'input'
} ;
osrm . table ( options , function ( err , response ) {
assert . equal ( response [ annotation ] . length , 2 ) ;
2018-12-11 12:21:57 -05:00
assert . equal ( response [ 'fallback_speed_cells' ] . length , 0 ) ;
2018-11-01 01:02:54 -04:00
} ) ;
} ) ;
2018-12-10 14:53:30 -05:00
test ( 'table: ' + annotation + ' table in Monaco with invalid fallback speeds and fallback coordinates' , function ( assert ) {
assert . plan ( 4 ) ;
var osrm = new OSRM ( { path : mld _data _path , algorithm : 'MLD' } ) ;
var options = {
coordinates : two _test _coordinates ,
annotations : [ annotation . slice ( 0 , - 1 ) ] ,
fallback _speed : - 1
} ;
assert . throws ( ( ) => osrm . table ( options , ( err , res ) => { } ) , /fallback_speed must be > 0/ , "should throw on invalid fallback_speeds" ) ;
options . fallback _speed = '10' ;
assert . throws ( ( ) => osrm . table ( options , ( err , res ) => { } ) , /fallback_speed must be a number/ , "should throw on invalid fallback_speeds" ) ;
options . fallback _speed = 10 ;
options . fallback _coordinate = 'bla' ;
assert . throws ( ( ) => osrm . table ( options , ( err , res ) => { } ) , /fallback_coordinate' param must be one of \[input, snapped\]/ , "should throw on invalid fallback_coordinate" ) ;
options . fallback _coordinate = 10 ;
assert . throws ( ( ) => osrm . table ( options , ( err , res ) => { } ) , /fallback_coordinate must be a string: \[input, snapped\]/ , "should throw on invalid fallback_coordinate" ) ;
} ) ;
2018-12-10 13:41:44 -05:00
test ( 'table: ' + annotation + ' table in Monaco with invalid scale factor' , function ( assert ) {
assert . plan ( 3 ) ;
var osrm = new OSRM ( { path : mld _data _path , algorithm : 'MLD' } ) ;
var options = {
coordinates : two _test _coordinates ,
annotations : [ annotation . slice ( 0 , - 1 ) ] ,
scale _factor : - 1
} ;
assert . throws ( ( ) => osrm . table ( options , ( err , res ) => { } ) , /scale_factor must be > 0/ , "should throw on invalid scale_factor value" ) ;
options . scale _factor = '-1' ;
assert . throws ( ( ) => osrm . table ( options , ( err , res ) => { } ) , /scale_factor must be a number/ , "should throw on invalid scale_factor value" ) ;
options . scale _factor = 0 ;
assert . throws ( ( ) => osrm . table ( options , ( err , res ) => { } ) , /scale_factor must be > 0/ , "should throw on invalid scale_factor value" ) ;
} ) ;
2017-08-16 17:35:02 -04:00
} ) ;