-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathunittest.c
More file actions
2106 lines (1884 loc) · 86.8 KB
/
Copy pathunittest.c
File metadata and controls
2106 lines (1884 loc) · 86.8 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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014-2016 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.
*/
/**
* This application performs a number of tests on the Mapcode C library.
* It helps to establish that all routines work properly.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include "../mapcodelib/mapcoder.h"
#include "../mapcodelib/internal_data.h"
#include "decode_test.h"
// If your platform does not support pthread.h, either add -DNO_POSIX_THREADS
// to your compiler command-line, or uncomment the following line:
// #define NO_POSIX_THREADS
#ifdef NO_POSIX_THREADS
// Fake implementation of pthread to not use threads at all:
#define pthread_mutex_lock(ignore)
#define pthread_mutex_unlock(ignore)
#define pthread_mutex_t int
#define PTHREAD_MUTEX_INITIALIZER 0
#define pthread_t int
#define pthread_join(ignore1, ignore2) (!ignore1)
#define pthread_create(ignore1, ignore2, func, context) func(context)
#define MAX_THREADS 1
#else
#include <pthread.h>
static const double METERS_PER_DEGREE_LAT = 110946.252133;
static const double METERS_PER_DEGREE_LON = 111319.490793;
#define MAX_THREADS 16 // Optimal: not too much, approx. nr of cores * 2, better no more than 32.
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#define REF(x) if (x) {} else {}
static int nrErrors = 0;
static void foundError(void) {
pthread_mutex_lock(&mutex);
++nrErrors;
pthread_mutex_unlock(&mutex);
}
static char *myConvertToRoman(char *asciiBuffer, const UWORD *utf16String) {
MapcodeElements mapcodeElements;
double lat, lon;
*asciiBuffer = 0;
decodeMapcodeToLatLonUtf16(&lat, &lon, utf16String, TERRITORY_FRA, &mapcodeElements);
sprintf(asciiBuffer, "%s%s%s%s%s",
mapcodeElements.territoryISO,
*mapcodeElements.territoryISO ? " " : "",
mapcodeElements.properMapcode,
*mapcodeElements.precisionExtension ? "-" : "",
mapcodeElements.precisionExtension);
return asciiBuffer;
}
static enum MapcodeError myParseMapcodeString(
MapcodeElements *mapcodeElements,
const char *string,
enum Territory territory) {
double lat, lon;
enum MapcodeError err = decodeMapcodeToLatLonUtf8(&lat, &lon, string, territory, mapcodeElements);
// filter out post-parsing errors
if (err == ERR_MISSING_TERRITORY || err == ERR_MAPCODE_UNDECODABLE || err == ERR_EXTENSION_UNDECODABLE) {
return ERR_OK;
}
return err;
}
static int testMapcodeFormats(void) {
int nrTests = 0;
static const struct {
const char *input; // user input
enum MapcodeError parseError; // expected error
enum MapcodeError decodeError; // expected error when decoded
} formattests[] = {
{"nld bc.xy-x1y", ERR_OK, ERR_OK},
{"pan a1.2e-b2c", ERR_OK, ERR_OK},
{"###################", ERR_INVALID_CHARACTER},
{"...................", ERR_UNEXPECTED_DOT},
{"1111111111111111.11", ERR_INVALID_MAPCODE_FORMAT},
{"US-XXXXXXXXXXXXXXXX", ERR_BAD_TERRITORY_FORMAT},
{"US-----------------", ERR_UNEXPECTED_HYPHEN},
{"-------------------", ERR_UNEXPECTED_HYPHEN},
{"cck XX.XX", ERR_OK, ERR_OK}, // nameless22
{"cze XX.XXX", ERR_OK, ERR_OK}, // nameless23
{"NLD XXX.XX", ERR_OK, ERR_OK}, // nameless32
{"VAT 5d.dd", ERR_OK, ERR_OK}, // Grid22
{"NLD XX.XXX", ERR_OK, ERR_OK}, // Grid23
{"bhr xxx.xx", ERR_OK, ERR_OK}, // Grid32
{"FRA XXX.XXX", ERR_OK, ERR_OK}, // Grid33
{"irl xx.xxxx", ERR_OK, ERR_OK}, // Grid24
{"cub xxxx.xx", ERR_OK, ERR_OK}, // Grid42
{"ben xxxx.xxx", ERR_OK, ERR_OK}, // Grid34
{"USA xxxx.xxxx", ERR_OK, ERR_OK}, // Grid44
{"US-AZ hhh.hh", ERR_OK, ERR_OK}, // HGrid32
{"Bel hhh.hhh", ERR_OK, ERR_OK}, // HGrid33
{"PAN hh.hhhh", ERR_OK, ERR_OK}, // HGrid24
{"GRC hhhh.hh", ERR_OK, ERR_OK}, // HGrid42
{"NZL hhhh.hhh", ERR_OK, ERR_OK}, // HGrid43
{"KAZ hhh.hhhh", ERR_OK, ERR_OK}, // HGrid34
{"RUS xxxx.xxxx", ERR_OK, ERR_OK}, // HGrid44
{"CN-SH hhhh.hhhh", ERR_OK, ERR_OK}, // HGrid44
{"VAT hhhhh.hhhh", ERR_OK, ERR_OK}, // HGrid54
{"hhhhh.hhhh", ERR_OK, ERR_OK}, // HGrid54
{"TUV hh.hhh", ERR_OK, ERR_OK}, // AutoHeader23
{"LVA L88.ZVR", ERR_OK, ERR_OK}, // AutoHeader33
{"WLF XLG.3GP", ERR_OK, ERR_OK}, // HGrid33 R
{"VAT j0q3.27r", ERR_OK, ERR_OK}, // HGrid43 R
{"PAK hhhh.hhhh", ERR_OK, ERR_OK}, // HGrid44 R
{"NLD 49.4V", ERR_OK, ERR_OK},
{"NLX 49.4V", ERR_UNKNOWN_TERRITORY},
{"49.4V", ERR_OK, ERR_MISSING_TERRITORY},
{"BRA 49.4V", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 0
{"BRA XXXXX.XXX", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 0
{"NLD XXXX.XXXX", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 0
{"NLD ZZ.ZZ", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 1 nameless
{"NLD Q000.000", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 2 grid
{"NLD L222.222", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 3 restricted
{"usa A222.22AA", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 4 grid
{"atf hhh.hhh", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 5 autoh zone
{"ASM zz.zzh", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 6 autoh out
{"nld ZNZ.RZG-B", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 0
{"WLF 01.AE-09V", ERR_OK, ERR_OK},
{"LVA LDV.ZVR-B ", ERR_OK, ERR_OK}, // AutoHeader
{"LVA LDV.ZVR-BY ", ERR_OK, ERR_EXTENSION_UNDECODABLE},
{"01.AE", ERR_OK, ERR_MISSING_TERRITORY},
{"nld 01.AE", ERR_OK, ERR_OK},
{"nld oi.AE", ERR_OK, ERR_OK},
{"oi.oi", ERR_ALL_DIGIT_CODE},
{"nld oi.OI-xxx", ERR_ALL_DIGIT_CODE},
{"CUB 3467.UY", ERR_OK, ERR_OK},
{"34.UY", ERR_OK, ERR_MISSING_TERRITORY},
{"mx XX.XX", ERR_OK, ERR_OK},
{"", ERR_DOT_MISSING},
{"ttat.tt ", ERR_INVALID_VOWEL},
{"ttat-tt tt.tt", ERR_INVALID_VOWEL},
{"ttat tt.tt", ERR_INVALID_VOWEL},
{"XXAX.XXXX", ERR_INVALID_VOWEL},
{"2A22.2222", ERR_INVALID_VOWEL},
{"22A2.2222", ERR_INVALID_VOWEL},
{"MAP.CODE", ERR_INVALID_VOWEL},
{"XAXX.XXXX", ERR_INVALID_VOWEL},
{"XXXA.XXXX", ERR_INVALID_VOWEL},
{"XXXAX.XXXX", ERR_INVALID_VOWEL},
{"XXXXA.XXXX", ERR_INVALID_VOWEL},
{"nld XXXX.XXXXA", ERR_INVALID_VOWEL},
{"nld XXXX.ALA", ERR_INVALID_VOWEL},
{"nld XXXX.LAXA", ERR_INVALID_VOWEL},
{"nld XXXX.LLLLA", ERR_INVALID_VOWEL},
{"nld XXXX.A2e", ERR_INVALID_VOWEL},
{"nld XXXX.2e2e", ERR_INVALID_VOWEL},
{"nld XXXX.2222u", ERR_INVALID_VOWEL},
{"222A.2222", ERR_INVALID_VOWEL},
{"222A2.2222", ERR_INVALID_VOWEL},
{"ttt 2222A.2222", ERR_INVALID_VOWEL},
{"2222.2AAA", ERR_INVALID_VOWEL},
{"A222.2AAA", ERR_INVALID_VOWEL},
{"usa 2222.22A2", ERR_OK, ERR_OK},
{"usa 2222.22AA", ERR_OK, ERR_OK},
{".123", ERR_UNEXPECTED_DOT},
{".xyz", ERR_UNEXPECTED_DOT},
{"x.xyz", ERR_UNEXPECTED_DOT},
{"xxx.z-12", ERR_UNEXPECTED_HYPHEN},
{"xx.xx.", ERR_UNEXPECTED_DOT},
{"xxxx xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"xxxxx xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"123", ERR_DOT_MISSING},
{"xxx.", ERR_MAPCODE_INCOMPLETE},
{"xxx.z", ERR_MAPCODE_INCOMPLETE},
{"NLD 49.4V-", ERR_MAPCODE_INCOMPLETE},
{"NLD 49.4V", ERR_OK, ERR_OK},
{" NLD 49.4V ", ERR_OK, ERR_OK},
{"NLD 49.4V-1", ERR_OK, ERR_OK},
{"NLD 49.4V-12", ERR_OK, ERR_OK},
{"NLD 49.4V-123", ERR_OK, ERR_OK},
{"NLD 49.4V-12345678", ERR_OK, ERR_OK},
{"NLD 49.4V- ", ERR_EXTENSION_INVALID_LENGTH},
{"NLD 49.4V-123456789", ERR_EXTENSION_INVALID_LENGTH},
{"49.4V-xxxxxxxxxxxxxx", ERR_EXTENSION_INVALID_LENGTH},
{"DD.DD- ", ERR_EXTENSION_INVALID_LENGTH},
{"nld DD.DD-", ERR_MAPCODE_INCOMPLETE},
{"TAM 49.4V", ERR_OK, ERR_OK},
{"BRA 49.4V", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 0
{"CA 49.4V", ERR_OK, ERR_OK},
{"N 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"XXXX ", ERR_BAD_TERRITORY_FORMAT},
{"XXXXX ", ERR_BAD_TERRITORY_FORMAT},
{"XXXX 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"XXXXX 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"-XX 49.4V", ERR_UNEXPECTED_HYPHEN},
{"X-XX 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"XXXX-XX 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"XX-X 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"XX-XXXX 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"XX-XXXE 49.4V", ERR_BAD_TERRITORY_FORMAT},
{"12.34", ERR_ALL_DIGIT_CODE},
{"NLD 12.34", ERR_ALL_DIGIT_CODE},
{"AAA 12.34", ERR_ALL_DIGIT_CODE},
{"AAA 12.34-XXX", ERR_ALL_DIGIT_CODE},
{"123 12.34-123", ERR_ALL_DIGIT_CODE},
{"xx-xx 12.34", ERR_ALL_DIGIT_CODE},
{"12-34 12.34", ERR_ALL_DIGIT_CODE},
{"CN-34 12.3X", ERR_OK, ERR_OK},
{" TAM XX.XX-XX ", ERR_OK, ERR_OK},
{" TAM XXX.XX-XX ", ERR_OK, ERR_OK},
{" TAM XX.XXX-XX ", ERR_OK, ERR_OK},
{" TAM XX.XXXX-XX ", ERR_OK, ERR_OK},
{" TAM XXX.XXX-XX ", ERR_OK, ERR_OK},
{" gab XXXX.XX-XX ", ERR_OK, ERR_OK},
{" kAZ XXX.XXXX-XX ", ERR_OK, ERR_OK},
{" IND XXXX.XXX-XX ", ERR_OK, ERR_OK},
{" USA XXXX.XXXX-XX ", ERR_OK, ERR_OK},
{" VAT XXXXX.XXXX-XX ", ERR_OK, ERR_OK},
{" NLD XXXXX.XXXX-XX ", ERR_OK, ERR_OK},
{" USA XXXXX.XXXX-XX ", ERR_OK, ERR_OK},
{" XXXXX.XXXX-XX ", ERR_OK, ERR_OK},
{" usa XXXXX.XXX-XX ", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // type 0
{" XXXXX.XXX-XX ", ERR_OK, ERR_MISSING_TERRITORY},
{"xx-xx.x xx.xx", ERR_UNEXPECTED_DOT},
{"xx-xx-x xx.xx", ERR_UNEXPECTED_HYPHEN},
{"xx.xx-x-x", ERR_UNEXPECTED_HYPHEN},
{"xx-xx xx-xx", ERR_UNEXPECTED_HYPHEN},
{"xx-xx xx-xx.xx", ERR_UNEXPECTED_HYPHEN},
{"xx.xx.xx", ERR_UNEXPECTED_DOT},
{"xx-xx xx.xx.xx", ERR_UNEXPECTED_DOT},
{"xx-xx xx.xx-xx-xx", ERR_UNEXPECTED_HYPHEN},
{"xx-xx xx.xx x", ERR_TRAILING_CHARACTERS},
{"xx-xx xx.xx-x x", ERR_TRAILING_CHARACTERS},
{"xx-xx xx.xx-x -", ERR_UNEXPECTED_HYPHEN},
{"xx-xx xx.xx-x .", ERR_UNEXPECTED_DOT},
{"xx-xx xx.xx-x 2", ERR_TRAILING_CHARACTERS},
{"xx-xx xx.x#x", ERR_INVALID_CHARACTER},
{"xx# xx.xx", ERR_INVALID_CHARACTER},
{"xx-xx #xx.xx", ERR_INVALID_CHARACTER},
{"xx-xx xx.xx-xx#xx", ERR_INVALID_CHARACTER},
{"xx-xx -xx.xx", ERR_UNEXPECTED_HYPHEN},
{"xx-xx .xx.xx", ERR_UNEXPECTED_DOT},
{".123", ERR_UNEXPECTED_DOT},
{" .123", ERR_UNEXPECTED_DOT},
{"", ERR_DOT_MISSING},
{" ", ERR_DOT_MISSING},
{"-xx.xx", ERR_UNEXPECTED_HYPHEN},
{" - xx.xx", ERR_UNEXPECTED_HYPHEN},
{"D xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"D.123", ERR_UNEXPECTED_DOT},
{"D", ERR_DOT_MISSING},
{"D-xxxxx", ERR_BAD_TERRITORY_FORMAT},
{"DD", ERR_DOT_MISSING},
{"DDDa.DDD", ERR_INVALID_VOWEL},
{"DDD", ERR_DOT_MISSING},
{"DDDD xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"DDDDE.xxxx", ERR_INVALID_VOWEL},
{"DDDD", ERR_DOT_MISSING},
{"DDDD-CA xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"DDDDD CA xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"DDDDDA xx.xx", ERR_INVALID_VOWEL},
{"DDDDD", ERR_DOT_MISSING},
{"DDDDD-CA xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"DDDDD..xxxx", ERR_UNEXPECTED_DOT},
{"DDDDD.", ERR_MAPCODE_INCOMPLETE},
{"DDDDD.-xxxx.xx", ERR_UNEXPECTED_HYPHEN},
{"DDD.L.LLL ", ERR_UNEXPECTED_DOT},
{"DDD.L", ERR_MAPCODE_INCOMPLETE},
{"DDD.L-xxxxxxxx", ERR_UNEXPECTED_HYPHEN},
{"DD.DD.CA", ERR_UNEXPECTED_DOT},
{"DD.DDD.CA", ERR_UNEXPECTED_DOT},
{"DD.DDDD.CA ", ERR_UNEXPECTED_DOT},
{"DD.DDDDA ", ERR_INVALID_VOWEL},
{"DD.DD-. ", ERR_UNEXPECTED_DOT},
{"DD.DD-", ERR_MAPCODE_INCOMPLETE},
{"DD.DD--XXX", ERR_UNEXPECTED_HYPHEN},
{"DD.DD-x. ", ERR_UNEXPECTED_DOT},
{"DD.DD-A", ERR_EXTENSION_INVALID_CHARACTER},
{"DD.DD-xA", ERR_EXTENSION_INVALID_CHARACTER},
{"DD.DD-xxxE", ERR_EXTENSION_INVALID_CHARACTER},
{"DD.DD-xxxxxu", ERR_EXTENSION_INVALID_CHARACTER},
{"DD.DD-x-xxx", ERR_UNEXPECTED_HYPHEN},
{"ta.xx ", ERR_INVALID_VOWEL},
{"ta", ERR_DOT_MISSING},
{"DAD- ", ERR_BAD_TERRITORY_FORMAT},
{"DAD-. ", ERR_UNEXPECTED_DOT},
{"DAD-", ERR_BAD_TERRITORY_FORMAT},
{"DAD--XXX", ERR_UNEXPECTED_HYPHEN},
{"DAD-X xx.xx", ERR_BAD_TERRITORY_FORMAT},
{"DAD-X. ", ERR_UNEXPECTED_DOT},
{"DAD-X", ERR_BAD_TERRITORY_FORMAT},
{"DAD-X-XXX", ERR_UNEXPECTED_HYPHEN},
{"DAD-XX.XX ", ERR_UNEXPECTED_DOT},
{"DAD-XX", ERR_DOT_MISSING},
{"DAD-XX-XX", ERR_UNEXPECTED_HYPHEN},
{"DAD-XXX.XX ", ERR_UNEXPECTED_DOT},
{"DAD-XXXX", ERR_BAD_TERRITORY_FORMAT},
{"DAD-XXXA", ERR_BAD_TERRITORY_FORMAT},
{"DAD-XXX", ERR_DOT_MISSING},
{"DAD-XXX-XX", ERR_UNEXPECTED_HYPHEN},
{"DAD-XX .XX ", ERR_UNEXPECTED_DOT},
{"DAD-XX ", ERR_DOT_MISSING},
{"DAD-XX -XX", ERR_UNEXPECTED_HYPHEN},
{"DD-DD A ", ERR_DOT_MISSING},
{"DD-DD A. ", ERR_UNEXPECTED_DOT},
{"DD-DD AA.33 ", ERR_INVALID_VOWEL},
{"DD-DD A", ERR_DOT_MISSING},
{"DD-DD A-XX", ERR_UNEXPECTED_HYPHEN},
{"DD-DD A3 ", ERR_DOT_MISSING},
{"DD-DD A3A.XX ", ERR_INVALID_VOWEL},
{"DD-DD A3", ERR_DOT_MISSING},
{"DD-DD A3-XX", ERR_UNEXPECTED_HYPHEN},
{"DD-DD A33 ", ERR_DOT_MISSING},
{"DD-DD A33A.XX", ERR_INVALID_VOWEL},
{"DD-DD A33", ERR_DOT_MISSING},
{"DD-DD A33-XX", ERR_UNEXPECTED_HYPHEN},
{"DD-DD xx.xx .", ERR_UNEXPECTED_DOT},
{"DD-DD xx.xx x", ERR_TRAILING_CHARACTERS},
{"DD-DD xx.xx a", ERR_TRAILING_CHARACTERS},
{"DD-DD xx.xx -x", ERR_UNEXPECTED_HYPHEN},
{"xx.xx .xx", ERR_UNEXPECTED_DOT},
{"xx.xx x", ERR_TRAILING_CHARACTERS},
{"xx.xx a", ERR_TRAILING_CHARACTERS},
{"xx.xx -123", ERR_UNEXPECTED_HYPHEN},
{" xx.xx-DD .", ERR_UNEXPECTED_DOT},
{" xx.xx-DD x", ERR_TRAILING_CHARACTERS},
{" xx.xx-DD a", ERR_TRAILING_CHARACTERS},
{"xx.xx xxxxxxxxxxxx", ERR_TRAILING_CHARACTERS},
{" xx.xx-DD -", ERR_UNEXPECTED_HYPHEN},
{"tta.ttt ", ERR_INVALID_VOWEL},
{"ttaa.ttt ", ERR_INVALID_VOWEL},
{"tta", ERR_DOT_MISSING},
{"DDD. ", ERR_INVALID_MAPCODE_FORMAT}, // 6/0 : white na dot
{"DDDDD. xxxx.xx", ERR_INVALID_MAPCODE_FORMAT}, // 6/0 : white na dot
{"DDD.L ", ERR_INVALID_MAPCODE_FORMAT}, // 7.0 : postfix too short
{"DDDDDD xx.xx", ERR_INVALID_MAPCODE_FORMAT}, // 5/2 : 6char ter
{"DDDDDD.xxx", ERR_INVALID_MAPCODE_FORMAT}, // 5/2 : 6char mc
// errors because there are too many letters after a postfix vowel
{"XXXX.AXXX", ERR_INVALID_VOWEL},
{"nld XXXX.AXX", ERR_INVALID_VOWEL},
{"nld XXXX.XAXX", ERR_INVALID_VOWEL},
{"nld XXXX.AXXA", ERR_INVALID_VOWEL},
{"2222.A22", ERR_INVALID_VOWEL},
{"2222.A222", ERR_INVALID_VOWEL},
{"2222.2A22", ERR_INVALID_VOWEL},
// 5th letter
{"nld DD.DDDDD ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
{"nld XXXX.XXXXX", ERR_OK, ERR_MAPCODE_UNDECODABLE},
{" TAM XX.XXXXX-XX ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
{" TAM XXX.XXXXX-XX ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
{" TAM XXXX.XXXXX-X ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
{" TAM XXXXX.XXXXX-X ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
// errors because the postfix has a 6th letter
{"DD.DDDDDD ", ERR_INVALID_MAPCODE_FORMAT},
{"nld XXXX.XXXXXX", ERR_INVALID_MAPCODE_FORMAT},
{" TAM XX.XXXXXX-XX ", ERR_INVALID_MAPCODE_FORMAT},
{" TAM XXX.XXXXXX-XX ", ERR_INVALID_MAPCODE_FORMAT},
{" TAM XXXX.XXXXXX-X ", ERR_INVALID_MAPCODE_FORMAT},
{" TAM XXXXX.XXXXXX-X ", ERR_INVALID_MAPCODE_FORMAT},
// Check tabs, spaces and control characters.
{"NLD 49.YV", ERR_OK, ERR_OK},
{" NLD 49.YV", ERR_OK, ERR_OK},
{"\tNLD 49.YV", ERR_OK, ERR_OK},
{"NLD 49.YV ", ERR_OK, ERR_OK},
{"NLD 49.YV\t", ERR_OK, ERR_OK},
{"NLD 49.YV", ERR_OK, ERR_OK},
{"NLD\t49.YV", ERR_OK, ERR_OK},
{"NLD\n49.YV", ERR_INVALID_CHARACTER},
{"NLD\r49.YV", ERR_INVALID_CHARACTER},
{"NLD\v49.YV", ERR_INVALID_CHARACTER},
{"NLD\b49.YV", ERR_INVALID_CHARACTER},
{"NLD\a49.YV", ERR_INVALID_CHARACTER},
{NULL, ERR_OK, ERR_OK}
};
int shouldSucceed = 0; // count nr of calls that SHOULD be successful.
int total = 0;
int succeeded = 0;
int i;
for (i = 0; formattests[i].input != NULL; ++i) {
MapcodeElements mapcodeElements;
enum MapcodeError parseError = myParseMapcodeString(&mapcodeElements, formattests[i].input, 0);
enum MapcodeError formatError = compareWithMapcodeFormatUtf8(formattests[i].input);
if (formattests[i].parseError == ERR_OK) {
shouldSucceed++;
}
nrTests++;
if (parseError != formatError) {
// there is a special case where parse knows about valid territories
if (formatError || formattests[i].parseError != ERR_UNKNOWN_TERRITORY) {
foundError();
printf("*** ERROR *** \"%s\" : myParseMapcodeString=%d, compareWithMapcodeFormatUtf8=%d\n",
formattests[i].input, parseError, formatError);
}
}
nrTests++;
if (formattests[i].parseError != parseError) {
foundError();
printf("*** ERROR *** compareWithMapcodeFormatUtf8(\"%s\") returns %d (%d expected)\n",
formattests[i].input,
parseError, formattests[i].parseError);
}
nrTests++;
++total;
if (parseError == 0) {
double lat, lon;
int decodeError = decodeMapcodeToLatLonUtf8(&lat, &lon, formattests[i].input, TERRITORY_UNKNOWN, NULL);
++succeeded;
if (decodeError != formattests[i].decodeError) {
foundError();
printf("*** ERROR *** myParseMapcodeString(\"%s\")=%d, expected %d\n", formattests[i].input,
decodeError,
formattests[i].decodeError);
}
}
}
if (succeeded != shouldSucceed) {
foundError();
printf("*** ERROR *** %d of %d myParseMapcodeString() calls succeeded (expected %d)\n", succeeded, total,
shouldSucceed);
}
return nrTests;
}
static int testAlphabetParser(void) {
int nrTests = 0;
static const struct {
const char *userInput;
const char *expected;
} parseTests[] = {
{"nld bc.xy-x1y", "NLD BC.XY-X1Y"},
{"nld Α0.12", "NLD 00.E0"}, // A-encoded greek
{"DNK РФ.ХХ", "DNK PQ.XX"}, // greek
{"GRC HP.NO-1Х2ХХ", "GRC HP.NO-1X2XX"},
{"PRT 31.E2-b2c", "PRT 31.E2-B2C"},
{"GBR רר.56ר", "GBR XX.XX"},// hebrew abjad
{"BEL طظ.56ط ", "BEL PQ.XP"}, // arab abjad
{"FRA ヒフ.ラヲ", "FRA PQ.XZ"},
{NULL, NULL}
};
int i;
for (i = 0; parseTests[i].userInput; i++) {
char romanized[MAX_MAPCODE_RESULT_LEN + 1];
MapcodeElements mapcodeElements;
enum MapcodeError parseError = myParseMapcodeString(&mapcodeElements, parseTests[i].userInput,
TERRITORY_UNKNOWN);
nrTests++;
if (parseError) {
foundError();
printf("*** ERROR *** myParseMapcodeString(\"%s\") failed with error %d (expected %s)\n",
parseTests[i].userInput, (int) parseError, parseTests[i].expected);
} else {
nrTests++;
sprintf(romanized, "%s %s%s%s",
mapcodeElements.territoryISO,
mapcodeElements.properMapcode,
*mapcodeElements.precisionExtension ? "-" : "",
mapcodeElements.precisionExtension);
if (strcmp(romanized, parseTests[i].expected) != 0) {
foundError();
printf("*** ERROR *** myParseMapcodeString(\"%s\") = \"%s\", (expected %s)\n", parseTests[i].userInput,
romanized, parseTests[i].expected);
} else {
double lat1, lon1, lat2, lon2;
int err1 = decodeMapcodeToLatLonUtf8(&lat1, &lon1, parseTests[i].userInput, TERRITORY_UNKNOWN, NULL);
int err2 = decodeMapcodeToLatLonUtf8(&lat2, &lon2, romanized, TERRITORY_UNKNOWN, NULL);
++nrTests;
if (err1 || err2) {
foundError();
printf("*** ERROR *** decoding \"%s\" returns %d, decoding \"%s\" returns %d\n",
parseTests[i].userInput, err1, romanized, err2);
}
if (lat1 != lat2 || lon1 != lon2) {
foundError();
printf("*** ERROR *** decoding \"%s\" returns (%f,%f), decoding \"%s\" returns (%f,%f)\n",
parseTests[i].userInput, lat1, lon1, romanized, lat2, lon2);
}
}
}
}
return nrTests;
}
// Show progress.
static void showTestProgress(int at, int max, int nrTests) {
static clock_t prevTick = 0;
// No worries, clock() is a very fast call.
clock_t tick = clock() / (CLOCKS_PER_SEC / 2);
if (tick != prevTick) {
prevTick = tick;
// Use stderr to not pollute logs.
fprintf(stderr, "\r%0.1f%% (executed %0.1fM tests)\r", (at * 100.0) / max, nrTests / 1000000.0);
}
}
static void printGeneratedMapcodes(const char *title, const Mapcodes *mapcodes) {
int i, nrresults = mapcodes->count;
printf(" %s: %d results", title, nrresults);
for (i = 0; i < nrresults; i++) {
const char *m = mapcodes->mapcode[i];
printf(" (%s)", m);
}
printf("\n");
}
// test encode x,y to M, decode M, re-encode back to M
static int testEncodeAndDecode(const char *str, double y, double x, int localsolutions, int globalsolutions) {
int nrTests = 0;
char clean[MAX_MAPCODE_RESULT_LEN + 1];
const char *p, *s;
int found = 0;
enum Territory tc = TERRITORY_NONE;
int len, i, err, nrresults;
Mapcodes mapcodes;
double lat, lon;
int precision = MAX_PRECISION_DIGITS;
if (y < -90) {
y = -90;
} else if (y > 90) {
y = 90;
}
// if str: determine "precision", territory "tc", and a "clean" copy of str
if (*str) {
char territory[MAX_ISOCODE_LEN + 1];
// find first territory letter in s
s = str;
while (*s > 0 && *s <= 32) {
s++;
}
// parse territory, if any
p = strchr(s, ' ');
len = p ? (int) (p - s) : 0;
if (p && len <= MAX_ISOCODE_LEN) {
// copy and recognize territory
memcpy(territory, s, (size_t) len);
territory[len] = 0;
tc = getTerritoryCode(territory, TERRITORY_NONE);
// make s skip to start of proper mapcode
s = p;
while (*s > 0 && *s <= 32) {
s++;
}
} else {
// assume s is the start of the proper mapcode
territory[0] = 0;
tc = getTerritoryCode("AAA", TERRITORY_NONE);
}
// build normalised version of source string in "clean"
len = (int) strlen(s);
while (len > 0 && s[len - 1] > 0 && s[len - 1] <= 32) {
len--;
}
i = (int) strlen(territory);
if (i) {
strcpy(clean, territory);
strcat(clean, " ");
i++;
}
if (len + i >= MAX_MAPCODE_RESULT_LEN) {
len = 0;
}
memcpy(clean + i, s, (size_t) len);
clean[len + i] = 0;
// determine precision of the source string
s = strchr(clean, '-');
if (s) {
precision = (int) strlen(s + 1);
} else {
precision = 0;
}
}
// test if correct nr of local solutions (if requested)
if (localsolutions) {
// encode
nrresults = encodeLatLonToMapcodes(&mapcodes, y, x, tc, precision);
++nrTests;
if (nrresults != localsolutions) {
foundError();
printf("*** ERROR *** encode(%0.8f, %0.8f,%d) does not deliver %d local solutions\n",
y, x, tc, localsolutions);
printGeneratedMapcodes("Delivered", &mapcodes);
}
// test that EXPECTED solution is there (if requested)
++nrTests;
for (i = 0; i < nrresults; i++) {
const char *m = mapcodes.mapcode[i];
if (strstr(m, clean) == m) {
found = 1;
break;
}
}
if (!found) {
foundError();
printf("*** ERROR *** encode(%0.8f, %0.8f) does not deliver \"%s\"\n", y, x, clean);
printGeneratedMapcodes("Delivered", &mapcodes);
}
}
// test if correct nr of global solutions (if requested)
if (globalsolutions > 0) {
++nrTests;
nrresults = encodeLatLonToMapcodes(&mapcodes, y, x, TERRITORY_UNKNOWN, precision);
if (nrresults != globalsolutions) {
foundError();
printf("*** ERROR *** encode(%0.8f, %0.8f) does not deliver %d global solutions\n", y, x, globalsolutions);
printGeneratedMapcodes("Delivered", &mapcodes);
}
}
// test all global solutions at all precisions...
for (precision = 0; precision <= 8; precision++) {
nrresults = encodeLatLonToMapcodes(&mapcodes, y, x, TERRITORY_UNKNOWN, precision);
for (i = 0; i < nrresults; i++) {
const char *strResult = mapcodes.mapcode[i];
// check if every solution decodes
++nrTests;
err = decodeMapcodeToLatLonUtf8(&lat, &lon, strResult, TERRITORY_UNKNOWN, NULL);
if (err) {
foundError();
printf("*** ERROR *** decode('%s') = no result, expected ~(%0.8f, %0.8f)\n", strResult, y, x);
} else {
double dm = distanceInMeters(y, x, lat, lon);
double maxerror = maxErrorInMeters(precision);
// check if decode is sufficiently close to the encoded coordinate
++nrTests;
if (dm > maxerror) {
foundError();
printf("*** ERROR *** decode('%s') = (%0.8f, %0.8f), which is %0.4f cm away (>%0.4f cm) from (%0.8f, %0.8f)\n",
strResult, lat, lon,
dm * 100.0, maxerror * 100.0, y, x);
} else {
Mapcodes mapcodesTerritory;
Mapcodes mapcodesParent;
enum Territory tc2 = TERRITORY_NONE;
enum Territory tcParent = TERRITORY_NONE;
int j;
char *e = strchr(strResult, ' ');
found = 0;
if (e) {
*e = 0;
tc2 = getTerritoryCode(strResult, TERRITORY_NONE);
tcParent = getParentCountryOf(tc2);
*e = ' ';
}
++nrTests;
// see if the original mapcode was generated
{
const int nr = encodeLatLonToMapcodes(&mapcodesTerritory, lat, lon, tc2, precision);
for (j = 0; j < nr; j++) {
if (strcmp(mapcodesTerritory.mapcode[j], strResult) == 0) {
found = 1;
break;
}
}
}
// if not: see if the original mapcode was generated for the parent
if (!found && (tcParent > _TERRITORY_MIN)) {
const int nr = encodeLatLonToMapcodes(&mapcodesParent, lat, lon, tcParent, precision);
for (j = 0; j < nr; j++) {
if (strcmp(strchr(mapcodesParent.mapcode[j], ' '), strchr(strResult, ' ')) == 0) {
found = 1;
break;
}
}
}
if (!found) { // within 7.5 meters, but not reproduced!
if (!multipleBordersNearby(lat, lon, tc2)) { // but SHOULD be reproduced!
foundError();
printf("*** ERROR *** %s does not re-encode (%0.15f,%0.15f) from (%0.15f,%0.15f)\n",
strResult, lat, lon, y, x);
printGeneratedMapcodes("Global ", &mapcodes);
printGeneratedMapcodes("Territory", &mapcodesTerritory);
if (tcParent > _TERRITORY_MIN) {
printGeneratedMapcodes("Parent ", &mapcodesParent);
}
}
}
}
}
}
}
return nrTests;
}
// test strings that are expected to FAIL a decode
static int testFailingDecodes(void) {
int nrTests = 0;
static const char *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 nondigit
"NLD 1x3.1A0", // A0 with nondigit
"NLD 12x.1A0", // A0 with nondigit
"NLD 123.xA0", // A0 with nondigit
"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 SHH.HHH", // grid out of encompassing
"NLD L222.222", // grid out of range (restricted)
"W9.SX94", // reported as an error case
0
};
int i;
for (i = 0; badcodes[i] != 0; i++) {
double lat, lon;
const char *str = badcodes[i];
int err;
++nrTests;
err = decodeMapcodeToLatLonUtf8(&lat, &lon, str, TERRITORY_UNKNOWN, NULL);
if (err >= 0) {
foundError();
printf("*** ERROR *** invalid mapcode \"%s\" decodes without error\n", str);
}
}
return nrTests;
}
#include "test_territories.h"
static int testTerritory(const char *alphaCode, enum Territory territory,
int isAlias, int needsParent, enum Territory tcParent) {
int nrTests = 0;
char nam[MAX_ISOCODE_LEN + 1];
unsigned int i;
for (i = 0; i <= strlen(alphaCode); i++) {
char alphacode[MAX_ISOCODE_LEN + 1];
int tn;
strcpy(alphacode, alphaCode);
if (!needsParent && (i == 0)) {
tn = getTerritoryCode(alphacode, TERRITORY_NONE);
++nrTests;
if (tn != territory) {
foundError();
printf("*** ERROR *** getTerritoryCode('%s')=%d but expected %d (%s)\n",
alphacode, tn, territory, getTerritoryIsoName(nam, territory, 0));
}
}
alphacode[i] = (char) tolower(alphacode[i]);
tn = getTerritoryCode(alphacode, tcParent);
++nrTests;
if (tn != territory) {
foundError();
printf("*** ERROR *** getTerritoryCode('%s',%s)=%d but expected %d\n", alphacode,
tcParent ? getTerritoryIsoName(nam, tcParent, 0) : "", tn, territory);
}
}
if ((tcParent > _TERRITORY_MIN) && !isAlias) {
getTerritoryIsoName(nam, territory, 0);
++nrTests;
// every non-alias either equals nam, or is the state in nam
if ((strcmp(nam, alphaCode) != 0) && (strcmp(nam + 3, alphaCode) != 0)) {
foundError();
printf("*** ERROR *** getTerritoryIsoName(%d)=\"%s\" which does not equal or contain \"%s\"\n",
territory, nam, alphaCode);
}
}
return nrTests;
}
static int testTerritories() {
int nrTests = 0;
int nr = sizeof(TEST_TERRITORIES) / sizeof(TEST_TERRITORIES[0]);
int i;
for (i = 0; i < nr; ++i) {
nrTests += testTerritory(TEST_TERRITORIES[i].codeISO, TEST_TERRITORIES[i].territory,
TEST_TERRITORIES[i].isAlias,
TEST_TERRITORIES[i].needsParent, TEST_TERRITORIES[i].parent);
}
return nrTests;
}
// test closely around a particular coordinate
static int testAround(double y, double x) {
int nrTests = 0;
nrTests += testEncodeAndDecode("", y + 0.00001, x + 0.00001, 0, 0);
nrTests += testEncodeAndDecode("", y + 0.00001, x, 0, 0);
nrTests += testEncodeAndDecode("", y + 0.00001, x - 0.00001, 0, 0);
nrTests += testEncodeAndDecode("", y, x + 0.00001, 0, 0);
nrTests += testEncodeAndDecode("", y, x, 0, 0);
nrTests += testEncodeAndDecode("", y, x - 0.00001, 0, 0);
nrTests += testEncodeAndDecode("", y - 0.00001, x + 0.00001, 0, 0);
nrTests += testEncodeAndDecode("", y - 0.00001, x, 0, 0);
nrTests += testEncodeAndDecode("", y - 0.00001, x - 0.00001, 0, 0);
return nrTests;
}
// This context holds a record to process and a return value (nrTests) per thread.
struct ContextTestAround {
int nrTests;
const TerritoryBoundary *territoryBoundaries;
};
static int joinThreads(pthread_t *threads, struct ContextTestAround *contexts, int total) {
int i = 0;
int nrTests = 0;
for (i = 0; i < total; ++i) {
if (pthread_join(threads[i], 0)) {
foundError();
printf("*** ERROR *** Error joining thread %d of %d\n", i, total);
return 0;
}
nrTests += contexts[i].nrTests;
}
return nrTests;
}
// perform testEncodeAndDecode for all elements of encode_test[] (from decode_test.h)
static int testEncodeDecode(void) {
int nrTests = 0;
int i = 0;
int nr = sizeof(ENCODE_TEST) / sizeof(ENCODE_TEST[0]) - 1;
printf("%d encodes\n", nr);
for (i = 0; i < nr; i++) {
const EncodeTestRecord *t = &ENCODE_TEST[i];
showTestProgress(i, nr, nrTests);
nrTests += testEncodeAndDecode(t->mapcode, t->latitude, t->longitude,
t->nrLocalMapcodes, t->nrGlobalMapcodes);
}
return nrTests;
}
static void *executeTestAround(void *context) {
int nrTests = 0;
double y, x, midx, midy, thirdx;
struct ContextTestAround *c = (struct ContextTestAround *) context;
const TerritoryBoundary *b = c->territoryBoundaries;
midy = (b->miny + b->maxy) / 2000000.0;
midx = (b->minx + b->maxx) / 2000000.0;
thirdx = (2 * b->minx + b->maxx) / 3000000.0;
nrTests += testAround(midy, midx);
y = (b->miny) / 1000000.0;
x = (b->minx) / 1000000.0;
nrTests += testAround(y, x);
nrTests += testAround(midy, x);
nrTests += testAround(y, midx);
nrTests += testAround(y, thirdx);
x = (b->maxx) / 1000000.0;
nrTests += testAround(y, x);
nrTests += testAround(midy, x);
y = (b->maxy) / 1000000.0;
x = (b->minx) / 1000000.0;
nrTests += testAround(y, x);
nrTests += testAround(y, midx);
x = (b->maxx) / 1000000.0;
nrTests += testAround(y, x);
nrTests += testAround(midy, x);
c->nrTests = nrTests;
return 0;
}
// test around all centers and corners of all territory rectangles
static int testReEncode(void) {
int nrTests = 0;
enum Territory ccode;
int m = 0;
int nrRecords = MAPCODE_BOUNDARY_MAX;
int nrThread = 0;
// Declare threads and contexts.
pthread_t threads[MAX_THREADS];
struct ContextTestAround contexts[MAX_THREADS];
printf("%d records\n", nrRecords);
for (ccode = _TERRITORY_MIN + 1; ccode < _TERRITORY_MAX; ccode++) {
int min_boundary = DATA_START[INDEX_OF_TERRITORY(ccode)];
int max_boundary = DATA_START[INDEX_OF_TERRITORY(ccode) + 1];
showTestProgress(max_boundary, nrRecords, nrTests);
// use internal knowledge of mapcoder to test all the territory boundaries
for (m = min_boundary; m < max_boundary; m++) {
const TerritoryBoundary *b = TERRITORY_BOUNDARY(m);
// Create context for thread.
contexts[nrThread].nrTests = 0;
contexts[nrThread].territoryBoundaries = b;
// Execute task on new thread.
if (pthread_create(&threads[nrThread], 0, executeTestAround, (void *) &contexts[nrThread])) {