Skip to content

Commit 1a3587f

Browse files
committed
Fixed unit test
1 parent 49f2b7c commit 1a3587f

File tree

2 files changed

+175
-133
lines changed

2 files changed

+175
-133
lines changed

mapcodelib/mapcoder.c

Lines changed: 104 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ static struct {
17981798
{0x03AD, 0x03c9, "EU??ABGDFZHQIKLMNCOJP?STYVXRW"}, // Greek lowercase
17991799
{0x10d0, 0x10ef, "AB?CE?D?UF?GHOJ?KLMINPQRSTVW?XYZ"}, // Georgisch lowercase
18001800
{0x0562, 0x0586, "BCDE??FGHI?J?KLM?N?U?PQ?R??STVWXYZ?OA"}, // Armenian lowercase
1801-
{0, 0, NULL}
1801+
{0, 0, NULL}
18021802
};
18031803

18041804
// Abjad forward declarations
@@ -2317,7 +2317,7 @@ static char *convertToAbjad(char *str, const char *source, int maxlen) {
23172317
int len = (int) strlen(source);
23182318
const char *rest = strchr(source, '-');
23192319
if (rest != NULL) {
2320-
len = ((int) (rest - source)) - 1;
2320+
len = ((int) (rest - source));
23212321
}
23222322
if (len >= maxlen) {
23232323
len = maxlen - 1;
@@ -2333,6 +2333,7 @@ static char *convertToAbjad(char *str, const char *source, int maxlen) {
23332333

23342334
len = (int) strlen(str);
23352335
dot = (int) (strchr(str, '.') - str);
2336+
23362337
form = dot * 10 + (len - dot - 1);
23372338

23382339
// see if >2 non-digits in a row
@@ -2348,9 +2349,17 @@ static char *convertToAbjad(char *str, const char *source, int maxlen) {
23482349
}
23492350
}
23502351
}
2351-
if (inarow < 3 &&
2352-
(form == 22 || form == 32 || form == 33 || form == 42 || form == 43 || form == 44 || form == 54)) {
2353-
// no need to do anything
2352+
if (dot < 2 || dot > 5 || (inarow < 3 &&
2353+
(form == 22 || form == 32 || form == 33 || form == 42 || form == 43 || form == 44 ||
2354+
form == 54))) {
2355+
// no need to do anything, return input unchanged
2356+
len = (int) strlen(source);
2357+
if (len >= maxlen) {
2358+
len = maxlen - 1;
2359+
}
2360+
memcpy(str, source, len);
2361+
str[len] = 0;
2362+
return str;
23542363
} else if (form >= 22 && form <= 54) {
23552364
char c1, c2, c3 = '?';
23562365
int c = decodeChar(str[2]);
@@ -2469,8 +2478,12 @@ static char *convertToAbjad(char *str, const char *source, int maxlen) {
24692478
}
24702479
repack_if_alldigits(str, 0);
24712480
if (rest) {
2472-
int len = (int) strlen(str);
2481+
len = (int) strlen(str);
2482+
int needed = (int) strlen(rest);
24732483
int tocopy = maxlen - len - 1;
2484+
if (tocopy > needed) {
2485+
tocopy = needed;
2486+
}
24742487
if (tocopy > 0) {
24752488
memcpy(str + len, rest, tocopy);
24762489
str[len + tocopy] = 0;
@@ -2485,102 +2498,115 @@ static void convertFromAbjad(char *s) {
24852498
char *postfix = strchr(s, '-');
24862499
if (postfix) {
24872500
*postfix = 0;
2488-
postfix++;
24892501
}
24902502

24912503
unpack_if_alldigits(s);
24922504

24932505
len = (int) strlen(s);
24942506
dot = (int) (strchr(s, '.') - s);
2495-
form = dot * 10 + (len - dot - 1);
2507+
form = (dot >= 2 && dot <= 5 ? dot * 10 + (len - dot - 1) : 0);
24962508

24972509
if (form == 23) {
24982510
c = decodeChar(s[3]) * 8 + (decodeChar(s[4]) - 18);
2499-
// s[0] = s[0];
2500-
// s[1] = s[1];
2501-
// s[2] = '.';
2502-
s[3] = encode_chars[c];
2503-
s[4] = s[5];
2504-
s[5] = 0;
2511+
if (c >= 0 && c < 31) {
2512+
// s[0] = s[0];
2513+
// s[1] = s[1];
2514+
// s[2] = '.';
2515+
s[3] = encode_chars[c];
2516+
s[4] = s[5];
2517+
s[5] = 0;
2518+
}
25052519
} else if (form == 24) {
25062520
c = decodeChar(s[3]) * 8 + (decodeChar(s[4]) - 18);
2507-
// s[0] = s[0];
2508-
// s[1] = s[1];
2509-
// s[2] = '.';
2510-
s[3] = '.';
2511-
s[4] = s[5];
2512-
s[5] = s[6];
2513-
s[6] = 0;
2514-
if (c >= 32) {
2515-
s[2] = encode_chars[c - 32];
2516-
} else {
2517-
s[3] = encode_chars[c];
2521+
if (c >= 0 && c < 63) {
2522+
// s[0] = s[0];
2523+
// s[1] = s[1];
2524+
// s[2] = '.';
2525+
s[3] = '.';
2526+
s[4] = s[5];
2527+
s[5] = s[6];
2528+
s[6] = 0;
2529+
if (c >= 32) {
2530+
s[2] = encode_chars[c - 32];
2531+
} else {
2532+
s[3] = encode_chars[c];
2533+
}
25182534
}
25192535
} else if (form == 34) {
25202536
c = (decodeChar(s[2]) * 10) + (decodeChar(s[5]) - 7);
2521-
// s[0] = s[0];
2522-
// s[1] = s[1];
2523-
s[2] = '.';
2524-
// s[3] = '.';
2525-
// s[4] = s[4];
2526-
s[5] = s[6];
2527-
s[6] = s[7];
2528-
s[7] = 0;
2529-
2530-
if (c < 31) {
2531-
s[3] = encode_chars[c];
2532-
} else if (c < 62) {
2533-
s[2] = encode_chars[c - 31];
2534-
} else {
2535-
s[2] = encode_chars[c - 62];
2536-
s[3] = s[4];
2537-
s[4] = '.';
2537+
if (c >= 0 && c < 93) {
2538+
// s[0] = s[0];
2539+
// s[1] = s[1];
2540+
s[2] = '.';
2541+
// s[3] = '.';
2542+
// s[4] = s[4];
2543+
s[5] = s[6];
2544+
s[6] = s[7];
2545+
s[7] = 0;
2546+
2547+
if (c < 31) {
2548+
s[3] = encode_chars[c];
2549+
} else if (c < 62) {
2550+
s[2] = encode_chars[c - 31];
2551+
} else {
2552+
s[2] = encode_chars[c - 62];
2553+
s[3] = s[4];
2554+
s[4] = '.';
2555+
}
25382556
}
25392557
} else if (form == 35) {
25402558
c = (decodeChar(s[2]) * 8) + (decodeChar(s[6]) - 18);
2541-
// s[0] = s[0];
2542-
// s[1] = s[1];
2543-
// s[3] = '.';
2544-
// s[4] = s[4];
2545-
// s[5] = s[5];
2546-
s[6] = s[7];
2547-
s[7] = s[8];
2548-
s[8] = 0;
2549-
if (c >= 32) {
2550-
s[2] = encode_chars[c - 32];
2551-
s[3] = s[4];
2552-
s[4] = '.';
2553-
} else {
2554-
s[2] = encode_chars[c];
2559+
if (c >= 0 && c < 63) {
2560+
// s[0] = s[0];
2561+
// s[1] = s[1];
2562+
// s[3] = '.';
2563+
// s[4] = s[4];
2564+
// s[5] = s[5];
2565+
s[6] = s[7];
2566+
s[7] = s[8];
2567+
s[8] = 0;
2568+
if (c >= 32) {
2569+
s[2] = encode_chars[c - 32];
2570+
s[3] = s[4];
2571+
s[4] = '.';
2572+
} else {
2573+
s[2] = encode_chars[c];
2574+
}
25552575
}
25562576
} else if (form == 45) {
25572577
c = (decodeChar(s[2]) * 100) + (decodeChar(s[5]) * 10) + (decodeChar(s[8]) - 39);
2558-
// s[0] = s[0];
2559-
// s[1] = s[1];
2560-
s[2] = encode_chars[c / 31];
2561-
// s[3] = s[3];
2562-
// s[4] = '.';
2563-
s[5] = s[6];
2564-
s[6] = s[7];
2565-
s[7] = s[9];
2566-
s[8] = encode_chars[c % 31];
2567-
s[9] = 0;
2578+
if (c >= 0 && c < 961) {
2579+
// s[0] = s[0];
2580+
// s[1] = s[1];
2581+
s[2] = encode_chars[c / 31];
2582+
// s[3] = s[3];
2583+
// s[4] = '.';
2584+
s[5] = s[6];
2585+
s[6] = s[7];
2586+
s[7] = s[9];
2587+
s[8] = encode_chars[c % 31];
2588+
s[9] = 0;
2589+
}
25682590
} else if (form == 55) {
25692591
c = (decodeChar(s[2]) * 100) + (decodeChar(s[6]) * 10) + (decodeChar(s[9]) - 39);
2570-
// s[0] = s[0];
2571-
// s[1] = s[1];
2572-
s[2] = encode_chars[c / 31];
2573-
// s[3] = s[3];
2574-
// s[4] = s[4];
2575-
// s[5] = '.';
2576-
s[6] = s[7];
2577-
s[7] = s[8];
2578-
s[8] = s[10];
2579-
s[9] = encode_chars[c % 31];
2580-
s[10] = 0;
2592+
if (c >= 0 && c < 961) {
2593+
// s[0] = s[0];
2594+
// s[1] = s[1];
2595+
s[2] = encode_chars[c / 31];
2596+
// s[3] = s[3];
2597+
// s[4] = s[4];
2598+
// s[5] = '.';
2599+
s[6] = s[7];
2600+
s[7] = s[8];
2601+
s[8] = s[10];
2602+
s[9] = encode_chars[c % 31];
2603+
s[10] = 0;
2604+
}
25812605
}
25822606
repack_if_alldigits(s, 0);
25832607
if (postfix) {
2584-
memmove(s + strlen(s), postfix, strlen(postfix) + 1);
2608+
int len = (int) strlen(s);
2609+
*postfix = '-';
2610+
memmove(s + len, postfix, strlen(postfix) + 1);
25852611
}
25862612
}

unittest/unittest.c

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,68 +34,84 @@ int nrTests = 0, nrErrors = 0, nrWarnings = 0;
3434

3535
// test the alphabet conversion routines
3636
static void alphabet_tests() {
37-
int i;
37+
int i,j;
3838
const char *str, *expect;
39+
static const char *testpairs[] = {
40+
".123",".123",
41+
"","",
42+
"-","-",
43+
".",".",
44+
"-123","-123",
45+
"-xyz","-XYZ",
46+
".xyz",".XYZ",
47+
"12.34","12.34",
48+
"OEUoi OIoi#%?-.abcdfghjklmnpqrstvwxyz0123456789euEUABCDFGHJKLMNPQRSTVWXYZ" , "OEUoi OIOI#%?-.ABCDFGHJKLMNPQRSTVWXYZ0123456789EUEUABCDFGHJ",
49+
"OEUoi OIoi#%?abcdfghjklmnpqrstvwxyz0123456789euEUABCDFGHJKLMNPQRSTVWXYZ" , "OEUoi OIOI#%?ABCDFGHJKLMNPQRSTVWXYZ0123456789EUEUABCDFGHJKL",
50+
" Oio 12.AU ","Oio 12.AU",
51+
"OMN 112.3EU","OMN 112.3EU",
52+
"49.4V","49.4V",
53+
"NLD 49.4V-xx123","NLD 49.4V-XX123",
54+
"xx.xx","XX.XX",
55+
"xx.xxx","XX.XXX",
56+
"xxx.xx","XXX.XX",
57+
"xx.xxxx","XX.XXXX",
58+
"xxx.xxx","XXX.XXX",
59+
"xxxx.xx","XXXX.XX",
60+
"xxx.xxxx","XXX.XXXX",
61+
"xxxx.xxx","XXXX.XXX",
62+
"xxxx.xxxx","XXXX.XXXX",
63+
"xxxxx.xxxx","XXXXX.XXXX",
64+
"pq.rs","PQ.RS",
65+
"bc.123","BC.123",
66+
"123.xy","123.XY",
67+
" nld 12.34E0","nld 12.34E0",
68+
"VVX.xxx","VVX.XXX",
69+
"x123.xx","X123.XX",
70+
"xxx.xxxx","XXX.XXXX",
71+
"12xx.xxx","12XX.XXX",
72+
"xxxx.xx12","XXXX.XX12",
73+
"99zxx.xxxx","99ZXX.XXXX",
74+
"xx.xx-P","XX.XX-P",
75+
"xx.xxx-pq","XX.XXX-PQ",
76+
"xxx.xx-123","XXX.XX-123",
77+
"xx.xxxx-pqRS","XX.XXXX-PQRS",
78+
"xxx.xxx-PQRSTUVW","XXX.XXX-PQRSTUVW",
79+
"xxxx.xx-pqrstuvw","XXXX.XX-PQRSTUVW",
80+
"xxx.xxxx-PQrsTU","XXX.XXXX-PQRSTU",
81+
"xxxx.xxx-09876543","XXXX.XXX-09876543",
82+
"xxxx.xxxx-PQRSTUVW","XXXX.XXXX-PQRSTUVW",
83+
"xxxxx.xxxx-PQRSTUVW","XXXXX.XXXX-PQRSTUVW",
84+
"pq.rs-PQRSTUVW","PQ.RS-PQRSTUVW",
85+
"bc.123-PQRSTUVW","BC.123-PQRSTUVW",
86+
"123.xy-PQRSTUVW","123.XY-PQRSTUVW",
87+
"12.34E0-PQRSTUVW","12.34E0-PQRSTUVW",
88+
"VVX.xxx-PQRSTUVW","VVX.XXX-PQRSTUVW",
89+
"x123.xx-PQRSTUVW","X123.XX-PQRSTUVW",
90+
"xxx.xxxx-PQRSTUVW","XXX.XXXX-PQRSTUVW",
91+
"12xx.xxx-PQRSTUVW","12XX.XXX-PQRSTUVW",
92+
"xxxx.xx12-PQRSTUVW","XXXX.XX12-PQRSTUVW",
93+
"99zxx.xxxx-PQRSTUVW","99ZXX.XXXX-PQRSTUVW",
94+
NULL
95+
};
3996

4097
printf("%d alphabets\n", MAPCODE_ALPHABETS_TOTAL);
4198

42-
for (i = 0; i < MAPCODE_ALPHABETS_TOTAL; i++) {
43-
UWORD enc[64];
44-
char dec[64];
45-
46-
// see if convertToAlphabet survives empty string
47-
nrTests++;
48-
str = "";
49-
convertToAlphabet(enc, 64, str, i);
50-
if (*enc) {
51-
nrErrors++;
52-
printf("*** ERROR *** convertToAlphabet(\"%s\",%d) = \"%s\"\n", str, i, dec);
53-
}
54-
else {
55-
// see if empty UTF16 converts to empty string
99+
for(j=0;testpairs[j]!=NULL;j+=2)
100+
{
101+
for (i = 0; i < MAPCODE_ALPHABETS_TOTAL; i++) {
102+
UWORD enc[64];
103+
char dec[64];
104+
// see if alphabets (re)convert as expected
105+
str = testpairs[j];
106+
expect = testpairs[j+1];
107+
convertToAlphabet(enc, 64, str, i);
108+
convertToRoman(dec, 60, enc);
56109
nrTests++;
57-
convertToRoman(dec, 64, enc);
58-
if (*dec) {
110+
if (strcmp(dec, expect)) {
59111
nrErrors++;
60-
printf("*** ERROR *** convertToRoman(\"\") = \"%s\"\n", dec);
61-
}
62-
}
63-
64-
// see if alphabets (re)convert as expected
65-
str = "OEUoi OIoi#%?-.abcdfghjklmnpqrstvwxyz0123456789ABCDFGHJKLMNPQRSTVWXYZ";
66-
expect = "OEUoi OIOI#%?-.ABCDFGHJKLMNPQRSTVWXYZ0123456789ABCDFGHJKLMN";
67-
convertToAlphabet(enc, 64, str, i);
68-
convertToRoman(dec, 60, enc);
69-
nrTests++;
70-
if (strlen(dec) != 59 || strcmp(dec, expect)) {
71-
nrErrors++;
72-
printf("*** ERROR *** convertToRoman(convertToAlphabet(\"%s\",%d))=\"%s\", expect=\"%s\"\n", str, i, dec, expect);
73-
}
74-
75-
// see if E/U voweled mapcodes (re)convert as expected
76-
str = "OMN 112.3EU";
77-
convertToAlphabet(enc, 64, str, i);
78-
convertToRoman(dec, 64, enc);
79-
nrTests++;
80-
if (strcmp(dec, str) != 0) {
81-
nrErrors++;
82-
printf("*** ERROR *** convertToRoman(convertToAlphabet(\"%s\",%d))=\"%s\"\n", str, i, dec);
83-
}
84-
else {
85-
nrTests++;
86-
{
87-
str = " Oio 112.3AU ";
88-
convertToAlphabet(enc, 64, str, i);
89-
convertToRoman(dec, 64, enc);
90-
nrTests++;
91-
if (strcmp(dec, "Oio 112.3AU") != 0) {
92-
nrErrors++;
93-
printf("*** ERROR *** convertToRoman(convertToAlphabet(\"%s\",%d))=\"%s\"\n", str, i, dec);
94-
}
112+
printf("convertToRoman(convertToAlphabet(\"%s\",%d))=\"%s\"\n", str, i, dec);
95113
}
96114
}
97-
98-
99115
}
100116
}
101117

@@ -251,8 +267,8 @@ static void testEncodeAndDecode(const char *str, double y, double x, int localso
251267
int tc2 = -1;
252268
int tcParent = -1;
253269
int j;
254-
found = 0;
255270
char *e = strchr(strResult, ' ');
271+
found = 0;
256272
if (e) {
257273
*e = 0;
258274
tc2 = convertTerritoryIsoNameToCode(strResult, 0);

0 commit comments

Comments
 (0)