]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/cintltst/ccaltst.c
ICU-62135.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / ccaltst.c
index 9333b51f0c102031af7d2736d32cf3bd920971d5..abebc091cebc0c92dec67c388c6cd25c0ffaa0ac 100644 (file)
@@ -1,3 +1,5 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /********************************************************************
  * Copyright (c) 1997-2016, International Business Machines
  * Corporation and others. All Rights Reserved.
@@ -39,7 +41,9 @@ void TestGetTZTransition(void);
 void TestGetWindowsTimeZoneID(void);
 void TestGetTimeZoneIDByWindowsID(void);
 void TestClear(void);
+void TestPersianCalOverflow(void);
 void TestGetDayPeriods(); /* Apple-specific */
+void TestJCalHeiseiNextEra(); /* Apple-specific */
 
 void addCalTest(TestNode** root);
 
@@ -63,7 +67,9 @@ void addCalTest(TestNode** root)
     addTest(root, &TestGetWindowsTimeZoneID, "tsformat/ccaltst/TestGetWindowsTimeZoneID");
     addTest(root, &TestGetTimeZoneIDByWindowsID, "tsformat/ccaltst/TestGetTimeZoneIDByWindowsID");
     addTest(root, &TestClear, "tsformat/ccaltst/TestClear");
+    addTest(root, &TestPersianCalOverflow, "tsformat/ccaltst/TestPersianCalOverflow");
     addTest(root, &TestGetDayPeriods, "tsformat/ccaltst/TestGetDayPeriods"); /* Apple-specific */
+    addTest(root, &TestJCalHeiseiNextEra, "tsformat/ccaltst/TestJCalHeiseiNextEra"); /* Apple-specific */
 }
 
 /* "GMT" */
@@ -113,7 +119,7 @@ static void TestCalendar()
     UDateFormat *datdef = 0;
     UChar *result = 0;
     int32_t resultlength, resultlengthneeded;
-    char tempMsgBuf[256];
+    char tempMsgBuf[1024];  // u_austrcpy() of some formatted dates & times.
     UChar zone1[32], zone2[32];
     const char *tzver = 0;
     UChar canonicalID[64];
@@ -259,7 +265,7 @@ static void TestCalendar()
     tzver = ucal_getTZDataVersion(&status);
     if (U_FAILURE(status)) {
         log_err_status(status, "FAIL: ucal_getTZDataVersion() => %s\n", u_errorName(status));
-    } else if (uprv_strlen(tzver) != 5 /*4 digits + 1 letter*/) {
+    } else if (uprv_strlen(tzver) < 5 || uprv_strlen(tzver) > 7 /*4 digits + 1-3 letters*/) {
         log_err("FAIL: Bad version string was returned by ucal_getTZDataVersion\n");
     } else {
         log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver);
@@ -686,7 +692,7 @@ static void TestGetSetDateAPI()
 
     /*Testing  if setDate works fine  */
     log_verbose("\nTesting the ucal_setDate() function \n");
