]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/cintltst/cmsccoll.c
ICU-461.12.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cmsccoll.c
index 8b193195512d6e14e027344bf37e441b027ee55a..f87b1eabd759563a071dd651e2a71aeb6652d2f7 100644 (file)
@@ -1,6 +1,7 @@
+
 /********************************************************************
  * COPYRIGHT:
- * Copyright (c) 2001-2006, International Business Machines Corporation and
+ * Copyright (c) 2001-2011, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 /*******************************************************************************
 #include "uassert.h"
 #include "unicode/parseerr.h"
 #include "unicode/ucnv.h"
+#include "unicode/ures.h"
+#include "unicode/uscript.h"
 #include "uparse.h"
+#include "putilimp.h"
+
 
 #define LEN(a) (sizeof(a)/sizeof(a[0]))
 
 #define MAX_TOKEN_LEN 16
 
-typedef int tst_strcoll(void *collator, const int object,
+typedef UCollationResult tst_strcoll(void *collator, const int object,
                         const UChar *source, const int sLen,
                         const UChar *target, const int tLen);
 
@@ -153,7 +158,7 @@ const static char shifted[][20] = {
 };
 
 const static UCollationResult shiftedTert[] = {
-  0,
+  UCOL_EQUAL,
   UCOL_EQUAL,
   UCOL_EQUAL,
   UCOL_LESS,
@@ -260,7 +265,7 @@ static void FunkyATest(void)
     UCollator  *myCollation;
     myCollation = ucol_open("en_US", &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;
     }
     log_verbose("Testing some A letters, for some reason\n");
@@ -393,7 +398,7 @@ static void BillFairmanTest(void) {
     if (lr) {
         cr = ures_getByKey(lr,"collations",0,&lec);
         if (cr) {
-            lp = ures_getLocale(cr,&lec);
+            lp = ures_getLocaleByType(cr, ULOC_ACTUAL_LOCALE, &lec);
             if (lp) {
                 if (U_SUCCESS(lec)) {
                     if(strcmp(lp, "fr") != 0) {
@@ -547,11 +552,14 @@ static void testCollator(UCollator *coll, UErrorCode *status) {
   uint32_t tempLen;
   UChar *rulesCopy = NULL;
   UParseError parseError;
+
+  uprv_memset(&src, 0, sizeof(UColTokenParser));
+
   src.opts = &opts;
 
   rules = ucol_getRules(coll, &ruleLen);
   if(U_SUCCESS(*status) && ruleLen > 0) {
-    rulesCopy = (UChar *)malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
+    rulesCopy = (UChar *)uprv_malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
     uprv_memcpy(rulesCopy, rules, ruleLen*sizeof(UChar));
     src.current = src.source = rulesCopy;
     src.end = rulesCopy+ruleLen;
@@ -559,6 +567,8 @@ static void testCollator(UCollator *coll, UErrorCode *status) {
     src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
     *first = *second = 0;
 
+       /* Note that as a result of tickets 7015 or 6912, ucol_tok_parseNextToken can cause the pointer to
+          the rules copy in src.source to get reallocated, freeing the original pointer in rulesCopy */
     while ((current = ucol_tok_parseNextToken(&src, startOfRules,&parseError, status)) != NULL) {
       strength = src.parsedToken.strength;
       chOffset = src.parsedToken.charsOffset;
@@ -575,17 +585,17 @@ static void testCollator(UCollator *coll, UErrorCode *status) {
       if(top_) { /* if reset is on top, the sequence is broken. We should have an empty string */
         second[0] = 0;
       } else {
-        u_strncpy(second,rulesCopy+chOffset, chLen);
+        u_strncpy(second,src.source+chOffset, chLen);
         second[chLen] = 0;
 
         if(exLen > 0 && firstEx == 0) {
-          u_strncat(first, rulesCopy+exOffset, exLen);
+          u_strncat(first, src.source+exOffset, exLen);
           first[firstLen+exLen] = 0;
         }
 
         if(lastReset == TRUE && prefixLen != 0) {
           u_strncpy(first+prefixLen, first, firstLen);
-          u_strncpy(first, rulesCopy+prefixOffset, prefixLen);
+          u_strncpy(first, src.source+prefixOffset, prefixLen);
           first[firstLen+prefixLen] = 0;
           firstLen = firstLen+prefixLen;
         }
@@ -641,20 +651,20 @@ static void testCollator(UCollator *coll, UErrorCode *status) {
         u_strcpy(first, second);
       }
     }
-    free(rulesCopy);
+    uprv_free(src.source);
   }
 }
 
