route_test.ml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. (*
  2. * route_test.ml
  3. *
  4. * Created by Marcus Rohrmoser on 16.05.20.
  5. * Copyright © 2020-2021 Marcus Rohrmoser mobile Software http://mro.name/~me. All rights reserved.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *)
  20. open Lib.Route
  21. let test_qs () =
  22. assert (
  23. match coord_from_qs "q=1.2,3.4" with Ok (1.2, 3.4) -> true | _ -> false);
  24. assert (
  25. match coord_from_qs "q=geo%3A47.5440%2C15.4396%3Fz%3D12" with
  26. | Ok (47.5440, 15.4396) -> true
  27. | _ -> false);
  28. match coord_from_qs "q=1.u2 , ; 3.4" with
  29. | Error (`NoMatch (_, original)) -> assert (original != "")
  30. | _ -> assert false
  31. let test_deg () =
  32. (*
  33. let la, lo =
  34. "q=47.427%C2%B0,+13.059%C2%B0" |> coord_from_qs |> Result.get_ok
  35. in
  36. Printf.printf "test_deg %f, %f" la lo;
  37. *)
  38. assert (Ok (47.427, 13.059) = ("q=47.427%C2%B0,+13.059%C2%B0" |> coord_from_qs));
  39. (* assert (Ok (47.427, 13.059) = ("q=47.427°, 13.059°" |> coord_from_qs)); *)
  40. (* assert (
  41. Ok (47.5440, 15.4396) = ("q=47° 25′ 37″ N, 13° 3′ 32″ O" |> coord_from_qs)); *)
  42. let la, lo =
  43. "q=47%C2%B0+25%E2%80%B2+37%E2%80%B3+N,+13%C2%B0+3%E2%80%B2+32%E2%80%B3+O"
  44. |> coord_from_qs |> Result.get_ok
  45. in
  46. (* Printf.printf "%f,%f\n" la lo; *)
  47. assert (47.427 -. la < 1e-1);
  48. assert (13.059 -. lo < 1e-1);
  49. let la', lo' =
  50. "q=47%C2%B0+25%E2%80%B2+37.2%E2%80%B3+N,+13%C2%B0+3%E2%80%B2+32.4%E2%80%B3+E"
  51. |> coord_from_qs |> Result.get_ok
  52. in
  53. (* Printf.printf "%f,%f\n" la' lo'; *)
  54. assert (47.427 -. la' < 1e-1);
  55. assert (13.059 -. lo' < 1e-1);
  56. assert true
  57. let test_deg_min_sec () =
  58. let open Tyre in
  59. let dms =
  60. let ws = pcre "( |\t|\\+|%20)*" in
  61. conv
  62. (fun ((d, (m, s)), he) ->
  63. let si = match he with "S" | "W" -> -1. | _ -> 1.
  64. and d' = d
  65. and m' = m
  66. and s' = s in
  67. si *. (d' +. ((m' +. (s' /. 60.)) /. 60.)))
  68. (fun v ->
  69. let si = if v < 0. then -1. else 1. in
  70. let d' = v *. si in
  71. let d = d' |> floor in
  72. let dr = d' -. d in
  73. let m' = dr *. 60. in
  74. let m = m' |> floor in
  75. let mr' = m' -. m in
  76. let se = mr' *. 60. in
  77. ((d, (m, se)), if si < 0. then "S" else "N"))
  78. (float <* pcre "°" <* ws
  79. <&> (float <* pcre "′" <* ws <&> (float <* pcre "″" <* ws))
  80. <&> pcre "[NSEW]")
  81. |> compile
  82. in
  83. let x = "47° 25′ 37″ N" |> exec dms |> Result.get_ok in
  84. (* Printf.printf "%f" x; *)
  85. assert (47.426944 -. x < 1.e-16);
  86. let x' = "47° 25′ 37″ E" |> exec dms |> Result.get_ok in
  87. (* Printf.printf "%f" x'; *)
  88. assert (47.426944 -. x' < 1.e-16)
  89. let test_2023_07 () =
  90. let la, lo =
  91. "q=53%C2%B0+01%27+48%22+N%2C+13%C2%B0+18%27+27%22+E"
  92. |> coord_from_qs |> Result.get_ok
  93. in
  94. la |> Assert2.equals_float "route test_2023_07" 1e-9 47.427;
  95. lo |> Assert2.equals_float "route test_2023_07" 1e-9 13.059;
  96. assert true
  97. let () =
  98. test_qs ();
  99. test_deg ();
  100. test_deg_min_sec ();
  101. test_2023_07 ();
  102. assert true