-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathgeohash_test.c
133 lines (85 loc) · 3.62 KB
/
geohash_test.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// geohash_test.c
// libgeohash
//
// Created by Derek Smith on 10/6/09.
// Copyright 2009 SimpleGeo. All rights reserved.
//
#include "geohash.h"
#include <string.h>
#include <stdio.h>
void check_coords(GeoCoord coord, GeoCoord expected) {
char* valueTitle = NULL;
double expectedValue;
double value;
if(coord.latitude != expected.latitude) {
valueTitle = "latitude";
expectedValue = expected.latitude;
value = coord.latitude;
} else if(coord.longitude != expected.longitude) {
valueTitle = "longitude";
expectedValue = expected.longitude;
value = coord.longitude;
} else if(coord.north != expected.north) {
valueTitle = "north";
expectedValue = expected.north;
value = coord.north;
} else if(expected.south != coord.south) {
valueTitle = "south";
expectedValue = expected.south;
value = coord.south;
} else if(coord.east != expected.east) {
valueTitle = "east";
expectedValue = expected.east;
value = coord.east;
} else if(expected.west != coord.west) {
valueTitle = "west";
expectedValue = expected.west;
value = coord.west;
}
if(valueTitle)
printf("Error: Expected %.16f but was %.16f for %s\n", expectedValue, value, valueTitle);
}
void checkHashes(char* hash, char* expected) {
if(strcmp(hash, expected) != 0) {
printf("Error: Expected hash = %s. (%s)\n", expected, hash);
}
}
void checkNeighbors(char** neighbors, char** expectedNeighbors) {
int i;
for(i = 0; i < 8; i++)
if(strcmp(neighbors[i], expectedNeighbors[i]) != 0)
printf("Error: Expected hash = %s at index %i. (%s)\n", expectedNeighbors[i], i, neighbors[i]);
}
int main() {
// Decoder
GeoCoord coord = geohash_decode("ezs42");
GeoCoord expectedCoord = {42.60498046875, -5.60302734375, 42.626953125, -5.5810546875, 42.5830078125, -5.625};
check_coords(coord, expectedCoord);
coord = geohash_decode("ezs42gx");
expectedCoord = (GeoCoord){42.602920532226562, -5.5817413330078125, 42.603607177734375, -5.581054687500000, 42.60223388671875, -5.582427978515625};
check_coords(coord, expectedCoord);
coord = geohash_decode("9xj5smj4w40");
expectedCoord = (GeoCoord){40.018140748143196, -105.27485780417919, 40.01814141869545, -105.27485713362694, 40.018140077590942, -105.27485847473145};
check_coords(coord, expectedCoord);
// Encoder
char* hash = geohash_encode(42.60498046875, -5.60302734375, 5);
checkHashes(hash, "ezs42");
hash = geohash_encode(40.018141, -105.274858, 12);
checkHashes(hash, "9xj5smj4w40m");
hash = geohash_encode(40.018141, -105.274858, 2);
checkHashes(hash, "9x");
hash = geohash_encode(40.018141, -105.274858, 0);
checkHashes(hash, "9xj5sm");
// Neighbors
char** neighbors = geohash_neighbors("ezs42");
char* expectedNeighbors[8] = {"ezs48", "ezs49", "ezs43", "ezs41", "ezs40", "ezefp", "ezefr", "ezefx"};
checkNeighbors(neighbors, expectedNeighbors);
expectedNeighbors[0] = "9xj5smj4w40q"; expectedNeighbors[1] = "9xj5smj4w40w";
expectedNeighbors[2] = "9xj5smj4w40t"; expectedNeighbors[3] = "9xj5smj4w40s";
expectedNeighbors[4] = "9xj5smj4w40k"; expectedNeighbors[5] = "9xj5smj4w40h";
expectedNeighbors[6] = "9xj5smj4w40j"; expectedNeighbors[7] = "9xj5smj4w40n";
neighbors = geohash_neighbors("9xj5smj4w40m");
checkNeighbors(neighbors, expectedNeighbors);
return 0;
}