Skip to content

Commit 732fcd8

Browse files
committed
Updated tests
1 parent 6ce04c9 commit 732fcd8

File tree

7 files changed

+62
-37
lines changed

7 files changed

+62
-37
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ decode Mapcodes.
6262

6363
This produces the following help text:
6464

65-
MAPCODE (version 2.5.1)
65+
MAPCODE (version 2.5.2)
6666
Copyright (C) 2014-2016 Stichting Mapcode Foundation
6767

6868
Usage:
@@ -138,6 +138,10 @@ footprint, for example for embedded applications.
138138

139139
## Release Notes
140140

141+
### 2.5.2
142+
143+
* Added locale support.
144+
141145
### 2.5.1
142146

143147
* Updated unit test to compile with plain C and added some test cases.

mapcodelib/mapcode_alphabets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum Alphabet {
5757
ALPHABET_ODIA,
5858
ALPHABET_KANNADA,
5959
ALPHABET_GUJARATI,
60-
_ALPHABET_MAX,
60+
_ALPHABET_MAX
6161
};
6262

6363
#ifdef __cplusplus

mapcodelib/mapcoder.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include "internal_territory_names_local.h"
2727
#include "internal_alphabet_recognizer.h"
2828

29+
// We must have a default language
30+
#define MAPCODE_SUPPORT_LANGUAGE_EN
31+
#define DEFAULT_TERRITORY_FULL_NAME TERRITORY_FULL_NAME_EN
32+
2933
#include "internal_territory_names_da.h"
3034
#include "internal_territory_names_de.h"
3135
#include "internal_territory_names_en.h"
@@ -106,6 +110,19 @@ static const double METERS_PER_DEGREE_LON = EARTH_CIRCUMFERENCE_X / 360.0;
106110

107111
static const int DEBUG_STOP_AT = -1; // to externally test-restrict internal encoding, do not use!
108112

