]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/cintltst/citertst.c
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / test / cintltst / citertst.c
index 055091c2fff2584e19329fa9ce2afd9028ea1b9a..d802502524b03450c4bb5ee264ed448991f6ef67 100644 (file)
@@ -1,6 +1,6 @@
 /********************************************************************
  * COPYRIGHT:
- * Copyright (c) 1997-2004, International Business Machines Corporation and
+ * Copyright (c) 1997-2008, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 /********************************************************************************
@@ -424,9 +424,9 @@ static void TestPrevious()
     UCollator *c1, *c2, *c3;
     UCollationElements *iter;
     UErrorCode status = U_ZERO_ERROR;
+    UChar test1[50];
+    UChar test2[50];
 
-    test1=(UChar*)malloc(sizeof(UChar) * 50);
-    test2=(UChar*)malloc(sizeof(UChar) * 50);
     u_uastrcpy(test1, "What subset of all possible test cases?");
     u_uastrcpy(test2, "has the highest probability of detecting");
     coll = ucol_open("en_US", &status);
@@ -551,8 +551,6 @@ static void TestPrevious()
     ucol_close(coll);
 
     free(source);
-    free(test1);
-    free(test2);
 }
 
 /**
@@ -564,11 +562,12 @@ static void TestOffset()
     UCollator *en_us=NULL;
     UCollationElements *iter, *pristine;
     int32_t offset;
-    int32_t *orders;
+    OrderAndOffset *orders;
     int32_t orderLength=0;
     int     count = 0;
-    test1=(UChar*)malloc(sizeof(UChar) * 50);
-    test2=(UChar*)malloc(sizeof(UChar) * 50);
+    UChar test1[50];
+    UChar test2[50];
+
     u_uastrcpy(test1, "What subset of all possible test cases?");
     u_uastrcpy(test2, "has the highest probability of detecting");
     en_us = ucol_open("en_US", &status);
@@ -650,7 +649,7 @@ static void TestOffset()
         switch (count) {
         case 0:
             if (ucol_getOffset(iter) != 1) {
-                log_err("ERROR: Offset of iteration should be 0\n");
+                log_err("ERROR: Offset of iteration should be 1\n");
             }
             break;
         case 3:
@@ -672,10 +671,16 @@ static void TestOffset()
         U_SUCCESS(status)) {
         switch (count) {
         case 0:
+        case 1:
             if (ucol_getOffset(iter) != 3) {
                 log_err("ERROR: Offset of iteration should be 3\n");
             }
             break;
+        case 2:
+            if (ucol_getOffset(iter) != 1) {
+                log_err("ERROR: Offset of iteration should be 1\n");
+            }
+            break;
         default:
             if (ucol_getOffset(iter) != 0) {
                 log_err("ERROR: Offset of iteration should be 0\n");
@@ -691,8 +696,6 @@ static void TestOffset()
 
     ucol_closeElements(iter);
     ucol_close(en_us);
-    free(test1);
-    free(test2);
 }
 
 /**
@@ -704,8 +707,9 @@ static void TestSetText()
     UErrorCode status = U_ZERO_ERROR;
     UCollator *en_us=NULL;
     UCollationElements *iter1, *iter2;
-    test1=(UChar*)malloc(sizeof(UChar) * 50);
-    test2=(UChar*)malloc(sizeof(UChar) * 50);
+    UChar test1[50];
+    UChar test2[50];
+
     u_uastrcpy(test1, "What subset of all possible test cases?");
     u_uastrcpy(test2, "has the highest probability of detecting");
     en_us = ucol_open("en_US", &status);
@@ -770,8 +774,6 @@ static void TestSetText()
     ucol_closeElements(iter2);
     ucol_closeElements(iter1);
     ucol_close(en_us);
-    free(test1);
-    free(test2);
 }
 
 /** @bug 4108762
@@ -941,7 +943,7 @@ static void TestSmallBuffer()
     UCollationElements *testiter,
                        *iter;
     int32_t             count = 0;
-    int32_t            *testorders,
+    OrderAndOffset     *testorders,
                        *orders;
 
     UChar teststr[500];
@@ -981,8 +983,8 @@ static void TestSmallBuffer()
 
       while (count != 0) {
           /* UCA collation element for 0x0F76 */
