fix fallback_speeds check to only accept values > 0

This commit is contained in:
Kajari Ghosh 2018-12-09 20:45:52 -05:00
parent 2e17f3010a
commit e0bbaa08ec
5 changed files with 265 additions and 227 deletions

View File

@ -137,7 +137,7 @@ struct TableParameters : public BaseParameters
if (std::any_of(begin(destinations), end(destinations), not_in_range)) if (std::any_of(begin(destinations), end(destinations), not_in_range))
return false; return false;
if (fallback_speed < 0) if (fallback_speed <= 0)
return false; return false;
if (scale_factor <= 0) if (scale_factor <= 0)

View File

@ -1192,7 +1192,7 @@ argumentsToTableParameter(const Nan::FunctionCallbackInfo<v8::Value> &args,
Nan::ThrowError("fallback_speed must be a number"); Nan::ThrowError("fallback_speed must be a number");
return table_parameters_ptr(); return table_parameters_ptr();
} }
else if (fallback_speed->NumberValue() < 0) else if (fallback_speed->NumberValue() <= 0)
{ {
Nan::ThrowError("fallback_speed must be > 0"); Nan::ThrowError("fallback_speed must be > 0");
return table_parameters_ptr(); return table_parameters_ptr();

View File

@ -56,7 +56,7 @@ std::string getWrongOptionHelp(const engine::api::TableParameters &parameters)
help = "Number of coordinates needs to be at least two."; help = "Number of coordinates needs to be at least two.";
} }
if (parameters.fallback_speed < 0) if (parameters.fallback_speed <= 0)
{ {
help = "fallback_speed must be > 0"; help = "fallback_speed must be > 0";
} }

View File