-    u_uastrcpy(temp, "Dec 17, 1971, 11:05:28 PM");
+    u_uastrcpy(temp, "Dec 17, 1971 at 11:05:28 PM");
     ucal_setDate(caldef,1971, UCAL_DECEMBER, 17, &status);
     if(U_FAILURE(status)){
         log_err("error in setting the calendar date : %s\n", u_errorName(status));
@@ -717,7 +723,7 @@ static void TestGetSetDateAPI()
 
     /*Testing if setDateTime works fine */
     log_verbose("\nTesting the ucal_setDateTime() function \n");
-    u_uastrcpy(temp, "May 3, 1972, 4:30:42 PM");
+    u_uastrcpy(temp, "May 3, 1972 at 4:30:42 PM");
     ucal_setDateTime(caldef,1972, UCAL_MAY, 3, 16, 30, 42, &status);
     if(U_FAILURE(status)){
         log_err("error in setting the calendar date : %s\n", u_errorName(status));
@@ -1967,7 +1973,7 @@ void TestAmbiguousWallTime() {
     UDate t, expected;
 
     u_uastrcpy(tzID, "America/New_York");
-    ucal = ucal_open(tzID, -1, NULL, UCAL_DEFAULT, &status);
+    ucal = ucal_open(tzID, -1, "en_US", UCAL_DEFAULT, &status);
     if (U_FAILURE(status)) {
         log_err("FAIL: Failed to create a calendar");
         return;
@@ -2506,6 +2512,37 @@ void TestClear() {
     }
 }
 
+void TestPersianCalOverflow() {
+    const char * locale = "bs_Cyrl@calendar=persian";
+    UErrorCode status = U_ZERO_ERROR;
+    UCalendar * ucal = ucal_open(NULL, 0, locale, UCAL_DEFAULT, &status);
+    if ( U_FAILURE(status) ) {
+        log_data_err("FAIL: ucal_open for locale %s, status %s\n", locale, u_errorName(status)); 
+    } else {
+        int32_t maxMonth = ucal_getLimit(ucal, UCAL_MONTH, UCAL_MAXIMUM, &status);
+        int32_t maxDayOfMonth = ucal_getLimit(ucal, UCAL_DATE, UCAL_MAXIMUM, &status);
+        if ( U_FAILURE(status) ) {
+            log_err("FAIL: ucal_getLimit MONTH/DATE for locale %s, status %s\n", locale, u_errorName(status)); 
+        } else {
+            int32_t jd, month, dayOfMonth;
+            for (jd = 67023580; jd <= 67023584; jd++) { // year 178171, int32_t overflow if jd >= 67023582
+                status = U_ZERO_ERROR;
+                ucal_clear(ucal);
+                ucal_set(ucal, UCAL_JULIAN_DAY, jd);
+                month = ucal_get(ucal, UCAL_MONTH, &status);
+                dayOfMonth = ucal_get(ucal, UCAL_DATE, &status);
+                if ( U_FAILURE(status) ) {
+                    log_err("FAIL: ucal_get MONTH/DATE for locale %s, julianDay %d, status %s\n", locale, jd, u_errorName(status)); 
+                } else if (month > maxMonth || dayOfMonth > maxDayOfMonth) {
+                    log_err("FAIL: locale %s, julianDay %d; maxMonth %d, got month %d; maxDayOfMonth %d, got dayOfMonth %d\n",
+                            locale, jd, maxMonth, month, maxDayOfMonth, dayOfMonth); 
+                }
+            }
+        }
+        ucal_close(ucal);
+    }
+}
+
 /* Apple-specific */
 typedef struct {
     const char * locale;
@@ -2544,4 +2581,60 @@ void TestGetDayPeriods() {
     }
 }
 
+void TestJCalHeiseiNextEra() {
+    UErrorCode status = U_ZERO_ERROR;
+    UCalendar *jCal = ucal_open(NULL, 0, "ja_JP@calendar=japanese", UCAL_DEFAULT, &status);
+    if ( U_FAILURE(status) ) {
+        log_data_err("FAIL: ucal_open for ja_JP@calendar=japanese, status %s\n", u_errorName(status));
+    } else {
+        ucal_clear(jCal); // This sets to 1970, in Showa
+        int32_t sEra = ucal_get(jCal, UCAL_ERA, &status); // Don't assume Showa is 234
+        if ( U_FAILURE(status) ) {
+            log_data_err("FAIL: ucal_get ERA for Showa, status %s\n", u_errorName(status));
+        } else {
+            int32_t iEra, eYear;
+            int32_t startYears[] = { 1926, 1989, 2019, 0 };
+            for (iEra = 1; iEra < 4; iEra++) {
+                status = U_ZERO_ERROR;
+                ucal_clear(jCal);
+                ucal_set(jCal, UCAL_ERA, sEra+iEra);
+                eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status);
+                if ( U_FAILURE(status) ) {
+                    log_err("FAIL: set %d, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status));
+                } else if (startYears[iEra] == 0) {
+                    // invalid era, start should be in the far future with non-negative millis
+                    if (eYear < 10000) {
+                        log_err("ERROR: set %d, invalid era should have faraway start year, but get %d\n", iEra, eYear);
+                    }
+                    UDate date = ucal_getMillis(jCal, &status);
+                    if ( U_FAILURE(status) ) {
+                        log_err("FAIL: set %d, ucal_getMillis, status %s\n", iEra, u_errorName(status));
+                    } else if (date < 0) {
+                        log_err("ERROR: set %d, ucal_getMillis should be positive, but get %.1f\n", iEra, date);
+                    }
+                } else if (eYear != startYears[iEra]) {
+                    log_err("ERROR: set %d, expected start year %d but get %d\n", iEra, startYears[iEra], eYear);
+                } else {
+                    ucal_add(jCal, UCAL_ERA, 1, &status);
+                    if ( U_FAILURE(status) ) {
+                        log_err("FAIL: set %d, ucal_add ERA 1, status %s\n", iEra, u_errorName(status));
+                    } else {
+                        eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status);
+                        if ( U_FAILURE(status) ) {
+                            log_err("FAIL: set %d then add ERA 1, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status));
+                        } else {
+                            // If this is the last valid era, we expect adding an era to pin to the current era
+                            int32_t nextEraStart = (startYears[iEra+1] == 0)? startYears[iEra]: startYears[iEra+1];
+                            if (eYear != nextEraStart) {
+                                log_err("ERROR: set %d then add ERA 1, expected start year %d but get %d\n", iEra, nextEraStart, eYear);
+                            }
+                        }
+                    }
+                }
+             }
+        }
+        ucal_close(jCal);
+    }
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */