-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathunittest.html
More file actions
737 lines (676 loc) · 29 KB
/
Copy pathunittest.html
File metadata and controls
737 lines (676 loc) · 29 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<!DOCTYPE html>
<!--
~ Copyright (C) 2003-2018 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.
-->
<html>
<head>
<title>Mapcode Javascript Unit Tests</title>
<script src="../ndata.js" type="text/javascript"></script>
<script src="../mapcode.js" type="text/javascript"></script>
<script src="test_territories.js" type="text/javascript"></script>
<script src="test_encodes.js" type="text/javascript"></script>
<script>
function out(str) {
var e = document.getElementById('out');
if (e) {
e.innerHTML = e.innerHTML + str + '<br>';
}
}
</script>
</head>
<body>
<h1>Mapcode Javascript Unit Tests</h1>
<DIV id="out"></DIV>
<DIV id="perc"></DIV>
<script type="text/javascript">
// globals to count tests, errors and warnings
var nrTests = 0;
var nrErrors = 0;
var nrWarnings = 0;
// test the alphabet conversion routines
var testpairs = [
"12.34E0-PQRSTZVW", "12.34E0-PQRSTZVW",
"OIoi#%?-.abcdfghjklmnpqrstvwxyz0123456789ABCDFGHJKLMNPQRSTVWXYZ", "OIOI#%?-.ABCDFGHJKLMNPQRSTVWXYZ0123456789ABCDFGHJKLMNPQRSTVWXYZ",
"OEUoi OIoi#%?-.abcdfghjklmnpqrstvwxyz0123456789ABCDFGHJKLMNPQRSTVWXYZ", "OEUOI OIOI#%?-.ABCDFGHJKLMNPQRSTVWXYZ0123456789ABCDFGHJKLMNPQRSTVWXYZ",
"OEUoi OIoi#%?-.abcdfghjklmnpqrstvwxyz0123456789euEUABCDFGHJKLMNPQRSTVWXYZ", "OEUOI OIOI#%?-.ABCDFGHJKLMNPQRSTVWXYZ0123456789EUEUABCDFGHJKLMNPQRSTVWXYZ",
"OEUoi OIoi#%?abcdfghjklmnpqrstvwxyz0123456789euEUABCDFGHJKLMNPQRSTVWXYZ", "OEUOI OIOI#%?ABCDFGHJKLMNPQRSTVWXYZ0123456789EUEUABCDFGHJKLMNPQRSTVWXYZ",
".", ".",
"-", "-",
"-abc", "-ABC",
"12.34", "12.34",
" Oio 12.AU ", "OIO 12.AU",
"OMN 112.3AU", "OMN 112.3AU",
"OMN 112.3EU", "OMN 112.3EU",
"OMN 100.3EU", "OMN 100.3EU",
"OMN 1Oo.3Oo", "OMN 1OO.3OO",
"49.4V", "49.4V",
"NLD 49.4V-xx123", "NLD 49.4V-XX123",
"xx.xx", "XX.XX",
"xx.xxx", "XX.XXX",
"xxx.xx", "XXX.XX",
"xx.xxxx", "XX.XXXX",
"xxx.xxx", "XXX.XXX",
"xxxx.xx", "XXXX.XX",
"xxx.xxxx", "XXX.XXXX",
"xxxx.xxx", "XXXX.XXX",
"xxxx.xxxx", "XXXX.XXXX",
"xxxxx.xxxx", "XXXXX.XXXX",
"pq.rs", "PQ.RS",
"bc.123", "BC.123",
"123.xy", "123.XY",
" nld 12.34E0", "NLD 12.34E0",
"VVX.xxx", "VVX.XXX",
"x123.xx", "X123.XX",
"xxx.xxxx", "XXX.XXXX",
"12xx.xxx", "12XX.XXX",
"xxxx.xx12", "XXXX.XX12",
"99zxx.xxxx", "99ZXX.XXXX",
"xx.xx-P", "XX.XX-P",
"xx.xxx-pq", "XX.XXX-PQ",
"xxx.xx-123", "XXX.XX-123",
"xx.xxxx-pqRS", "XX.XXXX-PQRS",
"xxx.xxx-PQRSTZVW", "XXX.XXX-PQRSTZVW",
"xxxx.xx-pqrstZvw", "XXXX.XX-PQRSTZVW",
"xxx.xxxx-PQrsTZ", "XXX.XXXX-PQRSTZ",
"xxxx.xxx-09876543", "XXXX.XXX-09876543",
"xxxx.xxxx-PQRSTZVW", "XXXX.XXXX-PQRSTZVW",
"xxxxx.xxxx-PQRSTZVW", "XXXXX.XXXX-PQRSTZVW",
"pq.rs-PQRSTZVW", "PQ.RS-PQRSTZVW",
"bc.123-PQRSTZVW", "BC.123-PQRSTZVW",
"123.xy-PQRSTZVW", "123.XY-PQRSTZVW",
"VVX.xxx-PQRSTZVW", "VVX.XXX-PQRSTZVW",
"x123.xx-PQRSTZVW", "X123.XX-PQRSTZVW",
"xxx.xxxx-PQRSTZVW", "XXX.XXXX-PQRSTZVW",
"12xx.xxx-PQRSTZVW", "12XX.XXX-PQRSTZVW",
"xxxx.xx12-PQRSTZVW", "XXXX.XX12-PQRSTZVW",
"99zxx.xxxx-PQRSTZVW", "99ZXX.XXXX-PQRSTZVW",
"", ""
];
var greek_versions = [
"26.53UK", "\u039161.328\u03A5",
"FR.B016", "\u0395\u03A81.0716",
"000.EU", "\u0391\u0030\u0030.23",
"PQ.YZ", "\u03a1\u0398.57\u0396",
"PQ.XYZ", "\u03a1\u0398.56\u03a5\u0396",
"PQR.YZ", "\u03a1\u0398.89\u03a5\u0396",
"PQ.RXYZ", "\u03a1\u03983.\u03a70\u03a5\u0396",
"PQR.XYZ", "\u03a1\u03986.\u03a71\u03a5\u0396",
"PQRX.YZ", "\u03a1\u03989.\u03a72\u03a5\u0396",
"PQR.SXYZ", "\u03a1\u03984.\u03a3\u03a79\u03a5\u0396",
"PQRS.XYZ", "\u03a1\u03988.\u03a3\u03a79\u03a5\u0396",
"PQRS.WXYZ", "\u03a1\u03987\u03a3.8\u03a9\u03a72\u03a5",
"PQRST.WXYZ", "\u03a1\u03987\u03a3\u03a4.8\u03a9\u03a72\u03a5",
"P4.YZ", "\u03a14.\u03a5\u0396",
"PQ.4Z", "\u03a1\u0398.4\u0396",
"PQ.4YZ", "\u03a1\u0398.26\u03a5\u0396",
"PQ4.YZ", "\u03a1\u03984.\u03a5\u0396",
"PQ.46YZ", "\u03a1\u03981.61\u03a5\u0396",
"PQ4.6YZ", "\u03a1\u03984.6\u03a5\u0396",
"PQ46.YZ", "\u03a1\u039846.\u03a5\u0396",
"PQ4.S6YZ", "\u03a1\u03982.\u03a366\u03a5\u0396",
"PQ4S.6YZ", "\u03a1\u03984\u03a3.6\u03a5\u0396",
"PQ4S.W6YZ", "\u03a1\u03984\u03a3.\u03a96\u03a5\u0396",
"PQ4PQ.6YZ9", "\u03a1\u03984\u03a1\u0398.6\u03a5\u03969",
"PQ.YZ-BCD", "\u03a1\u0398.57\u0396-\u0392\u039e\u0394",
"PQ.XYZ-BCD", "\u03a1\u0398.56\u03a5\u0396-\u0392\u039e\u0394",
"PQR.YZ-BCD", "\u03a1\u0398.89\u03a5\u0396-\u0392\u039e\u0394",
"PQ.RXYZ-BCD", "\u03a1\u03983.\u03a70\u03a5\u0396-\u0392\u039e\u0394",
"PQR.XYZ-BCD", "\u03a1\u03986.\u03a71\u03a5\u0396-\u0392\u039e\u0394",
"PQRX.YZ-BCD", "\u03a1\u03989.\u03a72\u03a5\u0396-\u0392\u039e\u0394",
"PQR.SXYZ-BCD", "\u03a1\u03984.\u03a3\u03a79\u03a5\u0396-\u0392\u039e\u0394",
"PQRS.XYZ-BCD", "\u03a1\u03988.\u03a3\u03a79\u03a5\u0396-\u0392\u039e\u0394",
"PQRS.WXYZ-BCD", "\u03a1\u03987\u03a3.8\u03a9\u03a72\u03a5-\u0392\u039e\u0394",
"PQRST.WXYZ-BCD", "\u03a1\u03987\u03a3\u03a4.8\u03a9\u03a72\u03a5-\u0392\u039e\u0394",
"P4.YZ-BCD", "\u03a14.\u03a5\u0396-\u0392\u039e\u0394",
"PQ.4Z-BCD", "\u03a1\u0398.4\u0396-\u0392\u039e\u0394",
"PQ.4YZ-BCD", "\u03a1\u0398.26\u03a5\u0396-\u0392\u039e\u0394",
"PQ4.YZ-BCD", "\u03a1\u03984.\u03a5\u0396-\u0392\u039e\u0394",
"PQ.46YZ-BCD", "\u03a1\u03981.61\u03a5\u0396-\u0392\u039e\u0394",
"PQ4.6YZ-BCD", "\u03a1\u03984.6\u03a5\u0396-\u0392\u039e\u0394",
"PQ46.YZ-BCD", "\u03a1\u039846.\u03a5\u0396-\u0392\u039e\u0394",
"PQ4.S6YZ-BCD", "\u03a1\u03982.\u03a366\u03a5\u0396-\u0392\u039e\u0394",
"PQ4S.6YZ-BCD", "\u03a1\u03984\u03a3.6\u03a5\u0396-\u0392\u039e\u0394",
"PQ4S.W6YZ-BCD", "\u03a1\u03984\u03a3.\u03a96\u03a5\u0396-\u0392\u039e\u0394",
"PQ4PQ.6YZ9-BCD", "\u03a1\u03984\u03a1\u0398.6\u03a5\u03969-\u0392\u039e\u0394",
"", ""
];
function alphabet_tests() {
out(MAXLANS + ' alphabets');
for (var t = 0; t < testpairs.length; t += 2) {
for (var i = 0; i < MAXLANS; i++) {
// see if alphabets (re)convert as expected
var str = testpairs[t];
var expect = testpairs[t + 1];
nrTests++;
var enc = convertToAlphabet(str, i);
var dec = convertToAlphabet(enc, 0);
if (dec != expect) {
nrErrors++;
out('*** ERROR ** convertToAlphabet(convertToAlphabet("' + str + '",' + i + '))= "' + dec + '", expected "' + expect + '"');
}
}
}
for (var t = 0; t < greek_versions.length; t += 2) {
// see if alphabets (re)convert as expected
var str = greek_versions[t];
var expect = greek_versions[t + 1];
nrTests++;
var enc = convertToAlphabet(str, 1); // to greek
if (enc != expect) {
nrErrors++;
out('*** ERROR *** convertToAlphabet("' + str + '",GREEK))= "' + enc + '", expected "' + expect + '"');
}
var dec = convertToAlphabet(enc, 0);
if (dec != str) {
nrErrors++;
out('*** ERROR *** convertToAlphabet(convertToAlphabet("' + str + '",' + i + '))= "' + dec + '", expected "' + str + '"');
}
}
}
function printGeneratedMapcodes(r, name) {
var n = r.length;
var t = ' ' + n + ' results generated (' + name + '):';
for (var i = 0; i < n; i++) {
t += ' (' + r[i].fullmapcode + ')';
}
out(t);
}
// perform an encode/decode test
function test_encode_decode(str, y, x, localsolutions, globalsolutions) {
nrTests++;
str = trim(str);
var sp = str.indexOf(' ');
var territory = (sp > 0) ? str.substr(0, sp) : 'AAA';
var precision = 2;
if (y < -90) {
y = -90;
} else if (y > 90) {
y = 90;
}
if (localsolutions || str != "") {
// encode locally
var r = encodeWithPrecision(y, x, precision, territory);
// test if correct nr of local solutions (if requested)
if (localsolutions) {
nrTests++;
if (r.length != localsolutions) {
nrErrors++;
out('*** ERROR *** encode(' + y.toFixed(10) + ', ' + x.toFixed(10) + ', "' + territory + '" ) does not deliver ' + localsolutions + ' solutions\n');
printGeneratedMapcodes(r, territory);
}
}
// test that expected solution is there
var foundlocal = 0;
for (var i = 0; i < r.length; i++) {
if (r[i].fullmapcode.indexOf(str) == 0) {
foundlocal = 1;
}
}
if (!foundlocal) {
nrErrors++;
out('*** ERROR *** encode(' + y.toFixed(10) + ', ' + x.toFixed(10) + ', "' + territory + '" ) does not deliver "' + str + '"');
printGeneratedMapcodes(r, territory);
}
}
// encode globally
r = encodeWithPrecision(y, x, precision);
if (globalsolutions) {
// test if correct nr of local solutions (if requested)
nrTests++;
if (r.length != globalsolutions) {
nrErrors++;
out('*** ERROR *** encode(' + y.toFixed(10) + ', ' + x.toFixed(10) + ') does not deliver ' + globalsolutions + ' global solutions\n');
printGeneratedMapcodes(r, territory);
}
}
// check that all global solutions are within 9 millimeters of coordinate
for (precision = 0; precision <= 8; precision++) {
r = encodeWithPrecision(y, x, precision);
for (i = 0; i < r.length; i++) {
str = r[i].fullmapcode;
// check conversion back and forth via Arabic script
nrTests++;
var dec = convertToAlphabet(convertToAlphabet(str, 14), 0);
if (dec != str) {
nrError++;
out('*** ERROR *** convert back and forth failed: ' + str);
}
nrTests++;
var p = decode(str);
if (p) {
var dm = distanceInMeters(y, x, p.y, p.x);
var maxerror = maxErrorInMeters(precision);
if (dm > maxerror) {
nrErrors++;
out('*** ERROR *** decode(' + str + ') = (' + p.y.toFixed(8) + ', ' + p.x.toFixed(8) + '), which is ' + (dm * 100).toFixed(4) + ' cm away (>' + (maxerror * 100) + ' cm) from (' + y.toFixed(11) + ', ' + x.toFixed(11) + ')');
}
else if (nrWarnings < 86) {
// see if decode encodes back to the same solution
var parent = getParentOf(r[i].territoryAlphaCode);
var r2 = encodeWithPrecision(p.y, p.x, precision, r[i].territoryAlphaCode);
var r3 = {length: 0};
var found = 0;
for (var i2 = 0; i2 < r2.length; i2++) {
if (r2[i2].fullmapcode == str) {
found = 1;
break;
}
}
// of, if inherited from parent country: the same parent solution
if (!found) {
if (parent >= 0) {
r3 = encodeWithPrecision(p.y, p.x, precision, parent);
for (var i3 = 0; i3 < r3.length; i3++) {
if (r3[i3].fullmapcode.split(" ")[1] == str.split(" ")[1]) {
found = 1;
break;
}
}
}
}
if (!found) {
if (!multipleBordersNearby(p.y, p.x, r[i].territoryAlphaCode)) // should re-encode
{
nrWarnings++;
out('*** WARNING *** decode(' + str + ') = (' + p.y.toFixed(15) + ', ' + p.x.toFixed(15) + ') does not re-encode from (' + y.toFixed(15) + ', ' + x.toFixed(15) + ')' + r[i].territoryAlphaCode);
printGeneratedMapcodes(r, "global");
printGeneratedMapcodes(r2, r[i].territoryAlphaCode);
printGeneratedMapcodes(r3, (parent < 0) ? "no parent" : parent);
}
}
}
}
else {
nrErrors++;
out('*** ERROR *** no result from decode(' + str + '), expected (' + y.toFixed(11) + ', ' + x.toFixed(11) + ')');
printGeneratedMapcodes(r, "global");
}
}
}
}
// test strings that are expected to FAIL a decode
function test_failing_decodes() {
var badcodes = [
"", // empty
"NLD 00.00", // all-digits
"12345.6789", // all-digits
"12345.6789-X", // all-digits
"GGG XX.XX", // unknown country
"GGG-GG XX.XX", // unknown country
"NLDX XX.XX", // unknown/long country
"NLDNLDNLD XX.XX", // unknown/long country
"USAUSA-CA XX.XX", // unknown/long country
"USA-CACA XX.XX", // unknown/long state
"US-CACACA XX.XX", // unknown/long state
"US-US XX00.XX00", // parent as state
"US-RU XX00.XX00", // parent as state
"CA-CA XX00.XX00", // state as country
"US-GG XX.XX", // unknown state (anywhere)
"RU-CA XX.XX", // unknown state (in RU)
"RUS-CA XX.XX", // unknown state (in RUS)
"NLD-CA XX.XX", // unknown state (NL has none)
"NLD X.XXX", // short prefix
"NLD XXXXXX.XX", // long prefix
"NLD XXX.X", // short postfix
"NLD XXX.XXXXX", // long postfix
"NLD XXXXX.XXX", // invalid codex 5+3
"NLD XXXX.XXXX", // non-existing codex in NLD
"NLD XXXX", // no dot
"NLD XXXXX", // no dot
"NLD XXX.", // no postfix
"NLD .XXX", // no prefix
"AAA x234.6789", // too short for AAA
"x234.6789", // too short for AAA
"NLD XXX..XXX", // 2 dots
"NLD XXX.XX.X", // 2 dots
"NLD XX.XX-Z", // Z in extension
"NLD XX.XX-1Z", // Z in extension
"NLD XX.XX-X-", // 2nd -
"NLD XX.XX-X-X", // 2nd -
// "NLD XXX.XXX-", // empty extension ALLOWED!
"NLD XX.XX-123456789", // extension too long
"NLD XXX.#XX", // invalid char
"NLD XXX.UXX", // invalid char
"NLD 123.A45", // A in invalid position
"NLD 123.E45", // E in invalid position
"NLD 123.U45", // U in invalid position
"NLD 123.1UE", // UE illegal vowel-encode
"NLD 123.1UU", // UU illegal
"NLD x23.1A0", // A0 with non-digit
"NLD 1x3.1A0", // A0 with non-digit
"NLD 12x.1A0", // A0 with non-digit
"NLD 123.xA0", // A0 with non-digit
"NLD 123.1U#", // U#
"NLD ZZ.ZZ", // nameless out of range
"NLD Q000.000", // grid out of range
"NLD ZZZ.ZZZ", // grid out of range
"NLD L222.222", // grid out of range (restricted)
"end"
];
var i;
for (i = 0; ; i++) {
var str = badcodes[i];
if (str == 'end') {
break;
}
nrTests++;
var p = decode(str);
if (p) {
nrErrors++;
out('*** ERROR *** invalid mapcode "' + str + '" should produce error');
}
}
}
// test encode/decodes from test data array; returns nonzero if more tests are needed
var next_encode_to_test = 0;
function test_encodes_decodes() {
var tests_per_timeslot = 50;
for (var i = next_encode_to_test; testdata[i] != -1; i += 5) {
if (nrErrors > 0) {
out('Too many errors!');
return 0;
}
if (tests_per_timeslot-- == 0) {
next_encode_to_test = i;
document.getElementById('perc').innerHTML = ((i / 5) * 100.0 / 15822).toFixed(2) + '%';
return 1;
}
test_encode_decode(testdata[i], testdata[i + 1], testdata[i + 2], testdata[i + 3], testdata[i + 4]);
}
document.getElementById('perc').innerHTML = '';
return 0;
}
var next_corner_to_test = 0;
function test_corner_encodes() {
var tests_per_timeslot = 20;
var last = dataLastRecord(ccode_earth);
for (var m = next_corner_to_test; m < last; m++) {
if (nrErrors > 20) {
out('Too many errors!');
return 0;
}
if (tests_per_timeslot-- == 0) {
next_corner_to_test = m;
document.getElementById('perc').innerHTML = ((m) * 100.0 / last).toFixed(2) + '%';
return 1;
}
var mm = minmaxSetup(m);
test_encode_decode("", (mm.miny + mm.maxy) / 2000000, (mm.minx + mm.maxx) / 2000000, 0, 0);
test_encode_decode("", mm.miny / 1000000.0, mm.minx / 1000000.0, 0, 0);
test_encode_decode("", (mm.maxy - 0.000001) / 1000000.0, (mm.maxx - 0.000001) / 1000000.0, 0, 0);
test_encode_decode("", (mm.maxy - 0.000001) / 1000000.0, (mm.minx + 0.000001) / 1000000.0, 0, 0);
test_encode_decode("", (mm.miny + 0.000001) / 1000000.0, (mm.maxx - 0.000001) / 1000000.0, 0, 0);
}
document.getElementById('perc').innerHTML = '';
return 0;
}
// perform tests on alpha codes (used from test_territories.js)
function test_territory(alphacode, tc, isAlias, needsParent, tcParent) {
var ccode = tc - 1;
var ccodeParent = tcParent - 1;
if (nrErrors > 30) {
return;
}
nrTests++;
var tn = getTerritoryNumber(alphacode, needsParent ? ccodeParent : 0);
if (tn != ccode) {
nrErrors++;
out('*** ERROR *** getTerritoryNumber(' + alphacode + (needsParent ? ',' + getTerritoryAlphaCode(ccodeParent) : "") + ')=' + tn + '=' + getTerritoryAlphaCode(tn) + ' but expected ' + ccode + '=' + getTerritoryAlphaCode(ccode));
}
if (needsParent == 0 && isAlias == 0 && (alphacode.length <= 3 || alphacode.charAt(3) != '-')) {
nrTests++;
var nam = getTerritoryAlphaCode(ccode);
if ((nam < 0) || (nam != alphacode && nam.indexOf('-' + alphacode) < 0)) {
nrErrors++;
out('*** ERROR *** getTerritoryAlphaCode(' + ccode + ')="' + nam + '" does not equal or contain "' + alphacode + '"');
}
}
}
function test_territory_insides() {
if (mapcode_javaversion > '2.1.2') {
var iTestData = [
"AAA", 0, 0, 0,
"AAA", 0, 5000, 0,
"AAA", 90, 0, 0,
"AAA", -90, 0, 0,
"AAA", 0, 180, 0,
"AAA", 0, -180, 0,
"ATA", -90, 0, 1, // in data version 2.2, ATA -90,0 has ZZ.ZZ and multiple borders
"ATA", -70, 0, 0,
"USA", 31, -70, 0, // international waters (not in state)
"MEX", 19, -115, 0, // international waters (not in state)
"MEX", 18.358525, -114.722672, 0, // Isla Clarion, not in a state
"MX-ROO", 20, -87, 0, // just in ROO
"MX-ROO", 20, -87.3, 0, // in ROO because in MEX
"MEX", 20, -87.3, 0, // in ROO because in MEX
"IND", 19, 87, 0,
"NLD", 52.6, 4.8, 0,
"US-WV", 40.18, -80.87, 0,
"USA", 40.18, -80.87, 0,
"US-FL", 24.7, -82.7, 0,
"USA", 24.7, -82.7, 0,
"IN-TG", 16.13, 78.75, 0,
"IN-AP", 16.13, 78.75, 0,
"IN-MH", 16.13, 78.75, 0,
"IN-PY", 16.13, 78.75, 0,
"IND", 16.13, 78.75, 0,
"USA", 40.7, -74, 0,
"US-NY", 40.7, -74, 1,
"MEX", 20.252060, -89.779821, 1,
"NLD", 52.467314, 4.494037, 1,
"MEX", 21.431778909671, -89.779828861356, 1,
"MEX", 21.431788272457, -89.779820144176, 1,
"end"];
for (var i = 0; iTestData[i] != "end"; i += 4) {
nrTests++;
if (multipleBordersNearby(iTestData[i + 1], iTestData[i + 2], iTestData[i]) != iTestData[i + 3]) {
nrErrors++;
out('*** ERROR *** ' + iTestData[i + 1] + ', ' + iTestData[i + 2] + ' multipleBordersNearby ' + iTestData[i] + ' != ' + iTestData[i + 3]);
}
}
}
}
function distance_tests() {
if (mapcode_javaversion > '2.1.0') {
var i, distance;
var coordpairs = [
// lat1, lon1, lat2, lon2, expected distance * 100000
1, 1, 1, 1, 0,
0, 0, 0, 1, 11131949079,
89, 0, 89, 1, 194279300,
3, 0, 3, 1, 11116693130,
-3, 0, -3, 1, 11116693130,
-3, -179.5, -3, 179.5, 11116693130,
-3, 179.5, -3, -179.5, 11116693130,
3, 8, 3, 9, 11116693130,
3, -8, 3, -9, 11116693130,
3, -0.5, 3, 0.5, 11116693130,
54, 5, 54.000001, 5, 11095,
54, 5, 54, 5.000001, 6543,
54, 5, 54.000001, 5.000001, 12880,
90, 0, 90, 50, 0,
0.11, 0.22, 0.12, 0.2333, 185011466,
-1];
for (i = 0; coordpairs[i] != -1; i += 5) {
nrTests++;
distance = distanceInMeters(
coordpairs[i], coordpairs[i + 1],
coordpairs[i + 2], coordpairs[i + 3]);
if (Math.round(100000.0 * distance) != coordpairs[i + 4]) {
nrErrors++;
out('*** ERROR *** distanceInMeters ' + i + ' failed: ' + distance);
}
}
}
if (mapcode_javaversion > '2.1.2') {
nrTests++;
if (maxErrorInMeters(0) < 7.48 || maxErrorInMeters(8) > 0.00001) {
nrErrors++;
out("*** ERROR *** maxErrorInMeters failed");
}
}
}
function territory_code_tests() {
var testdata = [
// expected answer, context, string
"ASM", "USA", "AS",
"MX-AGU", , "AG",
"MX-TAM", , "MEX-TAM",
"CXR", , "AUS-CX",
"BR-AL", "BRA", "AL",
"VAT", , "",
"", , "R",
"", , "RX",
"", , "RXX",
"RUS", , "RUS",
"", , "RUSSIA",
"USA", , "US",
"USA", , "USA",
"USA", , "usa",
"USA", , " usa ",
"", , "999",
"", , "-44 33",
"", , "666",
"USA", , "410",
"USA", , " 410 ",
"USA", , "410 MORE TEXT",
"USA", , "US CA",
"", , "US-TEST",
"USA", , "US OTHER TEXT",
"USA", , " US OTHER TEXT ",
"US-CA", , "US-CA",
"US-CA", , "US-CA OTHER TEXT",
"US-CA", , "USA-CA",
"RU-TT", , "RUS-TAM",
"", , "RUS-TAMX",
"RU-TT", , "RUS-TAM X OTHER TEXT",
"RU-AL", "rus", "AL",
"RU-AL", "RUS", "AL",
"RU-AL", "ru-tam", "AL",
"RU-AL", "RU-TAM", "AL",
"US-AL", "US", "AL",
"US-AL", "US-CA", "AL",
-999];
for (var i = 0; testdata[i] != -999; i += 3) {
var tc = getTerritoryAlphaCode(getTerritoryNumber(testdata[i + 2], testdata[i + 1]));
nrTests++;
if (tc != testdata[i]) {
nrErrors++;
out('*** ERROR *** getTerritoryAlphaCode("' + testdata[i + 2] + '", ' +
testdata[i + 1] + ')=' + tc + ', expected \"' + testdata[i] + '\"');
}
}
}
function territory_alphacode_tests() {
var testdata = [
// expected answer, territory, 0=local;1=international;2=shortest
"CA", "US-CA", 0, // CA is not ambiguous.
"US-CA", "US-CA", 1,
"CA", "US-CA", 2,
"CAM", "MX-CAM", 0, // Test for -CA and -CAM.
"MX-CAM", "MX-CAM", 1,
"CAM", "MX-CAM", 2,
"MN", "US-MN", 0,
"US-MN", "US-MN", 1,
"US-MN", "US-MN", 2,
"MN", "IN-MN", 0, // MN is ambiguous as a state.
"IN-MN", "IN-MN", 1,
"IN-MN", "IN-MN", 2,
"IN", "US-IN", 0, // IN is ambigious as a state/country.
"US-IN", "US-IN", 1,
"US-IN", "US-IN", 2,
"NLD", "NLD", 0, // NLD is never ambiguous.
"NLD", "NLD", 1,
"NLD", "NLD", 2,
-999
];
for (var i = 0; testdata[i] != -999; i += 3) {
var tc = getTerritoryAlphaCode(getTerritoryNumber(testdata[i + 1]), testdata[i + 2]);
nrTests++;
if (tc != testdata[i]) {
nrErrors++;
out('*** ERROR *** getTerritoryAlphaCode("' + testdata[i + 1] + '", ' + testdata[i + 2] + ')=' + tc +
', expected \"' + testdata[i] + '\"');
}
}
}
var myVar = setInterval(function () {
stepper()
}, 20);
var dostep = 0;
function stepper() {
switch (dostep) {
case 0:
out('Mapcode JavaScript version ' + mapcode_javaversion);
break;
case 1:
out('<HR>Alphabet tests');
break;
case 2:
alphabet_tests();
break;
case 3:
out('<HR>Distance tests');
distance_tests();
out('<HR>Territory tests');
out((ccode_earth + 1) + ' territories');
test_territory_insides();
break;
case 4:
territory_code_tests();
territory_alphacode_tests();
test_territories();
break;
case 5:
out('<hr>Incorrect format tests');
break;
case 6:
test_failing_decodes();
break;
case 7:
out('<HR>Encode/decode tests');
break;
case 8:
if (test_encodes_decodes()) {
dostep--;
}
break;
case 9:
out('<HR>Edge-case encode/decode tests');
break;
case 10:
if (test_corner_encodes()) {
dostep--;
}
break;
case 11:
out('<hr>Done<br>');
out('Executed ' + nrTests + ' tests, found ' + nrErrors + ' errors (and ' + nrWarnings + ') warnings');
if ((nrWarnings > 0) || (nrErrors > 0)) {
out("<b>UNIT TESTS FAILED!</b>");
}
else {
out("Unit tests passed");
}
break;
default:
clearInterval(myVar);
break;
}
dostep++;
}
</script>
</body>
</html>