-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathGeoHashTests.cs
More file actions
71 lines (62 loc) · 2.95 KB
/
GeoHashTests.cs
File metadata and controls
71 lines (62 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Globalization;
using Allure.NUnit;
using Garnet.server;
using NUnit.Framework;
using NUnit.Framework.Legacy;
namespace Garnet.test
{
[AllureNUnit]
[TestFixture]
public class GeoHashTests : AllureTestBase
{
[Test]
[TestCase(30.5388942218, 104.0555758833)]
[TestCase(27.988056, 86.925278)]
[TestCase(37.502669, 15.087269)]
[TestCase(38.115556, 13.361389)]
[TestCase(38.918250, -77.427944)]
[TestCase(-90.0, -180.0)]
[TestCase(0.0, 0.0)]
[TestCase(double.Epsilon, double.Epsilon)]
[TestCase(-double.Epsilon, -double.Epsilon)]
[TestCase(90.0, 180.0)]
[TestCase(89.99999999999999, 179.99999999999997)] // double.BitDecrement((Lat/Long)Max)
public void CanEncodeAndDecodeCoordinates(double latitude, double longitude)
{
const double Epsilon = 0.00001;
var hashinteger = GeoHash.GeoToLongValue(latitude, longitude);
var (actualLatitude, actualLongitude) = GeoHash.GetCoordinatesFromLong(hashinteger);
var latError = Math.Abs(latitude - actualLatitude);
var lonError = Math.Abs(longitude - actualLongitude);
ClassicAssert.IsTrue(latError <= Epsilon, "Math.Abs(latError)=" + latError.ToString("F16", CultureInfo.InvariantCulture));
ClassicAssert.IsTrue(lonError <= Epsilon, "Math.Abs(lonError)=" + latError.ToString("F16", CultureInfo.InvariantCulture));
}
[Test]
[TestCase(30.5388942218, 104.0555758833, 4024744861876082, "wm3vxz6vyw0")]
[TestCase(27.988056, 86.925278, 3636631039000829, "tuvz4p141z0")]
[TestCase(37.502669, 15.087269, 3476216502357864, "sqdtr74hyu0")]
[TestCase(38.115556, 13.361389, 3476004292229755, "sqc8b49rny0")]
[TestCase(38.918250, -77.427944, 1787100258949719, "dqbvqhfenp0")]
[TestCase(0.0, 0.0, 0xC000000000000, "s0000000000")]
[TestCase(-90.0, -180.0, 0, "00000000000")]
[TestCase(90.0, 180.0, 0xFFFFFFFFFFFFF, "zzzzzzzzzz0")]
[TestCase(89.99999999999999, 179.99999999999997, 0xFFFFFFFFFFFFF, "zzzzzzzzzz0")]
public void CanEncodeAndDecodeCoordinatesWithGeoHashCode(
double latitude,
double longitude,
long expectedHashInteger,
string expectedHash)
{
var hashInteger = GeoHash.GeoToLongValue(latitude, longitude);
var hash = GeoHash.GetGeoHashCode(hashInteger);
ClassicAssert.AreEqual(expectedHashInteger, hashInteger);
// Note: while we are comparing the entire textual representation of geohash (11 characters),
// the data is stored in 52-bit precision (not 55-bit as required by the GeoHash standard).
// For compatibility with Redis, the last character is always '0'.
ClassicAssert.AreEqual(expectedHash, hash);
}
}
}