]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/cintltst/callcoll.c
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / callcoll.c
index f57dc605a640cdb018714e2729e2c71a8fee1a38..68bb57fa3dee9944d01db5e304d97fc25faf9f80 100644 (file)
@@ -1,16 +1,18 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /********************************************************************
  * COPYRIGHT: 
 /********************************************************************
  * COPYRIGHT: 
- * Copyright (c) 1997-2006, International Business Machines Corporation and
+ * Copyright (c) 1997-2014, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
  * others. All Rights Reserved.
  ********************************************************************/
-/********************************************************************************
+/*******************************************************************************
 *
 * File CALLCOLL.C
 *
 * Modification History:
 *        Name                     Description            
 *     Madhu Katragadda              Ported for C API
 *
 * File CALLCOLL.C
 *
 * Modification History:
 *        Name                     Description            
 *     Madhu Katragadda              Ported for C API
-*********************************************************************************
+********************************************************************************
 */
 
 /*
 */
 
 /*
 
 #include "unicode/ucol.h"
 #include "unicode/uloc.h"
 
 #include "unicode/ucol.h"
 #include "unicode/uloc.h"
+#include "unicode/ures.h"
+#include "unicode/udata.h"
 #include "unicode/ucoleitr.h"
 #include "unicode/ustring.h"
 #include "unicode/uclean.h"
 #include "unicode/putil.h"
 #include "unicode/ucoleitr.h"
 #include "unicode/ustring.h"
 #include "unicode/uclean.h"
 #include "unicode/putil.h"
+#include "unicode/uenum.h"
 
 #include "cintltst.h"
 #include "ccolltst.h"
 
 #include "cintltst.h"
 #include "ccolltst.h"
@@ -49,7 +54,9 @@
 #include "calldata.h"
 #include "cstring.h"
 #include "cmemory.h"
 #include "calldata.h"
 #include "cstring.h"
 #include "cmemory.h"
-#include "ucol_imp.h"
+
+/* set to 1 to test offsets in backAndForth() */
+#define TEST_OFFSETS 0
 
 /* perform test with strength PRIMARY */
 static void TestPrimary(void);
 
 /* perform test with strength PRIMARY */
 static void TestPrimary(void);
@@ -84,6 +91,10 @@ static void TestJitterbug1098(void);
 
 static void TestFCDCrash(void);
 
 
 static void TestFCDCrash(void);
 
