Files
maptool/test/test_gnss_sim.rb
T

67 lines
2.9 KiB
Ruby

require_relative "test_helper"
require_relative "../lib/maptool/gnss_sim"
class GnssSimTest < Minitest::Test
# A 1 km route north, 36 km/h limit for the first half, untagged after.
class FixedRoute < Maptool::GnssSim
def random_route(_position)
{ geometry: { coordinates: [[17.0, 48.0], [17.0, 48.0 + 1000.0 / 111_320]] },
properties: { speed_limits: [[0.0, 40], [0.5, nil]] } }
end
end
def test_drives_the_route_at_its_limits_and_ends_at_its_end
fixes = []
sim = FixedRoute.new("http://valhalla", sleep: ->(_s) {})
last = sim.drive([17.0, 48.0]) { |fix| fixes << fix }
assert_in_delta 36 / 3.6, fixes.first[:speed], 0.01 # 90 % of 40
assert_in_delta 45 / 3.6, fixes.last[:speed], 0.01 # 90 % of an untagged road's 50
assert_in_delta 48.0 + 1000.0 / 111_320, fixes.last[:lat], 1e-9
assert_equal [17.0, 48.0 + 1000.0 / 111_320], last
assert fixes.each_cons(2).all? { |a, b| b[:lat] >= a[:lat] } # never backwards
assert_in_delta (1000 / (36 / 3.6) / 2 + 500 / (45 / 3.6)) / Maptool::GnssSim::TICK_S, fixes.size, 3 # a fix every TICK_S
end
# Started with the trip's route: from its start along it, before any random trip.
def test_starts_along_a_given_route
fixes = Queue.new
sim = Maptool::GnssSim.new("http://valhalla", sleep: ->(_s) {}, router: ->(*) { raise Maptool::Routing::RouteError, "done" })
route = { geometry: { coordinates: [[18.0, 49.0], [18.0, 49.001]] }, properties: { speed_limits: [[0.0, 50]] } }
sim.start(route: route) { |fix| fixes << fix }
first = fixes.pop
assert_in_delta 49.0, first[:lat], 0.0001
assert_in_delta 18.0, first[:lon], 1e-9
ensure
sim&.stop
end
# No road at the first random place: another one, inside the bbox.
class FirstPlaceAtSea < Maptool::GnssSim
attr_reader :tried
def random_route(position)
(@tried ||= []) << position
raise Maptool::Routing::RouteError, "no road" if @tried.size == 1
{ geometry: { coordinates: [position, position] }, properties: { distance_km: 5 } }
end
end
def test_a_random_start_is_inside_an_offline_country_with_a_road
sim = FirstPlaceAtSea.new("http://valhalla", random: Random.new(3))
position, route = sim.random_start([[17.0, 48.0, 18.0, 49.0]])
assert_equal 2, sim.tried.size
assert position[0].between?(17.0, 18.0) && position[1].between?(48.0, 49.0)
assert_equal position, route[:geometry][:coordinates].first
end
def test_a_route_under_a_km_is_another_try_and_none_at_all_an_error
lengths = [0.0, 0.2, 3.0]
router = ->(*) { { geometry: { coordinates: [[1, 2], [1, 3]] }, properties: { distance_km: lengths.shift } } }
sim = Maptool::GnssSim.new("http://valhalla", router: router)
assert_equal 3.0, sim.random_route([1, 2])[:properties][:distance_km]
lengths = [0.0] * 5
assert_raises(Maptool::Routing::RouteError) { sim.random_route([1, 2]) }
end
end