-          if ((count > 250 && testorders[-- count] != orders[1]) ||
-              (count <= 250 && testorders[-- count] != orders[0])) {
+          if ((count > 250 && testorders[-- count].order != orders[1].order) ||
+              (count <= 250 && testorders[-- count].order != orders[0].order)) {
               log_err("Error decomposition does not give the right collation element at %d count\n", count);
               break;
           }
@@ -1031,7 +1033,7 @@ static int32_t hex2num(char hex) {
 * @param codepoints array for storage, assuming size > 5
 * @return position at the end of the codepoint section
 */
-static char * getCodePoints(char *str, UChar *codepoints) {
+static char * getCodePoints(char *str, UChar *codepoints, UChar *contextCPs) {
     char *pStartCP = str;
     char *pEndCP   = str + 4;
 
@@ -1039,6 +1041,21 @@ static char * getCodePoints(char *str, UChar *codepoints) {
                           (hex2num(*(pStartCP + 1)) << 8) |
                           (hex2num(*(pStartCP + 2)) << 4) |
                           (hex2num(*(pStartCP + 3))));
+    if (*pEndCP == '|' || *(pEndCP+1) == '|') {
+        /* pre-context rule */
+        pStartCP = pEndCP;
+        while (*pStartCP==' ' || *pStartCP== '|' ) {
+            pStartCP++;
+        }
+        pEndCP = pStartCP+4;
+        *contextCPs = *codepoints;
+        *(++codepoints) = (UChar)((hex2num(*pStartCP) << 12) |
+                                  (hex2num(*(pStartCP + 1)) << 8) |
+                                  (hex2num(*(pStartCP + 2)) << 4) |
+                                  (hex2num(*(pStartCP + 3))));
+        contextCPs++;
+    }
+    *contextCPs = 0;
     codepoints ++;
     while (*pEndCP != ';') {
         pStartCP = pEndCP + 1;
@@ -1252,11 +1269,12 @@ static void TestCEs() {
     FileStream *file = NULL;
     char        line[1024];
     char       *str;
-    UChar       codepoints[5];
+    UChar       codepoints[10];
     uint32_t    ces[20];
     UErrorCode  status = U_ZERO_ERROR;
     UCollator          *coll = ucol_open("", &status);
     uint32_t lineNo = 0;
+    UChar       contextCPs[5];    
 
     if (U_FAILURE(status)) {
         log_err("Error in opening root collator\n");
@@ -1274,6 +1292,7 @@ static void TestCEs() {
     while (T_FileStream_readLine(file, line, sizeof(line)) != NULL) {
         int                 count = 0;
         UCollationElements *iter;
+        int32_t            preContextCeLen=0;
         lineNo++;
         /* skip this line if it is empty or a comment or is a return value
         or start of some variable section */
@@ -1282,7 +1301,7 @@ static void TestCEs() {
             continue;
         }
 
-        str = getCodePoints(line, codepoints);
+        str = getCodePoints(line, codepoints, contextCPs);
 
         /* these are 'fake' codepoints in the fractional UCA, and are used just 
          * for positioning of indirect values. They should not go through this
@@ -1291,8 +1310,19 @@ static void TestCEs() {
         if(*codepoints == 0xFDD0) {
           continue;
         }
+        if (*contextCPs != 0) {
+            iter = ucol_openElements(coll, contextCPs, -1, &status);
+            if (U_FAILURE(status)) {
+                log_err("Error in opening collation elements\n");
+                break;
+            }
+            while((ces[preContextCeLen] = ucol_next(iter, &status)) != (uint32_t)UCOL_NULLORDER) {
+                preContextCeLen++;
+            }
+            ucol_closeElements(iter);
+        }
 
-        getCEs(str, ces, &status);
+        getCEs(str, ces+preContextCeLen, &status);
         if (U_FAILURE(status)) {
             log_err("Error in parsing collation elements in FractionalUCA.txt\n");
             break;
@@ -1462,9 +1492,9 @@ static void TestCEBufferOverflow()
     str[UCOL_EXPAND_CE_BUFFER_SIZE] = 0x0042;   /* 'B' */
     iter = ucol_openElements(coll, str, UCOL_EXPAND_CE_BUFFER_SIZE + 1,
                              &status);
-    if (ucol_previous(iter, &status) != UCOL_NULLORDER ||
-        status != U_BUFFER_OVERFLOW_ERROR) {
-        log_err("CE buffer expected to overflow with long string of trail surrogates\n");
+    if (ucol_previous(iter, &status) == UCOL_NULLORDER ||
+        status == U_BUFFER_OVERFLOW_ERROR) {
+        log_err("CE buffer should not overflow with long string of trail surrogates\n");
     }
     ucol_closeElements(iter);
     ucol_close(coll);
@@ -1623,17 +1653,19 @@ static void TestCEValidity()
     /* tailored locales */
     char        locale[][11] = {"fr_FR", "ko_KR", "sh_YU", "th_TH", "zh_CN", "zh__PINYIN"};
     const char *loc;
-    FileStream *file = getFractionalUCA();
+    FileStream *file = NULL;
     char        line[1024];
     UChar       codepoints[10];
     int         count = 0;
     int         maxCount = 0;
+    UChar       contextCPs[3];
     UParseError parseError;
     if (U_FAILURE(status)) {
         log_err("en_US collator creation failed\n");
         return;
     }
     log_verbose("Testing UCA elements\n");
+    file = getFractionalUCA();
     if (file == NULL) {
         log_err("Fractional UCA data can not be opened\n");
         return;
@@ -1645,7 +1677,7 @@ static void TestCEValidity()
             continue;
         }
 
-        getCodePoints(line, codepoints);
+        getCodePoints(line, codepoints, contextCPs);
         checkCEValidity(coll, codepoints, u_strlen(codepoints), 5, 86);
     }
 
@@ -1818,17 +1850,19 @@ static void TestSortKeyValidity(void)
     /* en_US has no tailorings */
     UCollator  *coll        = ucol_open("en_US", &status);
     /* tailored locales */
-    char        locale[][6] = {"fr_FR\0", "ko_KR\0", "sh_YU\0", "th_TH\0", "zh_CN\0"};
-    FileStream *file = getFractionalUCA();
+    char        locale[][6] = {"fr_FR", "ko_KR", "sh_YU", "th_TH", "zh_CN"};
+    FileStream *file = NULL;
     char        line[1024];
     UChar       codepoints[10];
     int         count = 0;
+    UChar       contextCPs[5];
     UParseError parseError;
     if (U_FAILURE(status)) {
         log_err("en_US collator creation failed\n");
         return;
     }
     log_verbose("Testing UCA elements\n");
+    file = getFractionalUCA();
     if (file == NULL) {
         log_err("Fractional UCA data can not be opened\n");
         return;
@@ -1840,7 +1874,7 @@ static void TestSortKeyValidity(void)
             continue;
         }
 
-        getCodePoints(line, codepoints);
+        getCodePoints(line, codepoints, contextCPs);
         checkSortKeyValidity(coll, codepoints, u_strlen(codepoints));
     }