+static void TestJ5298(void);
+
+static void TestBadKey(void);
+
 const UCollationResult results[] = {
     UCOL_LESS,
     UCOL_LESS, /*UCOL_GREATER,*/
 const UCollationResult results[] = {
     UCOL_LESS,
     UCOL_LESS, /*UCOL_GREATER,*/
@@ -137,88 +148,86 @@ void uprv_appendByteToHexString(char *dst, uint8_t val) {
 
 /* this function makes a string with representation of a sortkey */
 static char* U_EXPORT2 sortKeyToString(const UCollator *coll, const uint8_t *sortkey, char *buffer, uint32_t *len) {
 
 /* this function makes a string with representation of a sortkey */
 static char* U_EXPORT2 sortKeyToString(const UCollator *coll, const uint8_t *sortkey, char *buffer, uint32_t *len) {
-  int32_t strength = UCOL_PRIMARY;
-  uint32_t res_size = 0;
-  UBool doneCase = FALSE;
+    int32_t strength = UCOL_PRIMARY;
+    uint32_t res_size = 0;
+    UBool doneCase = FALSE;
+    UErrorCode errorCode = U_ZERO_ERROR;
 
 
-  char *current = buffer;
-  const uint8_t *currentSk = sortkey;
+    char *current = buffer;
+    const uint8_t *currentSk = sortkey;
 
 
-  uprv_strcpy(current, "[");
+    uprv_strcpy(current, "[");
 
 
-  while(strength <= UCOL_QUATERNARY && strength <= coll->strength) {
-    if(strength > UCOL_PRIMARY) {
-      uprv_strcat(current, " . ");
-    }
-    while(*currentSk != 0x01 && *currentSk != 0x00) { /* print a level */
-      uprv_appendByteToHexString(current, *currentSk++);
-      uprv_strcat(current, " ");
-    }
-    if(coll->caseLevel == UCOL_ON && strength == UCOL_SECONDARY && doneCase == FALSE) {
-        doneCase = TRUE;
-    } else if(coll->caseLevel == UCOL_OFF || doneCase == TRUE || strength != UCOL_SECONDARY) {
-      strength ++;
-    }
-    uprv_appendByteToHexString(current, *currentSk++); /* This should print '01' */
-    if(strength == UCOL_QUATERNARY && coll->alternateHandling == UCOL_NON_IGNORABLE) {
-      break;
+    while(strength <= UCOL_QUATERNARY && strength <= ucol_getStrength(coll)) {
+        if(strength > UCOL_PRIMARY) {
+            uprv_strcat(current, " . ");
+        }
+        while(*currentSk != 0x01 && *currentSk != 0x00) { /* print a level */
+            uprv_appendByteToHexString(current, *currentSk++);
+            uprv_strcat(current, " ");
+        }
+        if(ucol_getAttribute(coll, UCOL_CASE_LEVEL, &errorCode) == UCOL_ON && strength == UCOL_SECONDARY && doneCase == FALSE) {
+            doneCase = TRUE;
+        } else if(ucol_getAttribute(coll, UCOL_CASE_LEVEL, &errorCode) == UCOL_OFF || doneCase == TRUE || strength != UCOL_SECONDARY) {
+            strength ++;
+        }
+        if (*currentSk) {
+            uprv_appendByteToHexString(current, *currentSk++); /* This should print '01' */
+        }
+        if(strength == UCOL_QUATERNARY && ucol_getAttribute(coll, UCOL_ALTERNATE_HANDLING, &errorCode) == UCOL_NON_IGNORABLE) {
+            break;
+        }
     }
     }
-  }
 
 
-  if(coll->strength == UCOL_IDENTICAL) {
-    uprv_strcat(current, " . ");
-    while(*currentSk != 0) {
-      uprv_appendByteToHexString(current, *currentSk++);
-      uprv_strcat(current, " ");
-    }
+    if(ucol_getStrength(coll) == UCOL_IDENTICAL) {
+        uprv_strcat(current, " . ");
+        while(*currentSk != 0) {
+            uprv_appendByteToHexString(current, *currentSk++);
+            uprv_strcat(current, " ");
+        }
 
 
-    uprv_appendByteToHexString(current, *currentSk++);
-  }
-  uprv_strcat(current, "]");
+        uprv_appendByteToHexString(current, *currentSk++);
+    }
+    uprv_strcat(current, "]");
 
 
-  if(res_size > *len) {
-    return NULL;
-  }
+    if(res_size > *len) {
+        return NULL;
+    }
 
 
-  return buffer;
+    return buffer;
 }
 
 void addAllCollTest(TestNode** root)
 {
 }
 
 void addAllCollTest(TestNode** root)
 {
-    
-    
     addTest(root, &TestPrimary, "tscoll/callcoll/TestPrimary");
     addTest(root, &TestSecondary, "tscoll/callcoll/TestSecondary");
     addTest(root, &TestTertiary, "tscoll/callcoll/TestTertiary");
     addTest(root, &TestIdentical, "tscoll/callcoll/TestIdentical");
     addTest(root, &TestExtra, "tscoll/callcoll/TestExtra");
     addTest(root, &TestPrimary, "tscoll/callcoll/TestPrimary");
     addTest(root, &TestSecondary, "tscoll/callcoll/TestSecondary");
     addTest(root, &TestTertiary, "tscoll/callcoll/TestTertiary");
     addTest(root, &TestIdentical, "tscoll/callcoll/TestIdentical");
     addTest(root, &TestExtra, "tscoll/callcoll/TestExtra");
-    addTest(root, &TestJB581, "tscoll/callcoll/TestJB581");      
-    addTest(root, &TestVariableTop, "tscoll/callcoll/TestVariableTop");      
+    addTest(root, &TestJB581, "tscoll/callcoll/TestJB581");
+    addTest(root, &TestVariableTop, "tscoll/callcoll/TestVariableTop");
     addTest(root, &TestSurrogates, "tscoll/callcoll/TestSurrogates");
     addTest(root, &TestInvalidRules, "tscoll/callcoll/TestInvalidRules");
     addTest(root, &TestSurrogates, "tscoll/callcoll/TestSurrogates");
     addTest(root, &TestInvalidRules, "tscoll/callcoll/TestInvalidRules");
-    addTest(root, &TestJB1401, "tscoll/callcoll/TestJB1401");      
-    addTest(root, &TestJitterbug1098, "tscoll/callcoll/TestJitterbug1098");     
+    addTest(root, &TestJB1401, "tscoll/callcoll/TestJB1401");
+    addTest(root, &TestJitterbug1098, "tscoll/callcoll/TestJitterbug1098");
     addTest(root, &TestFCDCrash, "tscoll/callcoll/TestFCDCrash");
     addTest(root, &TestFCDCrash, "tscoll/callcoll/TestFCDCrash");
-
-   }
+    addTest(root, &TestJ5298, "tscoll/callcoll/TestJ5298");
+    addTest(root, &TestBadKey, "tscoll/callcoll/TestBadKey");
+}
 
 UBool hasCollationElements(const char *locName) {
 
   UErrorCode status = U_ZERO_ERROR;
 
 UBool hasCollationElements(const char *locName) {
 
   UErrorCode status = U_ZERO_ERROR;
-  UResourceBundle *ColEl = NULL;
 
 
-  UResourceBundle *loc = ures_open(NULL, locName, &status);;
+  UResourceBundle *loc = ures_open(U_ICUDATA_NAME U_TREE_SEPARATOR_STRING "coll", locName, &status);;
 
   if(U_SUCCESS(status)) {
     status = U_ZERO_ERROR;
 
   if(U_SUCCESS(status)) {
     status = U_ZERO_ERROR;
-    ColEl = ures_getByKey(loc, "collations", ColEl, &status);
+    loc = ures_getByKey(loc, "collations", loc, &status);
+    ures_close(loc);
     if(status == U_ZERO_ERROR) { /* do the test - there are real elements */
     if(status == U_ZERO_ERROR) { /* do the test - there are real elements */
-      ures_close(ColEl);
-      ures_close(loc);
       return TRUE;
     }
       return TRUE;
     }
-    ures_close(ColEl);
-    ures_close(loc);
   }
   return FALSE;
 }
   }
   return FALSE;
 }
@@ -275,49 +284,84 @@ static void doTestVariant(UCollator* myCollation, const UChar source[], const UC
     UColAttributeValue norm = ucol_getAttribute(myCollation, UCOL_NORMALIZATION_MODE, &status);
 
     UCharIterator sIter, tIter;
     UColAttributeValue norm = ucol_getAttribute(myCollation, UCOL_NORMALIZATION_MODE, &status);
 
     UCharIterator sIter, tIter;
+
+    compareResult  = ucol_strcoll(myCollation, source, sLen, target, tLen);
+    if (compareResult != result) {
+        log_err("ucol_strcoll with explicit length returned wrong result (%i exp. %i): %s, %s\n",
+            compareResult, result, aescstrdup(source,-1), aescstrdup(target,-1));
+    }
+    compareResulta = ucol_strcoll(myCollation, source, -1,   target, -1); 
+    if (compareResulta != result) {
+        log_err("ucol_strcoll with null terminated strings returned wrong result (%i exp. %i): %s, %s\n",
+            compareResult, result, aescstrdup(source,-1), aescstrdup(target,-1));
+    }
+
     uiter_setString(&sIter, source, sLen);
     uiter_setString(&tIter, target, tLen);
     compareResultIter = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
     if(compareResultIter != result) {
     uiter_setString(&sIter, source, sLen);
     uiter_setString(&tIter, target, tLen);
     compareResultIter = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
     if(compareResultIter != result) {
-      log_err("different results in iterative comparison for UTF-16 encoded strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));
+        log_err("different results in iterative comparison for UTF-16 encoded strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));
     }
 
     }
 
-    /* convert the strings to UTF-8 and do try comparing with char iterator */
-    if(QUICK <= 0) { /*!QUICK*/
-      char utf8Source[256], utf8Target[256];
-      int32_t utf8SourceLen = 0, utf8TargetLen = 0;
-      u_strToUTF8(utf8Source, 256, &utf8SourceLen, source, sLen, &status);
-      if(U_FAILURE(status)) { /* probably buffer is not big enough */
-        log_verbose("Src UTF-8 buffer too small! Will not compare!\n");
-      } else {
-        u_strToUTF8(utf8Target, 256, &utf8TargetLen, target, tLen, &status);
-        if(U_SUCCESS(status)) { /* probably buffer is not big enough */
-          UCollationResult compareResultUTF8 = result, compareResultUTF8Norm = result;
-          /*UCharIterator sIter, tIter;*/
-          /*log_verbose("Strings converted to UTF-8:%s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));*/
-          uiter_setUTF8(&sIter, utf8Source, utf8SourceLen);
-          uiter_setUTF8(&tIter, utf8Target, utf8TargetLen);
-       /*uiter_setString(&sIter, source, sLen);
-      uiter_setString(&tIter, target, tLen);*/
-          compareResultUTF8 = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
-          ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
-          sIter.move(&sIter, 0, UITER_START);
-          tIter.move(&tIter, 0, UITER_START);
-          compareResultUTF8Norm = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
-          ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, norm, &status);
-          if(compareResultUTF8 != compareResultIter) {
-            log_err("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));
-          }
-          if(compareResultUTF8 != compareResultUTF8Norm) {
-            log_err("different results in iterative when normalization is turned on with UTF-8 strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));
-          }
+    /* convert the strings to UTF-8 and do try comparing with char iterator and ucol_strcollUTF8 */
+    {
+        char utf8Source[256], utf8Target[256];
+        int32_t utf8SourceLen = 0, utf8TargetLen = 0;
+
+        u_strToUTF8(utf8Source, 256, &utf8SourceLen, source, sLen, &status);
+        if(U_FAILURE(status)) { /* probably buffer is not big enough */
+            log_verbose("Src UTF-8 buffer too small! Will not compare!\n");
         } else {
         } else {
-          log_verbose("Target UTF-8 buffer too small! Did not compare!\n");
-        }
-        if(U_FAILURE(status)) {
-          log_verbose("UTF-8 strcoll failed! Ignoring result\n");
+            u_strToUTF8(utf8Target, 256, &utf8TargetLen, target, tLen, &status);
+            if(U_SUCCESS(status)) {
+                {
+                    /* ucol_strcollUTF8 */
+                    compareResulta = ucol_strcollUTF8(myCollation, utf8Source, utf8SourceLen, utf8Target, utf8TargetLen, &status);
+                    if (U_FAILURE(status)) {
+                        log_err("Error in ucol_strcollUTF8 with explicit length\n");
+                        status = U_ZERO_ERROR;
+                    } else if (compareResulta != result) {
+                        log_err("ucol_strcollUTF8 with explicit length returned wrong result (%i exp. %i): %s, %s\n",
+                            compareResulta, result, aescstrdup(source,-1), aescstrdup(target,-1));
+                    }
+                    compareResulta = ucol_strcollUTF8(myCollation, utf8Source, -1, utf8Target, -1, &status);
+                    if (U_FAILURE(status)) {
+                        log_err("Error in ucol_strcollUTF8 with null terminated strings\n");
+                        status = U_ZERO_ERROR;
+                    } else if (compareResulta != result) {
+                        log_err("ucol_strcollUTF8 with null terminated strings returned wrong result (%i exp. %i): %s, %s\n",
+                            compareResulta, result, aescstrdup(source,-1), aescstrdup(target,-1));
+                    }
+                }
+
+                {
+                    /* char iterator over UTF8 */
+                    UCollationResult compareResultUTF8Iter = result, compareResultUTF8IterNorm = result;
+
+                    uiter_setUTF8(&sIter, utf8Source, utf8SourceLen);
+                    uiter_setUTF8(&tIter, utf8Target, utf8TargetLen);
+                    compareResultUTF8Iter = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
+
+                    ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
+                    sIter.move(&sIter, 0, UITER_START);
+                    tIter.move(&tIter, 0, UITER_START);
+                    compareResultUTF8IterNorm = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
+
+                    ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, norm, &status);
+                    if(compareResultUTF8Iter != compareResultIter) {
+                        log_err("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));
+                    }
+                    if(compareResultUTF8Iter != compareResultUTF8IterNorm) {
+                        log_err("different results in iterative when normalization is turned on with UTF-8 strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));
+                    }
+                }
+            } else {
+                log_verbose("Target UTF-8 buffer too small! Did not compare!\n");
+            }
+            if(U_FAILURE(status)) {
+                log_verbose("UTF-8 strcoll failed! Ignoring result\n");
+            }
         }
         }
-      }
     }
 
     /* testing the partial sortkeys */
     }
 
     /* testing the partial sortkeys */
@@ -325,7 +369,7 @@ static void doTestVariant(UCollator* myCollation, const UChar source[], const UC
       int32_t i = 0;
       int32_t partialSizes[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
       int32_t partialSizesSize = 1;
       int32_t i = 0;
       int32_t partialSizes[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
       int32_t partialSizesSize = 1;
-      if(QUICK <= 0) {
+      if(getTestOption(QUICK_OPTION) <= 0) {
         partialSizesSize = 7;
       }
       /*log_verbose("partial sortkey test piecesize=");*/
         partialSizesSize = 7;
       }
       /*log_verbose("partial sortkey test piecesize=");*/
@@ -340,7 +384,7 @@ static void doTestVariant(UCollator* myCollation, const UChar source[], const UC
             aescstrdup(source,-1), aescstrdup(target,-1), partialSizes[i]);
         }
 
             aescstrdup(source,-1), aescstrdup(target,-1), partialSizes[i]);
         }
 
-        if(QUICK <= 0 && norm != UCOL_ON) {
+        if(getTestOption(QUICK_OPTION) <= 0 && norm != UCOL_ON) {
           /*log_verbose("N ");*/
           ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
           partialNormalizedSKResult = compareUsingPartials(myCollation, source, sLen, target, tLen, partialSizes[i], &status);
           /*log_verbose("N ");*/
           ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
           partialNormalizedSKResult = compareUsingPartials(myCollation, source, sLen, target, tLen, partialSizes[i], &status);
@@ -354,18 +398,12 @@ static void doTestVariant(UCollator* myCollation, const UChar source[], const UC
       /*log_verbose("\n");*/
     }
 
       /*log_verbose("\n");*/
     }
 
-    
-    compareResult  = ucol_strcoll(myCollation, source, sLen, target, tLen);
-    compareResulta = ucol_strcoll(myCollation, source, -1,   target, -1); 
-    if (compareResult != compareResulta) {
-        log_err("ucol_strcoll result from null terminated and explicit length strings differs.\n");
-    }
-
     sortklen1=ucol_getSortKey(myCollation, source, sLen,  NULL, 0);
     sortklen2=ucol_getSortKey(myCollation, target, tLen,  NULL, 0);
 
     sortklenmax = (sortklen1>sortklen2?sortklen1:sortklen2);
     sortklenmin = (sortklen1<sortklen2?sortklen1:sortklen2);
     sortklen1=ucol_getSortKey(myCollation, source, sLen,  NULL, 0);
     sortklen2=ucol_getSortKey(myCollation, target, tLen,  NULL, 0);
 
     sortklenmax = (sortklen1>sortklen2?sortklen1:sortklen2);
     sortklenmin = (sortklen1<sortklen2?sortklen1:sortklen2);
+    (void)sortklenmin;  /* Suppress set but not used warning. */
 
     sortKey1 =(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1));
     sortKey1a=(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1));
 
     sortKey1 =(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1));
     sortKey1a=(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1));
@@ -435,14 +473,15 @@ void doTest(UCollator* myCollation, const UChar source[], const UChar target[],
  * Return an integer array containing all of the collation orders
  * returned by calls to next on the specified iterator
  */
  * Return an integer array containing all of the collation orders
  * returned by calls to next on the specified iterator
  */
-int32_t* getOrders(UCollationElements *iter, int32_t *orderLength)
+OrderAndOffset* getOrders(UCollationElements *iter, int32_t *orderLength)
 {
     UErrorCode status;
     int32_t order;
     int32_t maxSize = 100;
     int32_t size = 0;
 {
     UErrorCode status;
     int32_t order;
     int32_t maxSize = 100;
     int32_t size = 0;
-    int32_t *temp;
-    int32_t *orders =(int32_t*)malloc(sizeof(int32_t) * maxSize);
+    int32_t offset = ucol_getOffset(iter);
+    OrderAndOffset *temp;
+    OrderAndOffset *orders =(OrderAndOffset *)malloc(sizeof(OrderAndOffset) * maxSize);
     status= U_ZERO_ERROR;
 
 
     status= U_ZERO_ERROR;
 
 
@@ -451,22 +490,26 @@ int32_t* getOrders(UCollationElements *iter, int32_t *orderLength)
         if (size == maxSize)
         {
             maxSize *= 2;
         if (size == maxSize)
         {
             maxSize *= 2;
-            temp = (int32_t*)malloc(sizeof(int32_t) * maxSize);
+            temp = (OrderAndOffset *)malloc(sizeof(OrderAndOffset) * maxSize);
 
 
-            memcpy(temp, orders, size * sizeof(int32_t));
+            memcpy(temp, orders, size * sizeof(OrderAndOffset));
             free(orders);
             orders = temp;
 
         }
 
             free(orders);
             orders = temp;
 
         }
 
-        orders[size++] = order;
+        orders[size].order  = order;
+        orders[size].offset = offset;
+
+        offset = ucol_getOffset(iter);
+        size += 1;
     }
 
     if (maxSize > size && size > 0)
     {
     }
 
     if (maxSize > size && size > 0)
     {
-        temp = (int32_t*)malloc(sizeof(int32_t) * size);
+        temp = (OrderAndOffset *)malloc(sizeof(OrderAndOffset) * size);
 
 
-        memcpy(temp, orders, size * sizeof(int32_t));
+        memcpy(temp, orders, size * sizeof(OrderAndOffset));
         free(orders);
         orders = temp;
 
         free(orders);
         orders = temp;
 
@@ -482,65 +525,78 @@ void
 backAndForth(UCollationElements *iter)
 {
     /* Run through the iterator forwards and stick it into an array */
 backAndForth(UCollationElements *iter)
 {
     /* Run through the iterator forwards and stick it into an array */
-    int32_t index, o;
+    int32_t idx, o;
     UErrorCode status = U_ZERO_ERROR;
     int32_t orderLength = 0;
     UErrorCode status = U_ZERO_ERROR;
     int32_t orderLength = 0;
-    int32_t *orders;
-    orders= getOrders(iter, &orderLength);
+    OrderAndOffset *orders = getOrders(iter, &orderLength);
 
 
     /* Now go through it backwards and make sure we get the same values */
 
 
     /* Now go through it backwards and make sure we get the same values */
-    index = orderLength;
+    idx = orderLength;
     ucol_reset(iter);
 
     /* synwee : changed */
     ucol_reset(iter);
 
     /* synwee : changed */
-    while ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER)
-    {
-        if (o != orders[-- index])
-        {
+    while ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER) {
+#if TEST_OFFSETS
+      int32_t offset = 
+#endif
+        ucol_getOffset(iter);
+
+      idx -= 1;
+      if (o != orders[idx].order) {
         if (o == 0)
         if (o == 0)
-          index ++;
-        else
-        {
-          while (index > 0 && orders[-- index] == 0)
-          {
+          idx ++;
+        else {
+          while (idx > 0 && orders[-- idx].order == 0) {
+            /* nothing... */
           }
           }
-          if (o != orders[index])
-          {
-            log_err("Mismatch at index : 0x%x\n", index);
-            return;
-        }
 
 
+          if (o != orders[idx].order) {
+              log_err("Mismatched order at index %d: 0x%8.8X vs. 0x%8.8X\n", idx,
+                orders[idx].order, o);
+            goto bail;
+          }
         }
       }
         }
       }
+
+#if TEST_OFFSETS
+      if (offset != orders[idx].offset) {
+        log_err("Mismatched offset at index %d: %d vs. %d\n", idx,
+            orders[idx].offset, offset);
+        goto bail;
+      }
+#endif
+
     }
 
     }
 
-    while (index != 0 && orders[index - 1] == 0) {
-      index --;
+    while (idx != 0 && orders[idx - 1].order == 0) {
+      idx -= 1;
     }
 
     }
 
-    if (index != 0)
-    {
-        log_err("Didn't get back to beginning - index is %d\n", index);
+    if (idx != 0) {
+        log_err("Didn't get back to beginning - index is %d\n", idx);
 
         ucol_reset(iter);
         log_err("\nnext: ");
 
         ucol_reset(iter);
         log_err("\nnext: ");
-        if ((o = ucol_next(iter, &status)) != UCOL_NULLORDER)
-        {
+
+        if ((o = ucol_next(iter, &status)) != UCOL_NULLORDER) {
             log_err("Error at %x\n", o);
         }
             log_err("Error at %x\n", o);
         }
+
         log_err("\nprev: ");
         log_err("\nprev: ");
-        if ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER)
-        {
+
+        if ((o = ucol_previous(iter, &status)) != UCOL_NULLORDER) {
             log_err("Error at %x\n", o);
         }
             log_err("Error at %x\n", o);
         }
+
         log_verbose("\n");
     }
 
         log_verbose("\n");
     }
 
+bail:
     free(orders);
 }
 
     free(orders);
 }
 
-void genericOrderingTestWithResult(UCollator *coll, const char *s[], uint32_t size, UCollationResult result) {
+void genericOrderingTestWithResult(UCollator *coll, const char * const s[], uint32_t size, UCollationResult result) {
   UChar t1[2048] = {0};
   UChar t2[2048] = {0};
   UCollationElements *iter;
   UChar t1[2048] = {0};
   UChar t2[2048] = {0};
   UCollationElements *iter;
@@ -571,11 +627,11 @@ void genericOrderingTestWithResult(UCollator *coll, const char *s[], uint32_t si
   ucol_closeElements(iter);
 }
 
   ucol_closeElements(iter);
 }
 
-void genericOrderingTest(UCollator *coll, const char *s[], uint32_t size) {
+void genericOrderingTest(UCollator *coll, const char * const s[], uint32_t size) {
   genericOrderingTestWithResult(coll, s, size, UCOL_LESS);
 }
 
   genericOrderingTestWithResult(coll, s, size, UCOL_LESS);
 }
 
-void genericLocaleStarter(const char *locale, const char *s[], uint32_t size) {
+void genericLocaleStarter(const char *locale, const char * const s[], uint32_t size) {
   UErrorCode status = U_ZERO_ERROR;
   UCollator *coll = ucol_open(locale, &status);
 
   UErrorCode status = U_ZERO_ERROR;
   UCollator *coll = ucol_open(locale, &status);
 
@@ -592,7 +648,7 @@ void genericLocaleStarter(const char *locale, const char *s[], uint32_t size) {
   ucol_close(coll);
 }
 
   ucol_close(coll);
 }
 
-void genericLocaleStarterWithResult(const char *locale, const char *s[], uint32_t size, UCollationResult result) {
+void genericLocaleStarterWithResult(const char *locale, const char * const s[], uint32_t size, UCollationResult result) {
   UErrorCode status = U_ZERO_ERROR;
   UCollator *coll = ucol_open(locale, &status);
 
   UErrorCode status = U_ZERO_ERROR;
   UCollator *coll = ucol_open(locale, &status);
 
@@ -610,7 +666,7 @@ void genericLocaleStarterWithResult(const char *locale, const char *s[], uint32_
 }
 
 /* currently not used with options */
 }
 
 /* currently not used with options */
-void genericRulesStarterWithOptionsAndResult(const char *rules, const char *s[], uint32_t size, const UColAttribute *attrs, const UColAttributeValue *values, uint32_t attsize, UCollationResult result) {
+void genericRulesStarterWithOptionsAndResult(const char *rules, const char * const s[], uint32_t size, const UColAttribute *attrs, const UColAttributeValue *values, uint32_t attsize, UCollationResult result) {
   UErrorCode status = U_ZERO_ERROR;
   UChar rlz[RULE_BUFFER_LEN] = { 0 };
   uint32_t rlen = u_unescape(rules, rlz, RULE_BUFFER_LEN);
   UErrorCode status = U_ZERO_ERROR;
   UChar rlz[RULE_BUFFER_LEN] = { 0 };
   uint32_t rlen = u_unescape(rules, rlz, RULE_BUFFER_LEN);
@@ -628,12 +684,12 @@ void genericRulesStarterWithOptionsAndResult(const char *rules, const char *s[],
 
     genericOrderingTestWithResult(coll, s, size, result);
   } else {
 
     genericOrderingTestWithResult(coll, s, size, result);
   } else {
-    log_err("Unable to open collator with rules %s\n", rules);
+    log_err_status(status, "Unable to open collator with rules %s\n", rules);
   }
   ucol_close(coll);
 }
 
   }
   ucol_close(coll);
 }
 
-void genericLocaleStarterWithOptionsAndResult(const char *locale, const char *s[], uint32_t size, const UColAttribute *attrs, const UColAttributeValue *values, uint32_t attsize, UCollationResult result) {
+void genericLocaleStarterWithOptionsAndResult(const char *locale, const char * const s[], uint32_t size, const UColAttribute *attrs, const UColAttributeValue *values, uint32_t attsize, UCollationResult result) {
   UErrorCode status = U_ZERO_ERROR;
   uint32_t i;
 
   UErrorCode status = U_ZERO_ERROR;
   uint32_t i;
 
@@ -650,16 +706,16 @@ void genericLocaleStarterWithOptionsAndResult(const char *locale, const char *s[
 
     genericOrderingTestWithResult(coll, s, size, result);
   } else {
 
     genericOrderingTestWithResult(coll, s, size, result);
   } else {
-    log_err("Unable to open collator for locale %s\n", locale);
+    log_err_status(status, "Unable to open collator for locale %s\n", locale);
   }
   ucol_close(coll);
 }
 
   }
   ucol_close(coll);
 }
 
-void genericLocaleStarterWithOptions(const char *locale, const char *s[], uint32_t size, const UColAttribute *attrs, const UColAttributeValue *values, uint32_t attsize) {
+void genericLocaleStarterWithOptions(const char *locale, const char * const s[], uint32_t size, const UColAttribute *attrs, const UColAttributeValue *values, uint32_t attsize) {
   genericLocaleStarterWithOptionsAndResult(locale, s, size, attrs, values, attsize, UCOL_LESS);
 }
 
   genericLocaleStarterWithOptionsAndResult(locale, s, size, attrs, values, attsize, UCOL_LESS);
 }
 
-void genericRulesStarterWithResult(const char *rules, const char *s[], uint32_t size, UCollationResult result) {
+void genericRulesStarterWithResult(const char *rules, const char * const s[], uint32_t size, UCollationResult result) {
   UErrorCode status = U_ZERO_ERROR;
   UChar rlz[RULE_BUFFER_LEN] = { 0 };
   uint32_t rlen = u_unescape(rules, rlz, RULE_BUFFER_LEN);
   UErrorCode status = U_ZERO_ERROR;
   UChar rlz[RULE_BUFFER_LEN] = { 0 };
   uint32_t rlen = u_unescape(rules, rlz, RULE_BUFFER_LEN);
@@ -678,24 +734,24 @@ void genericRulesStarterWithResult(const char *rules, const char *s[], uint32_t
   }
 }
 
   }
 }
 
-void genericRulesStarter(const char *rules, const char *s[], uint32_t size) {
+void genericRulesStarter(const char *rules, const char * const s[], uint32_t size) {
   genericRulesStarterWithResult(rules, s, size, UCOL_LESS);
 }
 
 static void TestTertiary()
 {
     int32_t len,i;
   genericRulesStarterWithResult(rules, s, size, UCOL_LESS);
 }
 
 static void TestTertiary()
 {
     int32_t len,i;
-    UChar *rules;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
-    const char* str="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    static const char str[]="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    UChar rules[sizeof(str)];
     len = strlen(str);
     len = strlen(str);
-    rules=(UChar*)malloc(sizeof(UChar*) * (len+1));
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
     if(U_FAILURE(status)){
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH, NULL, &status);
     if(U_FAILURE(status)){
-        log_err("ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        log_err_status(status, "ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        return;
     }
    
     ucol_setStrength(myCollation, UCOL_TERTIARY);
     }
    
     ucol_setStrength(myCollation, UCOL_TERTIARY);
@@ -703,7 +759,6 @@ static void TestTertiary()
     {
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
     {
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
-    free(rules);
     ucol_close(myCollation);
     myCollation = 0;
 }
     ucol_close(myCollation);
     myCollation = 0;
 }
@@ -711,17 +766,17 @@ static void TestTertiary()
 static void TestPrimary( )
 {
     int32_t len,i;
 static void TestPrimary( )
 {
     int32_t len,i;
-    UChar *rules;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
-    const char* str="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";   
+    static const char str[]="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";   
+    UChar rules[sizeof(str)];
     len = strlen(str);
     len = strlen(str);
-    rules=(UChar*)malloc(sizeof(UChar*) * (len+1));
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
     if(U_FAILURE(status)){
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
     if(U_FAILURE(status)){
-        log_err("ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        log_err_status(status, "ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        return;
     }
     ucol_setStrength(myCollation, UCOL_PRIMARY);
     
     }
     ucol_setStrength(myCollation, UCOL_PRIMARY);
     
@@ -730,7 +785,6 @@ static void TestPrimary( )
         
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
         
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
-    free(rules);
     ucol_close(myCollation);
     myCollation = 0;
 }
     ucol_close(myCollation);
     myCollation = 0;
 }
@@ -739,24 +793,23 @@ static void TestSecondary()
 {
     int32_t i;
     int32_t len;
 {
     int32_t i;
     int32_t len;
-    UChar *rules;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
-    const char* str="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    static const char str[]="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    UChar rules[sizeof(str)];
     len = strlen(str);
     len = strlen(str);
-    rules=(UChar*)malloc(sizeof(UChar*) * (len+1));
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
     if(U_FAILURE(status)){
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
     if(U_FAILURE(status)){
-        log_err("ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        log_err_status(status, "ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        return;
     }
     ucol_setStrength(myCollation, UCOL_SECONDARY);
     for (i = 26; i < 34 ; i++)
     {
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
     }
     ucol_setStrength(myCollation, UCOL_SECONDARY);
     for (i = 26; i < 34 ; i++)
     {
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
-    free(rules);
     ucol_close(myCollation);
     myCollation = 0;
 }
     ucol_close(myCollation);
     myCollation = 0;
 }
@@ -765,23 +818,22 @@ static void TestIdentical()
 {
     int32_t i;
     int32_t len;
 {
     int32_t i;
     int32_t len;
-    UChar *rules = 0;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
     UCollator *myCollation;
     UErrorCode status=U_ZERO_ERROR;
-    const char* str="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    static const char str[]="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    UChar rules[sizeof(str)];
     len = strlen(str);
     len = strlen(str);
-    rules=(UChar*)malloc(sizeof(UChar*) * (len+1));
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_IDENTICAL, NULL,&status);
     if(U_FAILURE(status)){
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_IDENTICAL, NULL,&status);
     if(U_FAILURE(status)){
-        log_err("ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        log_err_status(status, "ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        return;
     }
     for(i= 34; i<37; i++)
     {
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
     }
     for(i= 34; i<37; i++)
     {
         doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
     }
-    free(rules);
     ucol_close(myCollation);
     myCollation = 0;
 }
     ucol_close(myCollation);
     myCollation = 0;
 }
@@ -790,17 +842,17 @@ static void TestExtra()
 {
     int32_t i, j;
     int32_t len;
 {
     int32_t i, j;
     int32_t len;
-    UChar *rules;
     UCollator *myCollation;
     UErrorCode status = U_ZERO_ERROR;
     UCollator *myCollation;
     UErrorCode status = U_ZERO_ERROR;
-    const char* str="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    static const char str[]="& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
+    UChar rules[sizeof(str)];
     len = strlen(str);
     len = strlen(str);
-    rules=(UChar*)malloc(sizeof(UChar*) * (len+1));
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
     if(U_FAILURE(status)){
     u_uastrcpy(rules, str);
 
     myCollation=ucol_openRules(rules, len, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status);
     if(U_FAILURE(status)){
-        log_err("ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        log_err_status(status, "ERROR: in creation of rule based collator :%s\n", myErrorName(status));
+        return;
     }
     ucol_setStrength(myCollation, UCOL_TERTIARY);
     for (i = 0; i < COUNT_TEST_CASES-1 ; i++)
     }
     ucol_setStrength(myCollation, UCOL_TERTIARY);
     for (i = 0; i < COUNT_TEST_CASES-1 ; i++)
@@ -811,14 +863,12 @@ static void TestExtra()
             doTest(myCollation, testCases[i], testCases[j], UCOL_LESS);
         }
     }
             doTest(myCollation, testCases[i], testCases[j], UCOL_LESS);
         }
     }
-    free(rules);
     ucol_close(myCollation);
     myCollation = 0;
 }
 
 static void TestJB581(void)
 {
     ucol_close(myCollation);
     myCollation = 0;
 }
 
 static void TestJB581(void)
 {
-    UChar       dispName    [100]; 
     int32_t     bufferLen   = 0;
     UChar       source      [100];
     UChar       target      [100];
     int32_t     bufferLen   = 0;
     UChar       source      [100];
     UChar       target      [100];
@@ -837,9 +887,7 @@ static void TestJB581(void)
 
     myCollator = ucol_open("en_US", &status);
     if (U_FAILURE(status)){
 
     myCollator = ucol_open("en_US", &status);
     if (U_FAILURE(status)){
-        bufferLen = uloc_getDisplayName("en_US", 0, dispName, 100, &status);
-        /*Report the error with display name... */
-        log_err("ERROR: Failed to create the collator for : \"%s\"\n", dispName);
+        log_err_status(status, "ERROR: Failed to create the collator : %s\n", u_errorName(status));
         return;
     }
     result = ucol_strcoll(myCollator, source, -1, target, -1);
         return;
     }
     result = ucol_strcoll(myCollator, source, -1, target, -1);
@@ -858,6 +906,7 @@ static void TestJB581(void)
     }
     /* Now, do the same comparison with keys */
     sourceKeyOut = ucol_getSortKey(myCollator, source, -1, sourceKeyArray, 100);
     }
     /* Now, do the same comparison with keys */
     sourceKeyOut = ucol_getSortKey(myCollator, source, -1, sourceKeyArray, 100);
+    (void)sourceKeyOut;    /* Suppress set but not used warning. */
     targetKeyOut = ucol_getSortKey(myCollator, target, -1, targetKeyArray, 100);
     bufferLen = ((targetKeyOut > 100) ? 100 : targetKeyOut);
     if (memcmp(sourceKeyArray, targetKeyArray, bufferLen) != 0)
     targetKeyOut = ucol_getSortKey(myCollator, target, -1, targetKeyArray, 100);
     bufferLen = ((targetKeyOut > 100) ? 100 : targetKeyOut);
     if (memcmp(sourceKeyArray, targetKeyArray, bufferLen) != 0)
@@ -882,11 +931,7 @@ static void TestJB1401(void)
     
     myCollator = ucol_open("en_US", &status);
     if (U_FAILURE(status)){
     
     myCollator = ucol_open("en_US", &status);
     if (U_FAILURE(status)){
-        int32_t     bufferLen   = 0;
-        UChar       dispName    [100]; 
-        bufferLen = uloc_getDisplayName("en_US", 0, dispName, 100, &status);
-        /*Report the error with display name... */
-        log_err("ERROR: Failed to create the collator for : \"%s\"\n", dispName);
+        log_err_status(status, "ERROR: Failed to create the collator : %s\n", u_errorName(status));
         return;
     }
     ucol_setAttribute(myCollator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
         return;
     }
     ucol_setAttribute(myCollator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
@@ -972,9 +1017,16 @@ static void TestJB1401(void)
 */
 static void TestVariableTop(void)
 {
 */
 static void TestVariableTop(void)
 {
-    const char       *str          = "&z = [variable top]";
+#if 0
+    /*
+     * Starting with ICU 53, setting the variable top via a pseudo relation string
+     * is not supported any more.
+     * It was replaced by the [maxVariable symbol] setting.
+     * See ICU tickets #9958 and #8032.
+     */
+    static const char       str[]          = "&z = [variable top]";
           int         len          = strlen(str);
           int         len          = strlen(str);
-          UChar      *rules;
+          UChar      rules[sizeof(str)];
           UCollator  *myCollation;
           UCollator  *enCollation;
           UErrorCode  status       = U_ZERO_ERROR;
           UCollator  *myCollation;
           UCollator  *enCollation;
           UErrorCode  status       = U_ZERO_ERROR;
@@ -983,13 +1035,18 @@ static void TestVariableTop(void)
           uint8_t     result[20];
           uint8_t     expected[20];
 
           uint8_t     result[20];
           uint8_t     expected[20];
 
-    rules = (UChar*)malloc(sizeof(UChar*) * (len + 1));
     u_uastrcpy(rules, str);
 
     enCollation = ucol_open("en_US", &status);
     u_uastrcpy(rules, str);
 
     enCollation = ucol_open("en_US", &status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "ERROR: in creation of collator :%s\n", 
+                myErrorName(status));
+        return;
+    }
     myCollation = ucol_openRules(rules, len, UCOL_OFF, 
                                  UCOL_PRIMARY,NULL, &status);
     if (U_FAILURE(status)) {
     myCollation = ucol_openRules(rules, len, UCOL_OFF, 
                                  UCOL_PRIMARY,NULL, &status);
     if (U_FAILURE(status)) {
+        ucol_close(enCollation);
         log_err("ERROR: in creation of rule based collator :%s\n", 
                 myErrorName(status));
         return;
         log_err("ERROR: in creation of rule based collator :%s\n", 
                 myErrorName(status));
         return;
@@ -1029,11 +1086,11 @@ static void TestVariableTop(void)
         ch ++;
     }
   
         ch ++;
     }
   
-    free(rules);
     ucol_close(enCollation);
     ucol_close(myCollation);
     enCollation = NULL;
     myCollation = NULL;
     ucol_close(enCollation);
     ucol_close(myCollation);
     enCollation = NULL;
     myCollation = NULL;
+#endif
 }
 
 /**
 }
 
 /**
@@ -1043,11 +1100,11 @@ static void TestVariableTop(void)
   */
 static void TestSurrogates(void)
 {
   */
 static void TestSurrogates(void)
 {
-    const char       *str          = 
+    static const char       str[]          = 
                               "&z<'\\uD800\\uDC00'<'\\uD800\\uDC0A\\u0308'<A";
           int         len          = strlen(str);
           int         rlen         = 0;
                               "&z<'\\uD800\\uDC00'<'\\uD800\\uDC0A\\u0308'<A";
           int         len          = strlen(str);
           int         rlen         = 0;
-          UChar      *rules;
+          UChar      rules[sizeof(str)];
           UCollator  *myCollation;
           UCollator  *enCollation;
           UErrorCode  status       = U_ZERO_ERROR;
           UCollator  *myCollation;
           UCollator  *enCollation;
           UErrorCode  status       = U_ZERO_ERROR;
@@ -1060,13 +1117,18 @@ static void TestSurrogates(void)
           int enlen, mylen;
           
     /* tests for open rules with surrogate rules */
           int enlen, mylen;
           
     /* tests for open rules with surrogate rules */
-    rules = (UChar*)malloc(sizeof(UChar*) * (len + 1));
     rlen = u_unescape(str, rules, len);
     
     enCollation = ucol_open("en_US", &status);
     rlen = u_unescape(str, rules, len);
     
     enCollation = ucol_open("en_US", &status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "ERROR: in creation of collator :%s\n", 
+                myErrorName(status));
+        return;
+    }
     myCollation = ucol_openRules(rules, rlen, UCOL_OFF, 
                                  UCOL_TERTIARY,NULL, &status);
     if (U_FAILURE(status)) {
     myCollation = ucol_openRules(rules, rlen, UCOL_OFF, 
                                  UCOL_TERTIARY,NULL, &status);
     if (U_FAILURE(status)) {
+        ucol_close(enCollation);
         log_err("ERROR: in creation of rule based collator :%s\n", 
                 myErrorName(status));
         return;
         log_err("ERROR: in creation of rule based collator :%s\n", 
                 myErrorName(status));
         return;
@@ -1099,7 +1161,6 @@ static void TestSurrogates(void)
         log_verbose("Failed : non-tailored supplementary characters should have the same value\n");
     }
 
         log_verbose("Failed : non-tailored supplementary characters should have the same value\n");
     }
 
-    free(rules);
     ucol_close(enCollation);
     ucol_close(myCollation);
     enCollation = NULL;
     ucol_close(enCollation);
     ucol_close(myCollation);
     enCollation = NULL;
@@ -1119,13 +1180,13 @@ TestInvalidRules(){
         "& C < ch, cH, & Ch[variable top]"
     };
     static const char* preContextArr[MAX_ERROR_STATES] = {
         "& C < ch, cH, & Ch[variable top]"
     };
     static const char* preContextArr[MAX_ERROR_STATES] = {
-        "his should fail",
-        "& C < ch, cH",
+        " C < ch, cH, Ch",
+        "& C < ch, cH",
 
     };
     static const char* postContextArr[MAX_ERROR_STATES] = {
 
     };
     static const char* postContextArr[MAX_ERROR_STATES] = {
-        "<d",
-        " Ch[variable t"
+        "[this should fa",
+        ", & Ch[variable"
     };
     int i;
 
     };
     int i;
 
@@ -1144,11 +1205,14 @@ TestInvalidRules(){
         u_memset(parseError.postContext,0x0000,U_PARSE_CONTEXT_LEN);
         /* open the rules and test */
         coll = ucol_openRules(rules,u_strlen(rules),UCOL_OFF,UCOL_DEFAULT_STRENGTH,&parseError,&status);
         u_memset(parseError.postContext,0x0000,U_PARSE_CONTEXT_LEN);
         /* open the rules and test */
         coll = ucol_openRules(rules,u_strlen(rules),UCOL_OFF,UCOL_DEFAULT_STRENGTH,&parseError,&status);
+        (void)coll;   /* Suppress set but not used warning. */
         if(u_strcmp(parseError.preContext,preContextExp)!=0){
         if(u_strcmp(parseError.preContext,preContextExp)!=0){
-            log_err("preContext in UParseError for ucol_openRules does not match\n");
+            log_err_status(status, "preContext in UParseError for ucol_openRules does not match: \"%s\"\n",
+                           aescstrdup(parseError.preContext, -1));
         }
         if(u_strcmp(parseError.postContext,postContextExp)!=0){
         }
         if(u_strcmp(parseError.postContext,postContextExp)!=0){
-            log_err("postContext in UParseError for ucol_openRules does not match\n");
+            log_err_status(status, "postContext in UParseError for ucol_openRules does not match: \"%s\"\n",
+                           aescstrdup(parseError.postContext, -1));
         }
     }  
 }
         }
     }  
 }
@@ -1167,7 +1231,7 @@ TestJitterbug1098(){
          "&\\'<\\\\",
          "&\\\"<'\\'",
          "&'\"'<\\'",
          "&\\'<\\\\",
          "&\\\"<'\\'",
          "&'\"'<\\'",
-         '\0'
+         NULL
 
     };
     const UCollationResult results1098[] = {
 
     };
     const UCollationResult results1098[] = {
@@ -1190,7 +1254,7 @@ TestJitterbug1098(){
         u_uastrcpy(rule, rules[i]);
         c1 = ucol_openRules(rule, u_strlen(rule), UCOL_OFF, UCOL_DEFAULT_STRENGTH, &parseError, &status);
         if(U_FAILURE(status)){
         u_uastrcpy(rule, rules[i]);
         c1 = ucol_openRules(rule, u_strlen(rule), UCOL_OFF, UCOL_DEFAULT_STRENGTH, &parseError, &status);
         if(U_FAILURE(status)){
-            log_err("Could not parse the rules syntax. Error: %s ", u_errorName(status));
+            log_err_status(status, "Could not parse the rules syntax. Error: %s\n", u_errorName(status));
 
             if (status == U_PARSE_ERROR) {
                 u_UCharsToChars(parseError.preContext,preContext,20);
 
             if (status == U_PARSE_ERROR) {
                 u_UCharsToChars(parseError.preContext,preContext,20);
@@ -1207,21 +1271,6 @@ TestJitterbug1098(){
     }
 }
 
     }
 }
 
-/*  These tests do cleanup and reinitialize ICU in the course of their operation.
- *    The ICU data directory must be preserved across these operations.
- *    Here is a helper function to assist with that.
- */
-static char *safeGetICUDataDirectory() {
-    const char *dataDir = u_getDataDirectory();  /* Returned string vanashes with u_cleanup */
-    char *retStr = NULL;
-    if (dataDir != NULL) {
-        retStr = (char *)malloc(strlen(dataDir)+1);
-        strcpy(retStr, dataDir);
-    }
-    return retStr;
-}
-
-
 static void
 TestFCDCrash(void) {
     static const char *test[] = {
 static void
 TestFCDCrash(void) {
     static const char *test[] = {
@@ -1229,27 +1278,104 @@ TestFCDCrash(void) {
     "Grossist"
     };
 
     "Grossist"
     };
 
-    char *icuDataDir = safeGetICUDataDirectory();
     UErrorCode status = U_ZERO_ERROR;
     UCollator *coll = ucol_open("es", &status);
     if(U_FAILURE(status)) {
     UErrorCode status = U_ZERO_ERROR;
     UCollator *coll = ucol_open("es", &status);
     if(U_FAILURE(status)) {
-        log_err("Couldn't open collator\n");
+        log_err_status(status, "Couldn't open collator -> %s\n", u_errorName(status));
         return;
     }
     ucol_close(coll);
     coll = NULL;
         return;
     }
     ucol_close(coll);
     coll = NULL;
-    u_cleanup();
-    u_setDataDirectory(icuDataDir);
+    ctest_resetICU();
     coll = ucol_open("de_DE", &status);
     if(U_FAILURE(status)) {
     coll = ucol_open("de_DE", &status);
     if(U_FAILURE(status)) {
-        log_err("Couldn't open collator\n");
+        log_err_status(status, "Couldn't open collator -> %s\n", u_errorName(status));
         return;
     }
     ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
     genericOrderingTest(coll, test, 2);
     ucol_close(coll);
         return;
     }
     ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
     genericOrderingTest(coll, test, 2);
     ucol_close(coll);
-    free(icuDataDir);
+}
+
+/*static UBool
+find(UEnumeration* list, const char* str, UErrorCode* status){
+    const char* value = NULL;
+    int32_t length=0;
+    if(U_FAILURE(*status)){
+        return FALSE;
+    }
+    uenum_reset(list, status);
+    while( (value= uenum_next(list, &length, status))!=NULL){
+        if(strcmp(value, str)==0){
+            return TRUE;
+        }
+    }
+    return FALSE;
+}*/
+
+static void TestJ5298(void)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    char input[256], output[256];
+    UBool isAvailable;
+    int32_t i = 0;
+    UEnumeration* values = NULL;
+    const char *keywordValue = NULL;
+    log_verbose("Number of collator locales returned : %i \n", ucol_countAvailable());
+    values = ucol_getKeywordValues("collation", &status);
+    while ((keywordValue = uenum_next(values, NULL, &status)) != NULL) {
+        if (strncmp(keywordValue, "private-", 8) == 0) {
+            log_err("ucol_getKeywordValues() returns private collation keyword: %s\n", keywordValue);
+        }
+    }
+    for (i = 0; i < ucol_countAvailable(); i++) {
+        uenum_reset(values, &status);
+        while ((keywordValue = uenum_next(values, NULL, &status)) != NULL) {
+            strcpy(input, ucol_getAvailable(i));
+            if (strcmp(keywordValue, "standard") != 0) {
+                strcat(input, "@collation=");
+                strcat(input, keywordValue);
+            }
 
 
+            ucol_getFunctionalEquivalent(output, 256, "collation", input, &isAvailable, &status);
+            if (strcmp(input, output) == 0) { /* Unique locale, print it out */
+                log_verbose("%s, \n", output);
+            }
+        }
+    }
+    uenum_close(values);
+    log_verbose("\n");
 }
 
 }
 
+static const char* badKeyLocales[] = {
+       "@calendar=japanese;collation=search", // OK
+       "@calendar=japanese", // OK
+       "en@calendar=x", // OK
+       "ja@calendar=x", // OK
+       "en@collation=x", // OK
+       "ja@collation=x", // OK
+       "en@calendar=\x81", // OK
+       "ja@collation=private-kana", // fail, this string is cause of <rdar://problem/40930320>
+       "en@collation=\x81", // fail
+       "ja@calendar=japanese;collation=\x81", // fail
+       NULL
+};
+
+static void TestBadKey(void)
+{
+    const char* badLoc;
+    const char** badLocsPtr = badKeyLocales;
+    while ((badLoc = *badLocsPtr++) != NULL) {
+        UErrorCode status = U_ZERO_ERROR;
+        UCollator* uc = ucol_open(badLoc, &status);
+        if ( U_SUCCESS(status) ) {
+            if (uc == NULL) {
+                log_err("ucol_open sets SUCCESS but returns NULL, locale: %s\n", badLoc);
+            }
+            ucol_close(uc);
+        } else if (uc != NULL) {
+            log_err("ucol_open sets FAILURE but returns non-NULL, locale: %s\n", badLoc);
+        }
+    }
+}
 #endif /* #if !UCONFIG_NO_COLLATION */
 #endif /* #if !UCONFIG_NO_COLLATION */