geohash_test.ml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. (*
  2. * geohash_test.ml
  3. *
  4. * Created by Marcus Rohrmoser on 11.03.21.
  5. * Copyright © 2021-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 Optint.Int63
  21. open Geohash.Whitebox
  22. let assert_equals_int64 (test_name : string) (expected : t) (result : t) : 'a =
  23. Assert.assert_equals test_name to_string expected result
  24. let test_len () =
  25. assert ("zzzzzzzzzzzZ" < "zzzzzzzzzzzz");
  26. assert ("zzzzzzzzzzzz" < "zzzzzzzzzzzz ");
  27. assert ("zzzzzzzzzzzz" < "zzzzzzzzzzzz1");
  28. assert ("zzzzzzzzzzzz" > "u28brs0s00040");
  29. assert ("z" > "aa");
  30. assert ("a" < "b");
  31. assert (12 < ("u28brs0s00040" |> String.length))
  32. (*
  33. let test_spread () =
  34. (of_int 0x3) |> spread |> to_string
  35. |> Assert2.equals_string "test_spread #0" "0x5";
  36. shift_left ((of_int 0x3) |> spread) 1
  37. |> to_string
  38. |> Assert2.equals_string "test_spread #1" "0xa";
  39. shift_left (of_int 0xFFFFFFFF) 1
  40. |> spread |> Printf.sprintf "%#Lx"
  41. |> Assert2.equals_string "test_spread #1" "0x5555555555555554";
  42. 0xFFFFL |> spread |> Printf.sprintf "%#Lx"
  43. |> Assert2.equals_string "test_spread #0" "0x55555555";
  44. shift_left 0xFFFFL 1 |> spread |> Printf.sprintf "%#Lx"
  45. |> Assert2.equals_string "test_spread #1" "0x155555554"
  46. let test_interleave () =
  47. (0x3L, 0x3L) |> interleave |> Printf.sprintf "%#Lx"
  48. |> Assert2.equals_string "test_interleave #0" "0xf";
  49. (0x3L, 0x1L) |> interleave |> Printf.sprintf "%#Lx"
  50. |> Assert2.equals_string "test_interleave #1" "0x7"
  51. let test_deinterleave () =
  52. let a, b = 0x7L |> deinterleave in
  53. a |> Printf.sprintf "%#Lx"
  54. |> Assert2.equals_string "test_deinterleave #0" "0x3";
  55. b |> Printf.sprintf "%#Lx"
  56. |> Assert2.equals_string "test_deinterleave #1" "0x1"
  57. let test_quantize () =
  58. let la, lo = quantize (57.649111, 10.407440) in
  59. Assert2.equals_float "test_quantize #1" 3523045016. (to_float la) 0.1;
  60. Assert2.equals_float "test_quantize #2" 2271649243. (to_float lo) 0.1;
  61. shift_right ((la, lo) |> interleave) 4
  62. |> base32_encode 12
  63. |> Assert2.equals_string "test_quantize #3" "u4pruydqqvjw"
  64. *)
  65. let concat hi lo = shift_left (hi |> of_int) 32 |> logor (lo |> of_int)
  66. let test_base32_decode () =
  67. let t i a b =
  68. let x = a |> base32_decode |> Result.get_ok in
  69. (* Printf.eprintf "decode %#x\n" x; *)
  70. x |> assert_equals_int64 (Printf.sprintf "test_base32_decode #%d" i) b
  71. in
  72. t 10 "tuvz4p141zc1" (of_int64 0xceb7f254240fd61L);
  73. t 11 "ezs42" (of_int64 0xdfe082L);
  74. (* t 12 "u4pruydqqvj" (of_int64 0x6895bebccb5b71L); *)
  75. t 13 "zzs000000000" (of_int64 0x0fff000000000000L);
  76. t 50 "zzzzzzzzzzzz" (of_int64 0x0FFFFFFFFFFFFFFFL);
  77. t 60 "" (of_int64 0x0L)
  78. let test_base32_encode () =
  79. let t i p a b =
  80. shift_right a (5 * (12 - p))
  81. |> base32_encode p
  82. |> Assert2.equals_string (Printf.sprintf "test_base32_encode #%d" i) b
  83. in
  84. t 10 12 (concat 0xceb7f25 0x4240fd61) "tuvz4p141zc1";
  85. t 20 4 (concat 0xceb7f25 0x4240fd61) "tuvz";
  86. t 30 11 (concat 0xceb7f25 0x4240fd61) "tuvz4p141zc";
  87. base32_encode 12 (concat 0xff00000 0x00000000)
  88. |> Assert2.equals_string "test_base32_encode #1" "zw0000000000"
  89. let test_quantize () =
  90. let t i wgs84 b =
  91. shift_right (wgs84 |> quantize30 |> interleave) 0
  92. |> assert_equals_int64 (Printf.sprintf "test_quantize #%d" i) b
  93. in
  94. t 10 (27.988056, 86.925278) (of_int64 0xceb7f254240fd61L)
  95. let test_encode_a () =
  96. let t i p a b =
  97. a |> encode p |> Result.get_ok
  98. |> Assert2.equals_string (Printf.sprintf "test_encode_a #%d" i) b
  99. in
  100. t 1 12 (27.988056, 86.925278) "tuvz4p141zc1";
  101. t 10 12 (-25.382708, -49.265506) "6gkzwgjzn820";
  102. t 20 12 (57.649111, 10.407440) "u4pruydqqvjw";
  103. t 30 12 (47.879105, 12.634964) "u28brs0s0004";
  104. t 40 11 (47.879105, 12.634964) "u28brs0s000";
  105. t 50 1 (47.879105, 12.634964) "u"
  106. (* t 60 0 (47.879105, 12.634964) "" *)
  107. let test_decode_sunshine () =
  108. let t i a b =
  109. let (lat, lon), (dlat, dlon) = a |> decode |> Result.get_ok
  110. and (lat', lon'), (dlat', dlon') = b in
  111. Assert2.equals_float (Printf.sprintf "test_decode lat #%d" i) lat' lat 1e-6;
  112. Assert2.equals_float (Printf.sprintf "test_decode lon #%d" i) lon' lon 1e-6;
  113. Assert2.equals_float
  114. (Printf.sprintf "test_decode dlat #%d" i)
  115. dlat' dlat 1e-17;
  116. Assert2.equals_float
  117. (Printf.sprintf "test_decode dlon #%d" i)
  118. dlon' dlon 1e-17
  119. in
  120. (* https://github.com/francoisroyer/ocaml-geohash/blob/master/geohash.ml#L200 *)
  121. t 0 "u4pruydqqvj"
  122. ((57.649111, 10.407440), (6.70552253723e-07, 6.70552253723e-07));
  123. (* https://github.com/mariusae/ocaml-geohash/blob/master/lib_test/test.ml#L7 *)
  124. t 10 "9q8yyk8yuv"
  125. ((37.7749308944, -122.419415116), (2.68220901489e-06, 5.36441802979e-06));
  126. t 20 "u28brs0s0004"
  127. ((47.879105, 12.634964), (8.38190317154e-08, 1.67638063431e-07))
  128. let test_decode_fail () =
  129. assert ("u28brs0s00040" |> decode = Error '_');
  130. assert ("u28brs0s00041" |> decode = Error '_');
  131. assert ("_" |> decode = Error '_')
  132. let () =
  133. test_len ();
  134. (* test_spread ();
  135. test_interleave ();
  136. test_deinterleave ();
  137. *)
  138. test_base32_encode ();
  139. test_base32_decode ();
  140. test_quantize ();
  141. test_encode_a ();
  142. test_decode_sunshine ();
  143. test_decode_fail ()