+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
- * Copyright (c) 2013, International Business Machines Corporation
+ * Copyright (c) 2014-2016, International Business Machines Corporation
* and others. All Rights Reserved.
********************************************************************/
/* C API TEST FOR UREGION */
#include "unicode/uenum.h"
#include "cintltst.h"
#include "cmemory.h"
+#include "cstring.h"
static void TestKnownRegions(void);
static void TestGetContainedRegions(void);
static void TestGetContainedRegionsWithType(void);
static void TestGetContainingRegion(void);
static void TestGetContainingRegionWithType(void);
+static void TestGetPreferredValues(void);
+static void TestContains(void);
void addURegionTest(TestNode** root);
TESTCASE(TestGetContainedRegionsWithType);
TESTCASE(TestGetContainingRegion);
TESTCASE(TestGetContainingRegionWithType);
+ TESTCASE(TestGetPreferredValues);
+ TESTCASE(TestContains);
}
typedef struct KnownRegion {
const char *containingContinent;
} KnownRegion;
-#define LENGTHOF(array) (int32_t)(sizeof(array) / sizeof((array)[0]))
-
static KnownRegion knownRegions[] = {
// Code, Num, Parent, Type, Containing Continent
{ "TP" , 626, "035", URGN_TERRITORY, "142" },
{ "BS" , 44, "029", URGN_TERRITORY, "019" },
{ "BT" , 64, "034", URGN_TERRITORY, "142" },
{ "BU" , 104, "035", URGN_TERRITORY, "142" },
- { "BV" , 74, "QO" , URGN_TERRITORY, "009" },
+ { "BV" , 74, "005", URGN_TERRITORY, "019" },
{ "BW" , 72, "018", URGN_TERRITORY, "002" },
{ "BY" , 112, "151", URGN_TERRITORY, "150" },
{ "BZ" , 84, "013", URGN_TERRITORY, "019" },
{ "CA" , 124, "021", URGN_TERRITORY, "019" },
- { "CC" , 166, "QO" , URGN_TERRITORY, "009" },
+ { "CC" , 166, "053", URGN_TERRITORY, "009" },
{ "CD" , 180, "017", URGN_TERRITORY, "002" },
{ "CF" , 140, "017", URGN_TERRITORY, "002" },
{ "CG" , 178, "017", URGN_TERRITORY, "002" },
{ "CU" , 192, "029", URGN_TERRITORY, "019" },
{ "CV" , 132, "011", URGN_TERRITORY, "002" },
{ "CW" , 531, "029", URGN_TERRITORY, "019" },
- { "CX" , 162, "QO" , URGN_TERRITORY, "009" },
+ { "CX" , 162, "053", URGN_TERRITORY, "009" },
{ "CY" , 196, "145", URGN_TERRITORY, "142" },
{ "CZ" , 203, "151", URGN_TERRITORY, "150" },
{ "DD" , 276, "155", URGN_TERRITORY, "150" },
{ "GP" , 312, "029", URGN_TERRITORY, "019" },
{ "GQ" , 226, "017", URGN_TERRITORY, "002" },
{ "GR" , 300, "039", URGN_TERRITORY, "150" },
- { "GS" , 239, "QO" , URGN_TERRITORY, "009" },
+ { "GS" , 239, "005", URGN_TERRITORY, "019" },
{ "GT" , 320, "013", URGN_TERRITORY, "019" },
{ "GU" , 316, "057", URGN_TERRITORY, "009" },
{ "GW" , 624, "011", URGN_TERRITORY, "002" },
{ "GY" , 328, "005", URGN_TERRITORY, "019" },
{ "HK" , 344, "030", URGN_TERRITORY, "142" },
- { "HM" , 334, "QO" , URGN_TERRITORY, "009" },
+ { "HM" , 334, "053", URGN_TERRITORY, "009" },
{ "HN" , 340, "013", URGN_TERRITORY, "019" },
{ "HR" , 191, "039", URGN_TERRITORY, "150" },
{ "HT" , 332, "029", URGN_TERRITORY, "019" },
{ "IL" , 376, "145", URGN_TERRITORY, "142" },
{ "IM" , 833, "154", URGN_TERRITORY, "150" },
{ "IN" , 356, "034", URGN_TERRITORY, "142" },
- { "IO" , 86, "QO" , URGN_TERRITORY, "009" },
+ { "IO" , 86, "014", URGN_TERRITORY, "002" },
{ "IQ" , 368, "145", URGN_TERRITORY, "142" },
{ "IR" , 364, "034", URGN_TERRITORY, "142" },
{ "IS" , 352, "154", URGN_TERRITORY, "150" },
{ "SN" , 686, "011", URGN_TERRITORY, "002" },
{ "SO" , 706, "014", URGN_TERRITORY, "002" },
{ "SR" , 740, "005", URGN_TERRITORY, "019" },
- { "SS" , 728, "015", URGN_TERRITORY, "002" },
+ { "SS" , 728, "014", URGN_TERRITORY, "002" },
{ "ST" , 678, "017", URGN_TERRITORY, "002" },
{ "SU" , 810, NULL , URGN_DEPRECATED , NULL},
{ "SV" , 222, "013", URGN_TERRITORY, "019" },
{ "TA" , -1, "QO", URGN_TERRITORY, "009" },
{ "TC" , 796, "029", URGN_TERRITORY, "019" },
{ "TD" , 148, "017", URGN_TERRITORY, "002" },
- { "TF" , 260, "QO" , URGN_TERRITORY, "009" },
+ { "TF" , 260, "145", URGN_TERRITORY, "142" },
{ "TG" , 768, "011", URGN_TERRITORY, "002" },
{ "TH" , 764, "035", URGN_TERRITORY, "142" },
{ "TJ" , 762, "143", URGN_TERRITORY, "142" },
{ "TZ" , 834, "014", URGN_TERRITORY, "002" },
{ "UA" , 804, "151", URGN_TERRITORY, "150" },
{ "UG" , 800, "014", URGN_TERRITORY, "002" },
- { "UM" , 581, "QO" , URGN_TERRITORY, "009" },
+ { "UM" , 581, "057", URGN_TERRITORY, "009" },
{ "US" , 840, "021", URGN_TERRITORY, "019" },
{ "UY" , 858, "005", URGN_TERRITORY, "019" },
{ "UZ" , 860, "143", URGN_TERRITORY, "142" },
continue;
}
containedRegions = uregion_getContainedRegions(r, &status);
- if (containedRegions) {
+ if (containedRegions != NULL) {
while ((crID = uenum_next(containedRegions, NULL, &status)) != NULL && U_SUCCESS(status) ) {
const URegion *cr = uregion_getRegionFromCode(crID, &status);
const URegion *containingRegion = (cr)? uregion_getContainingRegion(cr) : NULL;
continue;
}
containedRegions = uregion_getContainedRegionsOfType(r, URGN_TERRITORY, &status);
- if (containedRegions) {
+ if (containedRegions != NULL) {
while ((crID = uenum_next(containedRegions, NULL, &status)) != NULL && U_SUCCESS(status) ) {
const URegion *cr = uregion_getRegionFromCode(crID, &status);
const URegion *containingRegion = (cr)? uregion_getContainingRegionOfType(cr, URGN_CONTINENT) : NULL;
}
}
+static const char * expectPrefRegions0[] = { "AN","CW","SX","BQ",NULL }; /* Netherlands Antilles */
+static const char * expectPrefRegions1[] = { "CS","RS","ME",NULL }; /* Serbia & Montenegro */
+static const char * expectPrefRegions2[] = { "FQ","AQ","TF",NULL }; /* French Southern and Antarctic Territories */
+static const char * expectPrefRegions3[] = { "NT","IQ","SA",NULL }; /* Neutral Zone */
+static const char * expectPrefRegions4[] = { "PC","FM","MH","MP","PW",NULL }; /* Pacific Islands Trust Territory */
+static const char * expectPrefRegions5[] = { "SU","RU","AM","AZ","BY","EE","GE","KZ","KG","LV","LT","MD","TJ","TM","UA","UZ",NULL }; /* Soviet Union */
+static const char ** expectPrefRegionsTestData[] = {
+ expectPrefRegions0,
+ expectPrefRegions1,
+ expectPrefRegions2,
+ expectPrefRegions3,
+ expectPrefRegions4,
+ expectPrefRegions5,
+ NULL
+};
+
+static void TestGetPreferredValues() {
+ const char *** testDataPtr = expectPrefRegionsTestData;
+ const char ** regionListPtr;
+ while ( (regionListPtr = *testDataPtr++) != NULL ) {
+ UErrorCode status = U_ZERO_ERROR;
+ const char * deprecatedCode = *regionListPtr++;
+ const URegion *r = uregion_getRegionFromCode(deprecatedCode, &status);
+ if ( U_SUCCESS(status) ) {
+ UEnumeration *preferredRegions = uregion_getPreferredValues(r, &status);
+ if ( U_SUCCESS(status) ) {
+ if (preferredRegions != NULL) {
+ const char * preferredCode;
+ while ( (preferredCode = *regionListPtr++) != NULL ) {
+ const char *check;
+ UBool found = FALSE;
+ uenum_reset(preferredRegions, &status);
+ while ((check = uenum_next(preferredRegions, NULL, &status)) != NULL && U_SUCCESS(status) ) {
+ if ( !uprv_strcmp(check,preferredCode) ) {
+ found = TRUE;
+ break;
+ }
+ }
+ if ( !found ) {
+ log_err("ERROR: uregion_getPreferredValues for region \"%s\" should have contained \"%s\" but it didn't.\n", uregion_getRegionCode(r), preferredCode);
+ }
+ }
+ uenum_close(preferredRegions);
+ }
+ } else {
+ log_err("ERROR: uregion_getPreferredValues failed for region %s.\n", uregion_getRegionCode(r));
+ }
+ } else {
+ log_data_err("ERROR: Known region %s was not recognized.\n", deprecatedCode);
+ }
+ }
+}
+
+static void TestContains() {
+ const KnownRegion * rd;
+ for (rd = knownRegions; rd->code != NULL ; rd++ ) {
+ UErrorCode status = U_ZERO_ERROR;
+ const URegion *r = uregion_getRegionFromCode(rd->code, &status);
+ if ( U_SUCCESS(status) ) {
+ const URegion *c = uregion_getContainingRegion(r);
+ while (c != NULL) {
+ if ( !uregion_contains(c, r) ) {
+ log_err("ERROR: Region \"%s\" should have contained \"%s\" but it didn't.\n", uregion_getRegionCode(c), uregion_getRegionCode(r) );
+ }
+ c = uregion_getContainingRegion(c);
+ }
+ } else {
+ log_data_err("ERROR: Known region %s was not recognized.\n", rd->code);
+ }
+ }
+}
#endif /* #if !UCONFIG_NO_FORMATTING */