forked from simplegeo/libgeohash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeohash_test.c
206 lines (142 loc) · 5.96 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// 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>
#include <stdlib.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);
exit(-11);
}
}
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]);
exit(-10);
}
}
int main() {
int i = 0; // for test loops
// Decoder
GeoCoord coord = geohash_decode("");
GeoCoord expectedCoord = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
check_coords(coord, expectedCoord);
coord = geohash_decode("ezs42");
expectedCoord = (GeoCoord){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);
coord = geohash_decode("4444444444444444444444444444444444444444444444444444444");
// bad hash characters should not crash it
coord = geohash_decode("☀☎");
// Encoder
char* hash = geohash_encode(42.60498046875, -5.60302734375, 5);
checkHashes(hash, "ezs42");
free(hash);
hash = geohash_encode(40.018141, -105.274858, 12);
checkHashes(hash, "9xj5smj4w40m");
free(hash);
hash = geohash_encode(40.018141, -105.274858, 2);
checkHashes(hash, "9x");
free(hash);
hash = geohash_encode(40.018141, -105.274858, 0);
checkHashes(hash, "9xj5sm");
free(hash);
hash = geohash_encode(40.018141, -105.274858, 900000000); //lol enormous
checkHashes(hash, "9xj5sm");
free(hash);
// Neighbors
char** neighbors = geohash_neighbors("ezs42");
char* expectedNeighbors[8] = {"ezs48", "ezs49", "ezs43", "ezs41", "ezs40", "ezefp", "ezefr", "ezefx"};
checkNeighbors(neighbors, expectedNeighbors);
geohash_free_neighbors(neighbors);
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);
geohash_free_neighbors(neighbors);
neighbors = geohash_neighbors("ezs42- "); // truncates the last invalid character of and 2 off the corners
expectedNeighbors[0] = "ezs42- "; expectedNeighbors[1] = "ezs42-";
expectedNeighbors[2] = "ezs42- "; expectedNeighbors[3] = "ezs42-";
expectedNeighbors[4] = "ezs42- "; expectedNeighbors[5] = "ezs42-";
expectedNeighbors[6] = "ezs42- "; expectedNeighbors[7] = "ezs42-";
checkNeighbors(neighbors, expectedNeighbors);
geohash_free_neighbors(neighbors);
neighbors = geohash_neighbors("☀");
for(i = 0; i < 8; i++){
if (!neighbors[i]){
exit(-80);
}
}
geohash_free_neighbors(neighbors);
neighbors = geohash_neighbors("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
for(i = 0; i < 8; i++){
// neighbors should be valid, but all individual ones should be null
if (!neighbors || neighbors[i]){
exit(-90);
}
}
geohash_free_neighbors(neighbors);
neighbors = geohash_neighbors(""); // invalid
for(i = 0; i < 8; i++){
// neighbors should be valid, but all individual ones should be null
if (!neighbors || neighbors[i]){
exit(-100);
}
}
geohash_free_neighbors(neighbors);
neighbors = geohash_neighbors(" ezs42"); // leading invalid chars generate neighbors
expectedNeighbors[0] = " ezs43"; expectedNeighbors[1] = " ezs49";
expectedNeighbors[2] = " ezs48"; expectedNeighbors[3] = " ezefx";
expectedNeighbors[4] = " ezefr"; expectedNeighbors[5] = " ezefp";
expectedNeighbors[6] = " ezs40"; expectedNeighbors[7] = " ezs41";
checkNeighbors(neighbors, expectedNeighbors);
geohash_free_neighbors(neighbors);
return 0;
}