-static int ucaTest(void *collator, const int object, const UChar *source, const int sLen, const UChar *target, const int tLen) {
+static UCollationResult ucaTest(void *collator, const int object, const UChar *source, const int sLen, const UChar *target, const int tLen) {
   UCollator *UCA = (UCollator *)collator;
   return ucol_strcoll(UCA, source, sLen, target, tLen);
 }
 
 /*
-static int winTest(void *collator, const int object, const UChar *source, const int sLen, const UChar *target, const int tLen) {
+static UCollationResult winTest(void *collator, const int object, const UChar *source, const int sLen, const UChar *target, const int tLen) {
 #ifdef U_WINDOWS
   LCID lcid = (LCID)collator;
-  return CompareString(lcid, 0, source, sLen, target, tLen);
+  return (UCollationResult)CompareString(lcid, 0, source, sLen, target, tLen);
 #else
   return 0;
 #endif
@@ -766,7 +776,7 @@ static void logFailure (const char *platform, const char *test,
   *sEsc = *tEsc = *s = *t = 0;
   if(error == TRUE) {
     log_err("Difference between expected and generated order. Run test with -v for more info\n");
-  } else if(VERBOSITY == 0) {
+  } else if(getTestOption(VERBOSITY_OPTION) == 0) {
     return;
   }
   for(i = 0; i<sLen; i++) {
@@ -925,6 +935,7 @@ static void testAgainstUCA(UCollator *coll, UCollator *UCA, const char *refName,
   uint32_t Windiff = 1;
   UParseError parseError;
 
+  uprv_memset(&src, 0, sizeof(UColTokenParser));
   src.opts = &opts;
 
   rules = ucol_getRules(coll, &ruleLen);
@@ -932,7 +943,7 @@ static void testAgainstUCA(UCollator *coll, UCollator *UCA, const char *refName,
   /*printOutRules(rules);*/
 
   if(U_SUCCESS(*status) && ruleLen > 0) {
-    rulesCopy = (UChar *)malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
+    rulesCopy = (UChar *)uprv_malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
     uprv_memcpy(rulesCopy, rules, ruleLen*sizeof(UChar));
     src.current = src.source = rulesCopy;
     src.end = rulesCopy+ruleLen;
@@ -940,6 +951,8 @@ static void testAgainstUCA(UCollator *coll, UCollator *UCA, const char *refName,
     src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
     *first = *second = 0;
 
+    /* Note that as a result of tickets 7015 or 6912, ucol_tok_parseNextToken can cause the pointer to
+       the rules copy in src.source to get reallocated, freeing the original pointer in rulesCopy */
     while ((current = ucol_tok_parseNextToken(&src, startOfRules, &parseError,status)) != NULL) {
       strength = src.parsedToken.strength;
       chOffset = src.parsedToken.charsOffset;
@@ -954,12 +967,12 @@ static void testAgainstUCA(UCollator *coll, UCollator *UCA, const char *refName,
       varT = (UBool)((specs & UCOL_TOK_VARIABLE_TOP) != 0);
       top_ = (UBool)((specs & UCOL_TOK_TOP) != 0);
 
-      u_strncpy(second,rulesCopy+chOffset, chLen);
+      u_strncpy(second,src.source+chOffset, chLen);
       second[chLen] = 0;
       secondLen = chLen;
 
       if(exLen > 0) {
-        u_strncat(first, rulesCopy+exOffset, exLen);
+        u_strncat(first, src.source+exOffset, exLen);
         first[firstLen+exLen] = 0;
         firstLen += exLen;
       }
@@ -985,7 +998,7 @@ static void testAgainstUCA(UCollator *coll, UCollator *UCA, const char *refName,
     if(Windiff == 0) {
       log_verbose("No immediate difference with Win32!\n");
     }
-    free(rulesCopy);
+    uprv_free(src.source);
   }
 }
 
@@ -1050,208 +1063,232 @@ typedef struct {
 static indirectBoundaries ucolIndirectBoundaries[15];
 static UBool indirectBoundariesSet = FALSE;
 static void setIndirectBoundaries(uint32_t indexR, uint32_t *start, uint32_t *end) {
-
-  /* Set values for the top - TODO: once we have values for all the indirects, we are going */
-  /* to initalize here. */
-  ucolIndirectBoundaries[indexR].startCE = start[0];
-  ucolIndirectBoundaries[indexR].startContCE = start[1];
-  if(end) {
-    ucolIndirectBoundaries[indexR].limitCE = end[0];
-    ucolIndirectBoundaries[indexR].limitContCE = end[1];
-  } else {
-    ucolIndirectBoundaries[indexR].limitCE = 0;
-    ucolIndirectBoundaries[indexR].limitContCE = 0;
-  }
+    /* Set values for the top - TODO: once we have values for all the indirects, we are going */
+    /* to initalize here. */
+    ucolIndirectBoundaries[indexR].startCE = start[0];
+    ucolIndirectBoundaries[indexR].startContCE = start[1];
+    if(end) {
+        ucolIndirectBoundaries[indexR].limitCE = end[0];
+        ucolIndirectBoundaries[indexR].limitContCE = end[1];
+    } else {
+        ucolIndirectBoundaries[indexR].limitCE = 0;
+        ucolIndirectBoundaries[indexR].limitContCE = 0;
+    }
 }
 
 static void testCEs(UCollator *coll, UErrorCode *status) {
+    const UChar *rules = NULL, *current = NULL;
+    int32_t ruleLen = 0;
+
+    uint32_t strength = 0;
+    uint32_t maxStrength = UCOL_IDENTICAL;
+    uint32_t baseCE, baseContCE, nextCE, nextContCE, currCE, currContCE;
+    uint32_t lastCE;
+    uint32_t lastContCE;
+
+    int32_t result = 0;
+    uint32_t chOffset = 0; uint32_t chLen = 0;
+    uint32_t exOffset = 0; uint32_t exLen = 0;
+    uint32_t prefixOffset = 0; uint32_t prefixLen = 0;
+    uint32_t oldOffset = 0;
+
+    /* uint32_t rExpsLen = 0; */
+    /* uint32_t firstLen = 0; */
+    uint16_t specs = 0;
+    UBool varT = FALSE; UBool top_ = TRUE;
+    UBool startOfRules = TRUE;
+    UBool before = FALSE;
+    UColTokenParser src;
+    UColOptionSet opts;
+    UParseError parseError;
+    UChar *rulesCopy = NULL;
+    collIterate *c = uprv_new_collIterate(status);
+    UCAConstants *consts = NULL;
+    uint32_t UCOL_RESET_TOP_VALUE, /*UCOL_RESET_TOP_CONT, */
+        UCOL_NEXT_TOP_VALUE, UCOL_NEXT_TOP_CONT;
+    const char *colLoc;
+    UCollator *UCA = ucol_open("root", status);
+
+    if (U_FAILURE(*status)) {
+        log_err("Could not open root collator %s\n", u_errorName(*status));
+        uprv_delete_collIterate(c);
+        return;
+    }
 
-  const UChar *rules = NULL, *current = NULL;
-  int32_t ruleLen = 0;
-
-  uint32_t strength = 0;
-  uint32_t maxStrength = UCOL_IDENTICAL;
-  uint32_t baseCE, baseContCE, nextCE, nextContCE, currCE, currContCE;
-  uint32_t lastCE;
-  uint32_t lastContCE;
-
-  int32_t result = 0;
-  uint32_t chOffset = 0; uint32_t chLen = 0;
-  uint32_t exOffset = 0; uint32_t exLen = 0;
-  uint32_t prefixOffset = 0; uint32_t prefixLen = 0;
-  uint32_t oldOffset = 0;
-
-  /* uint32_t rExpsLen = 0; */
-  /* uint32_t firstLen = 0; */
-  uint16_t specs = 0;
-  UBool varT = FALSE; UBool top_ = TRUE;
-  UBool startOfRules = TRUE;
-  UBool before = FALSE;
-  UColTokenParser src;
-  UColOptionSet opts;
-  UParseError parseError;
-  UChar *rulesCopy = NULL;
-  collIterate c;
-  UCollator *UCA = ucol_open("root", status);
-  UCAConstants *consts = (UCAConstants *)((uint8_t *)UCA->image + UCA->image->UCAConsts);
-  uint32_t UCOL_RESET_TOP_VALUE = consts->UCA_LAST_NON_VARIABLE[0], /*UCOL_RESET_TOP_CONT = consts->UCA_LAST_NON_VARIABLE[1], */
-           UCOL_NEXT_TOP_VALUE = consts->UCA_FIRST_IMPLICIT[0], UCOL_NEXT_TOP_CONT = consts->UCA_FIRST_IMPLICIT[1];
-
-  baseCE=baseContCE=nextCE=nextContCE=currCE=currContCE=lastCE=lastContCE = UCOL_NOT_FOUND;
-
-  src.opts = &opts;
-
-  rules = ucol_getRules(coll, &ruleLen);
-
-  src.invUCA = ucol_initInverseUCA(status);
-
-  if(indirectBoundariesSet == FALSE) {
-    /* UCOL_RESET_TOP_VALUE */
-    setIndirectBoundaries(0, consts->UCA_LAST_NON_VARIABLE, consts->UCA_FIRST_IMPLICIT);
-    /* UCOL_FIRST_PRIMARY_IGNORABLE */
-    setIndirectBoundaries(1, consts->UCA_FIRST_PRIMARY_IGNORABLE, 0);
-    /* UCOL_LAST_PRIMARY_IGNORABLE */
-    setIndirectBoundaries(2, consts->UCA_LAST_PRIMARY_IGNORABLE, 0);
-    /* UCOL_FIRST_SECONDARY_IGNORABLE */
-    setIndirectBoundaries(3, consts->UCA_FIRST_SECONDARY_IGNORABLE, 0);
-    /* UCOL_LAST_SECONDARY_IGNORABLE */
-    setIndirectBoundaries(4, consts->UCA_LAST_SECONDARY_IGNORABLE, 0);
-    /* UCOL_FIRST_TERTIARY_IGNORABLE */
-    setIndirectBoundaries(5, consts->UCA_FIRST_TERTIARY_IGNORABLE, 0);
-    /* UCOL_LAST_TERTIARY_IGNORABLE */
-    setIndirectBoundaries(6, consts->UCA_LAST_TERTIARY_IGNORABLE, 0);
-    /* UCOL_FIRST_VARIABLE */
-    setIndirectBoundaries(7, consts->UCA_FIRST_VARIABLE, 0);
-    /* UCOL_LAST_VARIABLE */
-    setIndirectBoundaries(8, consts->UCA_LAST_VARIABLE, 0);
-    /* UCOL_FIRST_NON_VARIABLE */
-    setIndirectBoundaries(9, consts->UCA_FIRST_NON_VARIABLE, 0);
-    /* UCOL_LAST_NON_VARIABLE */
-    setIndirectBoundaries(10, consts->UCA_LAST_NON_VARIABLE, consts->UCA_FIRST_IMPLICIT);
-    /* UCOL_FIRST_IMPLICIT */
-    setIndirectBoundaries(11, consts->UCA_FIRST_IMPLICIT, 0);
-    /* UCOL_LAST_IMPLICIT */
-    setIndirectBoundaries(12, consts->UCA_LAST_IMPLICIT, consts->UCA_FIRST_TRAILING);
-    /* UCOL_FIRST_TRAILING */
-    setIndirectBoundaries(13, consts->UCA_FIRST_TRAILING, 0);
-    /* UCOL_LAST_TRAILING */
-    setIndirectBoundaries(14, consts->UCA_LAST_TRAILING, 0);
-    ucolIndirectBoundaries[14].limitCE = (consts->UCA_PRIMARY_SPECIAL_MIN<<24);
-    indirectBoundariesSet = TRUE;
-  }
+    colLoc = ucol_getLocaleByType(coll, ULOC_ACTUAL_LOCALE, status);
+    if (U_FAILURE(*status)) {
+        log_err("Could not get collator name: %s\n", u_errorName(*status));
+        ucol_close(UCA);
+        uprv_delete_collIterate(c);
+        return;
+    }
 
+    uprv_memset(&src, 0, sizeof(UColTokenParser));
+
+    consts = (UCAConstants *)((uint8_t *)UCA->image + UCA->image->UCAConsts);
+    UCOL_RESET_TOP_VALUE = consts->UCA_LAST_NON_VARIABLE[0];
+    /*UCOL_RESET_TOP_CONT = consts->UCA_LAST_NON_VARIABLE[1]; */
+    UCOL_NEXT_TOP_VALUE = consts->UCA_FIRST_IMPLICIT[0];
+    UCOL_NEXT_TOP_CONT = consts->UCA_FIRST_IMPLICIT[1];
+
+    baseCE=baseContCE=nextCE=nextContCE=currCE=currContCE=lastCE=lastContCE = UCOL_NOT_FOUND;
+
+    src.opts = &opts;
+
+    rules = ucol_getRules(coll, &ruleLen);
+
+    src.invUCA = ucol_initInverseUCA(status);
+
+    if(indirectBoundariesSet == FALSE) {
+        /* UCOL_RESET_TOP_VALUE */
+        setIndirectBoundaries(0, consts->UCA_LAST_NON_VARIABLE, consts->UCA_FIRST_IMPLICIT);
+        /* UCOL_FIRST_PRIMARY_IGNORABLE */
+        setIndirectBoundaries(1, consts->UCA_FIRST_PRIMARY_IGNORABLE, 0);
+        /* UCOL_LAST_PRIMARY_IGNORABLE */
+        setIndirectBoundaries(2, consts->UCA_LAST_PRIMARY_IGNORABLE, 0);
+        /* UCOL_FIRST_SECONDARY_IGNORABLE */
+        setIndirectBoundaries(3, consts->UCA_FIRST_SECONDARY_IGNORABLE, 0);
+        /* UCOL_LAST_SECONDARY_IGNORABLE */
+        setIndirectBoundaries(4, consts->UCA_LAST_SECONDARY_IGNORABLE, 0);
+        /* UCOL_FIRST_TERTIARY_IGNORABLE */
+        setIndirectBoundaries(5, consts->UCA_FIRST_TERTIARY_IGNORABLE, 0);
+        /* UCOL_LAST_TERTIARY_IGNORABLE */
+        setIndirectBoundaries(6, consts->UCA_LAST_TERTIARY_IGNORABLE, 0);
+        /* UCOL_FIRST_VARIABLE */
+        setIndirectBoundaries(7, consts->UCA_FIRST_VARIABLE, 0);
+        /* UCOL_LAST_VARIABLE */
+        setIndirectBoundaries(8, consts->UCA_LAST_VARIABLE, 0);
+        /* UCOL_FIRST_NON_VARIABLE */
+        setIndirectBoundaries(9, consts->UCA_FIRST_NON_VARIABLE, 0);
+        /* UCOL_LAST_NON_VARIABLE */
+        setIndirectBoundaries(10, consts->UCA_LAST_NON_VARIABLE, consts->UCA_FIRST_IMPLICIT);
+        /* UCOL_FIRST_IMPLICIT */
+        setIndirectBoundaries(11, consts->UCA_FIRST_IMPLICIT, 0);
+        /* UCOL_LAST_IMPLICIT */
+        setIndirectBoundaries(12, consts->UCA_LAST_IMPLICIT, consts->UCA_FIRST_TRAILING);
+        /* UCOL_FIRST_TRAILING */
+        setIndirectBoundaries(13, consts->UCA_FIRST_TRAILING, 0);
+        /* UCOL_LAST_TRAILING */
+        setIndirectBoundaries(14, consts->UCA_LAST_TRAILING, 0);
+        ucolIndirectBoundaries[14].limitCE = (consts->UCA_PRIMARY_SPECIAL_MIN<<24);
+        indirectBoundariesSet = TRUE;
+    }
 
-  if(U_SUCCESS(*status) && ruleLen > 0) {
-    rulesCopy = (UChar *)malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
-    uprv_memcpy(rulesCopy, rules, ruleLen*sizeof(UChar));
-    src.current = src.source = rulesCopy;
-    src.end = rulesCopy+ruleLen;
-    src.extraCurrent = src.end;
-    src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
 
-    while ((current = ucol_tok_parseNextToken(&src, startOfRules, &parseError,status)) != NULL) {
-      strength = src.parsedToken.strength;
-      chOffset = src.parsedToken.charsOffset;
-      chLen = src.parsedToken.charsLen;
-      exOffset = src.parsedToken.extensionOffset;
-      exLen = src.parsedToken.extensionLen;
-      prefixOffset = src.parsedToken.prefixOffset;
-      prefixLen = src.parsedToken.prefixLen;
-      specs = src.parsedToken.flags;
+    if(U_SUCCESS(*status) && ruleLen > 0) {
+        rulesCopy = (UChar *)uprv_malloc((ruleLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
+        uprv_memcpy(rulesCopy, rules, ruleLen*sizeof(UChar));
+        src.current = src.source = rulesCopy;
+        src.end = rulesCopy+ruleLen;
+        src.extraCurrent = src.end;
+        src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
+
+           /* Note that as a result of tickets 7015 or 6912, ucol_tok_parseNextToken can cause the pointer to
+              the rules copy in src.source to get reallocated, freeing the original pointer in rulesCopy */
+        while ((current = ucol_tok_parseNextToken(&src, startOfRules, &parseError,status)) != NULL) {
+            strength = src.parsedToken.strength;
+            chOffset = src.parsedToken.charsOffset;
+            chLen = src.parsedToken.charsLen;
+            exOffset = src.parsedToken.extensionOffset;
+            exLen = src.parsedToken.extensionLen;
+            prefixOffset = src.parsedToken.prefixOffset;
+            prefixLen = src.parsedToken.prefixLen;
+            specs = src.parsedToken.flags;
+
+            startOfRules = FALSE;
+            varT = (UBool)((specs & UCOL_TOK_VARIABLE_TOP) != 0);
+            top_ = (UBool)((specs & UCOL_TOK_TOP) != 0);
+
+            uprv_init_collIterate(coll, src.source+chOffset, chLen, c, status);
+
+            currCE = ucol_getNextCE(coll, c, status);
+            if(currCE == 0 && UCOL_ISTHAIPREVOWEL(*(src.source+chOffset))) {
+                log_verbose("Thai prevowel detected. Will pick next CE\n");
+                currCE = ucol_getNextCE(coll, c, status);
+            }
 
-      startOfRules = FALSE;
-      varT = (UBool)((specs & UCOL_TOK_VARIABLE_TOP) != 0);
-      top_ = (UBool)((specs & UCOL_TOK_TOP) != 0);
+            currContCE = ucol_getNextCE(coll, c, status);
+            if(!isContinuation(currContCE)) {
+                currContCE = 0;
+            }
 
-      uprv_init_collIterate(coll, rulesCopy+chOffset, chLen, &c);
+            /* we need to repack CEs here */
 
-      currCE = ucol_getNextCE(coll, &c, status);
-      if(currCE == 0 && UCOL_ISTHAIPREVOWEL(*(rulesCopy+chOffset))) {
-        log_verbose("Thai prevowel detected. Will pick next CE\n");
-        currCE = ucol_getNextCE(coll, &c, status);
-      }
+            if(strength == UCOL_TOK_RESET) {
+                before = (UBool)((specs & UCOL_TOK_BEFORE) != 0);
+                if(top_ == TRUE) {
+                    int32_t tokenIndex = src.parsedToken.indirectIndex;
 
-      currContCE = ucol_getNextCE(coll, &c, status);
-      if(!isContinuation(currContCE)) {
-        currContCE = 0;
-      }
+                    nextCE = baseCE = currCE = ucolIndirectBoundaries[tokenIndex].startCE;
+                    nextContCE = baseContCE = currContCE = ucolIndirectBoundaries[tokenIndex].startContCE;
+                } else {
+                    nextCE = baseCE = currCE;
+                    nextContCE = baseContCE = currContCE;
+                }
+                maxStrength = UCOL_IDENTICAL;
+            } else {
+                if(strength < maxStrength) {
+                    maxStrength = strength;
+                    if(baseCE == UCOL_RESET_TOP_VALUE) {
+                        log_verbose("Resetting to [top]\n");
+                        nextCE = UCOL_NEXT_TOP_VALUE;
+                        nextContCE = UCOL_NEXT_TOP_CONT;
+                    } else {
+                        result = ucol_inv_getNextCE(&src, baseCE & 0xFFFFFF3F, baseContCE, &nextCE, &nextContCE, maxStrength);
+                    }
+                    if(result < 0) {
+                        if(ucol_isTailored(coll, *(src.source+oldOffset), status)) {
+                            log_verbose("Reset is tailored codepoint %04X, don't know how to continue, taking next test\n", *(src.source+oldOffset));
+                            return;
+                        } else {
+                            log_err("%s: couldn't find the CE\n", colLoc);
+                            return;
+                        }
+                    }
+                }
 
-      /* we need to repack CEs here */
+                currCE &= 0xFFFFFF3F;
+                currContCE &= 0xFFFFFFBF;
 
-      if(strength == UCOL_TOK_RESET) {
-        before = (UBool)((specs & UCOL_TOK_BEFORE) != 0);
-        if(top_ == TRUE) {
-          int32_t index = src.parsedToken.indirectIndex;
+                if(maxStrength == UCOL_IDENTICAL) {
+                    if(baseCE != currCE || baseContCE != currContCE) {
+                        log_err("%s: current CE  (initial strength UCOL_EQUAL)\n", colLoc);
+                    }
+                } else {
+                    if(strength == UCOL_IDENTICAL) {
+                        if(lastCE != currCE || lastContCE != currContCE) {
+                            log_err("%s: current CE  (initial strength UCOL_EQUAL)\n", colLoc);
+                        }
+                    } else {
+                        if(compareCEs(currCE, currContCE, nextCE, nextContCE) > 0) {
+                            /*if(currCE > nextCE || (currCE == nextCE && currContCE >= nextContCE)) {*/
+                            log_err("%s: current CE is not less than base CE\n", colLoc);
+                        }
+                        if(!before) {
+                            if(compareCEs(currCE, currContCE, lastCE, lastContCE) < 0) {
+                                /*if(currCE < lastCE || (currCE == lastCE && currContCE <= lastContCE)) {*/
+                                log_err("%s: sequence of generated CEs is broken\n", colLoc);
+                            }
+                        } else {
+                            before = FALSE;
+                            if(compareCEs(currCE, currContCE, lastCE, lastContCE) > 0) {
+                                /*if(currCE < lastCE || (currCE == lastCE && currContCE <= lastContCE)) {*/
+                                log_err("%s: sequence of generated CEs is broken\n", colLoc);
+                            }
+                        }
+                    }
+                }
 
-          nextCE = baseCE = currCE = ucolIndirectBoundaries[index].startCE;
-          nextContCE = baseContCE = currContCE = ucolIndirectBoundaries[index].startContCE;
-        } else {
-          nextCE = baseCE = currCE;
-          nextContCE = baseContCE = currContCE;
-        }
-        maxStrength = UCOL_IDENTICAL;
-      } else {
-        if(strength < maxStrength) {
-          maxStrength = strength;
-          if(baseCE == UCOL_RESET_TOP_VALUE) {
-              log_verbose("Resetting to [top]\n");
-              nextCE = UCOL_NEXT_TOP_VALUE;
-              nextContCE = UCOL_NEXT_TOP_CONT;
-          } else {
-            result = ucol_inv_getNextCE(&src, baseCE & 0xFFFFFF3F, baseContCE, &nextCE, &nextContCE, maxStrength);
-          }
-          if(result < 0) {
-            if(ucol_isTailored(coll, *(rulesCopy+oldOffset), status)) {
-              log_verbose("Reset is tailored codepoint %04X, don't know how to continue, taking next test\n", *(rulesCopy+oldOffset));
-              return;
-            } else {
-              log_err("couldn't find the CE\n");
-              return;
             }
-          }
-        }
 
-        currCE &= 0xFFFFFF3F;
-        currContCE &= 0xFFFFFFBF;
-
-        if(maxStrength == UCOL_IDENTICAL) {
-          if(baseCE != currCE || baseContCE != currContCE) {
-            log_err("current CE  (initial strength UCOL_EQUAL)\n");
-          }
-        } else {
-          if(strength == UCOL_IDENTICAL) {
-            if(lastCE != currCE || lastContCE != currContCE) {
-              log_err("current CE  (initial strength UCOL_EQUAL)\n");
-            }
-          } else {
-            if(compareCEs(currCE, currContCE, nextCE, nextContCE) > 0) {
-            /*if(currCE > nextCE || (currCE == nextCE && currContCE >= nextContCE)) {*/
-              log_err("current CE is not less than base CE\n");
-            }
-            if(!before) {
-              if(compareCEs(currCE, currContCE, lastCE, lastContCE) < 0) {
-              /*if(currCE < lastCE || (currCE == lastCE && currContCE <= lastContCE)) {*/
-                log_err("sequence of generated CEs is broken\n");
-              }
-            } else {
-              before = FALSE;
-              if(compareCEs(currCE, currContCE, lastCE, lastContCE) > 0) {
-              /*if(currCE < lastCE || (currCE == lastCE && currContCE <= lastContCE)) {*/
-                log_err("sequence of generated CEs is broken\n");
-              }
-            }
-          }
+            oldOffset = chOffset;
+            lastCE = currCE & 0xFFFFFF3F;
+            lastContCE = currContCE & 0xFFFFFFBF;
         }
-
-      }
-
-      oldOffset = chOffset;
-      lastCE = currCE & 0xFFFFFF3F;
-      lastContCE = currContCE & 0xFFFFFFBF;
+        uprv_free(src.source);
     }
-    free(rulesCopy);
-  }
-  ucol_close(UCA);
+    ucol_close(UCA);
+    uprv_delete_collIterate(c);
 }
 
 #if 0
@@ -1285,133 +1322,175 @@ static const char* rulesToTest[] = {
 
 
 static void TestCollations(void) {
-  int32_t noOfLoc = uloc_countAvailable();
-  int32_t i = 0, j = 0;
+    int32_t noOfLoc = uloc_countAvailable();
+    int32_t i = 0, j = 0;
 
-  UErrorCode status = U_ZERO_ERROR;
-  char cName[256];
-  UChar name[256];
-  int32_t nameSize;
+    UErrorCode status = U_ZERO_ERROR;
+    char cName[256];
+    UChar name[256];
+    int32_t nameSize;
 
 
-  const char *locName = NULL;
-  UCollator *coll = NULL;
-  UCollator *UCA = ucol_open("", &status);
-  UColAttributeValue oldStrength = ucol_getAttribute(UCA, UCOL_STRENGTH, &status);
-  ucol_setAttribute(UCA, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
+    const char *locName = NULL;
+    UCollator *coll = NULL;
+    UCollator *UCA = ucol_open("", &status);
+    UColAttributeValue oldStrength = ucol_getAttribute(UCA, UCOL_STRENGTH, &status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "Could not open UCA collator %s\n", u_errorName(status));
+        return;
+    }
+    ucol_setAttribute(UCA, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
 
-  for(i = 0; i<noOfLoc; i++) {
-    status = U_ZERO_ERROR;
-    locName = uloc_getAvailable(i);
-    if(uprv_strcmp("ja", locName) == 0) {
-      log_verbose("Don't know how to test prefixes\n");
-      continue;
-    }
-    if(hasCollationElements(locName)) {
-        nameSize = uloc_getDisplayName(locName, NULL, name, 256, &status);
-        for(j = 0; j<nameSize; j++) {
-          cName[j] = (char)name[j];
+    for(i = 0; i<noOfLoc; i++) {
+        status = U_ZERO_ERROR;
+        locName = uloc_getAvailable(i);
+        if(uprv_strcmp("ja", locName) == 0) {
+            log_verbose("Don't know how to test prefixes\n");
+            continue;
         }
-        cName[nameSize] = 0;
-        log_verbose("\nTesting locale %s (%s)\n", locName, cName);
-        coll = ucol_open(locName, &status);
-        if(U_SUCCESS(status)) {
-          testAgainstUCA(coll, UCA, "UCA", FALSE, &status);
-          ucol_close(coll);
-        } else {
-          log_err("Couldn't instantiate collator for locale %s, error: %s\n", locName, u_errorName(status));
-          status = U_ZERO_ERROR;
+        if(hasCollationElements(locName)) {
+            nameSize = uloc_getDisplayName(locName, NULL, name, 256, &status);
+            for(j = 0; j<nameSize; j++) {
+                cName[j] = (char)name[j];
+            }
+            cName[nameSize] = 0;
+            log_verbose("\nTesting locale %s (%s)\n", locName, cName);
+            coll = ucol_open(locName, &status);
+            if(U_SUCCESS(status)) {
+                testAgainstUCA(coll, UCA, "UCA", FALSE, &status);
+                ucol_close(coll);
+            } else {
+                log_err("Couldn't instantiate collator for locale %s, error: %s\n", locName, u_errorName(status));
+                status = U_ZERO_ERROR;
+            }
         }
     }
-  }
-  ucol_setAttribute(UCA, UCOL_STRENGTH, oldStrength, &status);
-  ucol_close(UCA);
+    ucol_setAttribute(UCA, UCOL_STRENGTH, oldStrength, &status);
+    ucol_close(UCA);
 }
 
 static void RamsRulesTest(void) {
-  UErrorCode status = U_ZERO_ERROR;
-  int32_t i = 0;
-  UCollator *coll = NULL;
-  UChar rule[2048];
-  uint32_t ruleLen;
-  int32_t noOfLoc = uloc_countAvailable();
-  const char *locName = NULL;
+    UErrorCode status = U_ZERO_ERROR;
+    int32_t i = 0;
+    UCollator *coll = NULL;
+    UChar rule[2048];
+    uint32_t ruleLen;
+    int32_t noOfLoc = uloc_countAvailable();
+    const char *locName = NULL;
 
-  log_verbose("RamsRulesTest\n");
+    log_verbose("RamsRulesTest\n");
 
-  for(i = 0; i<noOfLoc; i++) {
-    status = U_ZERO_ERROR;
-    locName = uloc_getAvailable(i);
-    if(hasCollationElements(locName)) {
-      if (uprv_strcmp("ja", locName)==0) {
-        log_verbose("Don't know how to test Japanese because of prefixes\n");
-        continue;
-      }
-      if (uprv_strcmp("de__PHONEBOOK", locName)==0) {
-        log_verbose("Don't know how to test Phonebook because the reset is on an expanding character\n");
-        continue;
-      }
-      log_verbose("Testing locale %s\n", locName);
-      coll = ucol_open(locName, &status);
-      if(U_SUCCESS(status)) {
-        if(coll->image->jamoSpecial == TRUE) {
-          log_err("%s has special JAMOs\n", locName);
+    if (uprv_strcmp("km", uloc_getDefault())==0 || uprv_strcmp("km_KH", uloc_getDefault())==0) {
+        /* This test will fail if the default locale is "km" or "km_KH". Enable after trac#6040. */
+        return;
+    }
+
+    for(i = 0; i<noOfLoc; i++) {
+        locName = uloc_getAvailable(i);
+        if(hasCollationElements(locName)) {
+            if (uprv_strcmp("ja", locName)==0) {
+                log_verbose("Don't know how to test Japanese because of prefixes\n");
+                continue;
+            }
+            if (uprv_strcmp("de__PHONEBOOK", locName)==0) {
+                log_verbose("Don't know how to test Phonebook because the reset is on an expanding character\n");
+                continue;
+            }
+            if (uprv_strcmp("bn", locName)==0 ||
+                uprv_strcmp("en_US_POSIX", locName)==0 ||
+                uprv_strcmp("km", locName)==0 ||
+                uprv_strcmp("km_KH", locName)==0 ||
+                uprv_strcmp("my", locName)==0 ||
+                uprv_strcmp("si", locName)==0 ||
+                uprv_strcmp("si_LK", locName)==0 ||
+                uprv_strcmp("zh", locName)==0 ||
+                uprv_strcmp("zh_Hant", locName)==0
+            ) {
+                log_verbose("Don't know how to test %s. "
+                            "TODO: Fix ticket #6040 and reenable RamsRulesTest for this locale.\n", locName);
+                continue;
+            }
+            log_verbose("Testing locale %s\n", locName);
+            status = U_ZERO_ERROR;
+            coll = ucol_open(locName, &status);
+            if(U_SUCCESS(status)) {
+              if((status != U_USING_DEFAULT_WARNING) && (status != U_USING_FALLBACK_WARNING)) {
+                if(coll->image->jamoSpecial == TRUE) {
+                  log_err("%s has special JAMOs\n", locName);
+                }
+                ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_OFF, &status);
+                testCollator(coll, &status);
+                testCEs(coll, &status);
+              } else {
+                log_verbose("Skipping %s: %s\n", locName, u_errorName(status));
+              }
+              ucol_close(coll);
+            } else {
+              log_err("Could not open %s: %s\n", locName, u_errorName(status));
+            }
         }
-        ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_OFF, &status);
-        testCollator(coll, &status);
-        testCEs(coll, &status);
-        ucol_close(coll);
-      }
     }
-  }
 
-  for(i = 0; i<sizeof(rulesToTest)/sizeof(rulesToTest[0]); i++) {
-    log_verbose("Testing rule: %s\n", rulesToTest[i]);
-    ruleLen = u_unescape(rulesToTest[i], rule, 2048);
-    coll = ucol_openRules(rule, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
-    if(U_SUCCESS(status)) {
-      testCollator(coll, &status);
-      testCEs(coll, &status);
-      ucol_close(coll);
+    for(i = 0; i<sizeof(rulesToTest)/sizeof(rulesToTest[0]); i++) {
+        log_verbose("Testing rule: %s\n", rulesToTest[i]);
+        ruleLen = u_unescape(rulesToTest[i], rule, 2048);
+        status = U_ZERO_ERROR;
+        coll = ucol_openRules(rule, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+        if(U_SUCCESS(status)) {
+            testCollator(coll, &status);
+            testCEs(coll, &status);
+            ucol_close(coll);
+        } else {
+          log_err_status(status, "Could not test rule: %s: '%s'\n", u_errorName(status), rulesToTest[i]);
+        }
     }
-  }
 
 }
 
 static void IsTailoredTest(void) {
-  UErrorCode status = U_ZERO_ERROR;
-  uint32_t i = 0;
-  UCollator *coll = NULL;
-  UChar rule[2048];
-  UChar tailored[2048];
-  UChar notTailored[2048];
-  uint32_t ruleLen, tailoredLen, notTailoredLen;
+    UErrorCode status = U_ZERO_ERROR;
+    uint32_t i = 0;
+    UCollator *coll = NULL;
+    UChar rule[2048];
+    UChar tailored[2048];
+    UChar notTailored[2048];
+    uint32_t ruleLen, tailoredLen, notTailoredLen;
 
-  log_verbose("IsTailoredTest\n");
+    log_verbose("IsTailoredTest\n");
 
-  u_uastrcpy(rule, "&Z < A, B, C;c < d");
-  ruleLen = u_strlen(rule);
+    u_uastrcpy(rule, "&Z < A, B, C;c < d");
+    ruleLen = u_strlen(rule);
 
-  u_uastrcpy(tailored, "ABCcd");
-  tailoredLen = u_strlen(tailored);
+    u_uastrcpy(tailored, "ABCcd");
+    tailoredLen = u_strlen(tailored);
 
-  u_uastrcpy(notTailored, "ZabD");
-  notTailoredLen = u_strlen(notTailored);
+    u_uastrcpy(notTailored, "ZabD");
+    notTailoredLen = u_strlen(notTailored);
 
-  coll = ucol_openRules(rule, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
-  if(U_SUCCESS(status)) {
-    for(i = 0; i<tailoredLen; i++) {
-      if(!ucol_isTailored(coll, tailored[i], &status)) {
-        log_err("%i: %04X should be tailored - it is reported as not\n", i, tailored[i]);
-      }
+    coll = ucol_openRules(rule, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+    if(U_SUCCESS(status)) {
+        for(i = 0; i<tailoredLen; i++) {
+            if(!ucol_isTailored(coll, tailored[i], &status)) {
+                log_err("%i: %04X should be tailored - it is reported as not\n", i, tailored[i]);
+            }
+        }
+        for(i = 0; i<notTailoredLen; i++) {
+            if(ucol_isTailored(coll, notTailored[i], &status)) {
+                log_err("%i: %04X should not be tailored - it is reported as it is\n", i, notTailored[i]);
+            }
+        }
+        ucol_close(coll);
     }
-    for(i = 0; i<notTailoredLen; i++) {
-      if(ucol_isTailored(coll, notTailored[i], &status)) {
-        log_err("%i: %04X should not be tailored - it is reported as it is\n", i, notTailored[i]);
-      }
+    else {
+        log_err_status(status, "Can't tailor rules\n");
+    }
+    /* Code coverage */
+    status = U_ZERO_ERROR;
+    coll = ucol_open("ja", &status);
+    if(!ucol_isTailored(coll, 0x4E9C, &status)) {
+        log_err_status(status, "0x4E9C should be tailored - it is reported as not\n");
     }
     ucol_close(coll);
-  }
 }
 
 
@@ -1436,29 +1515,29 @@ const static char chTest[][20] = {
 };
 
 static void TestChMove(void) {
-  UChar t1[256] = {0};
-  UChar t2[256] = {0};
+    UChar t1[256] = {0};
+    UChar t2[256] = {0};
 
-  uint32_t i = 0, j = 0;
-  uint32_t size = 0;
-  UErrorCode status = U_ZERO_ERROR;
+    uint32_t i = 0, j = 0;
+    uint32_t size = 0;
+    UErrorCode status = U_ZERO_ERROR;
 
-  UCollator *coll = ucol_open("cs", &status);
+    UCollator *coll = ucol_open("cs", &status);
 
-  if(U_SUCCESS(status)) {
-    size = sizeof(chTest)/sizeof(chTest[0]);
-    for(i = 0; i < size-1; i++) {
-      for(j = i+1; j < size; j++) {
-        u_unescape(chTest[i], t1, 256);
-        u_unescape(chTest[j], t2, 256);
-        doTest(coll, t1, t2, UCOL_LESS);
-      }
+    if(U_SUCCESS(status)) {
+        size = sizeof(chTest)/sizeof(chTest[0]);
+        for(i = 0; i < size-1; i++) {
+            for(j = i+1; j < size; j++) {
+                u_unescape(chTest[i], t1, 256);
+                u_unescape(chTest[j], t2, 256);
+                doTest(coll, t1, t2, UCOL_LESS);
+            }
+        }
     }
-  }
-  else {
-    log_err("Can't open collator");
-  }
-  ucol_close(coll);
+    else {
+        log_data_err("Can't open collator");
+    }
+    ucol_close(coll);
 }
 
 
@@ -1475,9 +1554,9 @@ const static char impTest[][20] = {
 
 
 static void TestImplicitTailoring(void) {
-  static struct {
+  static const struct {
     const char *rules;
-    const char *data[50];
+    const char *data[10];
     const uint32_t len;
   } tests[] = {
       { "&[before 1]\\u4e00 < b < c &[before 1]\\u4e00 < d < e", { "d", "e", "b", "c", "\\u4e00"}, 5 },
@@ -1545,7 +1624,11 @@ static void TestFCDProblem(void) {
   ucol_close(coll);
 }
 
-#define NORM_BUFFER_TEST_LEN 32
+/*
+The largest normalization form is 18 for NFKC/NFKD, 4 for NFD and 3 for NFC
+We're only using NFC/NFD in this test.
+*/
+#define NORM_BUFFER_TEST_LEN 18
 typedef struct {
   UChar32 u;
   UChar NFC[NORM_BUFFER_TEST_LEN];
@@ -1553,13 +1636,17 @@ typedef struct {
 } tester;
 
 static void TestComposeDecompose(void) {
+    /* [[:NFD_Inert=false:][:NFC_Inert=false:]] */
+    static const UChar UNICODESET_STR[] = {
+        0x5B,0x5B,0x3A,0x4E,0x46,0x44,0x5F,0x49,0x6E,0x65,0x72,0x74,0x3D,0x66,0x61,
+        0x6C,0x73,0x65,0x3A,0x5D,0x5B,0x3A,0x4E,0x46,0x43,0x5F,0x49,0x6E,0x65,0x72,
+        0x74,0x3D,0x66,0x61,0x6C,0x73,0x65,0x3A,0x5D,0x5D,0
+    };
     int32_t noOfLoc;
     int32_t i = 0, j = 0;
 
     UErrorCode status = U_ZERO_ERROR;
-
     const char *locName = NULL;
-
     uint32_t nfcSize;
     uint32_t nfdSize;
     tester **t;
@@ -1569,49 +1656,56 @@ static void TestComposeDecompose(void) {
     UChar comp[NORM_BUFFER_TEST_LEN];
     uint32_t len = 0;
     UCollationElements *iter;
+    USet *charsToTest = uset_openPattern(UNICODESET_STR, -1, &status);
+    int32_t charsToTestSize;
 
     noOfLoc = uloc_countAvailable();
 
-    t = malloc(0x30000 * sizeof(tester *));
-    t[0] = (tester *)malloc(sizeof(tester));
-    log_verbose("Testing UCA extensively\n");
     coll = ucol_open("", &status);
-    if(status == U_FILE_ACCESS_ERROR) {
-      log_data_err("Is your data around?\n");
-      return;
-    } else if(U_FAILURE(status)) {
-      log_err("Error opening collator\n");
-      return;
+    if (U_FAILURE(status)) {
+        log_data_err("Error opening collator -> %s (Are you missing data?)\n", u_errorName(status));
+        return;
     }
+    charsToTestSize = uset_size(charsToTest);
+    if (charsToTestSize <= 0) {
+        log_err("Set was zero. Missing data?\n");
+        return;
+    }
+    t = malloc(charsToTestSize * sizeof(tester *));
+    t[0] = (tester *)malloc(sizeof(tester));
+    log_verbose("Testing UCA extensively for %d characters\n", charsToTestSize);
 
-
-    for(u = 0; u < 0x30000; u++) {
-      len = 0;
-      UTF_APPEND_CHAR_UNSAFE(comp, len, u);
+    for(u = 0; u < charsToTestSize; u++) {
+        UChar32 ch = uset_charAt(charsToTest, u);
+        len = 0;
+        UTF_APPEND_CHAR_UNSAFE(comp, len, ch);
         nfcSize = unorm_normalize(comp, len, UNORM_NFC, 0, t[noCases]->NFC, NORM_BUFFER_TEST_LEN, &status);
         nfdSize = unorm_normalize(comp, len, UNORM_NFD, 0, t[noCases]->NFD, NORM_BUFFER_TEST_LEN, &status);
 
         if(nfcSize != nfdSize || (uprv_memcmp(t[noCases]->NFC, t[noCases]->NFD, nfcSize * sizeof(UChar)) != 0)
           || (len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0))) {
-            t[noCases]->u = u;
+            t[noCases]->u = ch;
             if(len != nfdSize || (uprv_memcmp(comp, t[noCases]->NFD, nfdSize * sizeof(UChar)) != 0)) {
-              u_strncpy(t[noCases]->NFC, comp, len);
-              t[noCases]->NFC[len] = 0;
+                u_strncpy(t[noCases]->NFC, comp, len);
+                t[noCases]->NFC[len] = 0;
             }
             noCases++;
             t[noCases] = (tester *)malloc(sizeof(tester));
             uprv_memset(t[noCases], 0, sizeof(tester));
         }
     }
+    log_verbose("Testing %d/%d of possible test cases\n", noCases, charsToTestSize);
+    uset_close(charsToTest);
+    charsToTest = NULL;
 
     for(u=0; u<(UChar32)noCases; u++) {
-      if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) {
-        log_err("Failure: codePoint %05X fails TestComposeDecompose in the UCA\n", t[u]->u);
-        doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL);
-      }
+        if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) {
+            log_err("Failure: codePoint %05X fails TestComposeDecompose in the UCA\n", t[u]->u);
+            doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL);
+        }
     }
     /*
-    for(u = 0; u < 0x30000; u++) {
+    for(u = 0; u < charsToTestSize; u++) {
       if(!(u&0xFFFF)) {
         log_verbose("%08X ", u);
       }
@@ -1649,16 +1743,16 @@ static void TestComposeDecompose(void) {
             iter = ucol_openElements(coll, t[u]->NFD, u_strlen(t[u]->NFD), &status);
 
             for(u=0; u<(UChar32)noCases; u++) {
-              if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) {
-                log_err("Failure: codePoint %05X fails TestComposeDecompose for locale %s\n", t[u]->u, cName);
-                doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL);
-                log_verbose("Testing NFC\n");
-                ucol_setText(iter, t[u]->NFC, u_strlen(t[u]->NFC), &status);
-                  backAndForth(iter);
-                log_verbose("Testing NFD\n");
-                  ucol_setText(iter, t[u]->NFD, u_strlen(t[u]->NFD), &status);
-                  backAndForth(iter);
-              }
+                if(!ucol_equal(coll, t[u]->NFC, -1, t[u]->NFD, -1)) {
+                    log_err("Failure: codePoint %05X fails TestComposeDecompose for locale %s\n", t[u]->u, cName);
+                    doTest(coll, t[u]->NFC, t[u]->NFD, UCOL_EQUAL);
+                    log_verbose("Testing NFC\n");
+                    ucol_setText(iter, t[u]->NFC, u_strlen(t[u]->NFC), &status);
+                    backAndForth(iter);
+                    log_verbose("Testing NFD\n");
+                    ucol_setText(iter, t[u]->NFD, u_strlen(t[u]->NFD), &status);
+                    backAndForth(iter);
+                }
             }
             ucol_closeElements(iter);
             ucol_close(coll);
@@ -1824,7 +1918,7 @@ static void TestJ815(void) {
 static void TestRedundantRules(void) {
   int32_t i;
 
-  struct {
+  static const struct {
       const char *rules;
       const char *expectedRules;
       const char *testdata[8];
@@ -2025,20 +2119,18 @@ static void TestCase(void)
 
     const static UCollationResult caseTestResults[][9] =
     {
-            { UCOL_LESS, UCOL_LESS, UCOL_LESS, 0, UCOL_LESS, UCOL_LESS, 0, 0, UCOL_LESS },
-            { UCOL_GREATER, UCOL_LESS, UCOL_LESS, 0, UCOL_LESS, UCOL_LESS, 0, 0, UCOL_GREATER },
-            { UCOL_LESS, UCOL_LESS, UCOL_LESS, 0, UCOL_GREATER, UCOL_LESS, 0, 0, UCOL_LESS },
-            { UCOL_GREATER, UCOL_LESS, UCOL_GREATER, 0, UCOL_LESS, UCOL_LESS, 0, 0, UCOL_GREATER }
-
+        { UCOL_LESS,    UCOL_LESS, UCOL_LESS,    UCOL_EQUAL, UCOL_LESS,    UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_LESS },
+        { UCOL_GREATER, UCOL_LESS, UCOL_LESS,    UCOL_EQUAL, UCOL_LESS,    UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_GREATER },
+        { UCOL_LESS,    UCOL_LESS, UCOL_LESS,    UCOL_EQUAL, UCOL_GREATER, UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_LESS },
+        { UCOL_GREATER, UCOL_LESS, UCOL_GREATER, UCOL_EQUAL, UCOL_LESS,    UCOL_LESS, UCOL_EQUAL, UCOL_EQUAL, UCOL_GREATER }
     };
 
     const static UColAttributeValue caseTestAttributes[][2] =
     {
-            { UCOL_LOWER_FIRST, UCOL_OFF},
-            { UCOL_UPPER_FIRST, UCOL_OFF},
-            { UCOL_LOWER_FIRST, UCOL_ON},
-            { UCOL_UPPER_FIRST, UCOL_ON}
-
+        { UCOL_LOWER_FIRST, UCOL_OFF},
+        { UCOL_UPPER_FIRST, UCOL_OFF},
+        { UCOL_LOWER_FIRST, UCOL_ON},
+        { UCOL_UPPER_FIRST, UCOL_ON}
     };
     int32_t i,j,k;
     UErrorCode status = U_ZERO_ERROR;
@@ -2047,7 +2139,7 @@ static void TestCase(void)
     myCollation = ucol_open("en_US", &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;
     }
     log_verbose("Testing different case settings\n");
@@ -2140,7 +2232,7 @@ static void TestIncrementalNormalize(void) {
     /*UChar baseA     =0x61;*/
     UChar baseA     =0x41;
 /*    UChar baseB     = 0x42;*/
-    UChar ccMix[]   = {0x316, 0x321, 0x300};
+    static const UChar ccMix[]   = {0x316, 0x321, 0x300};
     /*UChar ccMix[]   = {0x61, 0x61, 0x61};*/
     /*
         0x316 is combining grave accent below, cc=220
@@ -2148,8 +2240,8 @@ static void TestIncrementalNormalize(void) {
         0x300 is combining grave accent, cc=230
     */
 
-    /*int          maxSLen   = 2000;*/
-    int          maxSLen   = 64000;
+#define MAXSLEN 2000
+    /*int          maxSLen   = 64000;*/
     int          sLen;
     int          i;
 
@@ -2157,20 +2249,17 @@ static void TestIncrementalNormalize(void) {
     UErrorCode       status = U_ZERO_ERROR;
     UCollationResult result;
 
-    int32_t myQ = QUICK;
+    int32_t myQ = getTestOption(QUICK_OPTION);
 
-    if(QUICK < 0) {
-      QUICK = 1;
+    if(getTestOption(QUICK_OPTION) < 0) {
+        setTestOption(QUICK_OPTION, 1);
     }
 
     {
         /* Test 1.  Run very long unnormalized strings, to force overflow of*/
         /*          most buffers along the way.*/
-        UChar            *strA;
-        UChar            *strB;
-
-        strA = malloc((maxSLen+1) * sizeof(UChar));
-        strB = malloc((maxSLen+1) * sizeof(UChar));
+        UChar            strA[MAXSLEN+1];
+        UChar            strB[MAXSLEN+1];
 
         coll = ucol_open("en_US", &status);
         if(status == U_FILE_ACCESS_ERROR) {
@@ -2182,8 +2271,8 @@ static void TestIncrementalNormalize(void) {
         }
         ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
 
-        /*for (sLen = 257; sLen<maxSLen; sLen++) {*/
-        /*for (sLen = 4; sLen<maxSLen; sLen++) {*/
+        /*for (sLen = 257; sLen<MAXSLEN; sLen++) {*/
+        /*for (sLen = 4; sLen<MAXSLEN; sLen++) {*/
         /*for (sLen = 1000; sLen<1001; sLen++) {*/
         for (sLen = 500; sLen<501; sLen++) {
         /*for (sLen = 40000; sLen<65000; sLen+=1000) {*/
@@ -2201,19 +2290,17 @@ static void TestIncrementalNormalize(void) {
             ucol_setStrength(coll, UCOL_IDENTICAL);   /* Do again with the slow, general impl.*/
             doTest(coll, strA, strB, UCOL_EQUAL);
         }
-        free(strA);
-        free(strB);
     }
 
-    QUICK = myQ;
+    setTestOption(QUICK_OPTION, myQ);
 
 
     /*  Test 2:  Non-normal sequence in a string that extends to the last character*/
     /*         of the string.  Checks a couple of edge cases.*/
 
     {
-        UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0};
-        UChar strB[] = {0x41, 0xc0, 0x316, 0};
+        static const UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0};
+        static const UChar strB[] = {0x41, 0xc0, 0x316, 0};
         ucol_setStrength(coll, UCOL_TERTIARY);
         doTest(coll, strA, strB, UCOL_EQUAL);
     }
@@ -2226,8 +2313,8 @@ static void TestIncrementalNormalize(void) {
        * than d800 dc00
        */
         /*UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0xD801, 0xDC00, 0};*/
-        UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0xD800, 0xDC01, 0};
-        UChar strB[] = {0x41, 0xc0, 0x316, 0xD800, 0xDC00, 0};
+        static const UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0xD800, 0xDC01, 0};
+        static const UChar strB[] = {0x41, 0xc0, 0x316, 0xD800, 0xDC00, 0};
         ucol_setStrength(coll, UCOL_TERTIARY);
         doTest(coll, strA, strB, UCOL_GREATER);
     }
@@ -2235,8 +2322,8 @@ static void TestIncrementalNormalize(void) {
     /*  Test 4:  Imbedded nulls do not terminate a string when length is specified.*/
 
     {
-        UChar strA[] = {0x41, 0x00, 0x42, 0x00};
-        UChar strB[] = {0x41, 0x00, 0x00, 0x00};
+        static const UChar strA[] = {0x41, 0x00, 0x42, 0x00};
+        static const UChar strB[] = {0x41, 0x00, 0x00, 0x00};
         char  sortKeyA[50];
         char  sortKeyAz[50];
         char  sortKeyB[50];
@@ -2297,8 +2384,8 @@ static void TestIncrementalNormalize(void) {
     /*  Test 5:  Null characters in non-normal source strings.*/
 
     {
-        UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x42, 0x00};
-        UChar strB[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x00, 0x00};
+        static const UChar strA[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x42, 0x00};
+        static const UChar strB[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x00, 0x00};
         char  sortKeyA[50];
         char  sortKeyAz[50];
         char  sortKeyB[50];
@@ -2357,8 +2444,8 @@ static void TestIncrementalNormalize(void) {
     /*  Test 6:  Null character as base of a non-normal combining sequence.*/
 
     {
-        UChar strA[] = {0x41, 0x0, 0x300, 0x316, 0x41, 0x302, 0x00};
-        UChar strB[] = {0x41, 0x0, 0x302, 0x316, 0x41, 0x300, 0x00};
+        static const UChar strA[] = {0x41, 0x0, 0x300, 0x316, 0x41, 0x302, 0x00};
+        static const UChar strB[] = {0x41, 0x0, 0x302, 0x316, 0x41, 0x300, 0x00};
 
         result = ucol_strcoll(coll, strA, 5, strB, 5);
         if (result != UCOL_LESS) {
@@ -2466,7 +2553,7 @@ static void TestCompressOverlap(void) {
     coll = ucol_open("", &status);
 
     if (U_FAILURE(status)) {
-        log_err("Collator can't be created\n");
+        log_err_status(status, "Collator can't be created -> %s\n", u_errorName(status));
         return;
     }
     while (count < 149) {
@@ -2613,7 +2700,7 @@ static void TestContraction(void) {
         rlen = u_unescape(testrules[i], rule, 32);
         coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status);
         if (U_FAILURE(status)) {
-            log_err("Collator creation failed %s\n", testrules[i]);
+            log_err_status(status, "Collator creation failed %s -> %s\n", testrules[i], u_errorName(status));
             return;
         }
         iter1 = ucol_openElements(coll, testdata[i], 2, &status);
@@ -2739,7 +2826,7 @@ static void TestExpansion(void) {
         rlen = u_unescape(testrules[i], rule, 32);
         coll = ucol_openRules(rule, rlen, UCOL_ON, UCOL_TERTIARY,NULL, &status);
         if (U_FAILURE(status)) {
-            log_err("Collator creation failed %s\n", testrules[i]);
+            log_err_status(status, "Collator creation failed %s -> %s\n", testrules[i], u_errorName(status));
             return;
         }
 
@@ -2894,20 +2981,22 @@ static void TestVariableTopSetting(void) {
   UChar first[256] = { 0 };
   UChar second[256] = { 0 };
   UParseError parseError;
-  int32_t myQ = QUICK;
+  int32_t myQ = getTestOption(QUICK_OPTION);
+
+  uprv_memset(&src, 0, sizeof(UColTokenParser));
 
   src.opts = &opts;
 
-  if(QUICK <= 0) {
-    QUICK = 1;
+  if(getTestOption(QUICK_OPTION) <= 0) {
+    setTestOption(QUICK_OPTION, 1);
   }
 
   /* this test will fail when normalization is turned on */
   /* therefore we always turn off exhaustive mode for it */
-  if(1) { /* QUICK > 0*/
+  { /* QUICK > 0*/
     log_verbose("Slide variable top over UCARules\n");
     rulesLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rulesCopy, 0);
-    rulesCopy = (UChar *)malloc((rulesLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
+    rulesCopy = (UChar *)uprv_malloc((rulesLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE)*sizeof(UChar));
     rulesLen = ucol_getRulesEx(coll, UCOL_FULL_RULES, rulesCopy, rulesLen+UCOL_TOK_EXTRA_RULE_SPACE_SIZE);
 
     if(U_SUCCESS(status) && rulesLen > 0) {
@@ -2917,6 +3006,8 @@ static void TestVariableTopSetting(void) {
       src.extraCurrent = src.end;
       src.extraEnd = src.end+UCOL_TOK_EXTRA_RULE_SPACE_SIZE;
 
+         /* Note that as a result of tickets 7015 or 6912, ucol_tok_parseNextToken can cause the pointer to
+          the rules copy in src.source to get reallocated, freeing the original pointer in rulesCopy */
       while ((current = ucol_tok_parseNextToken(&src, startOfRules, &parseError,&status)) != NULL) {
         strength = src.parsedToken.strength;
         chOffset = src.parsedToken.charsOffset;
@@ -2928,13 +3019,13 @@ static void TestVariableTopSetting(void) {
         specs = src.parsedToken.flags;
 
         startOfRules = FALSE;
-        if(0) {
-          log_verbose("%04X %d ", *(rulesCopy+chOffset), chLen);
+        {
+          log_verbose("%04X %d ", *(src.source+chOffset), chLen);
         }
         if(strength == UCOL_PRIMARY) {
           status = U_ZERO_ERROR;
           varTopOriginal = ucol_getVariableTop(coll, &status);
-          varTop1 = ucol_setVariableTop(coll, rulesCopy+oldChOffset, oldChLen, &status);
+          varTop1 = ucol_setVariableTop(coll, src.source+oldChOffset, oldChLen, &status);
           if(U_FAILURE(status)) {
             char buffer[256];
             char *buf = buffer;
@@ -2942,19 +3033,20 @@ static void TestVariableTopSetting(void) {
             uint32_t CE = UCOL_NO_MORE_CES;
 
             /* before we start screaming, let's see if there is a problem with the rules */
-            collIterate s;
-            uprv_init_collIterate(coll, rulesCopy+oldChOffset, oldChLen, &s);
+            UErrorCode collIterateStatus = U_ZERO_ERROR;
+            collIterate *s = uprv_new_collIterate(&collIterateStatus);
+            uprv_init_collIterate(coll, src.source+oldChOffset, oldChLen, s, &collIterateStatus);
 
-            CE = ucol_getNextCE(coll, &s, &status);
+            CE = ucol_getNextCE(coll, s, &status);
 
             for(i = 0; i < oldChLen; i++) {
-              j = sprintf(buf, "%04X ", *(rulesCopy+oldChOffset+i));
+              j = sprintf(buf, "%04X ", *(src.source+oldChOffset+i));
               buf += j;
             }
             if(status == U_PRIMARY_TOO_LONG_ERROR) {
               log_verbose("= Expected failure for %s =", buffer);
             } else {
-              if(s.pos == s.endp) {
+              if(uprv_collIterateAtEnd(s)) {
                 log_err("Unexpected failure setting variable top at offset %d. Error %s. Codepoints: %s\n",
                   oldChOffset, u_errorName(status), buffer);
               } else {
@@ -2962,6 +3054,7 @@ static void TestVariableTopSetting(void) {
                   buffer);
               }
             }
+            uprv_delete_collIterate(s);
           }
           varTop2 = ucol_getVariableTop(coll, &status);
           if((varTop1 & 0xFFFF0000) != (varTop2 & 0xFFFF0000)) {
@@ -2971,25 +3064,25 @@ static void TestVariableTopSetting(void) {
 
           if((varTop1 & 0xFFFF0000) > 0 && oldExLen == 0) {
 
-            u_strncpy(first, rulesCopy+oldChOffset, oldChLen);
-            u_strncpy(first+oldChLen, rulesCopy+chOffset, chLen);
-            u_strncpy(first+oldChLen+chLen, rulesCopy+oldChOffset, oldChLen);
+            u_strncpy(first, src.source+oldChOffset, oldChLen);
+            u_strncpy(first+oldChLen, src.source+chOffset, chLen);
+            u_strncpy(first+oldChLen+chLen, src.source+oldChOffset, oldChLen);
             first[2*oldChLen+chLen] = 0;
 
             if(oldExLen == 0) {
-              u_strncpy(second, rulesCopy+chOffset, chLen);
+              u_strncpy(second, src.source+chOffset, chLen);
               second[chLen] = 0;
             } else { /* This is skipped momentarily, but should work once UCARules are fully UCA conformant */
-              u_strncpy(second, rulesCopy+oldExOffset, oldExLen);
-              u_strncpy(second+oldChLen, rulesCopy+chOffset, chLen);
-              u_strncpy(second+oldChLen+chLen, rulesCopy+oldExOffset, oldExLen);
+              u_strncpy(second, src.source+oldExOffset, oldExLen);
+              u_strncpy(second+oldChLen, src.source+chOffset, chLen);
+              u_strncpy(second+oldChLen+chLen, src.source+oldExOffset, oldExLen);
               second[2*oldExLen+chLen] = 0;
             }
             result = ucol_strcoll(coll, first, -1, second, -1);
             if(result == UCOL_EQUAL) {
               doTest(coll, first, second, UCOL_EQUAL);
             } else {
-              log_verbose("Suspicious strcoll result for %04X and %04X\n", *(rulesCopy+oldChOffset), *(rulesCopy+chOffset));
+              log_verbose("Suspicious strcoll result for %04X and %04X\n", *(src.source+oldChOffset), *(src.source+chOffset));
             }
           }
         }
@@ -3013,7 +3106,7 @@ static void TestVariableTopSetting(void) {
     status = U_ZERO_ERROR;
   }
 
-  QUICK = myQ;
+  setTestOption(QUICK_OPTION, myQ);
 
   log_verbose("Testing setting variable top to contractions\n");
   {
@@ -3021,14 +3114,21 @@ static void TestVariableTopSetting(void) {
     /*UChar *conts = (UChar *)((uint8_t *)coll->image + coll->image->UCAConsts+sizeof(UCAConstants));*/
     UChar *conts = (UChar *)((uint8_t *)coll->image + coll->image->contractionUCACombos);
     while(*conts != 0) {
-      if(*(conts+2) == 0) {
+      if((*(conts+2) == 0) || (*(conts+1)==0)) { /* contracts or pre-context contractions */
         varTop1 = ucol_setVariableTop(coll, conts, -1, &status);
       } else {
         varTop1 = ucol_setVariableTop(coll, conts, 3, &status);
       }
       if(U_FAILURE(status)) {
-        log_err("Couldn't set variable top to a contraction %04X %04X %04X\n",
-          *conts, *(conts+1), *(conts+2));
+        if(status == U_PRIMARY_TOO_LONG_ERROR) {
+          /* ucol_setVariableTop() is documented to not accept 3-byte primaries,
+           * therefore it is not an error when it complains about them. */
+          log_verbose("Couldn't set variable top to a contraction %04X %04X %04X - U_PRIMARY_TOO_LONG_ERROR\n",
+                      *conts, *(conts+1), *(conts+2));
+        } else {
+          log_err("Couldn't set variable top to a contraction %04X %04X %04X - %s\n",
+                  *conts, *(conts+1), *(conts+2), u_errorName(status));
+        }
         status = U_ZERO_ERROR;
       }
       conts+=3;
@@ -3068,7 +3168,7 @@ static void TestVariableTopSetting(void) {
   if(status != U_INTERNAL_PROGRAM_ERROR) {
     log_err("Bad reaction to passed error!\n");
   }
-  free(rulesCopy);
+  uprv_free(src.source);
   ucol_close(coll);
   } else {
     log_data_err("Couldn't open UCA collator\n");
@@ -3078,10 +3178,11 @@ static void TestVariableTopSetting(void) {
 
 static void TestNonChars(void) {
   static const char *test[] = {
-    "\\u0000",
-    "\\uFFFE", "\\uFFFF",
-      "\\U0001FFFE", "\\U0001FFFF",
-      "\\U0002FFFE", "\\U0002FFFF",
+      "\\u0000",  /* ignorable */
+      "\\uFFFE",  /* special merge-sort character with minimum non-ignorable weights */
+      "\\uFDD0", "\\uFDEF",
+      "\\U0001FFFE", "\\U0001FFFF",  /* UCA 6.0: noncharacters are treated like unassigned, */
+      "\\U0002FFFE", "\\U0002FFFF",  /* not like ignorable. */
       "\\U0003FFFE", "\\U0003FFFF",
       "\\U0004FFFE", "\\U0004FFFF",
       "\\U0005FFFE", "\\U0005FFFF",
@@ -3095,7 +3196,8 @@ static void TestNonChars(void) {
       "\\U000DFFFE", "\\U000DFFFF",
       "\\U000EFFFE", "\\U000EFFFF",
       "\\U000FFFFE", "\\U000FFFFF",
-      "\\U0010FFFE", "\\U0010FFFF"
+      "\\U0010FFFE", "\\U0010FFFF",
+      "\\uFFFF"  /* special character with maximum primary weight */
   };
   UErrorCode status = U_ZERO_ERROR;
   UCollator *coll = ucol_open("en_US", &status);
@@ -3103,9 +3205,9 @@ static void TestNonChars(void) {
   log_verbose("Test non characters\n");
 
   if(U_SUCCESS(status)) {
-    genericOrderingTestWithResult(coll, test, 35, UCOL_EQUAL);
+    genericOrderingTestWithResult(coll, test, 35, UCOL_LESS);
   } else {
-    log_err("Unable to open collator\n");
+    log_err_status(status, "Unable to open collator\n");
   }
 
   ucol_close(coll);
@@ -3189,7 +3291,7 @@ static void TestSurrogates(void) {
 static void TestPrefix(void) {
   uint32_t i;
 
-  static struct {
+  static const struct {
     const char *rules;
     const char *data[50];
     const uint32_t len;
@@ -3215,7 +3317,7 @@ static void TestPrefix(void) {
 /* JIS X 4061 collation order implementation                                   */
 static void TestNewJapanese(void) {
 
-  static const char *test1[] = {
+  static const char * const test1[] = {
       "\\u30b7\\u30e3\\u30fc\\u30ec",
       "\\u30b7\\u30e3\\u30a4",
       "\\u30b7\\u30e4\\u30a3",
@@ -3384,9 +3486,9 @@ static void TestStrCollIdenticalPrefix(void) {
 /* Contractions should have all their canonically equivalent */
 /* strings included */
 static void TestContractionClosure(void) {
-  static struct {
+  static const struct {
     const char *rules;
-    const char *data[50];
+    const char *data[10];
     const uint32_t len;
   } tests[] = {
     {   "&b=\\u00e4\\u00e4",
@@ -3404,9 +3506,9 @@ static void TestContractionClosure(void) {
 
 /* This tests also fails*/
 static void TestBeforePrefixFailure(void) {
-  static struct {
+  static const struct {
     const char *rules;
-    const char *data[50];
+    const char *data[10];
     const uint32_t len;
   } tests[] = {
     { "&g <<< a"
@@ -3518,11 +3620,42 @@ static void TestPrefixCompose(void) {
 static void TestRuleOptions(void) {
   /* values here are hardcoded and are correct for the current UCA
    * when the UCA changes, one might be forced to change these
-   * values. (\\u02d0, \\U00010FFFC etc...)
+   * values.
+   */
+
+  /*
+   * These strings contain the last character before [variable top]
+   * and the first and second characters (by primary weights) after it.
+   * See FractionalUCA.txt. For example:
+      [last variable [0C FE, 05, 05]] # U+10A7F OLD SOUTH ARABIAN NUMERIC INDICATOR
+      [variable top = 0C FE]
+      [first regular [0D 0A, 05, 05]] # U+0060 GRAVE ACCENT
+     and
+      00B4; [0D 0C, 05, 05]
+   *
+   * Note: Starting with UCA 6.0, the [variable top] collation element
+   * is not the weight of any character or string,
+   * which means that LAST_VARIABLE_CHAR_STRING sorts before [last variable].
+   */
+#define LAST_VARIABLE_CHAR_STRING "\\U00010A7F"
+#define FIRST_REGULAR_CHAR_STRING "\\u0060"
+#define SECOND_REGULAR_CHAR_STRING "\\u00B4"
+
+  /*
+   * This string has to match the character that has the [last regular] weight
+   * which changes with each UCA version.
+   * See the bottom of FractionalUCA.txt which says something like
+      [last regular [7A FE, 05, 05]] # U+1342E EGYPTIAN HIEROGLYPH AA032
+   *
+   * Note: Starting with UCA 6.0, the [last regular] collation element
+   * is not the weight of any character or string,
+   * which means that LAST_REGULAR_CHAR_STRING sorts before [last regular].
    */
-  static struct {
+#define LAST_REGULAR_CHAR_STRING "\\U0001342E"
+
+  static const struct {
     const char *rules;
-    const char *data[50];
+    const char *data[10];
     const uint32_t len;
   } tests[] = {
     /* - all befores here amount to zero */
@@ -3560,17 +3693,17 @@ static void TestRuleOptions(void) {
     },
 
     { "&[last variable]<a &[before 3][last variable]<<<c<<<b ",
-        {  "c", "b", "\\uD834\\uDF71", "a", "\\u02d0" }, 5
+        { LAST_VARIABLE_CHAR_STRING, "c", "b", /* [last variable] */ "a", FIRST_REGULAR_CHAR_STRING }, 5
     },
 
     { "&[first regular]<a"
       "&[before 1][first regular]<b",
-      { "b", "\\u02d0", "a", "\\u02d1"}, 4
+      { "b", FIRST_REGULAR_CHAR_STRING, "a", SECOND_REGULAR_CHAR_STRING }, 4
     },
 
     { "&[before 1][last regular]<b"
       "&[last regular]<a",
-        { "b", "\\uD808\\uDF6E", "a", "\\u4e00" }, 4
+        { LAST_REGULAR_CHAR_STRING, "b", /* [last regular] */ "a", "\\u4e00" }, 4
     },
 
     { "&[before 1][first implicit]<b"
@@ -3588,13 +3721,12 @@ static void TestRuleOptions(void) {
       "&[last secondary ignorable]<<y"
       "&[last tertiary ignorable]<<<w"
       "&[top]<u",
-      {"\\ufffb",  "w", "y", "\\u20e3", "x", "\\u137c", "z", "u"}, 7
+      {"\\ufffb",  "w", "y", "\\u20e3", "x", LAST_VARIABLE_CHAR_STRING, "z", "u"}, 7
     }
 
   };
   uint32_t i;
 
-
   for(i = 0; i<(sizeof(tests)/sizeof(tests[0])); i++) {
     genericRulesStarter(tests[i].rules, tests[i].data, tests[i].len);
   }
@@ -3607,9 +3739,9 @@ static void TestOptimize(void) {
    * Cannot really test, since the functionality
    * remains the same.
    */
-  static struct {
+  static const struct {
     const char *rules;
-    const char *data[50];
+    const char *data[10];
     const uint32_t len;
   } tests[] = {
     /* - all befores here amount to zero */
@@ -3713,7 +3845,7 @@ static void Alexis2(void) {
 
   UCollationResult resU16, resU16BE, resU8;
 
-  const char* pairs[][2] = {
+  static const char* const pairs[][2] = {
     { "\\ud800\\u0021", "\\uFFFC\\u0062"},
     { "\\u0435\\u0308\\u0334", "\\u0415\\u0334\\u0340" },
     { "\\u0E40\\u0021", "\\u00A1\\u0021"},
@@ -3783,7 +3915,7 @@ static void Alexis2(void) {
 
 static void TestHebrewUCA(void) {
   UErrorCode status = U_ZERO_ERROR;
-  const char *first[] = {
+  static const char *first[] = {
     "d790d6b8d79cd795d6bcd7a9",
     "d790d79cd79ed7a7d799d799d7a1",
     "d790d6b4d79ed795d6bcd7a9",
@@ -3797,6 +3929,10 @@ static void TestHebrewUCA(void) {
   int32_t sizeUTF16[3];
 
   UCollator *coll = ucol_open("", &status);
+  if (U_FAILURE(status)) {
+      log_err_status(status, "Could not open UCA collation %s\n", u_errorName(status));
+      return;
+  }
   /*ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);*/
 
   for(i = 0; i < sizeof(first)/sizeof(first[0]); i++) {
@@ -3820,7 +3956,7 @@ static void TestHebrewUCA(void) {
 }
 
 static void TestPartialSortKeyTermination(void) {
-  const char* cases[] = {
+  static const char* cases[] = {
     "\\u1234\\u1234\\udc00",
     "\\udc00\\ud800\\ud800"
   };
@@ -3852,12 +3988,12 @@ static void TestPartialSortKeyTermination(void) {
 }
 
 static void TestSettings(void) {
-  const char* cases[] = {
+  static const char* cases[] = {
     "apple",
       "Apple"
   };
 
-  const char* locales[] = {
+  static const char* locales[] = {
     "",
       "en"
   };
@@ -3886,179 +4022,183 @@ static void TestSettings(void) {
 }
 
 static int32_t TestEqualsForCollator(const char* locName, UCollator *source, UCollator *target) {
-  UErrorCode status = U_ZERO_ERROR;
-  int32_t errorNo = 0;
-  /*const UChar *sourceRules = NULL;*/
-  /*int32_t sourceRulesLen = 0;*/
-  UColAttributeValue french = UCOL_OFF;
-  int32_t cloneSize = 0;
-
-  if(!ucol_equals(source, target)) {
-    log_err("Same collators, different address not equal\n");
-    errorNo++;
-  }
-  ucol_close(target);
-  if(uprv_strcmp(ucol_getLocale(source, ULOC_REQUESTED_LOCALE, &status), ucol_getLocale(source, ULOC_ACTUAL_LOCALE, &status)) == 0) {
-    /* currently, safeClone is implemented through getRules/openRules
-     * so it is the same as the test below - I will comment that test out.
-     */
-    /* real thing */
-    target = ucol_safeClone(source, NULL, &cloneSize, &status);
-    if(U_FAILURE(status)) {
-      log_err("Error creating clone\n");
-      errorNo++;
-      return errorNo;
-    }
+    UErrorCode status = U_ZERO_ERROR;
+    int32_t errorNo = 0;
+    /*const UChar *sourceRules = NULL;*/
+    /*int32_t sourceRulesLen = 0;*/
+    UColAttributeValue french = UCOL_OFF;
+    int32_t cloneSize = 0;
+
     if(!ucol_equals(source, target)) {
-      log_err("Collator different from it's clone\n");
-      errorNo++;
-    }
-    french = ucol_getAttribute(source, UCOL_FRENCH_COLLATION, &status);
-    if(french == UCOL_ON) {
-      ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_OFF, &status);
-    } else {
-      ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
-    }
-    if(U_FAILURE(status)) {
-      log_err("Error setting attributes\n");
-      errorNo++;
-      return errorNo;
-    }
-    if(ucol_equals(source, target)) {
-      log_err("Collators same even when options changed\n");
-      errorNo++;
+        log_err("Same collators, different address not equal\n");
+        errorNo++;
     }
     ucol_close(target);
-    /* commented out since safeClone uses exactly the same technique */
-    /*
-    sourceRules = ucol_getRules(source, &sourceRulesLen);
-    target = ucol_openRules(sourceRules, sourceRulesLen, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
-    if(U_FAILURE(status)) {
-      log_err("Error instantiating target from rules\n");
-      errorNo++;
-      return errorNo;
-    }
-    if(!ucol_equals(source, target)) {
-      log_err("Collator different from collator that was created from the same rules\n");
-      errorNo++;
+    if(uprv_strcmp(ucol_getLocaleByType(source, ULOC_REQUESTED_LOCALE, &status), ucol_getLocaleByType(source, ULOC_ACTUAL_LOCALE, &status)) == 0) {
+        /* currently, safeClone is implemented through getRules/openRules
+        * so it is the same as the test below - I will comment that test out.
+        */
+        /* real thing */
+        target = ucol_safeClone(source, NULL, &cloneSize, &status);
+        if(U_FAILURE(status)) {
+            log_err("Error creating clone\n");
+            errorNo++;
+            return errorNo;
+        }
+        if(!ucol_equals(source, target)) {
+            log_err("Collator different from it's clone\n");
+            errorNo++;
+        }
+        french = ucol_getAttribute(source, UCOL_FRENCH_COLLATION, &status);
+        if(french == UCOL_ON) {
+            ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_OFF, &status);
+        } else {
+            ucol_setAttribute(target, UCOL_FRENCH_COLLATION, UCOL_ON, &status);
+        }
+        if(U_FAILURE(status)) {
+            log_err("Error setting attributes\n");
+            errorNo++;
+            return errorNo;
+        }
+        if(ucol_equals(source, target)) {
+            log_err("Collators same even when options changed\n");
+            errorNo++;
+        }
+        ucol_close(target);
+        /* commented out since safeClone uses exactly the same technique */
+        /*
+        sourceRules = ucol_getRules(source, &sourceRulesLen);
+        target = ucol_openRules(sourceRules, sourceRulesLen, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
+        if(U_FAILURE(status)) {
+        log_err("Error instantiating target from rules\n");
+        errorNo++;
+        return errorNo;
+        }
+        if(!ucol_equals(source, target)) {
+        log_err("Collator different from collator that was created from the same rules\n");
+        errorNo++;
+        }
+        ucol_close(target);
+        */
     }
-    ucol_close(target);
-    */
-  }
-  return errorNo;
+    return errorNo;
 }
 
 
 static void TestEquals(void) {
-  /* ucol_equals is not currently a public API. There is a chance that it will become
-   * something like this, but currently it is only used by RuleBasedCollator::operator==
-   */
-  /* test whether the two collators instantiated from the same locale are equal */
-  UErrorCode status = U_ZERO_ERROR;
-  UParseError parseError;
-  int32_t noOfLoc = uloc_countAvailable();
-  const char *locName = NULL;
-  UCollator *source = NULL, *target = NULL;
-  int32_t i = 0;
+    /* ucol_equals is not currently a public API. There is a chance that it will become
+    * something like this, but currently it is only used by RuleBasedCollator::operator==
+    */
+    /* test whether the two collators instantiated from the same locale are equal */
+    UErrorCode status = U_ZERO_ERROR;
+    UParseError parseError;
+    int32_t noOfLoc = uloc_countAvailable();
+    const char *locName = NULL;
+    UCollator *source = NULL, *target = NULL;
+    int32_t i = 0;
 
-  const char* rules[] = {
-    "&l < lj <<< Lj <<< LJ",
-      "&n < nj <<< Nj <<< NJ",
-      "&ae <<< \\u00e4",
-      "&AE <<< \\u00c4"
-  };
-  /*
-  const char* badRules[] = {
+    const char* rules[] = {
+        "&l < lj <<< Lj <<< LJ",
+        "&n < nj <<< Nj <<< NJ",
+        "&ae <<< \\u00e4",
+        "&AE <<< \\u00c4"
+    };
+    /*
+    const char* badRules[] = {
     "&l <<< Lj",
-      "&n < nj <<< nJ <<< NJ",
-      "&a <<< \\u00e4",
-      "&AE <<< \\u00c4 <<< x"
-  };
-  */
+    "&n < nj <<< nJ <<< NJ",
+    "&a <<< \\u00e4",
+    "&AE <<< \\u00c4 <<< x"
+    };
+    */
 
-  UChar sourceRules[1024], targetRules[1024];
-  int32_t sourceRulesSize = 0, targetRulesSize = 0;
-  int32_t rulesSize = sizeof(rules)/sizeof(rules[0]);
+    UChar sourceRules[1024], targetRules[1024];
+    int32_t sourceRulesSize = 0, targetRulesSize = 0;
+    int32_t rulesSize = sizeof(rules)/sizeof(rules[0]);
 
-  for(i = 0; i < rulesSize; i++) {
-    sourceRulesSize += u_unescape(rules[i], sourceRules+sourceRulesSize, 1024 - sourceRulesSize);
-    targetRulesSize += u_unescape(rules[rulesSize-i-1], targetRules+targetRulesSize, 1024 - targetRulesSize);
-  }
+    for(i = 0; i < rulesSize; i++) {
+        sourceRulesSize += u_unescape(rules[i], sourceRules+sourceRulesSize, 1024 - sourceRulesSize);
+        targetRulesSize += u_unescape(rules[rulesSize-i-1], targetRules+targetRulesSize, 1024 - targetRulesSize);
+    }
 
-  source = ucol_openRules(sourceRules, sourceRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
-  if(status == U_FILE_ACCESS_ERROR) {
-    log_data_err("Is your data around?\n");
-    return;
-  } else if(U_FAILURE(status)) {
-    log_err("Error opening collator\n");
-    return;
-  }
-  target = ucol_openRules(targetRules, targetRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
-  if(!ucol_equals(source, target)) {
-    log_err("Equivalent collators not equal!\n");
-  }
-  ucol_close(source);
-  ucol_close(target);
-
-  source = ucol_open("root", &status);
-  target = ucol_open("root", &status);
-  log_verbose("Testing root\n");
-  if(!ucol_equals(source, source)) {
-    log_err("Same collator not equal\n");
-  }
-  if(TestEqualsForCollator(locName, source, target)) {
-    log_err("Errors for root\n", locName);
-  }
-  ucol_close(source);
+    source = ucol_openRules(sourceRules, sourceRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
+    if(status == U_FILE_ACCESS_ERROR) {
+        log_data_err("Is your data around?\n");
+        return;
+    } else if(U_FAILURE(status)) {
+        log_err("Error opening collator\n");
+        return;
+    }
+    target = ucol_openRules(targetRules, targetRulesSize, UCOL_DEFAULT, UCOL_DEFAULT, &parseError, &status);
+    if(!ucol_equals(source, target)) {
+        log_err("Equivalent collators not equal!\n");
+    }
+    ucol_close(source);
+    ucol_close(target);
 
-  for(i = 0; i<noOfLoc; i++) {
-    status = U_ZERO_ERROR;
-    locName = uloc_getAvailable(i);
-    /*if(hasCollationElements(locName)) {*/
-      log_verbose("Testing equality for locale %s\n", locName);
-      source = ucol_open(locName, &status);
-      target = ucol_open(locName, &status);
-      if(TestEqualsForCollator(locName, source, target)) {
-        log_err("Errors for locale %s\n", locName);
-      }
-      ucol_close(source);
-    /*}*/
-  }
+    source = ucol_open("root", &status);
+    target = ucol_open("root", &status);
+    log_verbose("Testing root\n");
+    if(!ucol_equals(source, source)) {
+        log_err("Same collator not equal\n");
+    }
+    if(TestEqualsForCollator(locName, source, target)) {
+        log_err("Errors for root\n", locName);
+    }
+    ucol_close(source);
+
+    for(i = 0; i<noOfLoc; i++) {
+        status = U_ZERO_ERROR;
+        locName = uloc_getAvailable(i);
+        /*if(hasCollationElements(locName)) {*/
+        log_verbose("Testing equality for locale %s\n", locName);
+        source = ucol_open(locName, &status);
+        target = ucol_open(locName, &status);
+        if (U_FAILURE(status)) {
+            log_err("Error opening collator for locale %s  %s\n", locName, u_errorName(status));
+            continue;
+        }
+        if(TestEqualsForCollator(locName, source, target)) {
+            log_err("Errors for locale %s\n", locName);
+        }
+        ucol_close(source);
+        /*}*/
+    }
 }
 
 static void TestJ2726(void) {
-  UChar a[2] = { 0x61, 0x00 }; /*"a"*/
-  UChar aSpace[3] = { 0x61, 0x20, 0x00 }; /*"a "*/
-  UChar spaceA[3] = { 0x20, 0x61, 0x00 }; /*" a"*/
-  UErrorCode status = U_ZERO_ERROR;
-  UCollator *coll = ucol_open("en", &status);
-  ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
-  ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status);
-  doTest(coll, a, aSpace, UCOL_EQUAL);
-  doTest(coll, aSpace, a, UCOL_EQUAL);
-  doTest(coll, a, spaceA, UCOL_EQUAL);
-  doTest(coll, spaceA, a, UCOL_EQUAL);
-  doTest(coll, spaceA, aSpace, UCOL_EQUAL);
-  doTest(coll, aSpace, spaceA, UCOL_EQUAL);
-  ucol_close(coll);
+    UChar a[2] = { 0x61, 0x00 }; /*"a"*/
+    UChar aSpace[3] = { 0x61, 0x20, 0x00 }; /*"a "*/
+    UChar spaceA[3] = { 0x20, 0x61, 0x00 }; /*" a"*/
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator *coll = ucol_open("en", &status);
+    ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
+    ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status);
+    doTest(coll, a, aSpace, UCOL_EQUAL);
+    doTest(coll, aSpace, a, UCOL_EQUAL);
+    doTest(coll, a, spaceA, UCOL_EQUAL);
+    doTest(coll, spaceA, a, UCOL_EQUAL);
+    doTest(coll, spaceA, aSpace, UCOL_EQUAL);
+    doTest(coll, aSpace, spaceA, UCOL_EQUAL);
+    ucol_close(coll);
 }
 
 static void NullRule(void) {
-  UChar r[3] = {0};
-  UErrorCode status = U_ZERO_ERROR;
-  UCollator *coll = ucol_openRules(r, 1, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
-  if(U_SUCCESS(status)) {
-    log_err("This should have been an error!\n");
-    ucol_close(coll);
-  } else {
-    status = U_ZERO_ERROR;
-  }
-  coll = ucol_openRules(r, 0, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
-  if(U_FAILURE(status)) {
-    log_err("Empty rules should have produced a valid collator\n");
-  } else {
-    ucol_close(coll);
-  }
+    UChar r[3] = {0};
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator *coll = ucol_openRules(r, 1, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
+    if(U_SUCCESS(status)) {
+        log_err("This should have been an error!\n");
+        ucol_close(coll);
+    } else {
+        status = U_ZERO_ERROR;
+    }
+    coll = ucol_openRules(r, 0, UCOL_DEFAULT, UCOL_DEFAULT, NULL, &status);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "Empty rules should have produced a valid collator -> %s\n", u_errorName(status));
+    } else {
+        ucol_close(coll);
+    }
 }
 
 /**
@@ -4098,6 +4238,22 @@ static void TestNumericCollation(void)
     "avery42949672961",
     "avery42949672962",
     "avery429496729610"
+    };
+
+     const static char *longNumericStrings[]={
+     /* Some of these sort out of the order that would expected if digits-as-numbers handled arbitrarily-long digit strings.
+        In fact, a single collation element can represent a maximum of 254 digits as a number. Digit strings longer than that
+        are treated as multiple collation elements. */
+    "num9234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123z", /*253digits, num + 9.23E252 + z */
+    "num10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", /*254digits, num + 1.00E253 */
+    "num100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", /*255digits, num + 1.00E253 + 0, out of numeric order but expected */
+    "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", /*254digits, num + 1.23E253 */
+    "num123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345", /*255digits, num + 1.23E253 + 5 */
+    "num1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456", /*256digits, num + 1.23E253 + 56 */
+    "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567", /*257digits, num + 1.23E253 + 567 */
+    "num12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234a", /*254digits, num + 1.23E253 + a, out of numeric order but expected */
+    "num92345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", /*254digits, num + 9.23E253, out of numeric order but expected */
+    "num92345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234a", /*254digits, num + 9.23E253 + a, out of numeric order but expected */
     };
 
     const static char *supplementaryDigits[] = {
@@ -4141,12 +4297,13 @@ static void TestNumericCollation(void)
     /* Open our collator. */
     UCollator* coll = ucol_open("root", &status);
     if (U_FAILURE(status)){
-        log_err("ERROR: in using ucol_open()\n %s\n",
+        log_err_status(status, "ERROR: in using ucol_open() -> %s\n",
               myErrorName(status));
         return;
     }
     genericLocaleStarterWithOptions("root", basicTestStrings, sizeof(basicTestStrings)/sizeof(basicTestStrings[0]), &att, &val, 1);
     genericLocaleStarterWithOptions("root", thirtyTwoBitNumericStrings, sizeof(thirtyTwoBitNumericStrings)/sizeof(thirtyTwoBitNumericStrings[0]), &att, &val, 1);
+    genericLocaleStarterWithOptions("root", longNumericStrings, sizeof(longNumericStrings)/sizeof(longNumericStrings[0]), &att, &val, 1);
     genericLocaleStarterWithOptions("en_US", foreignDigits, sizeof(foreignDigits)/sizeof(foreignDigits[0]), &att, &val, 1);
     genericLocaleStarterWithOptions("root", supplementaryDigits, sizeof(supplementaryDigits)/sizeof(supplementaryDigits[0]), &att, &val, 1);
     genericLocaleStarterWithOptions("root", evenZeroes, sizeof(evenZeroes)/sizeof(evenZeroes[0]), &att, &val, 1);
@@ -4226,7 +4383,7 @@ static void TestImplicitGeneration(void) {
 
     UCollator *coll = ucol_open("root", &status);
     if(U_FAILURE(status)) {
-        log_err("Couldn't open UCA\n");
+        log_err_status(status, "Couldn't open UCA -> %s\n", u_errorName(status));
         return;
     }
 
@@ -4339,35 +4496,48 @@ static void TestSeparateTrees(void) {
 
 #if !UCONFIG_NO_SERVICE
     e = ucol_openAvailableLocales(&ec);
-    assertSuccess("ucol_openAvailableLocales", &ec);
-    assertTrue("ucol_openAvailableLocales!=0", e!=0);
-    n = checkUEnumeration("ucol_openAvailableLocales", e, AVAIL, LEN(AVAIL));
-    /* Don't need to check n because we check list */
-    uenum_close(e);
+    if (e != NULL) {
+        assertSuccess("ucol_openAvailableLocales", &ec);
+        assertTrue("ucol_openAvailableLocales!=0", e!=0);
+        n = checkUEnumeration("ucol_openAvailableLocales", e, AVAIL, LEN(AVAIL));
+        /* Don't need to check n because we check list */
+        uenum_close(e);
+    } else {
+        log_data_err("Error calling ucol_openAvailableLocales() -> %s (Are you missing data?)\n", u_errorName(ec));
+    }
 #endif
 
     e = ucol_getKeywords(&ec);
-    assertSuccess("ucol_getKeywords", &ec);
-    assertTrue("ucol_getKeywords!=0", e!=0);
-    n = checkUEnumeration("ucol_getKeywords", e, KW, LEN(KW));
-    /* Don't need to check n because we check list */
-    uenum_close(e);
+    if (e != NULL) {
+        assertSuccess("ucol_getKeywords", &ec);
+        assertTrue("ucol_getKeywords!=0", e!=0);
+        n = checkUEnumeration("ucol_getKeywords", e, KW, LEN(KW));
+        /* Don't need to check n because we check list */
+        uenum_close(e);
+    } else {
+        log_data_err("Error calling ucol_getKeywords() -> %s (Are you missing data?)\n", u_errorName(ec));
+    }
 
     e = ucol_getKeywordValues(KW[0], &ec);
-    assertSuccess("ucol_getKeywordValues", &ec);
-    assertTrue("ucol_getKeywordValues!=0", e!=0);
-    n = checkUEnumeration("ucol_getKeywordValues", e, KWVAL, LEN(KWVAL));
-    /* Don't need to check n because we check list */
-    uenum_close(e);
+    if (e != NULL) {
+        assertSuccess("ucol_getKeywordValues", &ec);
+        assertTrue("ucol_getKeywordValues!=0", e!=0);
+        n = checkUEnumeration("ucol_getKeywordValues", e, KWVAL, LEN(KWVAL));
+        /* Don't need to check n because we check list */
+        uenum_close(e);
+    } else {
+        log_data_err("Error calling ucol_getKeywordValues() -> %s (Are you missing data?)\n", u_errorName(ec));
+    }
 
     /* Try setting a warning before calling ucol_getKeywordValues */
     ec = U_USING_FALLBACK_WARNING;
     e = ucol_getKeywordValues(KW[0], &ec);
-    assertSuccess("ucol_getKeywordValues [with warning code set]", &ec);
-    assertTrue("ucol_getKeywordValues!=0 [with warning code set]", e!=0);
-    n = checkUEnumeration("ucol_getKeywordValues [with warning code set]", e, KWVAL, LEN(KWVAL));
-    /* Don't need to check n because we check list */
-    uenum_close(e);
+    if (assertSuccess("ucol_getKeywordValues [with warning code set]", &ec)) {
+        assertTrue("ucol_getKeywordValues!=0 [with warning code set]", e!=0);
+        n = checkUEnumeration("ucol_getKeywordValues [with warning code set]", e, KWVAL, LEN(KWVAL));
+        /* Don't need to check n because we check list */
+        uenum_close(e);
+    }
 
     /*
 U_DRAFT int32_t U_EXPORT2
@@ -4376,19 +4546,21 @@ ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity,
                              UErrorCode* status);
 }
 */
-    n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "fr",
+    n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "de",
                                      &isAvailable, &ec);
-    assertSuccess("getFunctionalEquivalent", &ec);
-    assertEquals("getFunctionalEquivalent(fr)", "fr", loc);
-    assertTrue("getFunctionalEquivalent(fr).isAvailable==TRUE",
-               isAvailable == TRUE);
+    if (assertSuccess("getFunctionalEquivalent", &ec)) {
+        assertEquals("getFunctionalEquivalent(de)", "de", loc);
+        assertTrue("getFunctionalEquivalent(de).isAvailable==TRUE",
+                   isAvailable == TRUE);
+    }
 
-    n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "fr_FR",
+    n = ucol_getFunctionalEquivalent(loc, sizeof(loc), "collation", "de_DE",
                                      &isAvailable, &ec);
-    assertSuccess("getFunctionalEquivalent", &ec);
-    assertEquals("getFunctionalEquivalent(fr_FR)", "fr", loc);
-    assertTrue("getFunctionalEquivalent(fr_FR).isAvailable==TRUE",
-               isAvailable == TRUE);
+    if (assertSuccess("getFunctionalEquivalent", &ec)) {
+        assertEquals("getFunctionalEquivalent(de_DE)", "de", loc);
+        assertTrue("getFunctionalEquivalent(de_DE).isAvailable==TRUE",
+                   isAvailable == TRUE);
+    }
 }
 
 /* supercedes TestJ784 */
@@ -4453,7 +4625,7 @@ static void TestBeforePinyin(void) {
 }
 
 static void TestBeforeTightening(void) {
-    struct {
+    static const struct {
         const char *rules;
         UErrorCode expectedStatus;
     } tests[] = {
@@ -4485,7 +4657,7 @@ static void TestBeforeTightening(void) {
         rlen = u_unescape(tests[i].rules, rlz, RULE_BUFFER_LEN);
         coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status);
         if(status != tests[i].expectedStatus) {
-            log_err("Opening a collator with rules %s returned error code %s, expected %s\n",
+            log_err_status(status, "Opening a collator with rules %s returned error code %s, expected %s\n",
                 tests[i].rules, u_errorName(status), u_errorName(tests[i].expectedStatus));
         }
         ucol_close(coll);
@@ -4558,9 +4730,9 @@ assert: m <<< x <<< X <<< \u24DC <<< M  << q <<< Q < z < n
 #if 0
 /* requires features not yet supported */
 static void TestMoreBefore(void) {
-    struct {
+    static const struct {
         const char* rules;
-        const char* order[20];
+        const char* order[16];
         int32_t size;
     } tests[] = {
         { "&m < a &[before 1] a < x <<< X << q <<< Q < z",
@@ -4618,7 +4790,7 @@ static void TestTailorNULL( void ) {
     coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status);
 
     if(U_FAILURE(status)) {
-        log_err("Could not open default collator!\n");
+        log_err_status(status, "Could not open default collator! -> %s\n", u_errorName(status));
     } else {
         res = ucol_strcoll(coll, &a, 1, &null, 1);
 
@@ -4630,33 +4802,6 @@ static void TestTailorNULL( void ) {
     ucol_close(coll);
 }
 
-static void
-TestThaiSortKey(void)
-{
-  UChar yamakan = 0x0E4E;
-  UErrorCode status = U_ZERO_ERROR;
-  uint8_t key[256];
-  int32_t keyLen = 0;
-  /* NOTE: there is a Thai tailoring that moves Yammakan. It should not move it, */
-  /* since it stays in the same relative position. This should be addressed in CLDR */
-  /* UCA 4.0 uint8_t expectedKey[256] = { 0x01, 0xd9, 0xb2, 0x01, 0x05, 0x00 }; */
-  /* UCA 4.1 uint8_t expectedKey[256] = { 0x01, 0xdb, 0x3a, 0x01, 0x05, 0x00 }; */
-  /* UCA 5.0 moves Yammakan */
-  uint8_t expectedKey[256] = { 0x01, 0xdc, 0xce, 0x01, 0x05, 0x00 }; 
-  UCollator *coll = ucol_open("th", &status);
-  if(U_FAILURE(status)) {
-    log_err("Could not open a collator, exiting (%s)\n", u_errorName(status));
-    return;
-  }
-
-  keyLen = ucol_getSortKey(coll, &yamakan, 1, key, 256);
-  if(strcmp((char *)key, (char *)expectedKey)) {
-    log_err("Yammakan key is different from ICU 34!\n");
-  }
-
-  ucol_close(coll);
-}
-
 static void
 TestUpperFirstQuaternary(void)
 {
@@ -4698,7 +4843,7 @@ TestJ5223(void)
   static UCollator *coll = NULL;
   coll = ucol_open("root", &status);
   if(U_FAILURE(status)) {
-    log_err("Couldn't open UCA\n");
+    log_err_status(status, "Couldn't open UCA -> %s\n", u_errorName(status));
     return;
   }
   ucol_setStrength(coll, UCOL_PRIMARY);
@@ -4707,7 +4852,7 @@ TestJ5223(void)
   if (U_FAILURE(status)) {
     log_err("Failed setting atributes\n");
     return;
-  } 
+  }
   sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, NULL, 0);
   if (sortkey_length > 256) return;
 
@@ -4746,81 +4891,1648 @@ TestJ5232(void)
         "\\u0e40\\u0e01\\u0e47\\u0e1a\\u0e40\\u0e25\\u0e47\\u0e21",
         "\\u0e40\\u0e01\\u0e47\\u0e1a\\u0e40\\u0e25\\u0e48\\u0e21"
     };
-    
+
     genericLocaleStarter("th", test, sizeof(test)/sizeof(test[0]));
 }
 
+static void
+TestJ5367(void)
+{
+    const static char *test[] = { "a", "y" };
+    const char* rules = "&Ny << Y &[first secondary ignorable] <<< a";
+    genericRulesStarter(rules, test, sizeof(test)/sizeof(test[0]));
+}
+
+static void
+TestVI5913(void)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    int32_t i, j;
+    UCollator *coll =NULL;
+    uint8_t  resColl[100], expColl[100];
+    int32_t  rLen, tLen, ruleLen, sLen, kLen;
+    UChar rule[256]={0x26, 0x62, 0x3c, 0x1FF3, 0};  /* &a<0x1FF3-omega with Ypogegrammeni*/
+    UChar rule2[256]={0x26, 0x7a, 0x3c, 0x0161, 0};  /* &z<s with caron*/
+    UChar rule3[256]={0x26, 0x7a, 0x3c, 0x0061, 0x00ea, 0};  /* &z<a+e with circumflex.*/
+    static const UChar tData[][20]={
+        {0x1EAC, 0},
+        {0x0041, 0x0323, 0x0302, 0},
+        {0x1EA0, 0x0302, 0},
+        {0x00C2, 0x0323, 0},
+        {0x1ED8, 0},  /* O with dot and circumflex */
+        {0x1ECC, 0x0302, 0},
+        {0x1EB7, 0},
+        {0x1EA1, 0x0306, 0},
+    };
+    static const UChar tailorData[][20]={
+        {0x1FA2, 0},  /* Omega with 3 combining marks */
+        {0x03C9, 0x0313, 0x0300, 0x0345, 0},
+        {0x1FF3, 0x0313, 0x0300, 0},
+        {0x1F60, 0x0300, 0x0345, 0},
+        {0x1F62, 0x0345, 0},
+        {0x1FA0, 0x0300, 0},
+    };
+    static const UChar tailorData2[][20]={
+        {0x1E63, 0x030C, 0},  /* s with dot below + caron */
+        {0x0073, 0x0323, 0x030C, 0},
+        {0x0073, 0x030C, 0x0323, 0},
+    };
+    static const UChar tailorData3[][20]={
+        {0x007a, 0},  /*  z */
+        {0x0061, 0x0065, 0},  /*  a + e */
+        {0x0061, 0x00ea, 0}, /* a + e with circumflex */
+        {0x0061, 0x1EC7, 0},  /* a+ e with dot below and circumflex */
+        {0x0061, 0x1EB9, 0x0302, 0}, /* a + e with dot below + combining circumflex */
+        {0x0061, 0x00EA, 0x0323, 0},  /* a + e with circumflex + combining dot below */
+        {0x00EA, 0x0323, 0},  /* e with circumflex + combining dot below */
+        {0x00EA, 0},  /* e with circumflex  */
+    };
 
+    /* Test Vietnamese sort. */
+    coll = ucol_open("vi", &status);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "Couldn't open collator -> %s\n", u_errorName(status));
+        return;
+    }
+    log_verbose("\n\nVI collation:");
+    if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[2], u_strlen(tData[2])) ) {
+        log_err("\\u1EAC not equals to \\u1EA0+\\u0302\n");
+    }
+    if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[3], u_strlen(tData[3])) ) {
+        log_err("\\u1EAC not equals to \\u00c2+\\u0323\n");
+    }
+    if ( !ucol_equal(coll, tData[5], u_strlen(tData[5]), tData[4], u_strlen(tData[4])) ) {
+        log_err("\\u1ED8 not equals to \\u1ECC+\\u0302\n");
+    }
+    if ( !ucol_equal(coll, tData[7], u_strlen(tData[7]), tData[6], u_strlen(tData[6])) ) {
+        log_err("\\u1EB7 not equals to \\u1EA1+\\u0306\n");
+    }
 
-#define TEST(x) addTest(root, &x, "tscoll/cmsccoll/" # x)
+    for (j=0; j<8; j++) {
+        tLen = u_strlen(tData[j]);
+        log_verbose("\n Data :%s  \tlen: %d key: ", tData[j], tLen);
+        rLen = ucol_getSortKey(coll, tData[j], tLen, resColl, 100);
+        for(i = 0; i<rLen; i++) {
+            log_verbose(" %02X", resColl[i]);
+        }
+    }
 
-void addMiscCollTest(TestNode** root)
-{
-    TEST(TestRuleOptions);
-    TEST(TestBeforePrefixFailure);
-    TEST(TestContractionClosure);
-    TEST(TestPrefixCompose);
-    TEST(TestStrCollIdenticalPrefix);
-    TEST(TestPrefix);
-    TEST(TestNewJapanese);
-    /*TEST(TestLimitations);*/
-    TEST(TestNonChars);
-    TEST(TestExtremeCompression);
-    TEST(TestSurrogates);
-    TEST(TestVariableTopSetting);
-    TEST(TestBocsuCoverage);
-    TEST(TestCyrillicTailoring);
-    TEST(TestCase);
-    TEST(IncompleteCntTest);
-    TEST(BlackBirdTest);
-    TEST(FunkyATest);
-    TEST(BillFairmanTest);
-    TEST(RamsRulesTest);
-    TEST(IsTailoredTest);
-    TEST(TestCollations);
-    TEST(TestChMove);
-    TEST(TestImplicitTailoring);
-    TEST(TestFCDProblem);
-    TEST(TestEmptyRule);
-    /*TEST(TestJ784);*/ /* 'zh' locale has changed - now it is getting tested by TestBeforePinyin */
-    TEST(TestJ815);
-    /*TEST(TestJ831);*/ /* we changed lv locale */
-    TEST(TestBefore);
-    TEST(TestRedundantRules);
-    TEST(TestExpansionSyntax);
-    TEST(TestHangulTailoring);
-    TEST(TestUCARules);
-    TEST(TestIncrementalNormalize);
-    TEST(TestComposeDecompose);
-    TEST(TestCompressOverlap);
-    TEST(TestContraction);
-    TEST(TestExpansion);
-    /*TEST(PrintMarkDavis);*/ /* this test doesn't test - just prints sortkeys */
-    /*TEST(TestGetCaseBit);*/ /*this one requires internal things to be exported */
-    TEST(TestOptimize);
-    TEST(TestSuppressContractions);
-    TEST(Alexis2);
-    TEST(TestHebrewUCA);
-    TEST(TestPartialSortKeyTermination);
-    TEST(TestSettings);
-    TEST(TestEquals);
-    TEST(TestJ2726);
-    TEST(NullRule);
-    TEST(TestNumericCollation);
-    TEST(TestTibetanConformance);
-    TEST(TestPinyinProblem);
-    TEST(TestImplicitGeneration);
-    TEST(TestSeparateTrees);
-    TEST(TestBeforePinyin);
-    TEST(TestBeforeTightening);
-    /*TEST(TestMoreBefore);*/
-    TEST(TestTailorNULL);
-    TEST(TestThaiSortKey);
-    TEST(TestUpperFirstQuaternary);
-    TEST(TestJ4960);
-    TEST(TestJ5223);
-    TEST(TestJ5232);
+    ucol_close(coll);
+
+    /* Test Romanian sort. */
+    coll = ucol_open("ro", &status);
+    log_verbose("\n\nRO collation:");
+    if ( !ucol_equal(coll, tData[0], u_strlen(tData[0]), tData[1], u_strlen(tData[1])) ) {
+        log_err("\\u1EAC not equals to \\u1EA0+\\u0302\n");
+    }
+    if ( !ucol_equal(coll, tData[4], u_strlen(tData[4]), tData[5], u_strlen(tData[5])) ) {
+        log_err("\\u1EAC not equals to \\u00c2+\\u0323\n");
+    }
+    if ( !ucol_equal(coll, tData[6], u_strlen(tData[6]), tData[7], u_strlen(tData[7])) ) {
+        log_err("\\u1EB7 not equals to \\u1EA1+\\u0306\n");
+    }
+
+    for (j=4; j<8; j++) {
+        tLen = u_strlen(tData[j]);
+        log_verbose("\n Data :%s  \tlen: %d key: ", tData[j], tLen);
+        rLen = ucol_getSortKey(coll, tData[j], tLen, resColl, 100);
+        for(i = 0; i<rLen; i++) {
+            log_verbose(" %02X", resColl[i]);
+        }
+    }
+    ucol_close(coll);
+
+    /* Test the precomposed Greek character with 3 combining marks. */
+    log_verbose("\n\nTailoring test: Greek character with 3 combining marks");
+    ruleLen = u_strlen(rule);
+    coll = ucol_openRules(rule, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+    if (U_FAILURE(status)) {
+        log_err("ucol_openRules failed with %s\n", u_errorName(status));
+        return;
+    }
+    sLen = u_strlen(tailorData[0]);
+    for (j=1; j<6; j++) {
+        tLen = u_strlen(tailorData[j]);
+        if ( !ucol_equal(coll, tailorData[0], sLen, tailorData[j], tLen))  {
+            log_err("\n \\u1FA2 not equals to data[%d]:%s\n", j, tailorData[j]);
+        }
+    }
+    /* Test getSortKey. */
+    tLen = u_strlen(tailorData[0]);
+    kLen=ucol_getSortKey(coll, tailorData[0], tLen, expColl, 100);
+    for (j=0; j<6; j++) {
+        tLen = u_strlen(tailorData[j]);
+        rLen = ucol_getSortKey(coll, tailorData[j], tLen, resColl, 100);
+        if ( kLen!=rLen || uprv_memcmp(expColl, resColl, rLen*sizeof(uint8_t))!=0 ) {
+            log_err("\n Data[%d] :%s  \tlen: %d key: ", j, tailorData[j], tLen);
+            for(i = 0; i<rLen; i++) {
+                log_err(" %02X", resColl[i]);
+            }
+        }
+    }
+    ucol_close(coll);
+
+    log_verbose("\n\nTailoring test for s with caron:");
+    ruleLen = u_strlen(rule2);
+    coll = ucol_openRules(rule2, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+    tLen = u_strlen(tailorData2[0]);
+    kLen=ucol_getSortKey(coll, tailorData2[0], tLen, expColl, 100);
+    for (j=1; j<3; j++) {
+        tLen = u_strlen(tailorData2[j]);
+        rLen = ucol_getSortKey(coll, tailorData2[j], tLen, resColl, 100);
+        if ( kLen!=rLen || uprv_memcmp(expColl, resColl, rLen*sizeof(uint8_t))!=0 ) {
+            log_err("\n After tailoring Data[%d] :%s  \tlen: %d key: ", j, tailorData[j], tLen);
+            for(i = 0; i<rLen; i++) {
+                log_err(" %02X", resColl[i]);
+            }
+        }
+    }
+    ucol_close(coll);
+
+    log_verbose("\n\nTailoring test for &z< ae with circumflex:");
+    ruleLen = u_strlen(rule3);
+    coll = ucol_openRules(rule3, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+    tLen = u_strlen(tailorData3[3]);
+    kLen=ucol_getSortKey(coll, tailorData3[3], tLen, expColl, 100);
+    for (j=4; j<6; j++) {
+        tLen = u_strlen(tailorData3[j]);
+        rLen = ucol_getSortKey(coll, tailorData3[j], tLen, resColl, 100);
+
+        if ( kLen!=rLen || uprv_memcmp(expColl, resColl, rLen*sizeof(uint8_t))!=0 ) {
+            log_err("\n After tailoring Data[%d] :%s  \tlen: %d key: ", j, tailorData[j], tLen);
+            for(i = 0; i<rLen; i++) {
+                log_err(" %02X", resColl[i]);
+            }
+        }
+
+        log_verbose("\n Test Data[%d] :%s  \tlen: %d key: ", j, tailorData[j], tLen);
+         for(i = 0; i<rLen; i++) {
+             log_verbose(" %02X", resColl[i]);
+         }
+    }
+    ucol_close(coll);
 }
 
-#endif /* #if !UCONFIG_NO_COLLATION */
+static void
+TestTailor6179(void)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    int32_t i;
+    UCollator *coll =NULL;
+    uint8_t  resColl[100];
+    int32_t  rLen, tLen, ruleLen;
+    /* &[last primary ignorable]<< a  &[first primary ignorable]<<b */
+    UChar rule1[256]={0x26,0x5B,0x6C,0x61,0x73,0x74,0x20,0x70,0x72,0x69,0x6D,0x61,0x72,0x79,
+            0x20,0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,0x3C,0x20,0x61,0x20,
+            0x26,0x5B,0x66,0x69,0x72,0x73,0x74,0x20,0x70,0x72,0x69,0x6D,0x61,0x72,0x79,0x20,
+            0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,0x3C,0x62,0x20, 0};
+    /* &[last secondary ignorable]<<< a &[first secondary ignorable]<<<b */
+    UChar rule2[256]={0x26,0x5B,0x6C,0x61,0x73,0x74,0x20,0x73,0x65,0x63,0x6F,0x6E,0x64,0x61,
+            0x72,0x79,0x20,0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,0x3C,0x3C,
+            0x61,0x20,0x26,0x5B,0x66,0x69,0x72,0x73,0x74,0x20,0x73,0x65,0x63,0x6F,0x6E,
+            0x64,0x61,0x72,0x79,0x20,0x69,0x67,0x6E,0x6F,0x72,0x61,0x62,0x6C,0x65,0x5D,0x3C,
+            0x3C,0x3C,0x20,0x62,0};
+
+    UChar tData1[][20]={
+        {0x61, 0},
+        {0x62, 0},
+        { 0xFDD0,0x009E, 0}
+    };
+    UChar tData2[][20]={
+            {0x61, 0},
+            {0x62, 0},
+            { 0xFDD0,0x009E, 0}
+     };
+
+    /*
+     * These values from FractionalUCA.txt will change,
+     * and need to be updated here.
+     */
+    uint8_t firstPrimaryIgnCE[6]={1, 87, 1, 5, 1, 0};
+    uint8_t lastPrimaryIgnCE[6]={1, 0xE3, 0xC9, 1, 5, 0};
+    uint8_t firstSecondaryIgnCE[6]={1, 1, 0x3f, 0x03, 0};
+    uint8_t lastSecondaryIgnCE[6]={1, 1, 0x3f, 0x03, 0};
 
+    /* Test [Last Primary ignorable] */
+
+    log_verbose("\n\nTailoring test: &[last primary ignorable]<<a  &[first primary ignorable]<<b ");
+    ruleLen = u_strlen(rule1);
+    coll = ucol_openRules(rule1, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "Tailoring test: &[last primary ignorable] failed! -> %s\n", u_errorName(status));
+        return;
+    }
+    tLen = u_strlen(tData1[0]);
+    rLen = ucol_getSortKey(coll, tData1[0], tLen, resColl, 100);
+    if (uprv_memcmp(resColl, lastPrimaryIgnCE, uprv_min(rLen,6)) < 0) {
+        log_err("\n Data[%d] :%s  \tlen: %d key: ", 0, tData1[0], rLen);
+        for(i = 0; i<rLen; i++) {
+            log_err(" %02X", resColl[i]);
+        }
+    }
+    tLen = u_strlen(tData1[1]);
+    rLen = ucol_getSortKey(coll, tData1[1], tLen, resColl, 100);
+    if (uprv_memcmp(resColl, firstPrimaryIgnCE, uprv_min(rLen, 6)) < 0) {
+        log_err("\n Data[%d] :%s  \tlen: %d key: ", 1, tData1[1], rLen);
+        for(i = 0; i<rLen; i++) {
+            log_err(" %02X", resColl[i]);
+        }
+    }
+    ucol_close(coll);
+
+
+    /* Test [Last Secondary ignorable] */
+    log_verbose("\n\nTailoring test: &[last secondary ignorable]<<<a  &[first secondary ignorable]<<<b ");
+    ruleLen = u_strlen(rule1);
+    coll = ucol_openRules(rule2, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+    if (U_FAILURE(status)) {
+        log_err("Tailoring test: &[last primary ignorable] failed!");
+        return;
+    }
+    tLen = u_strlen(tData2[0]);
+    rLen = ucol_getSortKey(coll, tData2[0], tLen, resColl, 100);
+    log_verbose("\n Data[%d] :%s  \tlen: %d key: ", 0, tData2[0], rLen);
+    for(i = 0; i<rLen; i++) {
+        log_verbose(" %02X", resColl[i]);
+    }
+    if (uprv_memcmp(resColl, lastSecondaryIgnCE, uprv_min(rLen, 3)) < 0) {
+        log_err("\n Data[%d] :%s  \tlen: %d key: ", 0, tData2[0], rLen);
+        for(i = 0; i<rLen; i++) {
+            log_err(" %02X", resColl[i]);
+        }
+    }
+    tLen = u_strlen(tData2[1]);
+    rLen = ucol_getSortKey(coll, tData2[1], tLen, resColl, 100);
+    log_verbose("\n Data[%d] :%s  \tlen: %d key: ", 1, tData2[1], rLen);
+    for(i = 0; i<rLen; i++) {
+        log_verbose(" %02X", resColl[i]);
+    }
+    if (uprv_memcmp(resColl, firstSecondaryIgnCE, uprv_min(rLen, 4)) < 0) {
+        log_err("\n Data[%d] :%s  \tlen: %d key: ", 1, tData2[1], rLen);
+        for(i = 0; i<rLen; i++) {
+            log_err(" %02X", resColl[i]);
+        }
+    }
+    ucol_close(coll);
+}
+
+static void
+TestUCAPrecontext(void)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    int32_t i, j;
+    UCollator *coll =NULL;
+    uint8_t  resColl[100], prevColl[100];
+    int32_t  rLen, tLen, ruleLen;
+    UChar rule1[256]= {0x26, 0xb7, 0x3c, 0x61, 0}; /* & middle-dot < a */
+    UChar rule2[256]= {0x26, 0x4C, 0xb7, 0x3c, 0x3c, 0x61, 0};
+    /* & l middle-dot << a  a is an expansion. */
+
+    UChar tData1[][20]={
+            { 0xb7, 0},  /* standalone middle dot(0xb7) */
+            { 0x387, 0}, /* standalone middle dot(0x387) */
+            { 0x61, 0},  /* a */
+            { 0x6C, 0},  /* l */
+            { 0x4C, 0x0332, 0},  /* l with [first primary ignorable] */
+            { 0x6C, 0xb7, 0},  /* l with middle dot(0xb7) */
+            { 0x6C, 0x387, 0}, /* l with middle dot(0x387) */
+            { 0x4C, 0xb7, 0},  /* L with middle dot(0xb7) */
+            { 0x4C, 0x387, 0}, /* L with middle dot(0x387) */
+            { 0x6C, 0x61, 0x387, 0}, /* la  with middle dot(0x387) */
+            { 0x4C, 0x61, 0xb7, 0},  /* La with middle dot(0xb7) */
+     };
+
+    log_verbose("\n\nEN collation:");
+    coll = ucol_open("en", &status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "Tailoring test: &z <<a|- failed! -> %s\n", u_errorName(status));
+        return;
+    }
+    for (j=0; j<11; j++) {
+        tLen = u_strlen(tData1[j]);
+        rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
+        if ((j>0) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
+            log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
+                    j, tData1[j]);
+        }
+        log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
+        for(i = 0; i<rLen; i++) {
+            log_verbose(" %02X", resColl[i]);
+        }
+        uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
+     }
+     ucol_close(coll);
+
+
+     log_verbose("\n\nJA collation:");
+     coll = ucol_open("ja", &status);
+     if (U_FAILURE(status)) {
+         log_err("Tailoring test: &z <<a|- failed!");
+         return;
+     }
+     for (j=0; j<11; j++) {
+         tLen = u_strlen(tData1[j]);
+         rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
+         if ((j>0) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
+             log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
+                     j, tData1[j]);
+         }
+         log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
+         for(i = 0; i<rLen; i++) {
+             log_verbose(" %02X", resColl[i]);
+         }
+         uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
+      }
+      ucol_close(coll);
+
+
+      log_verbose("\n\nTailoring test: & middle dot < a ");
+      ruleLen = u_strlen(rule1);
+      coll = ucol_openRules(rule1, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+      if (U_FAILURE(status)) {
+          log_err("Tailoring test: & middle dot < a failed!");
+          return;
+      }
+      for (j=0; j<11; j++) {
+          tLen = u_strlen(tData1[j]);
+          rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
+          if ((j>0) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
+              log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
+                      j, tData1[j]);
+          }
+          log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
+          for(i = 0; i<rLen; i++) {
+              log_verbose(" %02X", resColl[i]);
+          }
+          uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
+       }
+       ucol_close(coll);
+
+
+       log_verbose("\n\nTailoring test: & l middle-dot << a ");
+       ruleLen = u_strlen(rule2);
+       coll = ucol_openRules(rule2, ruleLen, UCOL_OFF, UCOL_TERTIARY, NULL,&status);
+       if (U_FAILURE(status)) {
+           log_err("Tailoring test: & l middle-dot << a failed!");
+           return;
+       }
+       for (j=0; j<11; j++) {
+           tLen = u_strlen(tData1[j]);
+           rLen = ucol_getSortKey(coll, tData1[j], tLen, resColl, 100);
+           if ((j>0) && (j!=3) && (strcmp((char *)resColl, (char *)prevColl)<0)) {
+               log_err("\n Expecting greater key than previous test case: Data[%d] :%s.",
+                       j, tData1[j]);
+           }
+           if ((j==3)&&(strcmp((char *)resColl, (char *)prevColl)>0)) {
+               log_err("\n Expecting smaller key than previous test case: Data[%d] :%s.",
+                       j, tData1[j]);
+           }
+           log_verbose("\n Data[%d] :%s  \tlen: %d key: ", j, tData1[j], rLen);
+           for(i = 0; i<rLen; i++) {
+               log_verbose(" %02X", resColl[i]);
+           }
+           uprv_memcpy(prevColl, resColl, sizeof(uint8_t)*(rLen+1));
+        }
+        ucol_close(coll);
+}
+
+static void
+TestOutOfBuffer5468(void)
+{
+    static const char *test = "\\u4e00";
+    UChar ustr[256];
+    int32_t ustr_length = u_unescape(test, ustr, 256);
+    unsigned char shortKeyBuf[1];
+    int32_t sortkey_length;
+    UErrorCode status = U_ZERO_ERROR;
+    static UCollator *coll = NULL;
+
+    coll = ucol_open("root", &status);
+    if(U_FAILURE(status)) {
+      log_err_status(status, "Couldn't open UCA -> %s\n", u_errorName(status));
+      return;
+    }
+    ucol_setStrength(coll, UCOL_PRIMARY);
+    ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_PRIMARY, &status);
+    ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
+    if (U_FAILURE(status)) {
+      log_err("Failed setting atributes\n");
+      return;
+    }
+
+    sortkey_length = ucol_getSortKey(coll, ustr, ustr_length, shortKeyBuf, sizeof(shortKeyBuf));
+    if (sortkey_length != 4) {
+        log_err("expecting length of sortKey is 4  got:%d ", sortkey_length);
+    }
+    log_verbose("length of sortKey is %d", sortkey_length);
+    ucol_close(coll);
+}
+
+#define TSKC_DATA_SIZE 5
+#define TSKC_BUF_SIZE  50
+static void
+TestSortKeyConsistency(void)
+{
+    UErrorCode icuRC = U_ZERO_ERROR;
+    UCollator* ucol;
+    UChar data[] = { 0xFFFD, 0x0006, 0x0006, 0x0006, 0xFFFD};
+
+    uint8_t bufFull[TSKC_DATA_SIZE][TSKC_BUF_SIZE];
+    uint8_t bufPart[TSKC_DATA_SIZE][TSKC_BUF_SIZE];
+    int32_t i, j, i2;
+
+    ucol = ucol_openFromShortString("LEN_S4", FALSE, NULL, &icuRC);
+    if (U_FAILURE(icuRC))
+    {
+        log_err_status(icuRC, "ucol_openFromShortString failed -> %s\n", u_errorName(icuRC));
+        return;
+    }
+
+    for (i = 0; i < TSKC_DATA_SIZE; i++)
+    {
+        UCharIterator uiter;
+        uint32_t state[2] = { 0, 0 };
+        int32_t dataLen = i+1;
+        for (j=0; j<TSKC_BUF_SIZE; j++)
+            bufFull[i][j] = bufPart[i][j] = 0;
+
+        /* Full sort key */
+        ucol_getSortKey(ucol, data, dataLen, bufFull[i], TSKC_BUF_SIZE);
+
+        /* Partial sort key */
+        uiter_setString(&uiter, data, dataLen);
+        ucol_nextSortKeyPart(ucol, &uiter, state, bufPart[i], TSKC_BUF_SIZE, &icuRC);
+        if (U_FAILURE(icuRC))
+        {
+            log_err("ucol_nextSortKeyPart failed\n");
+            ucol_close(ucol);
+            return;
+        }
+
+        for (i2=0; i2<i; i2++)
+        {
+            UBool fullMatch = TRUE;
+            UBool partMatch = TRUE;
+            for (j=0; j<TSKC_BUF_SIZE; j++)
+            {
+                fullMatch = fullMatch && (bufFull[i][j] != bufFull[i2][j]);
+                partMatch = partMatch && (bufPart[i][j] != bufPart[i2][j]);
+            }
+            if (fullMatch != partMatch) {
+                log_err(fullMatch ? "full key was consistent, but partial key changed\n"
+                                  : "partial key was consistent, but full key changed\n");
+                ucol_close(ucol);
+                return;
+            }
+        }
+    }
+
+    /*=============================================*/
+   ucol_close(ucol);
+}
+
+/* ticket: 6101 */
+static void TestCroatianSortKey(void) {
+    const char* collString = "LHR_AN_CX_EX_FX_HX_NX_S3";
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator *ucol;
+    UCharIterator iter;
+
+    static const UChar text[] = { 0x0044, 0xD81A };
+
+    size_t length = sizeof(text)/sizeof(*text);
+
+    uint8_t textSortKey[32];
+    size_t lenSortKey = 32;
+    size_t actualSortKeyLen;
+    uint32_t uStateInfo[2] = { 0, 0 };
+
+    ucol = ucol_openFromShortString(collString, FALSE, NULL, &status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "ucol_openFromShortString error in Craotian test. -> %s\n", u_errorName(status));
+        return;
+    }
+
+    uiter_setString(&iter, text, length);
+
+    actualSortKeyLen = ucol_nextSortKeyPart(
+        ucol, &iter, (uint32_t*)uStateInfo,
+        textSortKey, lenSortKey, &status
+        );
+
+    if (actualSortKeyLen == lenSortKey) {
+        log_err("ucol_nextSortKeyPart did not give correct result in Croatian test.\n");
+    }
+
+    ucol_close(ucol);
+}
+
+/* ticket: 6140 */
+/* This test ensures that codepoints such as 0x3099 are flagged correctly by the collator since
+ * they are both Hiragana and Katakana
+ */
+#define SORTKEYLEN 50
+static void TestHiragana(void) {
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator* ucol;
+    UCollationResult strcollresult;
+    UChar data1[] = { 0x3058, 0x30B8 }; /* Hiragana and Katakana letter Zi */
+    UChar data2[] = { 0x3057, 0x3099, 0x30B7, 0x3099 };
+    int32_t data1Len = sizeof(data1)/sizeof(*data1);
+    int32_t data2Len = sizeof(data2)/sizeof(*data2);
+    int32_t i, j;
+    uint8_t sortKey1[SORTKEYLEN];
+    uint8_t sortKey2[SORTKEYLEN];
+
+    UCharIterator uiter1;
+    UCharIterator uiter2;
+    uint32_t state1[2] = { 0, 0 };
+    uint32_t state2[2] = { 0, 0 };
+    int32_t keySize1;
+    int32_t keySize2;
+
+    ucol = ucol_openFromShortString("LJA_AN_CX_EX_FX_HO_NX_S4", FALSE, NULL,
+            &status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "Error status: %s; Unable to open collator from short string.\n", u_errorName(status));
+        return;
+    }
+
+    /* Start of full sort keys */
+    /* Full sort key1 */
+    keySize1 = ucol_getSortKey(ucol, data1, data1Len, sortKey1, SORTKEYLEN);
+    /* Full sort key2 */
+    keySize2 = ucol_getSortKey(ucol, data2, data2Len, sortKey2, SORTKEYLEN);
+    if (keySize1 == keySize2) {
+        for (i = 0; i < keySize1; i++) {
+            if (sortKey1[i] != sortKey2[i]) {
+                log_err("Full sort keys are different. Should be equal.");
+            }
+        }
+    } else {
+        log_err("Full sort keys sizes doesn't match: %d %d", keySize1, keySize2);
+    }
+    /* End of full sort keys */
+
+    /* Start of partial sort keys */
+    /* Partial sort key1 */
+    uiter_setString(&uiter1, data1, data1Len);
+    keySize1 = ucol_nextSortKeyPart(ucol, &uiter1, state1, sortKey1, SORTKEYLEN, &status);
+    /* Partial sort key2 */
+    uiter_setString(&uiter2, data2, data2Len);
+    keySize2 = ucol_nextSortKeyPart(ucol, &uiter2, state2, sortKey2, SORTKEYLEN, &status);
+    if (U_SUCCESS(status) && keySize1 == keySize2) {
+        for (j = 0; j < keySize1; j++) {
+            if (sortKey1[j] != sortKey2[j]) {
+                log_err("Partial sort keys are different. Should be equal");
+            }
+        }
+    } else {
+        log_err("Error Status: %s or Partial sort keys sizes doesn't match: %d %d", u_errorName(status), keySize1, keySize2);
+    }
+    /* End of partial sort keys */
+
+    /* Start of strcoll */
+    /* Use ucol_strcoll() to determine ordering */
+    strcollresult = ucol_strcoll(ucol, data1, data1Len, data2, data2Len);
+    if (strcollresult != UCOL_EQUAL) {
+        log_err("Result from ucol_strcoll() should be UCOL_EQUAL.");
+    }
+
+    ucol_close(ucol);
+}
+
+/* Convenient struct for running collation tests */
+typedef struct {
+  const UChar source[MAX_TOKEN_LEN];  /* String on left */
+  const UChar target[MAX_TOKEN_LEN];  /* String on right */
+  UCollationResult result;            /* -1, 0 or +1, depending on collation */
+} OneTestCase;
+
+/*
+ * Utility function to test one collation test case.
+ * @param testcases Array of test cases.
+ * @param n_testcases Size of the array testcases.
+ * @param str_rules Array of rules.  These rules should be specifying the same rule in different formats.
+ * @param n_rules Size of the array str_rules.
+ */
+static void doTestOneTestCase(const OneTestCase testcases[],
+                              int n_testcases,
+                              const char* str_rules[],
+                              int n_rules)
+{
+  int rule_no, testcase_no;
+  UChar rule[500]; 
+  int32_t length = 0;
+  UErrorCode status = U_ZERO_ERROR;
+  UParseError parse_error;
+  UCollator  *myCollation;
+
+  for (rule_no = 0; rule_no < n_rules; ++rule_no) {
+
+    length = u_unescape(str_rules[rule_no], rule, 500);
+    if (length == 0) {
+        log_err("ERROR: The rule cannot be unescaped: %s\n");
+        return;
+    }
+    myCollation = ucol_openRules(rule, length, UCOL_ON, UCOL_TERTIARY, &parse_error, &status);
+    if(U_FAILURE(status)){
+        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
+        return;
+    }
+    log_verbose("Testing the <<* syntax\n");
+    ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
+    ucol_setStrength(myCollation, UCOL_TERTIARY);
+    for (testcase_no = 0; testcase_no < n_testcases; ++testcase_no) {
+      doTest(myCollation,
+             testcases[testcase_no].source,
+             testcases[testcase_no].target,
+             testcases[testcase_no].result
+             );
+    }
+    ucol_close(myCollation);
+  }
+}
+
+const static OneTestCase rangeTestcases[] = {
+  { {0x0061},                            {0x0062},                          UCOL_LESS }, /* "a" < "b" */
+  { {0x0062},                            {0x0063},                          UCOL_LESS }, /* "b" < "c" */
+  { {0x0061},                            {0x0063},                          UCOL_LESS }, /* "a" < "c" */
+
+  { {0x0062},                            {0x006b},                          UCOL_LESS }, /* "b" << "k" */
+  { {0x006b},                            {0x006c},                          UCOL_LESS }, /* "k" << "l" */
+  { {0x0062},                            {0x006c},                          UCOL_LESS }, /* "b" << "l" */
+  { {0x0061},                            {0x006c},                          UCOL_LESS }, /* "a" < "l" */
+  { {0x0061},                            {0x006d},                          UCOL_LESS },  /* "a" < "m" */
+
+  { {0x0079},                            {0x006d},                          UCOL_LESS },  /* "y" < "f" */
+  { {0x0079},                            {0x0067},                          UCOL_LESS },  /* "y" < "g" */
+  { {0x0061},                            {0x0068},                          UCOL_LESS },  /* "y" < "h" */
+  { {0x0061},                            {0x0065},                          UCOL_LESS },  /* "g" < "e" */
+
+  { {0x0061},                            {0x0031},                          UCOL_EQUAL }, /* "a" = "1" */
+  { {0x0061},                            {0x0032},                          UCOL_EQUAL }, /* "a" = "2" */
+  { {0x0061},                            {0x0033},                          UCOL_EQUAL }, /* "a" = "3" */
+  { {0x0061},                            {0x0066},                          UCOL_LESS }, /* "a" < "f" */
+  { {0x006c, 0x0061},                    {0x006b, 0x0062},                  UCOL_LESS },  /* "la" < "123" */
+  { {0x0061, 0x0061, 0x0061},            {0x0031, 0x0032, 0x0033},          UCOL_EQUAL }, /* "aaa" = "123" */
+  { {0x0062},                            {0x007a},                          UCOL_LESS },  /* "b" < "z" */
+  { {0x0061, 0x007a, 0x0062},            {0x0032, 0x0079, 0x006d},          UCOL_LESS }, /* "azm" = "2yc" */
+};
+
+static int nRangeTestcases = LEN(rangeTestcases);
+
+const static OneTestCase rangeTestcasesSupplemental[] = {
+  { {0xfffe},                            {0xffff},                          UCOL_LESS }, /* U+FFFE < U+FFFF */
+  { {0xffff},                            {0xd800, 0xdc00},                  UCOL_LESS }, /* U+FFFF < U+10000 */
+  { {0xd800, 0xdc00},                    {0xd800, 0xdc01},                  UCOL_LESS }, /* U+10000 < U+10001 */
+  { {0xfffe},                            {0xd800, 0xdc01},                  UCOL_LESS }, /* U+FFFE < U+10001 */
+  { {0xd800, 0xdc01},                    {0xd800, 0xdc02},                  UCOL_LESS }, /* U+10000 < U+10001 */
+  { {0xd800, 0xdc01},                    {0xd800, 0xdc02},                  UCOL_LESS }, /* U+10000 < U+10001 */
+  { {0xfffe},                            {0xd800, 0xdc02},                  UCOL_LESS }, /* U+FFFE < U+10001 */
+};
+
+static int nRangeTestcasesSupplemental = LEN(rangeTestcasesSupplemental);
+
+const static OneTestCase rangeTestcasesQwerty[] = {
+  { {0x0071},                            {0x0077},                          UCOL_LESS }, /* "q" < "w" */
+  { {0x0077},                            {0x0065},                          UCOL_LESS }, /* "w" < "e" */
+
+  { {0x0079},                            {0x0075},                          UCOL_LESS }, /* "y" < "u" */
+  { {0x0071},                            {0x0075},                          UCOL_LESS }, /* "q" << "u" */
+
+  { {0x0074},                            {0x0069},                          UCOL_LESS }, /* "t" << "i" */
+  { {0x006f},                            {0x0070},                          UCOL_LESS }, /* "o" << "p" */
+
+  { {0x0079},                            {0x0065},                          UCOL_LESS },  /* "y" < "e" */
+  { {0x0069},                            {0x0075},                          UCOL_LESS },  /* "i" < "u" */
+
+  { {0x0071, 0x0075, 0x0065, 0x0073, 0x0074},
+    {0x0077, 0x0065, 0x0072, 0x0065},                                       UCOL_LESS }, /* "quest" < "were" */
+  { {0x0071, 0x0075, 0x0061, 0x0063, 0x006b},
+    {0x0071, 0x0075, 0x0065, 0x0073, 0x0074},                               UCOL_LESS }, /* "quack" < "quest" */
+};
+
+static int nRangeTestcasesQwerty = LEN(rangeTestcasesQwerty);
+
+static void TestSameStrengthList(void)
+{
+  const char* strRules[] = {
+    /* Normal */
+    "&a<b<c<d &b<<k<<l<<m &k<<<x<<<y<<<z  &y<f<g<h<e &a=1=2=3", 
+
+    /* Lists */
+    "&a<*bcd &b<<*klm &k<<<*xyz &y<*fghe &a=*123", 
+  };
+  doTestOneTestCase(rangeTestcases, nRangeTestcases, strRules, LEN(strRules));
+}
+
+static void TestSameStrengthListQuoted(void)
+{
+  const char* strRules[] = {
+    /* Lists with quoted characters */
+    "&\\u0061<*bcd &b<<*klm &k<<<*xyz &y<*f\\u0067\\u0068e &a=*123",
+    "&'\\u0061'<*bcd &b<<*klm &k<<<*xyz &y<*f'\\u0067\\u0068'e &a=*123",
+
+    "&\\u0061<*b\\u0063d &b<<*klm &k<<<*xyz &\\u0079<*fgh\\u0065 &a=*\\u0031\\u0032\\u0033",
+    "&'\\u0061'<*b'\\u0063'd &b<<*klm &k<<<*xyz &'\\u0079'<*fgh'\\u0065' &a=*'\\u0031\\u0032\\u0033'",
+
+    "&\\u0061<*\\u0062c\\u0064 &b<<*klm &k<<<*xyz  &y<*fghe &a=*\\u0031\\u0032\\u0033", 
+    "&'\\u0061'<*'\\u0062'c'\\u0064' &b<<*klm &k<<<*xyz  &y<*fghe &a=*'\\u0031\\u0032\\u0033'", 
+  };
+  doTestOneTestCase(rangeTestcases, nRangeTestcases, strRules, LEN(strRules));
+}
+
+static void TestSameStrengthListSupplemental(void)
+{
+  const char* strRules[] = {
+    "&\\ufffe<\\uffff<\\U00010000<\\U00010001<\\U00010002",
+    "&\\ufffe<\\uffff<\\ud800\\udc00<\\ud800\\udc01<\\ud800\\udc02",
+    "&\\ufffe<*\\uffff\\U00010000\\U00010001\\U00010002",
+    "&\\ufffe<*\\uffff\\ud800\\udc00\\ud800\\udc01\\ud800\\udc02",
+  };
+  doTestOneTestCase(rangeTestcasesSupplemental, nRangeTestcasesSupplemental, strRules, LEN(strRules));
+}
+
+static void TestSameStrengthListQwerty(void)
+{
+  const char* strRules[] = {
+    "&q<w<e<r &w<<t<<y<<u &t<<<i<<<o<<<p &o=a=s=d",   /* Normal */
+    "&q<*wer &w<<*tyu &t<<<*iop &o=*asd",             /* Lists  */
+    "&\\u0071<\\u0077<\\u0065<\\u0072 &\\u0077<<\\u0074<<\\u0079<<\\u0075 &\\u0074<<<\\u0069<<<\\u006f<<<\\u0070 &\\u006f=\\u0061=\\u0073=\\u0064",
+    "&'\\u0071'<\\u0077<\\u0065<\\u0072 &\\u0077<<'\\u0074'<<\\u0079<<\\u0075 &\\u0074<<<\\u0069<<<'\\u006f'<<<\\u0070 &\\u006f=\\u0061='\\u0073'=\\u0064",
+    "&\\u0071<*\\u0077\\u0065\\u0072 &\\u0077<<*\\u0074\\u0079\\u0075 &\\u0074<<<*\\u0069\\u006f\\u0070 &\\u006f=*\\u0061\\u0073\\u0064",
+
+    /* Quoted characters also will work if two quoted characters are not consecutive.  */
+    "&\\u0071<*'\\u0077'\\u0065\\u0072 &\\u0077<<*\\u0074'\\u0079'\\u0075 &\\u0074<<<*\\u0069\\u006f'\\u0070' &'\\u006f'=*\\u0061\\u0073\\u0064",
+
+    /* Consecutive quoted charactes do not work, because a '' will be treated as a quote character. */
+    /* "&\\u0071<*'\\u0077''\\u0065''\\u0072' &\\u0077<<*'\\u0074''\\u0079''\\u0075' &\\u0074<<<*'\\u0069''\\u006f''\\u0070' &'\\u006f'=*\\u0061\\u0073\\u0064",*/
+
+ };
+  doTestOneTestCase(rangeTestcasesQwerty, nRangeTestcasesQwerty, strRules, LEN(strRules));
+}
+
+static void TestSameStrengthListQuotedQwerty(void)
+{
+  const char* strRules[] = {
+    "&q<w<e<r &w<<t<<y<<u &t<<<i<<<o<<<p &o=a=s=d",   /* Normal */
+    "&q<*wer &w<<*tyu &t<<<*iop &o=*asd",             /* Lists  */
+    "&q<*w'e'r &w<<*'t'yu &t<<<*io'p' &o=*'a's'd'",   /* Lists with quotes */
+
+    /* Lists with continuous quotes may not work, because '' will be treated as a quote character. */
+    /* "&q<*'w''e''r' &w<<*'t''y''u' &t<<<*'i''o''p' &o=*'a''s''d'", */
+   };
+  doTestOneTestCase(rangeTestcasesQwerty, nRangeTestcasesQwerty, strRules, LEN(strRules));
+}
+
+static void TestSameStrengthListRanges(void)
+{
+  const char* strRules[] = {
+    "&a<*b-d &b<<*k-m &k<<<*x-z &y<*f-he &a=*1-3",
+  };
+  doTestOneTestCase(rangeTestcases, nRangeTestcases, strRules, LEN(strRules));
+}
+
+static void TestSameStrengthListSupplementalRanges(void)
+{
+  const char* strRules[] = {
+    "&\\ufffe<*\\uffff-\\U00010002",
+  };
+  doTestOneTestCase(rangeTestcasesSupplemental, nRangeTestcasesSupplemental, strRules, LEN(strRules));
+}
+
+static void TestSpecialCharacters(void)
+{
+  const char* strRules[] = {
+    /* Normal */
+    "&';'<'+'<','<'-'<'&'<'*'",
+
+    /* List */
+    "&';'<*'+,-&*'",
+
+    /* Range */
+    "&';'<*'+'-'-&*'", 
+  };
+
+  const static OneTestCase specialCharacterStrings[] = {
+    { {0x003b}, {0x002b}, UCOL_LESS },  /* ; < + */
+    { {0x002b}, {0x002c}, UCOL_LESS },  /* + < , */
+    { {0x002c}, {0x002d}, UCOL_LESS },  /* , < - */
+    { {0x002d}, {0x0026}, UCOL_LESS },  /* - < & */
+  };
+  doTestOneTestCase(specialCharacterStrings, LEN(specialCharacterStrings), strRules, LEN(strRules));
+}
+
+static void TestPrivateUseCharacters(void)
+{
+  const char* strRules[] = {
+    /* Normal */
+    "&'\\u5ea7'<'\\uE2D8'<'\\uE2D9'<'\\uE2DA'<'\\uE2DB'<'\\uE2DC'<'\\u4e8d'",
+    "&\\u5ea7<\\uE2D8<\\uE2D9<\\uE2DA<\\uE2DB<\\uE2DC<\\u4e8d", 
+  };
+
+  const static OneTestCase privateUseCharacterStrings[] = {
+    { {0x5ea7}, {0xe2d8}, UCOL_LESS },
+    { {0xe2d8}, {0xe2d9}, UCOL_LESS },
+    { {0xe2d9}, {0xe2da}, UCOL_LESS },
+    { {0xe2da}, {0xe2db}, UCOL_LESS },
+    { {0xe2db}, {0xe2dc}, UCOL_LESS },
+    { {0xe2dc}, {0x4e8d}, UCOL_LESS },
+  };
+  doTestOneTestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), strRules, LEN(strRules));
+}
+
+static void TestPrivateUseCharactersInList(void)
+{
+  const char* strRules[] = {
+    /* List */
+    "&'\\u5ea7'<*'\\uE2D8\\uE2D9\\uE2DA\\uE2DB\\uE2DC\\u4e8d'",
+    /* "&'\\u5ea7'<*\\uE2D8'\\uE2D9\\uE2DA'\\uE2DB'\\uE2DC\\u4e8d'", */
+    "&\\u5ea7<*\\uE2D8\\uE2D9\\uE2DA\\uE2DB\\uE2DC\\u4e8d",
+  };
+
+  const static OneTestCase privateUseCharacterStrings[] = {
+    { {0x5ea7}, {0xe2d8}, UCOL_LESS },
+    { {0xe2d8}, {0xe2d9}, UCOL_LESS },
+    { {0xe2d9}, {0xe2da}, UCOL_LESS },
+    { {0xe2da}, {0xe2db}, UCOL_LESS },
+    { {0xe2db}, {0xe2dc}, UCOL_LESS },
+    { {0xe2dc}, {0x4e8d}, UCOL_LESS },
+  };
+  doTestOneTestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), strRules, LEN(strRules));
+}
+
+static void TestPrivateUseCharactersInRange(void)
+{
+  const char* strRules[] = {
+    /* Range */
+    "&'\\u5ea7'<*'\\uE2D8'-'\\uE2DC\\u4e8d'",
+    "&\\u5ea7<*\\uE2D8-\\uE2DC\\u4e8d",
+    /* "&\\u5ea7<\\uE2D8'\\uE2D8'-'\\uE2D9'\\uE2DA-\\uE2DB\\uE2DC\\u4e8d", */
+  };
+
+  const static OneTestCase privateUseCharacterStrings[] = {
+    { {0x5ea7}, {0xe2d8}, UCOL_LESS },
+    { {0xe2d8}, {0xe2d9}, UCOL_LESS },
+    { {0xe2d9}, {0xe2da}, UCOL_LESS },
+    { {0xe2da}, {0xe2db}, UCOL_LESS },
+    { {0xe2db}, {0xe2dc}, UCOL_LESS },
+    { {0xe2dc}, {0x4e8d}, UCOL_LESS },
+  };
+  doTestOneTestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), strRules, LEN(strRules));
+}
+
+static void TestInvalidListsAndRanges(void)
+{
+  const char* invalidRules[] = {
+    /* Range not in starred expression */
+    "&\\ufffe<\\uffff-\\U00010002",
+
+    /* Range without start */
+    "&a<*-c",
+
+    /* Range without end */
+    "&a<*b-",
+
+    /* More than one hyphen */
+    "&a<*b-g-l",
+
+    /* Range in the wrong order */
+    "&a<*k-b",
+
+  };
+
+  UChar rule[500];
+  UErrorCode status = U_ZERO_ERROR;
+  UParseError parse_error;
+  int n_rules = LEN(invalidRules);
+  int rule_no;
+  int length;
+  UCollator  *myCollation;
+
+  for (rule_no = 0; rule_no < n_rules; ++rule_no) {
+
+    length = u_unescape(invalidRules[rule_no], rule, 500);
+    if (length == 0) {
+        log_err("ERROR: The rule cannot be unescaped: %s\n");
+        return;
+    }
+    myCollation = ucol_openRules(rule, length, UCOL_ON, UCOL_TERTIARY, &parse_error, &status);
+    if(!U_FAILURE(status)){
+      log_err("ERROR: Could not cause a failure as expected: \n");
+    }
+    status = U_ZERO_ERROR;
+  }
+}
+
+/*
+ * This test ensures that characters placed before a character in a different script have the same lead byte
+ * in their collation key before and after script reordering.
+ */
+static void TestBeforeRuleWithScriptReordering(void)
+{
+    UParseError error;
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator  *myCollation;
+    char srules[500] = "&[before 1]\\u03b1 < \\u0e01";
+    UChar rules[500];
+    uint32_t rulesLength = 0;
+    int32_t reorderCodes[1] = {USCRIPT_GREEK};
+    UCollationResult collResult;
+
+    uint8_t baseKey[256];
+    uint32_t baseKeyLength;
+    uint8_t beforeKey[256];
+    uint32_t beforeKeyLength;
+
+    UChar base[] = { 0x03b1 }; /* base */
+    int32_t baseLen = sizeof(base)/sizeof(*base);
+
+    UChar before[] = { 0x0e01 }; /* ko kai */
+    int32_t beforeLen = sizeof(before)/sizeof(*before);
+
+    /*UChar *data[] = { before, base };
+    genericRulesStarter(srules, data, 2);*/
+    
+    log_verbose("Testing the &[before 1] rule with [reorder grek]\n");
+
+
+    /* build collator */
+    log_verbose("Testing the &[before 1] rule with [scriptReorder grek]\n");
+
+    rulesLength = u_unescape(srules, rules, LEN(rules));
+    myCollation = ucol_openRules(rules, rulesLength, UCOL_ON, UCOL_TERTIARY, &error, &status);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
+        return;
+    }
+
+    /* check collation results - before rule applied but not script reordering */
+    collResult = ucol_strcoll(myCollation, base, baseLen, before, beforeLen);
+    if (collResult != UCOL_GREATER) {
+        log_err("Collation result not correct before script reordering = %d\n", collResult);
+    }
+
+    /* check the lead byte of the collation keys before script reordering */
+    baseKeyLength = ucol_getSortKey(myCollation, base, baseLen, baseKey, 256);
+    beforeKeyLength = ucol_getSortKey(myCollation, before, beforeLen, beforeKey, 256);
+    if (baseKey[0] != beforeKey[0]) {
+      log_err("Different lead byte for sort keys using before rule and before script reordering. base character lead byte = %02x, before character lead byte = %02x\n", baseKey[0], beforeKey[0]);
+   }
+
+    /* reorder the scripts */
+    ucol_setReorderCodes(myCollation, reorderCodes, 1, &status);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "ERROR: while setting script order: %s\n", myErrorName(status));
+        return;
+    }
+
+    /* check collation results - before rule applied and after script reordering */
+    collResult = ucol_strcoll(myCollation, base, baseLen, before, beforeLen);
+    if (collResult != UCOL_GREATER) {
+        log_err("Collation result not correct after script reordering = %d\n", collResult);
+    }
+    
+    /* check the lead byte of the collation keys after script reordering */
+    ucol_getSortKey(myCollation, base, baseLen, baseKey, 256);
+    ucol_getSortKey(myCollation, before, beforeLen, beforeKey, 256);
+    if (baseKey[0] != beforeKey[0]) {
+        log_err("Different lead byte for sort keys using before fule and after script reordering. base character lead byte = %02x, before character lead byte = %02x\n", baseKey[0], beforeKey[0]);
+    }
+
+    ucol_close(myCollation);
+}
+
+/*
+ * Test that in a primary-compressed sort key all bytes except the first one are unchanged under script reordering.
+ */
+static void TestNonLeadBytesDuringCollationReordering(void)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator  *myCollation;
+    int32_t reorderCodes[1] = {USCRIPT_GREEK};
+    UCollationResult collResult;
+
+    uint8_t baseKey[256];
+    uint32_t baseKeyLength;
+    uint8_t reorderKey[256];
+    uint32_t reorderKeyLength;
+
+    UChar testString[] = { 0x03b1, 0x03b2, 0x03b3 };
+    
+    int i;
+
+
+    log_verbose("Testing non-lead bytes in a sort key with and without reordering\n");
+
+    /* build collator tertiary */
+    myCollation = ucol_open("", &status);
+    ucol_setStrength(myCollation, UCOL_TERTIARY);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
+        return;
+    }
+    baseKeyLength = ucol_getSortKey(myCollation, testString, LEN(testString), baseKey, 256);
+
+    ucol_setReorderCodes(myCollation, reorderCodes, LEN(reorderCodes), &status);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
+        return;
+    }
+    reorderKeyLength = ucol_getSortKey(myCollation, testString, LEN(testString), reorderKey, 256);
+    
+    if (baseKeyLength != reorderKeyLength) {
+        log_err("Key lengths not the same during reordering.\n", collResult);
+        return;
+    }
+    
+    for (i = 1; i < baseKeyLength; i++) {
+        if (baseKey[i] != reorderKey[i]) {
+            log_err("Collation key bytes not the same at position %d.\n", i);
+            return;
+        }
+    } 
+    ucol_close(myCollation);
+
+    /* build collator quaternary */
+    myCollation = ucol_open("", &status);
+    ucol_setStrength(myCollation, UCOL_QUATERNARY);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
+        return;
+    }
+    baseKeyLength = ucol_getSortKey(myCollation, testString, LEN(testString), baseKey, 256);
+
+    ucol_setReorderCodes(myCollation, reorderCodes, LEN(reorderCodes), &status);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
+        return;
+    }
+    reorderKeyLength = ucol_getSortKey(myCollation, testString, LEN(testString), reorderKey, 256);
+    
+    if (baseKeyLength != reorderKeyLength) {
+        log_err("Key lengths not the same during reordering.\n", collResult);
+        return;
+    }
+    
+    for (i = 1; i < baseKeyLength; i++) {
+        if (baseKey[i] != reorderKey[i]) {
+            log_err("Collation key bytes not the same at position %d.\n", i);
+            return;
+        }
+    }
+    ucol_close(myCollation);
+}
+
+/*
+ * Test reordering API.
+ */
+static void TestReorderingAPI(void)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator  *myCollation;
+    int32_t reorderCodes[3] = {USCRIPT_GREEK, USCRIPT_HAN, UCOL_REORDER_CODE_PUNCTUATION};
+    UCollationResult collResult;
+    int32_t retrievedReorderCodesLength;
+    UChar greekString[] = { 0x03b1 };
+    UChar punctuationString[] = { 0x203e };
+
+    log_verbose("Testing non-lead bytes in a sort key with and without reordering\n");
+
+    /* build collator tertiary */
+    myCollation = ucol_open("", &status);
+    ucol_setStrength(myCollation, UCOL_TERTIARY);
+    if(U_FAILURE(status)) {
+        log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
+        return;
+    }
+
+    /* set the reorderding */
+    ucol_setReorderCodes(myCollation, reorderCodes, LEN(reorderCodes), &status);
+    if (U_FAILURE(status)) {
+        log_err_status(status, "ERROR: setting reorder codes: %s\n", myErrorName(status));
+        return;
+    }
+    
+    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, NULL, 0, &status);
+    if (status != U_BUFFER_OVERFLOW_ERROR) {
+        log_err_status(status, "ERROR: getting error codes should have returned U_BUFFER_OVERFLOW_ERROR : %s\n", myErrorName(status));
+        return;
+    }
+    status = U_ZERO_ERROR;
+    if (retrievedReorderCodesLength != LEN(reorderCodes)) {
+        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, LEN(reorderCodes));
+        return;
+    }
+    collResult = ucol_strcoll(myCollation, greekString, LEN(greekString), punctuationString, LEN(punctuationString));
+    if (collResult != UCOL_LESS) {
+        log_err_status(status, "ERROR: collation result should have been UCOL_LESS\n");
+        return;
+    }
+    
+    /* clear the reordering */
+    ucol_setReorderCodes(myCollation, NULL, 0, &status);    
+    if (U_FAILURE(status)) {
+        log_err_status(status, "ERROR: setting reorder codes to NULL: %s\n", myErrorName(status));
+        return;
+    }
+
+    retrievedReorderCodesLength = ucol_getReorderCodes(myCollation, NULL, 0, &status);
+    if (retrievedReorderCodesLength != 0) {
+        log_err_status(status, "ERROR: retrieved reorder codes length was %d but should have been %d\n", retrievedReorderCodesLength, 0);
+        return;
+    }
+
+    collResult = ucol_strcoll(myCollation, greekString, LEN(greekString), punctuationString, LEN(punctuationString));
+    if (collResult != UCOL_GREATER) {
+        log_err_status(status, "ERROR: collation result should have been UCOL_GREATER\n");
+        return;
+    }
+
+    ucol_close(myCollation);
+}
+
+/*
+ * Utility function to test one collation reordering test case.
+ * @param testcases Array of test cases.
+ * @param n_testcases Size of the array testcases.
+ * @param str_rules Array of rules.  These rules should be specifying the same rule in different formats.
+ * @param n_rules Size of the array str_rules.
+ */
+static void doTestOneReorderingAPITestCase(const OneTestCase testCases[], uint32_t testCasesLen, const int32_t reorderTokens[], int32_t reorderTokensLen)
+{
+    int testCaseNum;
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator  *myCollation;
+
+    for (testCaseNum = 0; testCaseNum < testCasesLen; ++testCaseNum) {
+        myCollation = ucol_open("", &status);
+        if (U_FAILURE(status)) {
+            log_err_status(status, "ERROR: in creation of collator: %s\n", myErrorName(status));
+            return;
+        }
+        ucol_setReorderCodes(myCollation, reorderTokens, reorderTokensLen, &status);
+        if(U_FAILURE(status)) {
+            log_err_status(status, "ERROR: while setting script order: %s\n", myErrorName(status));
+            return;
+        }
+        
+        for (testCaseNum = 0; testCaseNum < testCasesLen; ++testCaseNum) {
+            doTest(myCollation,
+                testCases[testCaseNum].source,
+                testCases[testCaseNum].target,
+                testCases[testCaseNum].result
+            );
+        }
+        ucol_close(myCollation);
+    }
+}
+
+static void TestGreekFirstReorder(void)
+{
+    const char* strRules[] = {
+        "[reorder Grek]"
+    };
+
+    const int32_t apiRules[] = {
+        USCRIPT_GREEK
+    };
+    
+    const static OneTestCase privateUseCharacterStrings[] = {
+        { {0x0391}, {0x0391}, UCOL_EQUAL },
+        { {0x0041}, {0x0391}, UCOL_GREATER },
+        { {0x03B1, 0x0041}, {0x03B1, 0x0391}, UCOL_GREATER },
+        { {0x0060}, {0x0391}, UCOL_LESS },
+        { {0x0391}, {0xe2dc}, UCOL_LESS },
+        { {0x0391}, {0x0060}, UCOL_GREATER },
+    };
+
+    /* Test rules creation */
+    doTestOneTestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), strRules, LEN(strRules));
+
+    /* Test collation reordering API */
+    doTestOneReorderingAPITestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), apiRules, LEN(apiRules));
+}
+
+static void TestGreekLastReorder(void)
+{
+    const char* strRules[] = {
+        "[reorder Zzzz Grek]"
+    };
+
+    const int32_t apiRules[] = {
+        USCRIPT_UNKNOWN, USCRIPT_GREEK
+    };
+    
+    const static OneTestCase privateUseCharacterStrings[] = {
+        { {0x0391}, {0x0391}, UCOL_EQUAL },
+        { {0x0041}, {0x0391}, UCOL_LESS },
+        { {0x03B1, 0x0041}, {0x03B1, 0x0391}, UCOL_LESS },
+        { {0x0060}, {0x0391}, UCOL_LESS },
+        { {0x0391}, {0xe2dc}, UCOL_GREATER },
+    };
+    
+    /* Test rules creation */
+    doTestOneTestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), strRules, LEN(strRules));
+
+    /* Test collation reordering API */
+    doTestOneReorderingAPITestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), apiRules, LEN(apiRules));
+}
+
+static void TestNonScriptReorder(void)
+{
+    const char* strRules[] = {
+        "[reorder Grek Symbol DIGIT Latn Punct space Zzzz cURRENCy]"
+    };
+
+    const int32_t apiRules[] = {
+        USCRIPT_GREEK, UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_DIGIT, USCRIPT_LATIN, 
+        UCOL_REORDER_CODE_PUNCTUATION, UCOL_REORDER_CODE_SPACE, USCRIPT_UNKNOWN, 
+        UCOL_REORDER_CODE_CURRENCY
+    };
+
+    const static OneTestCase privateUseCharacterStrings[] = {
+        { {0x0391}, {0x0041}, UCOL_LESS },
+        { {0x0041}, {0x0391}, UCOL_GREATER },
+        { {0x0060}, {0x0041}, UCOL_LESS },
+        { {0x0060}, {0x0391}, UCOL_GREATER },
+        { {0x0024}, {0x0041}, UCOL_GREATER },
+    };
+    
+    /* Test rules creation */
+    doTestOneTestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), strRules, LEN(strRules));
+
+    /* Test collation reordering API */
+    doTestOneReorderingAPITestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), apiRules, LEN(apiRules));
+}
+
+static void TestHaniReorder(void)
+{
+    const char* strRules[] = {
+        "[reorder Hani]"
+    };
+    const int32_t apiRules[] = {
+        USCRIPT_HAN
+    };
+
+    const static OneTestCase privateUseCharacterStrings[] = {
+        { {0x4e00}, {0x0041}, UCOL_LESS },
+        { {0x4e00}, {0x0060}, UCOL_GREATER },
+        { {0xD86D, 0xDF40}, {0x0041}, UCOL_LESS },
+        { {0xD86D, 0xDF40}, {0x0060}, UCOL_GREATER },
+        { {0x4e00}, {0xD86D, 0xDF40}, UCOL_LESS },
+        { {0xfa27}, {0x0041}, UCOL_LESS },
+        { {0xD869, 0xDF00}, {0x0041}, UCOL_LESS },
+    };
+    
+    /* Test rules creation */
+    doTestOneTestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), strRules, LEN(strRules));
+
+    /* Test collation reordering API */
+    doTestOneReorderingAPITestCase(privateUseCharacterStrings, LEN(privateUseCharacterStrings), apiRules, LEN(apiRules));
+}
+
+static void TestMultipleReorder()
+{
+    const char* strRules[] = {
+        "[reorder Grek Zzzz DIGIT Latn Hani]"
+    };
+
+    const int32_t apiRules[] = {
+        USCRIPT_GREEK, USCRIPT_UNKNOWN, UCOL_REORDER_CODE_DIGIT, USCRIPT_LATIN, USCRIPT_HAN
+    };
+        
+    const static OneTestCase collationTestCases[] = {
+        { {0x0391}, {0x0041}, UCOL_LESS},
+        { {0x0031}, {0x0041}, UCOL_LESS},
+        { {0x0041}, {0x4e00}, UCOL_LESS},
+    };
+
+    /* Test rules creation */
+    doTestOneTestCase(collationTestCases, LEN(collationTestCases), strRules, LEN(strRules));
+
+    /* Test collation reordering API */
+    doTestOneReorderingAPITestCase(collationTestCases, LEN(collationTestCases), apiRules, LEN(apiRules));
+}
+
+static int compare_uint8_t_arrays(const uint8_t* a, const uint8_t* b)
+{
+  for (; *a == *b; ++a, ++b) {
+    if (*a == 0) {
+      return 0;
+    }
+  }
+  return (*a < *b ? -1 : 1);
+}
+
+static void TestImport(void)
+{
+    UCollator* vicoll;
+    UCollator* escoll;
+    UCollator* viescoll;
+    UCollator* importviescoll;
+    UParseError error;
+    UErrorCode status = U_ZERO_ERROR;
+    UChar* virules;
+    int32_t viruleslength;
+    UChar* esrules;
+    int32_t esruleslength;
+    UChar* viesrules;
+    int32_t viesruleslength;
+    char srules[500] = "[import vi][import es]";
+    UChar rules[500];
+    uint32_t length = 0;
+    int32_t itemCount;
+    int32_t i, k;
+    UChar32 start;
+    UChar32 end;
+    UChar str[500];
+    int32_t strLength;
+
+    uint8_t sk1[500];
+    uint8_t sk2[500];
+
+    UBool b;
+    USet* tailoredSet;
+    USet* importTailoredSet;
+
+
+    vicoll = ucol_open("vi", &status);
+    if(U_FAILURE(status)){
+        log_err_status(status, "ERROR: Call ucol_open(\"vi\", ...): %s\n", myErrorName(status));
+        return;
+    }
+
+    virules = (UChar*) ucol_getRules(vicoll, &viruleslength);
+    escoll = ucol_open("es", &status);
+    esrules = (UChar*) ucol_getRules(escoll, &esruleslength);
+    viesrules = (UChar*)uprv_malloc((viruleslength+esruleslength+1)*sizeof(UChar*));
+    viesrules[0] = 0;
+    u_strcat(viesrules, virules);
+    u_strcat(viesrules, esrules);
+    viesruleslength = viruleslength + esruleslength;
+    viescoll = ucol_openRules(viesrules, viesruleslength, UCOL_ON, UCOL_TERTIARY, &error, &status);
+
+    /* u_strFromUTF8(rules, 500, &length, srules, strlen(srules), &status); */
+    length = u_unescape(srules, rules, 500);
+    importviescoll = ucol_openRules(rules, length, UCOL_ON, UCOL_TERTIARY, &error, &status);
+    if(U_FAILURE(status)){
+        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
+        return;
+    }
+
+    tailoredSet = ucol_getTailoredSet(viescoll, &status);
+    importTailoredSet = ucol_getTailoredSet(importviescoll, &status);
+
+    if(!uset_equals(tailoredSet, importTailoredSet)){
+        log_err("Tailored sets not equal");
+    }
+
+    uset_close(importTailoredSet);
+
+    itemCount = uset_getItemCount(tailoredSet);
+
+    for( i = 0; i < itemCount; i++){
+        strLength = uset_getItem(tailoredSet, i, &start, &end, str, 500, &status);
+        if(strLength < 2){
+            for (; start <= end; start++){
+                k = 0;
+                U16_APPEND(str, k, 500, start, b);
+                ucol_getSortKey(viescoll, str, 1, sk1, 500);
+                ucol_getSortKey(importviescoll, str, 1, sk2, 500);
+                if(compare_uint8_t_arrays(sk1, sk2) != 0){
+                    log_err("Sort key for %s not equal\n", str);
+                    break;
+                }
+            }
+        }else{
+            ucol_getSortKey(viescoll, str, strLength, sk1, 500);
+            ucol_getSortKey(importviescoll, str, strLength, sk2, 500);
+            if(compare_uint8_t_arrays(sk1, sk2) != 0){
+                log_err("ZZSort key for %s not equal\n", str);
+                break;
+            }
+
+        }
+    }
+
+    uset_close(tailoredSet);
+
+    uprv_free(viesrules);
+
+    ucol_close(vicoll);
+    ucol_close(escoll);
+    ucol_close(viescoll);
+    ucol_close(importviescoll);
+}
+
+static void TestImportWithType(void)
+{
+    UCollator* vicoll;
+    UCollator* decoll;
+    UCollator* videcoll;
+    UCollator* importvidecoll;
+    UParseError error;
+    UErrorCode status = U_ZERO_ERROR;
+    const UChar* virules;
+    int32_t viruleslength;
+    const UChar* derules;
+    int32_t deruleslength;
+    UChar* viderules;
+    int32_t videruleslength;
+    const char srules[500] = "[import vi][import de-u-co-phonebk]";
+    UChar rules[500];
+    uint32_t length = 0;
+    int32_t itemCount;
+    int32_t i, k;
+    UChar32 start;
+    UChar32 end;
+    UChar str[500];
+    int32_t strLength;
+
+    uint8_t sk1[500];
+    uint8_t sk2[500];
+
+    USet* tailoredSet;
+    USet* importTailoredSet;
+
+    vicoll = ucol_open("vi", &status);
+    if(U_FAILURE(status)){
+        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
+        return;
+    }
+    virules = ucol_getRules(vicoll, &viruleslength);
+    /* decoll = ucol_open("de@collation=phonebook", &status); */
+    decoll = ucol_open("de-u-co-phonebk", &status);
+    if(U_FAILURE(status)){
+        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
+        return;
+    }
+
+
+    derules = ucol_getRules(decoll, &deruleslength);
+    viderules = (UChar*)uprv_malloc((viruleslength+deruleslength+1)*sizeof(UChar*));
+    viderules[0] = 0;
+    u_strcat(viderules, virules);
+    u_strcat(viderules, derules);
+    videruleslength = viruleslength + deruleslength;
+    videcoll = ucol_openRules(viderules, videruleslength, UCOL_ON, UCOL_TERTIARY, &error, &status);
+
+    /* u_strFromUTF8(rules, 500, &length, srules, strlen(srules), &status); */
+    length = u_unescape(srules, rules, 500);
+    importvidecoll = ucol_openRules(rules, length, UCOL_ON, UCOL_TERTIARY, &error, &status);
+    if(U_FAILURE(status)){
+        log_err_status(status, "ERROR: in creation of rule based collator: %s\n", myErrorName(status));
+        return;
+    }
+
+    tailoredSet = ucol_getTailoredSet(videcoll, &status);
+    importTailoredSet = ucol_getTailoredSet(importvidecoll, &status);
+
+    if(!uset_equals(tailoredSet, importTailoredSet)){
+        log_err("Tailored sets not equal");
+    }
+
+    uset_close(importTailoredSet);
+
+    itemCount = uset_getItemCount(tailoredSet);
+
+    for( i = 0; i < itemCount; i++){
+        strLength = uset_getItem(tailoredSet, i, &start, &end, str, 500, &status);
+        if(strLength < 2){
+            for (; start <= end; start++){
+                k = 0;
+                U16_APPEND_UNSAFE(str, k, start);
+                ucol_getSortKey(videcoll, str, 1, sk1, 500);
+                ucol_getSortKey(importvidecoll, str, 1, sk2, 500);
+                if(compare_uint8_t_arrays(sk1, sk2) != 0){
+                    log_err("Sort key for %s not equal\n", str);
+                    break;
+                }
+            }
+        }else{
+            ucol_getSortKey(videcoll, str, strLength, sk1, 500);
+            ucol_getSortKey(importvidecoll, str, strLength, sk2, 500);
+            if(compare_uint8_t_arrays(sk1, sk2) != 0){
+                log_err("Sort key for %s not equal\n", str);
+                break;
+            }
+
+        }
+    }
+
+    uset_close(tailoredSet);
+
+    uprv_free(viderules);
+
+    ucol_close(videcoll);
+    ucol_close(importvidecoll);
+    ucol_close(vicoll);
+    ucol_close(decoll);
+
+}
+
+
+#define TEST(x) addTest(root, &x, "tscoll/cmsccoll/" # x)
+
+void addMiscCollTest(TestNode** root)
+{
+    TEST(TestRuleOptions);
+    TEST(TestBeforePrefixFailure);
+    TEST(TestContractionClosure);
+    TEST(TestPrefixCompose);
+    TEST(TestStrCollIdenticalPrefix);
+    TEST(TestPrefix);
+    TEST(TestNewJapanese);
+    /*TEST(TestLimitations);*/
+    TEST(TestNonChars);
+    TEST(TestExtremeCompression);
+    TEST(TestSurrogates);
+    TEST(TestVariableTopSetting);
+    TEST(TestBocsuCoverage);
+    TEST(TestCyrillicTailoring);
+    TEST(TestCase);
+    TEST(IncompleteCntTest);
+    TEST(BlackBirdTest);
+    TEST(FunkyATest);
+    TEST(BillFairmanTest);
+    TEST(RamsRulesTest);
+    TEST(IsTailoredTest);
+    TEST(TestCollations);
+    TEST(TestChMove);
+    TEST(TestImplicitTailoring);
+    TEST(TestFCDProblem);
+    TEST(TestEmptyRule);
+    /*TEST(TestJ784);*/ /* 'zh' locale has changed - now it is getting tested by TestBeforePinyin */
+    TEST(TestJ815);
+    /*TEST(TestJ831);*/ /* we changed lv locale */
+    TEST(TestBefore);
+    TEST(TestRedundantRules);
+    TEST(TestExpansionSyntax);
+    TEST(TestHangulTailoring);
+    TEST(TestUCARules);
+    TEST(TestIncrementalNormalize);
+    TEST(TestComposeDecompose);
+    TEST(TestCompressOverlap);
+    TEST(TestContraction);
+    TEST(TestExpansion);
+    /*TEST(PrintMarkDavis);*/ /* this test doesn't test - just prints sortkeys */
+    /*TEST(TestGetCaseBit);*/ /*this one requires internal things to be exported */
+    TEST(TestOptimize);
+    TEST(TestSuppressContractions);
+    TEST(Alexis2);
+    TEST(TestHebrewUCA);
+    TEST(TestPartialSortKeyTermination);
+    TEST(TestSettings);
+    TEST(TestEquals);
+    TEST(TestJ2726);
+    TEST(NullRule);
+    TEST(TestNumericCollation);
+    TEST(TestTibetanConformance);
+    TEST(TestPinyinProblem);
+    TEST(TestImplicitGeneration);
+    TEST(TestSeparateTrees);
+    TEST(TestBeforePinyin);
+    TEST(TestBeforeTightening);
+    /*TEST(TestMoreBefore);*/
+    TEST(TestTailorNULL);
+    TEST(TestUpperFirstQuaternary);
+    TEST(TestJ4960);
+    TEST(TestJ5223);
+    TEST(TestJ5232);
+    TEST(TestJ5367);
+    TEST(TestHiragana);
+    TEST(TestSortKeyConsistency);
+    TEST(TestVI5913);  /* VI, RO tailored rules */
+    TEST(TestCroatianSortKey);
+    TEST(TestTailor6179);
+    TEST(TestUCAPrecontext);
+    TEST(TestOutOfBuffer5468);
+    TEST(TestSameStrengthList);
+
+    TEST(TestSameStrengthListQuoted);
+    TEST(TestSameStrengthListSupplemental);
+    TEST(TestSameStrengthListQwerty);
+    TEST(TestSameStrengthListQuotedQwerty);
+    TEST(TestSameStrengthListRanges);
+    TEST(TestSameStrengthListSupplementalRanges);
+    TEST(TestSpecialCharacters);
+    TEST(TestPrivateUseCharacters);
+    TEST(TestPrivateUseCharactersInList);
+    TEST(TestPrivateUseCharactersInRange);
+    TEST(TestInvalidListsAndRanges);
+    TEST(TestImport);
+    TEST(TestImportWithType);
+
+    TEST(TestBeforeRuleWithScriptReordering);
+    TEST(TestNonLeadBytesDuringCollationReordering);
+    TEST(TestReorderingAPI);
+    TEST(TestGreekFirstReorder);
+    TEST(TestGreekLastReorder);
+    TEST(TestNonScriptReorder);
+    TEST(TestHaniReorder);
+    TEST(TestMultipleReorder);
+}
+
+#endif /* #if !UCONFIG_NO_COLLATION */