Skip to content

Commit 32395ab

Browse files
committed
Added self check option in executable name
1 parent 52a09e6 commit 32395ab

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

utility/mapcode.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@
2727
* and their Mapcodes, wrapped as a grid around the Earth;
2828
*
2929
* - a number of "random uniformly distributed" coordinates, which forms a set of
30-
* random coordiantes on the surface of Earth; or
30+
* random coordinates on the surface of Earth; or
3131
*
3232
* - a set which consists of typical Mapcode "boundaries" and "edge cases", based
3333
* on the internal implementation of the boundaries database of the Mapcode
3434
* implementation.
35+
*
36+
* If the executable is named mapcode_debug, the self-checking mechanism is
37+
* activated. Note, however, that the self checks may fail for certain decodes
38+
* even though the decodes are OK.
3539
*/
3640

3741
#include <stdio.h>
@@ -42,11 +46,10 @@
4246
#define my_isnan(x) (false)
4347
#define my_round(x) ((long) (floor((x) + 0.5)))
4448

45-
static const int SELF_CHECK = 0;
46-
static const int SELF_CHECK_EXIT = 0;
49+
static int selfCheckEnabled = 0;
4750

48-
static const int NORMAL_ERROR = 1;
49-
static const int INTERNAL_ERROR = 2;
51+
static const int NORMAL_ERROR = 1;
52+
static const int INTERNAL_ERROR = 2;
5053

5154
/**
5255
* Some global constants to be used.
@@ -74,7 +77,7 @@ static double lonLargestNrOfResults = 0.0;
7477
* whenever a incorrect amount or combination of parameters is entered.
7578
*/
7679
static void usage(const char* appName) {
77-
printf("MAPCODE (version %s%s)\n", mapcode_cversion, SELF_CHECK ? ", self-checking" : "");
80+
printf("MAPCODE (version %s)\n", mapcode_cversion);
7881
printf("Copyright (C) 2014-2015 Stichting Mapcode Foundation\n");
7982
printf("\n");
8083
printf("Usage:\n");
@@ -201,7 +204,7 @@ static void selfCheckLatLonToMapcode(const double lat, double lon, const char* t
201204
fprintf(stderr, "error: encoding lat/lon to mapcode failure; "
202205
"cannot encode lat=%f, lon=%f (default territory=%s)\n",
203206
lat, lon, territory);
204-
if (SELF_CHECK_EXIT) {
207+
if (selfCheckEnabled) {
205208
exit(INTERNAL_ERROR);
206209
}
207210
return;
@@ -228,7 +231,7 @@ static void selfCheckLatLonToMapcode(const double lat, double lon, const char* t
228231
"mapcode '%s %s' decodes to lat=%f(%f), lon=%f(%f), "
229232
"which does not encode back to '%s %s'\n",
230233
territory, mapcode, lat, limitLat, lon, limitLon, territory, mapcode);
231-
if (SELF_CHECK_EXIT) {
234+
if (selfCheckEnabled) {
232235
exit(INTERNAL_ERROR);
233236
}
234237
return;
@@ -249,7 +252,7 @@ static void selfCheckMapcodeToLatLon(const char* territory, const char* mapcode,
249252
if (err != 0) {
250253
fprintf(stderr, "error: decoding mapcode to lat/lon failure; "
251254
"cannot decode '%s %s')\n", territory, mapcode);
252-
if (SELF_CHECK_EXIT) {
255+
if (selfCheckEnabled) {
253256
exit(INTERNAL_ERROR);
254257
}
255258
return;
@@ -264,7 +267,7 @@ static void selfCheckMapcodeToLatLon(const char* territory, const char* mapcode,
264267
"lat=%f, lon=%f produces mapcode %s %s, "
265268
"which decodes to lat=%f (delta=%f), lon=%f (delta=%f)\n",
266269
lat, lon, territory, mapcode, foundLat, deltaLat, foundLon, deltaLon);
267-
if (SELF_CHECK_EXIT) {
270+
if (selfCheckEnabled) {
268271
exit(INTERNAL_ERROR);
269272
}
270273
return;
@@ -348,7 +351,7 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError, in
348351
printf("%s %s\n", foundTerritory, foundMapcode);
349352

350353
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
351-
if (SELF_CHECK) {
354+
if (selfCheckEnabled) {
352355
selfCheckLatLonToMapcode(lat, lon, foundTerritory, foundMapcode, extraDigits);
353356
selfCheckMapcodeToLatLon(foundTerritory, foundMapcode, lat, lon);
354357
}
@@ -419,6 +422,10 @@ int main(const int argc, const char** argv)
419422

420423
// Provide usage message if no arguments specified.
421424
const char* appName = argv[0];
425+
selfCheckEnabled = (strstr(appName, "debug") != 0);
426+
if (selfCheckEnabled) {
427+
fprintf(stderr, "(debug mode: self checking enabled)\n");
428+
}
422429
if (argc < 2) {
423430
usage(appName);
424431
return NORMAL_ERROR;
@@ -459,7 +466,7 @@ int main(const int argc, const char** argv)
459466
printf("%f %f\n", lat, lon);
460467

461468
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
462-
if (SELF_CHECK) {
469+
if (selfCheckEnabled) {
463470
const char* suffix = strstr(mapcode, "-");
464471
extraDigits = 0;
465472
if (suffix != 0) {
@@ -524,7 +531,7 @@ int main(const int argc, const char** argv)
524531
printf("%s %s\n", foundTerritory, foundMapcode);
525532

526533
// Self-checking code to see if decoder produces the lat/lon for all of these Mapcodes.
527-
if (SELF_CHECK) {
534+
if (selfCheckEnabled) {
528535
selfCheckMapcodeToLatLon(foundTerritory, foundMapcode, lat, lon);
529536
}
530537
}

0 commit comments

Comments
 (0)