From 5fbc69fe581be81e837e846a9d0663d9909d0702 Mon Sep 17 00:00:00 2001 From: adoerr Date: Tue, 14 Jun 2016 14:19:49 +0200 Subject: [PATCH] Added test cases Signed-off-by: adoerr --- geojson/geohash/geohash_test.go | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/geojson/geohash/geohash_test.go b/geojson/geohash/geohash_test.go index cc7716a4..214b2268 100644 --- a/geojson/geohash/geohash_test.go +++ b/geojson/geohash/geohash_test.go @@ -23,3 +23,39 @@ func TestABC(t *testing.T) { t.Fatalf("bad geohash %v,%v %v,%v", lat, lon, lat2, lon2) } } + +// TestEqualsWebserviceHash checks whether an encoded geohash is equal to a +// geohash encoded by geohash.org for identical lat/lon values. +func TestEqualsWebserviceHash(t *testing.T) { + lat, lon := 27.173117, 78.042122 + hash, err := Encode(lat, lon, 12) + if err != nil { + t.Fatal(err) + } + + hash2 := "tsz6xfswchpu" + if hash != hash2 { + t.Errorf("geohash should be equal %v, %v", hash, hash2) + } +} + +func TestNearbyHasCommonPrefix(t *testing.T) { + lat, lon := 27.174583139355413, 78.04258346557617 + hash, err := Encode(lat, lon, 32) + if err != nil { + t.Fatal(err) + } + + lat2, lon2 := 27.174559277910305, 78.04163932800293 + hash2, err := Encode(lat2, lon2, 32) + if err != nil { + t.Fatal(err) + } + + // common prefix should be at least of length 7 + pref := hash[:7] + pref2 := hash2[:7] + if pref != pref2 { + t.Errorf("prefix should be equal %v, %v", pref, pref2) + } +}