-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfulltest
More file actions
executable file
·472 lines (451 loc) · 11.3 KB
/
Copy pathfulltest
File metadata and controls
executable file
·472 lines (451 loc) · 11.3 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# -----------------------------------------------------------------------------
# Copyright (C) 2003-2019 Stichting Mapcode Foundation (http://www.mapcode.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
#!/bin/bash
# Test dir
export TDIR=test
if [ ! -d $TDIR ] ; then
TDIR=.
fi
# Precision, in fraction of degree, depending of encoding precision
EPSILON[0]="2.5E-4"
EPSILON[1]="5.0E-5"
EPSILON[2]="8.0E-6"
EPSILON[3]="2.0E-6"
EPSILON[4]="3.5E-7"
EPSILON[5]="6.5E-8"
EPSILON[6]="9.5E-9"
EPSILON[7]="2.0E-9"
EPSILON[8]="3.5E-10"
# Defaults
export DEBUG=""
export PRECISION=2
export SCENARIO_ONLY=0
export TEST=0
# Elapsed time
T0=`date "+%s"`
# DEBUG mode logs each operation
# PRECISION is 0 to 8, default 2
# SCENARIO_ONLY play only the scenario
# TEST mode plays the scenario, tests some language conversions,
# the encoding_ok and 1% of the decoding_ok
function usage {
echo "Usage: `basename $0` [ -p <prec> | --precision=<prec> ] [ -d | --debug ]"
echo " [ -s | --scenario_only ] [ -t | --test ]"
echo " or `basename $0` [ -h | --help ]"
echo " Precision is from 0 to 8, default: $PRECISION"
echo " Debug logs each individual test"
echo " Scenario_only plays only the scenario"
echo " Test mode plays the scenario, tests some language features, the encoding_ok"
echo " and 1% of the decoding_ok"
}
function error {
echo "ERROR: $1." 1>&2
exit 1
}
# Parse arguments
while [ -n "$1" ] ; do
case "$1" in
("-h"| "--help")
usage
exit 0
;;
("-d"|"--debug")
export DEBUG="-d"
;;
("-p")
export PRECISION=$2
shift
;;
(--precision=*)
export PRECISION=${1#*=}
;;
("-s"|"--scenario_only")
export SCENARIO_ONLY=1
;;
("-t"|"--test")
export TEST=1
;;
(*)
error "Invalid arguments"
;;
esac
shift
done
if [ \( $SCENARIO_ONLY -eq 1 \) -a \( $TEST -eq 1 \) ] ; then
error "Options \"scenario_only\" and \"test\" are mutually exclusive"
fi
let P=0+$PRECISION
if [ \( $P -lt 0 \) -a \( $P -gt 8 \) ] ; then
error "Invalid precision $PRECISION"
fi
echo Precision $PRECISION
PRECISION=P${PRECISION}
# Check that $2 = $3
# if $1 is -d then compare floats modulo EPS ($4) (1.0E-4, 1.0E-5, 1.0E-6)
# Echo 0 if OK and 1 otherwise
function check {
if [ "$1" = "-d" ] ; then
echo $2 | awk -v EXPECT="$3" -v EPS=${EPSILON[$4]} '
function abs(v) {return v < 0 ? -v : v}
{
NEXPECT=split(EXPECT, EXPECTS)
NOUTPUT=split($0, OUTPUTS);
if (NEXPECT != NOUTPUT) {print "1"; exit}
for (i = 1; i < NEXPECT+1; i++) {
E = 0.0+EXPECTS[i]
O = 0.0+OUTPUTS[i]
D = abs(E - O)
if (D > EPS) {
if (E < 0.0) E += 360.0
if (O < 0.0) O += 360.0
D = abs(E - O)
if (D > EPS) {
print "1"
}
}
}
print "0"
}
'
else
if [ "$2" = "$3" ] ; then
echo 0
else
echo 1
fi
fi
}
# Play Scenario
echo Scenario:
# For each line
# Read line until end of file
while IFS=' ' read -r a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 ; do
if [[ $a1 =~ ^#|^$ ]] ; then
continue
fi
# Process the line
let I=1
INPUT=""
OUTPUT=""
let ININ=1
CMD=""
while true ; do
declare arg="a$I"
if [ "${!arg}" = "" ] ; then
# End of line
break
fi
if [ -z "$CMD" ] ; then
CMD=${!arg};
fi
if [ "${!arg}" = "=>" ] ; then
# Switch to output
let ININ=0
elif [ $ININ -eq 1 ] ; then
INPUT=$INPUT${!arg}" "
else
OUTPUT=$OUTPUT${!arg}" "
fi
let I=$I+1
done
# Do test and save exit status
export result=`t_mapcode $INPUT`
export code=$?
# Check exit status
if [ $code -ne 0 ] ; then
error "mapcode has exited with code $code"
exit 1
fi
# Get last (line of) result
# Skip up to "=> " included and append a space (cause OUTPUT has one)
result=`echo $result | awk -F "=> " '{print $NF}'`" "
# Check that result is expected
res=`check "$CMD" "$result" "$OUTPUT" 0`
if [ "$res" = "0" ] ; then
if [ -n "$DEBUG" ] ; then
echo "$INPUT => $OUTPUT"
fi
else
error "$INPUT => $result instead of $OUTPUT"
fi
done < $TDIR/Scenario
echo OK.
if [ "$SCENARIO_ONLY" = "1" ] ; then
exit 0
fi
# Languages
echo Languages
if [ $TEST -eq 1 ] ; then
# Some lines in test mode, here are the Numbers of effective lines
# plus all the lines after the last
export TESTS=(1 174 298 478 561 4788)
let I=0
else
# All the tests
let I=1
fi
PrevLang=x
let L=1
while IFS=' ' read -r Lang Input Output Back ; do
if [ $TEST -eq 1 ] ; then
# Some specific effective lines
if [ -n "${TESTS[$I]}" ] ; then
let TL=${TESTS[$I]}
else
# Last item of the list has been reached
let TL=${TL}+1
fi
else
# All the lines
let TL=$I
fi
if [ $TL -ne $L ] ; then
let L=$L+1
continue
fi
# End of file
if [[ $Lang =~ ^#|^$ ]] ; then
continue
fi
if [ -n "$DEBUG" ] ; then
echo -l "$Input" "$Lang"
elif [ "$Lang" != "$PrevLang" ] ; then
echo -n $Lang " "
fi
PrevLang=$Lang
# From Input in Roman to Output in Lang
Got_Output=`t_mapcode -l $Input $Lang`
export code=$?
if [ $code -ne 0 ] ; then
error "mapcode has exited with code $code"
exit 1
fi
res=`check "-t" "$Got_Output" "$Output" 0`
if [ "$res" != "0" ] ; then
error "$Input $Lang => $Got_Output instead of $Output"
fi
# From Output back into Input
Got_Back=`t_mapcode -l $Output`
export code=$?
if [ $code -ne 0 ] ; then
error "mapcode has exited with code $code"
exit 1
fi
res=`check "-t" "$Got_Back" "$Back" 0`
if [ "$res" != "0" ] ; then
error "$Output => $Got_Back instead of $Back"
fi
let I=$I+1
let L=$L+1
done <$TDIR/Alphabets
if [ -z "$DEBUG" ] ; then
echo
fi
echo OK
if [ $TEST -eq 0 ] ; then
# Check Territory failures
echo Failure on territory:
# Info on $1 in context $2 shall lead to error (exit 1)
function territory_failure {
if [ -n "$DEBUG" ] ; then
echo -t "$1" "$2"
fi
t_mapcode -t "$1" "$2" > /dev/null 2>&1
res=$?
if [ $res -ne 1 ] ; then
error "t_mapcode -t \"$1\" "$2" has exited with code $res i.o. 1"
fi
}
territory_failure ""
territory_failure " "
territory_failure USA " "
territory_failure " " "US "
territory_failure "Toto"
territory_failure AL Toto
# For each line
while IFS=' ' read -r ctx ter ; do
if [[ $ctx =~ ^#|^$ ]] ; then
continue
fi
if [ -z "$ter" ] ; then
ter=$ctx
ctx=""
fi
territory_failure $ter $ctx
done <$TDIR/TerritoryFailures
echo OK.
fi
if [ $TEST -eq 0 ] ; then
# Check decoding failures
echo Failure to decode:
# Decoding $1 in context $2 shall lead to error (exit 1)
function decode_failure {
if [ -n "$DEBUG" ] ; then
echo -d "$1" "$2"
fi
t_mapcode -d "$1" $2 > /dev/null 2>&1
res=$?
if [ $res -ne 1 ] ; then
error "t_mapcode -d \"$1\" $2 has exited with code $res i.o. 1"
fi
}
decode_failure ""
decode_failure "QD3CL.PX8J-K3'"
decode_failure "QD3CL.P'X8J-K3"
# For each line
while IFS=' ' read -r ctx cod ; do
if [[ $ctx =~ ^#|^$ ]] ; then
continue
fi
if [ -z "$cod" ] ; then
cod=$ctx
ctx=""
fi
decode_failure $ctx $cod
done < $TDIR/DecodeFailures
echo OK.
fi
# EncodeOk
echo Encode
# For each line
while IFS=' ' read -r lat lon ; do
if [[ $lat =~ ^#|^$ ]] ; then
continue
fi
if [ -z "$lat" ] ; then
break;
fi
# Encode
result=`t_mapcode -c $lat $lon $PRECISION`
if [ $? -ne 0 ] ; then
error "t_mapcode -c $lat $lon $PRECISION has failed"
fi
# Parse result:
# lat lon => Short Code, then either 'Code Short' or 'Long', then num
result=`echo $result | awk '
(substr($6, 1, 1) == "'\''") && (substr($6, length($6), 1) == "'\''") {
print substr($6, 2, length($6)-2)
exit
}
{
print substr($6, 2) " " substr($7, 1, length($7)-1)
}
'`
ctx=`echo $result | awk '{print $1}'`
cod=`echo $result | awk '{print $2}'`
if [ -z "$cod" ] ; then
cod=$ctx
ctx=""
fi
# Decode
dec=`t_mapcode -d $ctx $cod`
if [ $? -ne 0 ] ; then
error "t_mapcode -d $cod $ctx has failed"
fi
dec=`echo $dec | cut -d ">" -f 2 | cut -d " " -f 2-`
# Check decoded lat lon versus origin
res=`check "-d" "$dec" "$lat $lon" $PRECISION`
if [ "$res" != "0" ] ; then
error "$lat $lon $PRECISION => $result => $dec"
else
if [ -n "$DEBUG" ] ; then
echo "$lat $lon => $result => $dec"
fi
fi
done <$TDIR/EncodeOk
echo OK.
# Decode
echo Decode:
# Count line ("=>") of output
function count {
echo "$*" | awk '
BEGIN {FS="=>"}
{print NF - 1}
'
}
# Decoding in context $1 the mapcode $2 shall lead to lat lon $3 and $4
# Ecoding lat lon $3 and $4 in context $1 shall lead to $5 mapcodes
# Ecoding lat lon $3 and $4 without context $1 shall lead to $6 mapcodes
# Encoding lat lon $3 and $4 in context $1 with $PRECISION then decoding
# shall lead to $3 and $4
function decode {
if [ -n "$DEBUG" ] ; then
echo -d "$1" "$2"
fi
result=`t_mapcode -d $1 $2`
result=`echo $result | cut -d ">" -f 2 | cut -d " " -f 2-`
res=`check "-d" "$3 $4" "$result" 0`
if [ "$res" != "0" ] ; then
error "t_mapcode -d $1 $2 => $result instead of $3 $4"
fi
# Count number of all local codes
res=`t_mapcode -c $3 $4 $1 all`
let nloc=0+`count $res`
if [ $nloc -ne $5 ] ; then
error "t_mapcode -c $3 $4 $1 all => $nloc results instead of $5"
fi
# Count number of all global codes
res=`t_mapcode -c $3 $4 all`
let nloc=0+`count $res`
if [ $nloc -ne $6 ] ; then
error "t_mapcode -c $3 $4 all => $nloc results instead of $6"
exit 1
fi
# Check decoded lat lon versus provided
enc=`t_mapcode -c $3 $4 $1 $PRECISION`
enc=`echo $enc | cut -d ">" -f 2 | cut -d " " -f 2-3`
dec=`t_mapcode -d $enc`
dec=`echo $dec | cut -d ">" -f 2`
res=`check "-d" "$dec" "$3 $4" $PRECISION`
if [ "$res" != "0" ] ; then
error "$3 $4 $1 $PRECISION => $enc => $dec"
fi
}
# Theses one have no context
decode "" "ZH0H0.H1HG" "90" "0" 1 1
decode "" "ZZ0X0.B16G-4K" "90" "179" 1 1
decode "" "Z0000.010G" "90" "-179.999999" 1 1
decode "" "ZZ0Z0.Z1YG" "90.000000000000" "179.999999999999" 1 1
# Play Decode scenario
# For each line
# Read ctx cod lat lon loc glo
let L=0
let R=0
while IFS=' ' read -r ctx cod lat lon loc glo ; do
if [[ $ctx =~ ^#|^$ ]] ; then
continue
fi
# Process each line, or 1% in test mode
if [ \( $TEST -eq 0 \) -o \( $R -eq 0 \) ] ; then
decode $ctx $cod $lat $lon $loc $glo
fi
R=$((L % 100))
if [ \( $R -eq 0 \) -a \( -z "$DEBUG" \) ] ; then
echo -n "."
fi
let L=$L+1
done < $TDIR/DecodeOk
echo
if [ $TEST -eq 0 ] ; then
T1=`date "+%s"`
echo $T0 $T1 | awk '
{D=$2-$1
printf "Done in %dmn%ds\n", D/60, D%60
}
'
else
echo OK.
fi