]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/cintltst/udatpg_test.c
ICU-551.30.tar.gz
[apple/icu.git] / icuSources / test / cintltst / udatpg_test.c
index 0e53345c33058442fee4bb3f76d4e94d650c3335..930455c41dc244abeac371961659295b1cc90d0b 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2007, International Business Machines
+*   Copyright (C) 2007-2014, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -38,11 +38,13 @@ void addDateTimePatternGeneratorTest(TestNode** root);
 static void TestOpenClose(void);
 static void TestUsage(void);
 static void TestBuilder(void);
 static void TestOpenClose(void);
 static void TestUsage(void);
 static void TestBuilder(void);
+static void TestOptions(void);
 
 void addDateTimePatternGeneratorTest(TestNode** root) {
     TESTCASE(TestOpenClose);
     TESTCASE(TestUsage);
     TESTCASE(TestBuilder);
 
 void addDateTimePatternGeneratorTest(TestNode** root) {
     TESTCASE(TestOpenClose);
     TESTCASE(TestUsage);
     TESTCASE(TestBuilder);
+    TESTCASE(TestOptions);
 }
 
 /*
 }
 
 /*
@@ -52,7 +54,7 @@ void addDateTimePatternGeneratorTest(TestNode** root) {
 static const UChar pipeString[]={ 0x7c, 0x0a };
 
 static const UChar testSkeleton1[]={ 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
 static const UChar pipeString[]={ 0x7c, 0x0a };
 
 static const UChar testSkeleton1[]={ 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
-static const UChar expectingBestPattern[]={ 0x48, 0x48, 0x2e, 0x6d, 0x6d, 0 }; /* HH.mm */
+static const UChar expectingBestPattern[]={ 0x48, 0x2e, 0x6d, 0x6d, 0 }; /* H.mm */
 static const UChar testPattern[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0 }; /* HH:mm */
 static const UChar expectingSkeleton[]= { 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
 static const UChar expectingBaseSkeleton[]= { 0x48, 0x6d, 0 }; /* HHmm */
 static const UChar testPattern[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0 }; /* HH:mm */
 static const UChar expectingSkeleton[]= { 0x48, 0x48, 0x6d, 0x6d, 0 }; /* HHmm */
 static const UChar expectingBaseSkeleton[]= { 0x48, 0x6d, 0 }; /* HHmm */
@@ -76,7 +78,7 @@ static void TestOpenClose() {
     /* Open a DateTimePatternGenerator for the default locale. */
     dtpg=udatpg_open(NULL, &errorCode);
     if(U_FAILURE(errorCode)) {
     /* Open a DateTimePatternGenerator for the default locale. */
     dtpg=udatpg_open(NULL, &errorCode);
     if(U_FAILURE(errorCode)) {
-        log_err("udatpg_open(NULL) failed - %s\n", u_errorName(errorCode));
+        log_err_status(errorCode, "udatpg_open(NULL) failed - %s\n", u_errorName(errorCode));
         return;
     }
     udatpg_close(dtpg);
         return;
     }
     udatpg_close(dtpg);
@@ -109,9 +111,24 @@ static void TestOpenClose() {
     udatpg_close(dtpg2);
 }
 
     udatpg_close(dtpg2);
 }
 
+typedef struct {
+    UDateTimePatternField field;
+    UChar name[12];
+} AppendItemNameData;
+
+static const AppendItemNameData appendItemNameData[] = { /* for Finnish */
+    { UDATPG_YEAR_FIELD,    {0x0076,0x0075,0x006F,0x0073,0x0069,0} }, /* "vuosi" */
+    { UDATPG_MONTH_FIELD,   {0x006B,0x0075,0x0075,0x006B,0x0061,0x0075,0x0073,0x0069,0} }, /* "kuukausi" */
+    { UDATPG_WEEKDAY_FIELD, {0x0076,0x0069,0x0069,0x006B,0x006F,0x006E,0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
+    { UDATPG_DAY_FIELD,     {0x0070,0x00E4,0x0069,0x0076,0x00E4,0} },
+    { UDATPG_HOUR_FIELD,    {0x0074,0x0075,0x006E,0x0074,0x0069,0} }, /* "tunti" */
+    { UDATPG_FIELD_COUNT,   {0}        }  /* terminator */
+};
+
 static void TestUsage() {
     UErrorCode errorCode=U_ZERO_ERROR;
     UDateTimePatternGenerator *dtpg;
 static void TestUsage() {
     UErrorCode errorCode=U_ZERO_ERROR;
     UDateTimePatternGenerator *dtpg;
+    const AppendItemNameData * appItemNameDataPtr;
     UChar bestPattern[20];
     UChar result[20];
     int32_t length;    
     UChar bestPattern[20];
     UChar result[20];
     int32_t length;    
@@ -120,7 +137,7 @@ static void TestUsage() {
     
     dtpg=udatpg_open("fi", &errorCode);
     if(U_FAILURE(errorCode)) {
     
     dtpg=udatpg_open("fi", &errorCode);
     if(U_FAILURE(errorCode)) {
-        log_err("udatpg_open(fi) failed - %s\n", u_errorName(errorCode));
+        log_err_status(errorCode, "udatpg_open(fi) failed - %s\n", u_errorName(errorCode));
         return;
     }
     length = udatpg_getBestPattern(dtpg, testSkeleton1, 4,
         return;
     }
     length = udatpg_getBestPattern(dtpg, testSkeleton1, 4,
@@ -191,6 +208,14 @@ static void TestUsage() {
         return;
     }
     
         return;
     }
     
+    for (appItemNameDataPtr = appendItemNameData; appItemNameDataPtr->field <  UDATPG_FIELD_COUNT; appItemNameDataPtr++) {
+        int32_t nameLength;
+        const UChar * namePtr = udatpg_getAppendItemName(dtpg, appItemNameDataPtr->field, &nameLength);
+        if ( namePtr == NULL || u_strncmp(appItemNameDataPtr->name, namePtr, nameLength) != 0 ) {
+            log_err("udatpg_getAppendItemName returns invalid name for field %d\n", (int)appItemNameDataPtr->field);
+        }
+    }
+    
     /* set append name to hr */
     udatpg_setAppendItemName( dtpg, UDATPG_HOUR_FIELD, appendItemName, 7 );
     r = udatpg_getAppendItemName(dtpg, UDATPG_HOUR_FIELD, &length);
     /* set append name to hr */
     udatpg_setAppendItemName( dtpg, UDATPG_HOUR_FIELD, appendItemName, 7 );
     r = udatpg_getAppendItemName(dtpg, UDATPG_HOUR_FIELD, &length);
@@ -226,7 +251,7 @@ static void TestBuilder() {
     UChar   pattern[40], formatted[40];
     UDateFormat *formatter;
     UDate sampleDate = 837039928046.0;
     UChar   pattern[40], formatted[40];
     UDateFormat *formatter;
     UDate sampleDate = 837039928046.0;
-       static const char locale[]= "fr";
+    static const char locale[]= "fr";
     UErrorCode status=U_ZERO_ERROR;
     
     /* test create an empty DateTimePatternGenerator */
     UErrorCode status=U_ZERO_ERROR;
     
     /* test create an empty DateTimePatternGenerator */
@@ -323,7 +348,7 @@ static void TestBuilder() {
     /* get a pattern for an abbreviated month and day */
     length = udatpg_getBestPattern(generator, skeleton, 4,
                                    pattern, patternCapacity, &status);
     /* get a pattern for an abbreviated month and day */
     length = udatpg_getBestPattern(generator, skeleton, 4,
                                    pattern, patternCapacity, &status);
-    formatter = udat_open(UDAT_IGNORE, UDAT_DEFAULT, locale, timeZoneGMT, -1,
+    formatter = udat_open(UDAT_PATTERN, UDAT_PATTERN, locale, timeZoneGMT, -1,
                           pattern, length, &status);
     if (formatter==NULL) {
         log_err("Failed to initialize the UDateFormat of the sample code in Userguide.\n");
                           pattern, length, &status);
     if (formatter==NULL) {
         log_err("Failed to initialize the UDateFormat of the sample code in Userguide.\n");
@@ -347,4 +372,67 @@ static void TestBuilder() {
     udat_close(formatter);
 }
 
     udat_close(formatter);
 }
 
+typedef struct DTPtnGenOptionsData {
+    const char *                    locale;
+    const UChar *                   skel;
+    UDateTimePatternMatchOptions    options;
+    const UChar *                   expectedPattern;
+} DTPtnGenOptionsData;
+enum { kTestOptionsPatLenMax = 32 };
+
+static const UChar skel_Hmm[]     = { 0x0048, 0x006D, 0x006D, 0 };
+static const UChar skel_HHmm[]    = { 0x0048, 0x0048, 0x006D, 0x006D, 0 };
+static const UChar skel_hhmm[]    = { 0x0068, 0x0068, 0x006D, 0x006D, 0 };
+static const UChar patn_hcmm_a[]  = { 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h:mm a */
+static const UChar patn_HHcmm[]   = { 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0 }; /* HH:mm */
+static const UChar patn_hhcmm_a[] = { 0x0068, 0x0068, 0x003A, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh:mm a */
+static const UChar patn_HHpmm[]   = { 0x0048, 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* HH.mm */
+static const UChar patn_hpmm_a[]  = { 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* h.mm a */
+static const UChar patn_Hpmm[]    = { 0x0048, 0x002E, 0x006D, 0x006D, 0 }; /* H.mm */
+static const UChar patn_hhpmm_a[] = { 0x0068, 0x0068, 0x002E, 0x006D, 0x006D, 0x0020, 0x0061, 0 }; /* hh.mm a */
+
+static void TestOptions() {
+    const DTPtnGenOptionsData testData[] = {
+        /*loc   skel       options                       expectedPattern */
+        { "en", skel_Hmm,  UDATPG_MATCH_NO_OPTIONS,        patn_HHcmm   },
+        { "en", skel_HHmm, UDATPG_MATCH_NO_OPTIONS,        patn_HHcmm   },
+        { "en", skel_hhmm, UDATPG_MATCH_NO_OPTIONS,        patn_hcmm_a  },
+        { "en", skel_Hmm,  UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm   },
+        { "en", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm   },
+        { "en", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhcmm_a },
+        { "be", skel_Hmm,  UDATPG_MATCH_NO_OPTIONS,        patn_HHpmm   },
+        { "be", skel_HHmm, UDATPG_MATCH_NO_OPTIONS,        patn_HHpmm   },
+        { "be", skel_hhmm, UDATPG_MATCH_NO_OPTIONS,        patn_hpmm_a  },
+        { "be", skel_Hmm,  UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_Hpmm    },
+        { "be", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHpmm   },
+        { "be", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhpmm_a },
+    };
+
+    int count = sizeof(testData) / sizeof(testData[0]);
+    const DTPtnGenOptionsData * testDataPtr = testData;
+
+    for (; count-- > 0; ++testDataPtr) {
+        UErrorCode status = U_ZERO_ERROR;
+        UDateTimePatternGenerator * dtpgen = udatpg_open(testDataPtr->locale, &status);
+        if ( U_SUCCESS(status) ) {
+            UChar pattern[kTestOptionsPatLenMax];
+            int32_t patLen = udatpg_getBestPatternWithOptions(dtpgen, testDataPtr->skel, -1,
+                                                              testDataPtr->options, pattern,
+                                                              kTestOptionsPatLenMax, &status);
+            if ( U_FAILURE(status) || u_strncmp(pattern, testDataPtr->expectedPattern, patLen+1) != 0 ) {
+                char skelBytes[kTestOptionsPatLenMax];
+                char expectedPatternBytes[kTestOptionsPatLenMax];
+                char patternBytes[kTestOptionsPatLenMax];
+                log_err("ERROR udatpg_getBestPatternWithOptions, locale %s, skeleton %s, options 0x%04X, expected pattern %s, got %s, status %d\n",
+                        testDataPtr->locale, u_austrncpy(skelBytes,testDataPtr->skel,kTestOptionsPatLenMax), testDataPtr->options,
+                        u_austrncpy(expectedPatternBytes,testDataPtr->expectedPattern,kTestOptionsPatLenMax),
+                        u_austrncpy(patternBytes,pattern,kTestOptionsPatLenMax), status );
+            }
+            udatpg_close(dtpgen);
+        } else {
+            log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr->locale, myErrorName(status));
+        }
+    }
+}
+
 #endif
 #endif