113+
typedef struct {
114+
const char *locale;
115+
const char **territoryFullNames;
116+
} LocaleRegistryItem;
117+
118+
static const LocaleRegistryItem LOCALE_REGISTRY[] = {
119+
{"DA", TERRITORY_FULL_NAME_DA},
120+
{"DE", TERRITORY_FULL_NAME_DE},
121+
{"EN", TERRITORY_FULL_NAME_EN},
122+
{"FR", TERRITORY_FULL_NAME_FR},
123+
{"NL", TERRITORY_FULL_NAME_NL}
124+
};
125+
109126
// important information about the 8 parents
110127
static const char *PARENTS_3 = "USA,IND,CAN,AUS,MEX,BRA,RUS,CHN,";
111128
static const char *PARENTS_2 = "US,IN,CA,AU,MX,BR,RU,CN,";
@@ -3023,19 +3040,6 @@ const TerritoryAlphabets *getAlphabetsForTerritory(enum Territory territory) {
30233040
//
30243041
///////////////////////////////////////////////////////////////////////////////////////////////
30253042

3026-
typedef struct {
3027-
const char* locale;
3028-
const char** territoryFullnames;
3029-
} LocaleRegistryItem;
3030-
3031-
static const LocaleRegistryItem LOCALE_REGISTRY[] = {
3032-
{ "DA", TERRITORY_FULL_NAME_DA },
3033-
{ "DE", TERRITORY_FULL_NAME_DE },
3034-
{ "EN", TERRITORY_FULL_NAME_EN },
3035-
{ "FR", TERRITORY_FULL_NAME_FR },
3036-
{ "NL", TERRITORY_FULL_NAME_NL }
3037-
};
3038-
30393043
static int getFullTerritoryName_internal(
30403044
char *territoryName,
30413045
enum Territory territory,
@@ -3046,34 +3050,42 @@ static int getFullTerritoryName_internal(
30463050
const char *s;
30473051
const char *pipePtr;
30483052
const char **namelist = NULL;
3049-
char localeUpper[3] = "";
30503053

30513054
ASSERT(territoryName);
30523055

3053-
if ((!locale && (strlen(locale) < 2)) || alternative < 0 || territory <= _TERRITORY_MIN || territory >= _TERRITORY_MAX) {
3056+
if (alternative < 0 || territory <= _TERRITORY_MIN || territory >= _TERRITORY_MAX) {
30543057
*territoryName = 0;
30553058
return 0;
30563059
}
3057-
localeUpper[0] = locale[0];
3058-
localeUpper[1] = locale[1];
3059-
localeUpper[2] = 0;
30603060

30613061
if (locale == NULL) {
3062+
3063+
// Use local names if locale is null.
30623064
namelist = TERRITORY_FULL_NAME_LOCAL;
3065+
} else if (strlen(locale) < 2) {
3066+
3067+
// Locale is invalid.
3068+
namelist = NULL;
30633069
} else {
3070+
3071+
// Try and get correct list.
3072+
char localeUpper[3] = "";
30643073
int i;
3074+
localeUpper[0] = locale[0];
3075+
localeUpper[1] = locale[1];
3076+
localeUpper[2] = 0;
30653077
namelist = NULL;
30663078
for (i = 0; i < (int) (sizeof(LOCALE_REGISTRY) / sizeof(LOCALE_REGISTRY[0])); ++i) {
3067-
if (strcmp(LOCALE_REGISTRY[i].locale, localeUpper)) {
3068-
namelist = LOCALE_REGISTRY[i].territoryFullnames;
3079+
if (!strcmp(LOCALE_REGISTRY[i].locale, localeUpper)) {
3080+
namelist = LOCALE_REGISTRY[i].territoryFullNames;
30693081
break;
30703082
}
30713083
}
30723084
}
30733085

3086+
// Use English if locale is invalid.
30743087
if (namelist == NULL || namelist[0] == NULL) {
3075-
*territoryName = 0;
3076-
return 0;
3088+
namelist = DEFAULT_TERRITORY_FULL_NAME;
30773089
}
30783090

30793091
s = namelist[INDEX_OF_TERRITORY(territory)];

mapcodelib/mapcoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern "C" {
2525
#include "mapcode_alphabets.h"
2626

2727

28-
#define MAPCODE_C_VERSION "2.5.1"
28+
#define MAPCODE_C_VERSION "2.5.2"
2929
#define UWORD unsigned short int // 2-byte unsigned integer.
3030
#define MAX_NR_OF_MAPCODE_RESULTS 22 // Max. number of results ever returned by encoder (e.g. for 26.904899, 95.138515).
3131
#define MAX_PROPER_MAPCODE_LEN 11 // Max. number of characters in a proper mapcode (including the dot, excl. precision extension).
@@ -104,7 +104,7 @@ enum MapcodeError {
104104

105105
// all OK.
106106

107-
ERR_OK = 0,
107+
ERR_OK = 0
108108
};
109109

110110

test/run_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ date | tee -a $OUT
55
echo "" | tee -a $OUT
66
./run_normal.sh | tee -a $OUT
77
./run_sanitizer.sh | tee -a $OUT
8-
./run_gcov.h | tee -a $OUT
8+
./run_gcov.sh | tee -a $OUT
99
./run_gprof.sh | tee -a $OUT
1010
./run_valgrind.sh | tee -a $OUT
1111
./run_compare.sh | tee -a $OUT

test/unittest.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ static int testGetFullTerritoryNameInLocale(const char *locale, const char *expe
16331633
if (strcmp(expectedName, gotName)) {
16341634
char s[MAX_ISOCODE_LEN + 1];
16351635
foundError();
1636-
printf("*** ERROR *** getFullTerritoryNameInLocaleEnglish error, expected name '%s', but got '%s' for territory %s, alternative %d\n",
1636+
printf("*** ERROR *** getFullTerritoryNameInLocale error, expected name '%s', but got '%s' for territory %s, alternative %d\n",
16371637
expectedName, gotName, getTerritoryIsoName(s, territory, 0), alternative);
16381638
}
16391639
++nrTests;
@@ -1732,18 +1732,25 @@ int testGetFullTerritoryName(void) {
17321732
int i;
17331733
char territoryName[2048]; // large so we can test overflow
17341734
static const char *locales_to_test[] = {
1735-
"DA", "DE", "EN", "FR", "NL",
1736-
"LOCAL"
1737-
};
1735+
"DA", "DE", "EN", "FR", "NL"};
17381736

1739-
nrTests += testGetFullTerritoryNameInLocale("??", "", TERRITORY_VAT, 0);
1737+
nrTests += testGetFullTerritoryNameInLocale(NULL, "Vatican", TERRITORY_VAT, 0);
1738+
nrTests += testGetFullTerritoryNameInLocale("", "Vatican", TERRITORY_VAT, 0);
1739+
nrTests += testGetFullTerritoryNameInLocale("E", "Vatican", TERRITORY_VAT, 0);
1740+
nrTests += testGetFullTerritoryNameInLocale("EN", "Vatican", TERRITORY_VAT, 0);
1741+
nrTests += testGetFullTerritoryNameInLocale("??", "Vatican", TERRITORY_VAT, 0);
17401742

1741-
for (i = 0; i < (int) (sizeof(locales_to_test) / sizeof(const char *)); i++) {
1743+
for (i = 0; i < (int) (sizeof(locales_to_test) / sizeof(locales_to_test[0])); i++) {
17421744
const char *locale = locales_to_test[i];
17431745
int nrInLocale = 0;
17441746
nrTests += testGetFullTerritoryNameInLocale(locale, "", _TERRITORY_MIN, 0);
17451747
nrTests += testGetFullTerritoryNameInLocale(locale, "", _TERRITORY_MAX, 0);
1746-
for (territory = _TERRITORY_MIN + 1; territory < _TERRITORY_MAX; ++territory) {
1748+
// for (territory = _TERRITORY_MIN + 1; territory < _TERRITORY_MAX; ++territory) {
1749+
for (territory = TERRITORY_NLD; territory < TERRITORY_EST; ++territory) {
1750+
char expectedName[MAX_TERRITORY_FULLNAME_LEN + 1];
1751+
getFullTerritoryNameInLocale(expectedName, territory, 0, 0);
1752+
nrTests += testGetFullTerritoryNameInLocale(locale, expectedName, territory, 0);
1753+
nrTests += testGetFullTerritoryNameInLocale("", expectedName, territory, 0);
17471754
nrTests += testGetFullTerritoryNameInLocale(locale, "", territory, -1);
17481755
nrTests += testGetFullTerritoryNameInLocale(locale, "", territory, 999);
17491756
for (alternative = 0;; alternative++) {
@@ -1765,7 +1772,6 @@ int testGetFullTerritoryName(void) {
17651772
}
17661773
}
17671774
}
1768-
fprintf(stderr," %d names in locale %s\n", nrInLocale, locale); //TODO @@@
17691775
}
17701776

17711777
#ifdef MAPCODE_SUPPORT_LANGUAGE_LOCAL

utility/mapcode.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
#include <ctype.h>
4646
#include <time.h>
4747
#include "../mapcodelib/mapcoder.h"
48-
#include "../mapcodelib/internal_territory_names_english.h"
48+
49+
#define MAPCODE_SUPPORT_LANGUAGE_EN
50+
51+
#include "../mapcodelib/internal_territory_names_en.h"
4952
#include "../mapcodelib/internal_data.h"
5053
#include "../mapcodelib/internal_iso3166_data.h"
5154

@@ -558,7 +561,7 @@ int main(const int argc, const char **argv) {
558561
printf(",");
559562

560563
// Use internal knowledge of TERRITORY_FULL_NAME to show aliases of full territory name.
561-
char *names = strdup(TERRITORY_FULL_NAME[INDEX_OF_TERRITORY(ccode)]);
564+
char *names = strdup(TERRITORY_FULL_NAME_EN[INDEX_OF_TERRITORY(ccode)]);
562565
char *s = names;
563566
while (s) {
564567
if (s != names) {

0 commit comments

Comments
 (0)