-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo_test.go
293 lines (278 loc) · 11.6 KB
/
info_test.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package srtm
import (
"errors"
"image"
"testing"
)
func TestIndexToCoordinatesSRTM1(t *testing.T) {
var point image.Point
point, _ = IndexToCoordinates(0, SRTM1Format)
if point.X != 0 || point.Y != 0 {
t.Error("IndexToCoordinates(0) should return (0,0), but returned", point)
}
point, _ = IndexToCoordinates(1, SRTM1Format)
if point.X != 1 || point.Y != 0 {
t.Error("IndexToCoordinates(1) should return (1,0), but returned", point)
}
point, _ = IndexToCoordinates(3600, SRTM1Format)
if point.X != 3600 || point.Y != 0 {
t.Error("IndexToCoordinates(3600) should return (3600,0), but returned", point)
}
// Note we are 0-indexed, so the last index is 3600, not 3601, which is in the next row.
point, _ = IndexToCoordinates(3601, SRTM1Format)
if point.X != 0 || point.Y != 1 {
t.Error("IndexToCoordinates(3601) should return (0,1), but returned", point)
}
point, _ = IndexToCoordinates(7201, SRTM1Format)
if point.X != 3600 || point.Y != 1 {
t.Error("IndexToCoordinates(7201) should return (3600,1), but returned", point)
}
point, _ = IndexToCoordinates(12967200, SRTM1Format)
if point.X != 3600 || point.Y != 3600 {
t.Error("IndexToCoordinates(25934402) should return (3600,3600), but returned", point)
}
}
func TestIndexToCoordinateSRTM3(t *testing.T) {
var point image.Point
point, _ = IndexToCoordinates(0, SRTM3Format)
if point.X != 0 || point.Y != 0 {
t.Error("IndexToCoordinates(0) should return (0,0), but returned", point)
}
point, _ = IndexToCoordinates(1, SRTM3Format)
if point.X != 1 || point.Y != 0 {
t.Error("IndexToCoordinates(1) should return (1,0), but returned", point)
}
point, _ = IndexToCoordinates(1200, SRTM3Format)
if point.X != 1200 || point.Y != 0 {
t.Error("IndexToCoordinates(1200) should return (1200,0), but returned", point)
}
// Note we are 0-indexed, so the last index is 1200, not 1201, which is in the next row.
point, _ = IndexToCoordinates(1201, SRTM3Format)
if point.X != 0 || point.Y != 1 {
t.Error("IndexToCoordinates(1201) should return (0,1), but returned", point)
}
point, _ = IndexToCoordinates(2401, SRTM3Format)
if point.X != 1200 || point.Y != 1 {
t.Error("IndexToCoordinates(2401) should return (1200,1), but returned", point)
}
point, _ = IndexToCoordinates(1442400, SRTM3Format)
if point.X != 1200 || point.Y != 1200 {
t.Error("IndexToCoordinates(432400) should return (1200,1200), but returned", point.X, point.Y)
}
}
func TestCoordinatesToIndexSRTM1(t *testing.T) {
var index int
index, _ = CoordinatesToIndex(image.Point{0, 0}, SRTM1Format)
if index != 0 {
t.Error("CoordinatesToIndex(0,0) should return 0, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{1, 0}, SRTM1Format)
if index != 1 {
t.Error("CoordinatesToIndex(1,0) should return 1, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{3600, 0}, SRTM1Format)
if index != 3600 {
t.Error("CoordinatesToIndex(3600,0) should return 3600, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{0, 1}, SRTM1Format)
if index != 3601 {
t.Error("CoordinatesToIndex(0,1) should return 3601, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{3600, 1}, SRTM1Format)
if index != 7201 {
t.Error("CoordinatesToIndex(3600,1) should return 7201, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{3600, 3600}, SRTM1Format)
if index != 12967200 {
t.Error("CoordinatesToIndex(3600,3600) should return 12967200, but returned", index)
}
}
func TestCoordinatesToIndexSRTM3(t *testing.T) {
var index int
index, _ = CoordinatesToIndex(image.Point{0, 0}, SRTM3Format)
if index != 0 {
t.Error("CoordinatesToIndex(0,0) should return 0, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{1, 0}, SRTM3Format)
if index != 1 {
t.Error("CoordinatesToIndex(1,0) should return 1, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{1200, 0}, SRTM3Format)
if index != 1200 {
t.Error("CoordinatesToIndex(1200,0) should return 1200, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{0, 1}, SRTM3Format)
if index != 1201 {
t.Error("CoordinatesToIndex(0,1) should return 1201, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{1200, 1}, SRTM3Format)
if index != 2401 {
t.Error("CoordinatesToIndex(1200,1) should return 2401, but returned", index)
}
index, _ = CoordinatesToIndex(image.Point{1200, 1200}, SRTM3Format)
if index != 1442400 {
t.Error("CoordinatesToIndex(1200,1200) should return 1442400, but returned", index)
}
}
func TestCoordinatesToIndexSRTM1Error(t *testing.T) {
_, err := CoordinatesToIndex(image.Point{-1, 0}, SRTM1Format)
if err == nil {
t.Error("CoordinatesToIndex(-1,0) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(-1,0) should return an ErrPointOufOfBounds error, but returned", err)
}
_, err = CoordinatesToIndex(image.Point{0, -1}, SRTM1Format)
if err == nil {
t.Error("CoordinatesToIndex(0,-1) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(0,-1) should return an ErrPointOufOfBounds error, but returned", err)
}
_, err = CoordinatesToIndex(image.Point{3601, 0}, SRTM1Format)
if err == nil {
t.Error("CoordinatesToIndex(3601,0) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(3601,0) should return an ErrPointOufOfBounds error, but returned", err)
}
_, err = CoordinatesToIndex(image.Point{0, 3601}, SRTM1Format)
if err == nil {
t.Error("CoordinatesToIndex(0,3601) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(0,3601) should return an ErrPointOufOfBounds error, but returned", err)
}
}
func TestCoordinatesToIndexSRTM3Error(t *testing.T) {
_, err := CoordinatesToIndex(image.Point{-1, 0}, SRTM3Format)
if err == nil {
t.Error("CoordinatesToIndex(-1,0) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(-1,0) should return an ErrPointOufOfBounds error, but returned", err)
}
_, err = CoordinatesToIndex(image.Point{0, -1}, SRTM3Format)
if err == nil {
t.Error("CoordinatesToIndex(0,-1) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(0,-1) should return an ErrPointOufOfBounds error, but returned", err)
}
_, err = CoordinatesToIndex(image.Point{1201, 0}, SRTM3Format)
if err == nil {
t.Error("CoordinatesToIndex(1201,0) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(1201,0) should return an ErrPointOufOfBounds error, but returned", err)
}
_, err = CoordinatesToIndex(image.Point{0, 1201}, SRTM3Format)
if err == nil {
t.Error("CoordinatesToIndex(0,1201) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(0,1201) should return an ErrPointOufOfBounds error, but returned", err)
}
}
func TestIsIndexInBounds(t *testing.T) {
if IsIndexInBounds(0, SRTM1Format) == false {
t.Error("IsIndexInBounds(0) should return true, but returned false")
}
if IsIndexInBounds(12967200, SRTM1Format) == false {
t.Error("IsIndexInBounds(12967200) should return true, but returned false")
}
if IsIndexInBounds(12967201, SRTM1Format) == true {
t.Error("IsIndexInBounds(12967201) should return false, but returned true")
}
if IsIndexInBounds(-1, SRTM1Format) == true {
t.Error("IsIndexInBounds(-1) should return false, but returned true")
}
if IsIndexInBounds(0, SRTM3Format) == false {
t.Error("IsIndexInBounds(0) should return true, but returned false")
}
if IsIndexInBounds(1442400, SRTM3Format) == false {
t.Error("IsIndexInBounds(1442400) should return true, but returned false")
}
if IsIndexInBounds(1442401, SRTM3Format) == true {
t.Error("IsIndexInBounds(1442401) should return false, but returned true")
}
if IsIndexInBounds(-1, SRTM3Format) == true {
t.Error("IsIndexInBounds(-1) should return false, but returned true")
}
}
func TestIsPointInBounds(t *testing.T) {
if IsPointInBounds(image.Point{0, 0}, SRTM1Format) == false {
t.Error("IsPointInBounds(0,0) should return true, but returned false")
}
if IsPointInBounds(image.Point{3600, 3600}, SRTM1Format) == false {
t.Error("IsPointInBounds(3600,3600) should return true, but returned false")
}
if IsPointInBounds(image.Point{3601, 3601}, SRTM1Format) == true {
t.Error("IsPointInBounds(3601,3601) should return false, but returned true")
}
if IsPointInBounds(image.Point{-1, -1}, SRTM1Format) == true {
t.Error("IsPointInBounds(-1,-1) should return false, but returned true")
}
if IsPointInBounds(image.Point{0, 0}, SRTM3Format) == false {
t.Error("IsPointInBounds(0,0) should return true, but returned false")
}
if IsPointInBounds(image.Point{1200, 1200}, SRTM3Format) == false {
t.Error("IsPointInBounds(1200,1200) should return true, but returned false")
}
if IsPointInBounds(image.Point{1201, 1201}, SRTM3Format) == true {
t.Error("IsPointInBounds(1201,1201) should return false, but returned true")
}
if IsPointInBounds(image.Point{-1, -1}, SRTM3Format) == true {
t.Error("IsPointInBounds(-1,-1) should return false, but returned true")
}
}
func TestWrappedErrorMessage(t *testing.T) {
_, err := IndexToCoordinates(-1, SRTM1Format)
if err == nil {
t.Error("IndexToCoordinates(-1) should return an error, but returned nil")
}
if errors.Is(err, ErrIndexOutOfBounds) == false {
t.Error("IndexToCoordinates(-1) should return an ErrIndexOutOfBounds error, but returned", err)
}
if err.Error() != "index out of bounds for SRTM image format: SRTM1, index: -1" {
t.Error("IndexToCoordinates(-1) should return an error with message\n"+
"'index out of bounds for SRTM image format: SRTM1, index: -1'\n"+
"but returned", err)
}
_, err = CoordinatesToIndex(image.Point{-1, -1}, SRTM1Format)
if err == nil {
t.Error("CoordinatesToIndex(-1,-1) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(-1,-1) should return an ErrPointOutOfBounds error, but returned", err)
}
if err.Error() != "point out of bounds for SRTM image format: SRTM1, point: (-1,-1)" {
t.Error("CoordinatesToIndex(-1,-1) should return an error with message\n"+
"'point out of bounds for SRTM image format: SRTM1, point: (-1,-1)'\n"+
"but returned", err)
}
_, err = IndexToCoordinates(40000000, SRTM3Format)
if err == nil {
t.Error("IndexToCoordinates(40000000) should return an error, but returned nil")
}
if errors.Is(err, ErrIndexOutOfBounds) == false {
t.Error("IndexToCoordinates(40000000) should return an ErrIndexOutOfBounds error, but returned", err)
}
if err.Error() != "index out of bounds for SRTM image format: SRTM3, index: 40000000" {
t.Error("IndexToCoordinates(40000000) should return an error with message\n"+
"'index out of bounds for SRTM image format: SRTM3, index: 40000000'\n"+
"but returned", err)
}
_, err = CoordinatesToIndex(image.Point{4000, 4000}, SRTM3Format)
if err == nil {
t.Error("CoordinatesToIndex(4000,4000) should return an error, but returned nil")
}
if errors.Is(err, ErrPointOutOfBounds) == false {
t.Error("CoordinatesToIndex(4000,4000) should return an ErrPointOutOfBounds error, but returned", err)
}
if err.Error() != "point out of bounds for SRTM image format: SRTM3, point: (4000,4000)" {
t.Error("CoordinatesToIndex(4000,4000) should return an error with message\n"+
"'point out of bounds for SRTM image format: SRTM3, point: (4000,4000)'\n"+
"but returned", err)
}
}