Skip to content

Commit 7b58601

Browse files
committed
Fixed bug in parseMapcodeString, rename compareWithMapcodeFormat to hasMapcodeFormat
1 parent 9ca3b3d commit 7b58601

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

mapcodelib/mapcoder.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ UWORD *convertToAlphabet(UWORD *utf16String, int maxLength, const char *asciiStr
19401940

19411941
///////////////////////////////////////////////////////////////////////////////////////////////
19421942
//
1943-
// compareWithMapcodeFormat & parseMapcode
1943+
// hasMapcodeFormat & parseMapcode
19441944
//
19451945
///////////////////////////////////////////////////////////////////////////////////////////////
19461946

@@ -2083,7 +2083,7 @@ int parseMapcodeString(MapcodeElements *mapcodeFormat, const char *asciiString,
20832083
return -(1000 + 10 * state + token);
20842084
} else if (newstate == STATE_GO) {
20852085
int ret = (nondigits ? (vowels > 2 ? -6 : 0) : (vowels > 0 && vowels <= 3 ? 0 : -5));
2086-
if (ret == 0) {
2086+
if (ret == 0 && mapcodeFormat) {
20872087
if (*mapcodeFormat->territoryISO) {
20882088
mapcodeFormat->territoryCode = getTerritoryCode(mapcodeFormat->territoryISO, territoryCode - 1);
20892089
} else {
@@ -2101,9 +2101,8 @@ int parseMapcodeString(MapcodeElements *mapcodeFormat, const char *asciiString,
21012101
}
21022102
}
21032103

2104-
int compareWithMapcodeFormat(const char *asciiString, int fullcode) {
2105-
parseMapcodeString(NULL, asciiString, fullcode, 0);
2106-
return -1;
2104+
int hasMapcodeFormat(const char *asciiString, int includesTerritory) {
2105+
return parseMapcodeString(NULL, asciiString, includesTerritory, 0);
21072106
}
21082107

21092108

mapcodelib/mapcoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ int decodeMapcodeToLatLon(
174174
* Special value COMPARE_MAPCODE_MISSING_CHARACTERS (-999) indicates the string could be a Mapcode, but it seems
175175
* to lack some characters.
176176
*/
177-
int compareWithMapcodeFormat(
177+
int hasMapcodeFormat(
178178
const char *asciiString,
179179
int includesTerritory);
180180

unittest/unittest.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,31 +387,48 @@ static int test_mapcode_formats(void) {
387387

388388
NULL, NULL
389389
};
390+
int shouldSucceed = 29; // Number of calls to parse() that should be successful.
391+
int total = 0;
392+
int succeeded = 0;
390393
for (i = 0; testpairs[i] != NULL; i += 2) {
391394
char str[MAX_MAPCODE_RESULT_LEN + 16];
392-
MapcodeElements mapcodeFormat;
393-
int err = parseMapcodeString(&mapcodeFormat, testpairs[i], 1, 0);
395+
MapcodeElements mapcodeElements;
396+
int result = parseMapcodeString(&mapcodeElements, testpairs[i], 1, 0);
397+
int format = hasMapcodeFormat(testpairs[i], 1);
398+
394399
nrTests++;
395-
if (err == 0) {
400+
if ((!result && format) || (result && !format)) {
401+
found_error();
402+
printf("*** ERROR *** parseMapcodeString=%d, hasMapcodeFormat=%d\n", result, format);
403+
}
404+
405+
nrTests++;
406+
++total;
407+
if (result == 0) {
408+
++succeeded;
396409
sprintf(str, "%s%s%s%s%s|%d",
397-
mapcodeFormat.territoryISO,
398-
*mapcodeFormat.territoryISO ? " " : "",
399-
mapcodeFormat.properMapcode,
400-
*mapcodeFormat.precisionExtension ? "-" : "",
401-
mapcodeFormat.precisionExtension,
402-
(mapcodeFormat.indexOfDot * 9) + (int) strlen(mapcodeFormat.properMapcode) - 1);
410+
mapcodeElements.territoryISO,
411+
*mapcodeElements.territoryISO ? " " : "",
412+
mapcodeElements.properMapcode,
413+
*mapcodeElements.precisionExtension ? "-" : "",
414+
mapcodeElements.precisionExtension,
415+
(mapcodeElements.indexOfDot * 9) + (int) strlen(mapcodeElements.properMapcode) - 1);
403416
if (strcmp(str, testpairs[i + 1]) != 0) {
404417
found_error();
405-
printf("*** ERROR *** compareWithMapcodeFormat(\"%s\") succeeded with \"%s\"\n", testpairs[i], str);
418+
printf("*** ERROR *** parseMapcodeString(\"%s\") succeeded with \"%s\"\n", testpairs[i], str);
406419
}
407420
} else {
408-
sprintf(str, "%d", err);
421+
sprintf(str, "%d", result);
409422
if (testpairs[i + 1][0] != 0 && strcmp(str, testpairs[i + 1]) != 0) {
410423
found_error();
411-
printf("*** ERROR *** compareWithMapcodeFormat(\"%s\") failed unexpectedly %d\n", testpairs[i], err);
424+
printf("*** ERROR *** hasMapcodeFormat(\"%s\") failed unexpectedly %d\n", testpairs[i], result);
412425
}
413426
}
414427
}
428+
if (succeeded != shouldSucceed) {
429+
found_error();
430+
printf("*** ERROR *** Too few parseMapcodeString() calls succeeded (%d of %d, expected %d)\n", succeeded, total, shouldSucceed);
431+
}
415432
return nrTests;
416433
}
417434

0 commit comments

Comments
 (0)