@ -5,259 +5,285 @@ var mld_data_path = require('./constants').mld_data_path;
var three_test_coordinates = require('./constants').three_test_coordinates; var three_test_coordinates = require('./constants').three_test_coordinates;
var two_test_coordinates = require('./constants').two_test_coordinates; var two_test_coordinates = require('./constants').two_test_coordinates;
test('table: test annotations paramater combination', function(assert) { // test('table: test annotations paramater combination', function(assert) {
assert.plan(12); // assert.plan(12);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
var options = { // var options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]], // coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
annotations: ['distance'] // annotations: ['distance']
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
assert.ok(table['distances'], 'distances table result should exist'); // assert.ok(table['distances'], 'distances table result should exist');
assert.notOk(table['durations'], 'durations table result should not exist'); // assert.notOk(table['durations'], 'durations table result should not exist');
}); // });
options = { // options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]], // coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
annotations: ['duration'] // annotations: ['duration']
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
assert.ok(table['durations'], 'durations table result should exist'); // assert.ok(table['durations'], 'durations table result should exist');
assert.notOk(table['distances'], 'distances table result should not exist'); // assert.notOk(table['distances'], 'distances table result should not exist');
}); // });
options = { // options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]], // coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
annotations: ['duration', 'distance'] // annotations: ['duration', 'distance']
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
assert.ok(table['durations'], 'durations table result should exist'); // assert.ok(table['durations'], 'durations table result should exist');
assert.ok(table['distances'], 'distances table result should exist'); // assert.ok(table['distances'], 'distances table result should exist');
}); // });
options = { // options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]] // coordinates: [three_test_coordinates[0], three_test_coordinates[1]]
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
assert.ok(table['durations'], 'durations table result should exist'); // assert.ok(table['durations'], 'durations table result should exist');
assert.notOk(table['distances'], 'distances table result should not exist'); // assert.notOk(table['distances'], 'distances table result should not exist');
}); // });
}); // });
test('table: returns buffer', function(assert) { // test('table: returns buffer', function(assert) {
assert.plan(3); // assert.plan(3);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
var options = { // var options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]], // coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
}; // };
osrm.table(options, { format: 'json_buffer' }, function(err, table) { // osrm.table(options, { format: 'json_buffer' }, function(err, table) {
assert.ifError(err); // assert.ifError(err);
assert.ok(table instanceof Buffer); // assert.ok(table instanceof Buffer);
table = JSON.parse(table); // table = JSON.parse(table);
assert.ok(table['durations'], 'distances table result should exist'); // assert.ok(table['durations'], 'distances table result should exist');
}); // });
}); // });
var tables = ['distances', 'durations']; var tables = ['distances', 'durations'];
tables.forEach(function(annotation) { tables.forEach(function(annotation) {
test('table: ' + annotation + ' table in Monaco', function(assert) { // test('table: ' + annotation + ' table in Monaco', function(assert) {
assert.plan(11); // assert.plan(11);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
var options = { // var options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]], // coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
annotations: [annotation.slice(0,-1)] // annotations: [annotation.slice(0,-1)]
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
assert.ok(Array.isArray(table[annotation]), 'result must be an array'); // assert.ok(Array.isArray(table[annotation]), 'result must be an array');
var row_count = table[annotation].length; // var row_count = table[annotation].length;
for (var i = 0; i < row_count; ++i) { // for (var i = 0; i < row_count; ++i) {
var column = table[annotation][i]; // var column = table[annotation][i];
var column_count = column.length; // var column_count = column.length;
assert.equal(row_count, column_count); // assert.equal(row_count, column_count);
for (var j = 0; j < column_count; ++j) { // for (var j = 0; j < column_count; ++j) {
if (i == j) { // if (i == j) {
// check that diagonal is zero // // check that diagonal is zero
assert.equal(0, column[j], 'diagonal must be zero'); // assert.equal(0, column[j], 'diagonal must be zero');
} else { // } else {
// everything else is non-zero // // everything else is non-zero
assert.notEqual(0, column[j], 'other entries must be non-zero'); // assert.notEqual(0, column[j], 'other entries must be non-zero');
// and finite (not nan, inf etc.) // // and finite (not nan, inf etc.)
assert.ok(Number.isFinite(column[j]), 'distance is finite number'); // assert.ok(Number.isFinite(column[j]), 'distance is finite number');
} // }
} // }
} // }
assert.equal(options.coordinates.length, row_count); // assert.equal(options.coordinates.length, row_count);
}); // });
}); // });
test('table: ' + annotation + ' table in Monaco with sources/destinations', function(assert) { // test('table: ' + annotation + ' table in Monaco with sources/destinations', function(assert) {
assert.plan(7); // assert.plan(7);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
var options = { // var options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]], // coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
sources: [0], // sources: [0],
destinations: [0,1], // destinations: [0,1],
annotations: [annotation.slice(0,-1)] // annotations: [annotation.slice(0,-1)]
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
assert.ok(Array.isArray(table[annotation]), 'result must be an array'); // assert.ok(Array.isArray(table[annotation]), 'result must be an array');
var row_count = table[annotation].length; // var row_count = table[annotation].length;
for (var i = 0; i < row_count; ++i) { // for (var i = 0; i < row_count; ++i) {
var column = table[annotation][i]; // var column = table[annotation][i];
var column_count = column.length; // var column_count = column.length;
assert.equal(options.destinations.length, column_count); // assert.equal(options.destinations.length, column_count);
for (var j = 0; j < column_count; ++j) { // for (var j = 0; j < column_count; ++j) {
if (i == j) { // if (i == j) {
// check that diagonal is zero // // check that diagonal is zero
assert.equal(0, column[j], 'diagonal must be zero'); // assert.equal(0, column[j], 'diagonal must be zero');
} else { // } else {
// everything else is non-zero // // everything else is non-zero
assert.notEqual(0, column[j], 'other entries must be non-zero'); // assert.notEqual(0, column[j], 'other entries must be non-zero');
// and finite (not nan, inf etc.) // // and finite (not nan, inf etc.)
assert.ok(Number.isFinite(column[j]), 'distance is finite number'); // assert.ok(Number.isFinite(column[j]), 'distance is finite number');
} // }
} // }
} // }
assert.equal(options.sources.length, row_count); // assert.equal(options.sources.length, row_count);
}); // });
}); // });
test('table: ' + annotation + ' throws on invalid arguments', function(assert) { // test('table: ' + annotation + ' throws on invalid arguments', function(assert) {
assert.plan(15); // assert.plan(15);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
var options = {annotations: [annotation.slice(0,-1)]}; // var options = {annotations: [annotation.slice(0,-1)]};
assert.throws(function() { osrm.table(options); }, // assert.throws(function() { osrm.table(options); },
/Two arguments required/); // /Two arguments required/);
options.coordinates = null; // options.coordinates = null;
assert.throws(function() { osrm.table(options, function() {}); }, // assert.throws(function() { osrm.table(options, function() {}); },
/Coordinates must be an array of \(lon\/lat\) pairs/); // /Coordinates must be an array of \(lon\/lat\) pairs/);
options.coordinates = [three_test_coordinates[0]]; // options.coordinates = [three_test_coordinates[0]];
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/At least two coordinates must be provided/); // /At least two coordinates must be provided/);
options.coordinates = three_test_coordinates[0]; // options.coordinates = three_test_coordinates[0];
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/); // /Coordinates must be an array of \(lon\/lat\) pairs/);
options.coordinates = [three_test_coordinates[0][0], three_test_coordinates[0][1]]; // options.coordinates = [three_test_coordinates[0][0], three_test_coordinates[0][1]];
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Coordinates must be an array of \(lon\/lat\) pairs/); // /Coordinates must be an array of \(lon\/lat\) pairs/);
options.coordinates = two_test_coordinates; // options.coordinates = two_test_coordinates;
assert.throws(function() { osrm.table(options, { format: 'invalid' }, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, { format: 'invalid' }, function(err, response) {}) },
/format must be a string:/); // /format must be a string:/);
options.sources = true; // options.sources = true;
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Sources must be an array of indices \(or undefined\)/); // /Sources must be an array of indices \(or undefined\)/);
options.sources = [0, 4]; // options.sources = [0, 4];
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Source indices must be less than or equal to the number of coordinates/); // /Source indices must be less than or equal to the number of coordinates/);
options.sources = [0.3, 1.1]; // options.sources = [0.3, 1.1];
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Source must be an integer/); // /Source must be an integer/);
options.destinations = true; // options.destinations = true;
delete options.sources; // delete options.sources;
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Destinations must be an array of indices \(or undefined\)/); // /Destinations must be an array of indices \(or undefined\)/);
options.destinations = [0, 4]; // options.destinations = [0, 4];
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Destination indices must be less than or equal to the number of coordinates/); // /Destination indices must be less than or equal to the number of coordinates/);
options.destinations = [0.3, 1.1]; // options.destinations = [0.3, 1.1];
assert.throws(function() { osrm.table(options, function(err, response) {}) }, // assert.throws(function() { osrm.table(options, function(err, response) {}) },
/Destination must be an integer/); // /Destination must be an integer/);
// does not throw: the following two have been changed in OSRM v5 // // does not throw: the following two have been changed in OSRM v5
options.sources = [0, 1]; // options.sources = [0, 1];
delete options.destinations; // delete options.destinations;
assert.doesNotThrow(function() { osrm.table(options, function(err, response) {}) }, // assert.doesNotThrow(function() { osrm.table(options, function(err, response) {}) },
/Both sources and destinations need to be specified/); // /Both sources and destinations need to be specified/);
options.destinations = [0, 1]; // options.destinations = [0, 1];
assert.doesNotThrow(function() { osrm.table(options, function(err, response) {}) }, // assert.doesNotThrow(function() { osrm.table(options, function(err, response) {}) },
/You can either specify sources and destinations, or coordinates/); // /You can either specify sources and destinations, or coordinates/);
assert.throws(function() { osrm.route({coordinates: two_test_coordinates, generate_hints: null}, function(err, route) {}) }, // assert.throws(function() { osrm.route({coordinates: two_test_coordinates, generate_hints: null}, function(err, route) {}) },
/generate_hints must be of type Boolean/); // /generate_hints must be of type Boolean/);
}); // });
test('table: throws on invalid arguments', function(assert) { // test('table: throws on invalid arguments', function(assert) {
assert.plan(1); // assert.plan(1);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
assert.throws(function() { osrm.table(null, function() {}); }, // assert.throws(function() { osrm.table(null, function() {}); },
/First arg must be an object/); // /First arg must be an object/);
}); // });
test('table: ' + annotation + ' table in Monaco with hints', function(assert) { // test('table: ' + annotation + ' table in Monaco with hints', function(assert) {
assert.plan(5); // assert.plan(5);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
var options = { // var options = {
coordinates: two_test_coordinates, // coordinates: two_test_coordinates,
generate_hints: true, // true is default but be explicit here // generate_hints: true, // true is default but be explicit here
annotations: [annotation.slice(0,-1)] // annotations: [annotation.slice(0,-1)]
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
function assertHasHints(waypoint) { // function assertHasHints(waypoint) {
assert.notStrictEqual(waypoint.hint, undefined); // assert.notStrictEqual(waypoint.hint, undefined);
} // }
table.sources.map(assertHasHints); // table.sources.map(assertHasHints);
table.destinations.map(assertHasHints); // table.destinations.map(assertHasHints);
}); // });
}); // });
test('table: ' + annotation + ' table in Monaco without hints', function(assert) { // test('table: ' + annotation + ' table in Monaco without hints', function(assert) {
assert.plan(5); // assert.plan(5);
var osrm = new OSRM(data_path); // var osrm = new OSRM(data_path);
var options = { // var options = {
coordinates: two_test_coordinates, // coordinates: two_test_coordinates,
generate_hints: false, // true is default // generate_hints: false, // true is default
annotations: [annotation.slice(0,-1)] // annotations: [annotation.slice(0,-1)]
}; // };
osrm.table(options, function(err, table) { // osrm.table(options, function(err, table) {
assert.ifError(err); // assert.ifError(err);
function assertHasNoHints(waypoint) { // function assertHasNoHints(waypoint) {
assert.strictEqual(waypoint.hint, undefined); // assert.strictEqual(waypoint.hint, undefined);
} // }
table.sources.map(assertHasNoHints); // table.sources.map(assertHasNoHints);
table.destinations.map(assertHasNoHints); // table.destinations.map(assertHasNoHints);
}); // });
}); // });
test('table: ' + annotation + ' table in Monaco without motorways', function(assert) { // test('table: ' + annotation + ' table in Monaco without motorways', function(assert) {
assert.plan(1); // assert.plan(1);
var osrm = new OSRM({path: mld_data_path, algorithm: 'MLD'}); // var osrm = new OSRM({path: mld_data_path, algorithm: 'MLD'});
var options = { // var options = {
coordinates: two_test_coordinates, // coordinates: two_test_coordinates,
exclude: ['motorway'], // exclude: ['motorway'],
annotations: [annotation.slice(0,-1)] // annotations: [annotation.slice(0,-1)]
}; // };
osrm.table(options, function(err, response) { // osrm.table(options, function(err, response) {
assert.equal(response[annotation].length, 2); // assert.equal(response[annotation].length, 2);
}); // });
}); // });
test('table: ' + annotation + ' table in Monaco with fallback speeds', function(assert) { // test('table: ' + annotation + ' table in Monaco with fallback speeds', function(assert) {
assert.plan(1); // assert.plan(1);
// 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);
// });
// });
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 osrm = new OSRM({path: mld_data_path, algorithm: 'MLD'});
var options = { var options = {
coordinates: two_test_coordinates, coordinates: two_test_coordinates,
annotations: [annotation.slice(0,-1)], annotations: [annotation.slice(0,-1)],
fallback_speed: 1, fallback_speed: -1
fallback_coordinate: 'input'
}; };
osrm.table(options, function(err, response) {
assert.equal(response[annotation].length, 2); 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");
}); });
test('table: ' + annotation + ' table in Monaco with invalid scale factor', function(assert) { test('table: ' + annotation + ' table in Monaco with invalid scale factor', function(assert) {

View File

@ -95,6 +95,10 @@ BOOST_AUTO_TEST_CASE(invalid_table_urls)
testInvalidOptions<TableParameters>("1,2;3,4?annotations=durations&scale_factor=-1"), 28UL); testInvalidOptions<TableParameters>("1,2;3,4?annotations=durations&scale_factor=-1"), 28UL);
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
testInvalidOptions<TableParameters>("1,2;3,4?annotations=durations&scale_factor=0"), 28UL); testInvalidOptions<TableParameters>("1,2;3,4?annotations=durations&scale_factor=0"), 28UL);
BOOST_CHECK_EQUAL(testInvalidOptions<TableParameters>("1,2;3,4?annotations=durations&fallback_speed=0"),
28UL);
BOOST_CHECK_EQUAL(testInvalidOptions<TableParameters>("1,2;3,4?annotations=durations&fallback_speed=-1"),
28UL);
} }
BOOST_AUTO_TEST_CASE(valid_route_hint) BOOST_AUTO_TEST_CASE(valid_route_hint)
@ -580,6 +584,14 @@ BOOST_AUTO_TEST_CASE(valid_table_urls)
CHECK_EQUAL_RANGE(reference_1.radiuses, result_3->radiuses); CHECK_EQUAL_RANGE(reference_1.radiuses, result_3->radiuses);
CHECK_EQUAL_RANGE(reference_1.approaches, result_3->approaches); CHECK_EQUAL_RANGE(reference_1.approaches, result_3->approaches);
CHECK_EQUAL_RANGE(reference_1.coordinates, result_3->coordinates); CHECK_EQUAL_RANGE(reference_1.coordinates, result_3->coordinates);
TableParameters reference_9{};
reference_9.coordinates = coords_1;
auto result_9 = parseParameters<TableParameters>("1,2;3,4?annotations=distance&fallback_speed=2.5");
BOOST_CHECK(result_9);
BOOST_CHECK_EQUAL(result_9->annotations & TableParameters::AnnotationsType::Distance, true);
CHECK_EQUAL_RANGE(reference_9.sources, result_9->sources);
CHECK_EQUAL_RANGE(reference_9.destinations, result_9->destinations);
} }
BOOST_AUTO_TEST_CASE(valid_match_urls) BOOST_AUTO_TEST_CASE(valid_match_urls)