]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/ssearch.cpp
ICU-511.27.tar.gz
[apple/icu.git] / icuSources / test / intltest / ssearch.cpp
index 1b9a4aa6695138a0826e099c54b468b8af01c672..39bd6152b90a8dcb8f43e0854499fd32bc859845 100644 (file)
@@ -1,42 +1,32 @@
 /*
  **********************************************************************
- *   Copyright (C) 2005-2008, International Business Machines
+ *   Copyright (C) 2005-2013, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  **********************************************************************
  */
 
-
 #include "unicode/utypes.h"
 
 #if !UCONFIG_NO_COLLATION
 
-#include "unicode/unistr.h"
-#include "unicode/putil.h"
-#include "unicode/usearch.h"
-
 #include "cmemory.h"
+#include "cstring.h"
+#include "ucol_imp.h"
+
 #include "unicode/coll.h"
 #include "unicode/tblcoll.h"
-#include "unicode/coleitr.h"
-#include "unicode/ucoleitr.h"
-
-#include "unicode/regex.h"        // TODO: make conditional on regexp being built.
-
-#include "unicode/uniset.h"
+#include "unicode/usearch.h"
 #include "unicode/uset.h"
 #include "unicode/ustring.h"
-#include "hash.h"
-#include "uhash.h"
-#include "ucol_imp.h"
 
-#include "intltest.h"
-#include "ssearch.h"
+#include "unicode/coleitr.h"
+#include "unicode/regex.h"        // TODO: make conditional on regexp being built.
 
+#include "colldata.h"
+#include "ssearch.h"
 #include "xmlparser.h"
 
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
+#include <stdio.h>  // for sprintf
 
 char testId[100];
 
@@ -44,13 +34,15 @@ char testId[100];
     errln("Failure in file %s, line %d, test ID = \"%s\"", __FILE__, __LINE__, testId);}}
 
 #define TEST_ASSERT_M(x, m) {if (!(x)) { \
-    errln("Failure in file %s, line %d.   \"%s\"", __FILE__, __LINE__, m);return;}}
+    dataerrln("Failure in file %s, line %d.   \"%s\"", __FILE__, __LINE__, m);return;}}
 
 #define TEST_ASSERT_SUCCESS(errcode) {if (U_FAILURE(errcode)) { \
-    errln("Failure in file %s, line %d, test ID \"%s\", status = \"%s\"", \
+    dataerrln("Failure in file %s, line %d, test ID \"%s\", status = \"%s\"", \
           __FILE__, __LINE__, testId, u_errorName(errcode));}}
 
 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
+#define NEW_ARRAY(type, count) (type *) uprv_malloc((count) * sizeof(type))
+#define DELETE_ARRAY(array) uprv_free((void *) (array))
 
 //---------------------------------------------------------------------------
 //
@@ -81,6 +73,18 @@ void SSearchTest::runIndexedTest( int32_t index, UBool exec, const char* &name,
         case 2: name = "monkeyTest";
             if (exec) monkeyTest(params);
             break;
+
+        case 3: name = "sharpSTest";
+            if (exec) sharpSTest();
+            break;
+
+        case 4: name = "goodSuffixTest";
+            if (exec) goodSuffixTest();
+            break;
+
+        case 5: name = "searchTime";
+            if (exec) searchTime();
+            break;
 #endif
         default: name = "";
             break; //needed to end loop
@@ -108,7 +112,7 @@ const char *SSearchTest::getPath(char buffer[2048], const char *filename) {
 
 void SSearchTest::searchTest()
 {
-#if !UCONFIG_NO_REGULAR_EXPRESSIONS
+#if !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_FILE_IO
     UErrorCode status = U_ZERO_ERROR;
     char path[PATH_BUFFER_SIZE];
     const char *testFilePath = getPath(path, "ssearch.xml");
@@ -117,9 +121,9 @@ void SSearchTest::searchTest()
         return; /* Couldn't get path: error message already output. */
     }
 
-    UXMLParser  *parser = UXMLParser::createParser(status);
+    LocalPointer<UXMLParser> parser(UXMLParser::createParser(status));
     TEST_ASSERT_SUCCESS(status);
-    UXMLElement *root   = parser->parseFile(testFilePath, status);
+    LocalPointer<UXMLElement> root(parser->parseFile(testFilePath, status));
     TEST_ASSERT_SUCCESS(status);
     if (U_FAILURE(status)) {
         return;
@@ -155,7 +159,7 @@ void SSearchTest::searchTest()
         //    Default is tertiary if the XML attribute is missing from the test case.
         //
         const UnicodeString *strength = testCase->getAttribute("strength");
-        UColAttributeValue collatorStrength;
+        UColAttributeValue collatorStrength = UCOL_PRIMARY;
         if      (strength==NULL)          { collatorStrength = UCOL_TERTIARY;}
         else if (*strength=="PRIMARY")    { collatorStrength = UCOL_PRIMARY;}
         else if (*strength=="SECONDARY")  { collatorStrength = UCOL_SECONDARY;}
@@ -181,6 +185,16 @@ void SSearchTest::searchTest()
             normalize = UCOL_ON;
         }
 
+        //
+        // Get the alternate_handling flag. Default is UCOL_NON_IGNORABLE.
+        //
+        UColAttributeValue alternateHandling = UCOL_NON_IGNORABLE;
+        const UnicodeString *alt = testCase->getAttribute("alternate_handling");
+        TEST_ASSERT (alt == NULL || *alt == "SHIFTED" || *alt == "NON_IGNORABLE");
+        if (alt != NULL && *alt == "SHIFTED") {
+            alternateHandling = UCOL_SHIFTED;
+        }
+
         const UnicodeString defLocale("en");
         char  clocale[100];
         const UnicodeString *locale   = testCase->getAttribute("locale");
@@ -196,7 +210,7 @@ void SSearchTest::searchTest()
         int32_t        expectedMatchStart = -1;
         int32_t        expectedMatchLimit = -1;
         const UXMLElement  *n;
-        int                nodeCount = 0;
+        int32_t                nodeCount = 0;
 
         n = testCase->getChildElement("pattern");
         TEST_ASSERT(n != NULL);
@@ -215,7 +229,7 @@ void SSearchTest::searchTest()
             target.append(text);
             nodeCount++;
         }
-        
+
         n = testCase->getChildElement("m");
         if (n!=NULL) {
             expectedMatchStart = target.length();
@@ -237,23 +251,22 @@ void SSearchTest::searchTest()
         //  Check that there weren't extra things in the XML
         TEST_ASSERT(nodeCount == testCase->countChildren());
 
-        // Open a collotor and StringSearch based on the parameters
+        // Open a collator and StringSearch based on the parameters
         //   obtained from the XML.
         //
         status = U_ZERO_ERROR;
-        UCollator *collator = ucol_open(clocale, &status);
-        ucol_setStrength(collator, collatorStrength);
-        ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, normalize, &status);
-        UStringSearch *uss = usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
-                                         target.getBuffer(), target.length(),
-                                         collator,
-                                         NULL,     // the break iterator
-                                         &status);
-                                         
+        LocalUCollatorPointer collator(ucol_open(clocale, &status));
+        ucol_setStrength(collator.getAlias(), collatorStrength);
+        ucol_setAttribute(collator.getAlias(), UCOL_NORMALIZATION_MODE, normalize, &status);
+        ucol_setAttribute(collator.getAlias(), UCOL_ALTERNATE_HANDLING, alternateHandling, &status);
+        LocalUStringSearchPointer uss(usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
+                                                               target.getBuffer(), target.length(),
+                                                               collator.getAlias(),
+                                                               NULL,     // the break iterator
+                                                               &status));
+
         TEST_ASSERT_SUCCESS(status);
         if (U_FAILURE(status)) {
-            usearch_close(uss);
-            ucol_close(collator);
             continue;
         }
 
@@ -264,11 +277,11 @@ void SSearchTest::searchTest()
         //
         // Do the search, check the match result against the expected results.
         //
-        foundMatch= usearch_search(uss, 0, &foundStart, &foundLimit, &status);
+        foundMatch= usearch_search(uss.getAlias(), 0, &foundStart, &foundLimit, &status);
         TEST_ASSERT_SUCCESS(status);
-        if (foundMatch && expectedMatchStart<0 ||
-            foundStart != expectedMatchStart   ||
-            foundLimit != expectedMatchLimit) {
+        if ((foundMatch && expectedMatchStart<0) ||
+            (foundStart != expectedMatchStart)   ||
+            (foundLimit != expectedMatchLimit)) {
                 TEST_ASSERT(FALSE);   //  ouput generic error position
                 infoln("Found, expected match start = %d, %d \n"
                        "Found, expected match limit = %d, %d",
@@ -281,37 +294,29 @@ void SSearchTest::searchTest()
             expectedMatchStart = foundStart;
             expectedMatchLimit = foundLimit;
 
-            foundMatch = usearch_search(uss, foundLimit, &foundStart, &foundLimit, &status);
+            foundMatch = usearch_search(uss.getAlias(), foundLimit, &foundStart, &foundLimit, &status);
         }
 
-        usearch_close(uss);
-       
-        uss = usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
+        uss.adoptInstead(usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
             target.getBuffer(), target.length(),
-            collator,
+            collator.getAlias(),
             NULL,
-            &status);
+            &status));
 
         //
         // Do the backwards search, check the match result against the expected results.
         //
-        foundMatch= usearch_searchBackwards(uss, target.length(), &foundStart, &foundLimit, &status);
+        foundMatch= usearch_searchBackwards(uss.getAlias(), target.length(), &foundStart, &foundLimit, &status);
         TEST_ASSERT_SUCCESS(status);
-        if (foundMatch && expectedMatchStart<0 ||
-            foundStart != expectedMatchStart   ||
-            foundLimit != expectedMatchLimit) {
+        if ((foundMatch && expectedMatchStart<0) ||
+            (foundStart != expectedMatchStart)   ||
+            (foundLimit != expectedMatchLimit)) {
                 TEST_ASSERT(FALSE);   //  ouput generic error position
                 infoln("Found, expected backwards match start = %d, %d \n"
                        "Found, expected backwards match limit = %d, %d",
                 foundStart, expectedMatchStart, foundLimit, expectedMatchLimit);
         }
-
-        usearch_close(uss);
-        ucol_close(collator);
     }
-
-    delete root;
-    delete parser;
 #endif
 }
 
@@ -346,7 +351,7 @@ private:
 };
 
 OrderList::OrderList()
-  : list(NULL), listSize(0), listMax(16)
+  : list(NULL),  listMax(16), listSize(0)
 {
     list = new Order[listMax];
 }
@@ -359,7 +364,7 @@ OrderList::OrderList(UCollator *coll, const UnicodeString &string, int32_t strin
     uint32_t strengthMask = 0;
     int32_t order, low, high;
 
-    switch (ucol_getStrength(coll)) 
+    switch (ucol_getStrength(coll))
     {
     default:
         strengthMask |= UCOL_TERTIARYORDERMASK;
@@ -549,6 +554,10 @@ static char *printOrders(char *buffer, OrderList &list)
 void SSearchTest::offsetTest()
 {
     const char *test[] = {
+        // The sequence \u0FB3\u0F71\u0F71\u0F80 contains a discontiguous
+        // contraction (\u0FB3\u0F71\u0F80) logically followed by \u0F71.
+        "\\u1E33\\u0FB3\\u0F71\\u0F71\\u0F80\\uD835\\uDF6C\\u01B0",
+
         "\\ua191\\u16ef\\u2036\\u017a",
 
 #if 0
@@ -565,20 +574,24 @@ void SSearchTest::offsetTest()
         "\\u0310\\u0311\\u0312\\u0313\\u0314\\u0315\\u0316\\u0317\\u0318\\u0319\\u031A\\u031B\\u031C\\u031D\\u031E\\u031F"
         "\\u0320\\u0321\\u0322\\u0323\\u0324\\u0325\\u0326\\u0327\\u0328\\u0329\\u032A\\u032B\\u032C\\u032D\\u032E\\u032F"
         "\\u0330\\u0331\\u0332\\u0333\\u0334\\u0335\\u0336\\u0337\\u0338\\u0339\\u033A\\u033B\\u033C\\u033D\\u033E\\u033F"
-        "\\u0340\\u0341\\u0342\\u0343\\u0344\\u0345\\u0346\\u0347\\u0348\\u0349\\u034A\\u034B\\u034C\\u034D\\u034E",
+        "\\u0340\\u0341\\u0342\\u0343\\u0344\\u0345\\u0346\\u0347\\u0348\\u0349\\u034A\\u034B\\u034C\\u034D\\u034E", // currently not working, see #8081
 
-        "\\u02FE\\u02FF\\u0300\\u0301\\u0302\\u0303\\u0316\\u0317\\u0318",
+        "\\u02FE\\u02FF\\u0300\\u0301\\u0302\\u0303\\u0316\\u0317\\u0318", // currently not working, see #8081
+        "a\\u02FF\\u0301\\u0316", // currently not working, see #8081
+        "a\\u02FF\\u0316\\u0301",
+        "a\\u0430\\u0301\\u0316",
+        "a\\u0430\\u0316\\u0301",
         "abc\\u0E41\\u0301\\u0316",
-               "abc\\u0E41\\u0316\\u0301",
-               "\\u0E41\\u0301\\u0316",
-               "\\u0E41\\u0316\\u0301",
-               "a\\u0301\\u0316",
-               "a\\u0316\\u0301",
-               "\\uAC52\\uAC53",
-               "\\u34CA\\u34CB",
-               "\\u11ED\\u11EE",
-               "\\u30C3\\u30D0",
-               "p\\u00E9ch\\u00E9",
+        "abc\\u0E41\\u0316\\u0301",
+        "\\u0E41\\u0301\\u0316",
+        "\\u0E41\\u0316\\u0301",
+        "a\\u0301\\u0316",
+        "a\\u0316\\u0301",
+        "\\uAC52\\uAC53",
+        "\\u34CA\\u34CB",
+        "\\u11ED\\u11EE",
+        "\\u30C3\\u30D0",
+        "p\\u00E9ch\\u00E9",
         "a\\u0301\\u0325",
         "a\\u0300\\u0325",
         "a\\u0325\\u0300",
@@ -606,7 +619,7 @@ void SSearchTest::offsetTest()
     UErrorCode status = U_ZERO_ERROR;
     RuleBasedCollator *col = (RuleBasedCollator *) Collator::createInstance(Locale::getEnglish(), status);
     if (U_FAILURE(status)) {
-        errln("Failed to create collator in offsetTest!");
+        errcheckln(status, "Failed to create collator in offsetTest! - %s", u_errorName(status));
         return;
     }
     char buffer[4096];  // A bit of a hack... just happens to be long enough for all the test cases...
@@ -616,6 +629,9 @@ void SSearchTest::offsetTest()
     col->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
 
     for(int32_t i = 0; i < testCount; i += 1) {
+        if (!isICUVersionAtLeast(52, 0, 1) && i>=4 && i<=6) {
+            continue; // timebomb until ticket #9156 (was #8081) is resolved
+        }
         UnicodeString ts = CharsToUnicodeString(test[i]);
         CollationElementIterator *iter = col->createCollationElementIterator(ts);
         OrderList forwardList;
@@ -673,338 +689,7 @@ void SSearchTest::offsetTest()
     delete col;
 }
 
-class CEList
-{
-public:
-    CEList(UCollator *coll, const UnicodeString &string);
-    ~CEList();
-
-    int32_t size() const;
-    int32_t get(int32_t index) const;
-    UBool matchesAt(int32_t offset, const CEList *other) const; 
-
-private:
-    void add(int32_t ce);
-
-    int32_t *ces;
-    int32_t listMax;
-    int32_t listSize;
-};
-
-CEList::CEList(UCollator *coll, const UnicodeString &string)
-    : ces(NULL), listMax(8), listSize(0)
-{
-    UErrorCode status = U_ZERO_ERROR;
-    UCollationElements *elems = ucol_openElements(coll, string.getBuffer(), string.length(), &status);
-    uint32_t strengthMask = 0;
-    int32_t order;
-
 #if 0
-    switch (ucol_getStrength(coll)) 
-    {
-    default:
-        strengthMask |= UCOL_TERTIARYORDERMASK;
-        /* fall through */
-
-    case UCOL_SECONDARY:
-        strengthMask |= UCOL_SECONDARYORDERMASK;
-        /* fall through */
-
-    case UCOL_PRIMARY:
-        strengthMask |= UCOL_PRIMARYORDERMASK;
-    }
-#else
-    strengthMask = UCOL_PRIMARYORDERMASK;
-#endif
-
-    ces = new int32_t[listMax];
-
-    while ((order = ucol_next(elems, &status)) != UCOL_NULLORDER) {
-        order &= strengthMask;
-
-        if (order == UCOL_IGNORABLE) {
-            continue;
-        }
-
-        add(order);
-    }
-
-    ucol_closeElements(elems);
-}
-
-CEList::~CEList()
-{
-    delete[] ces;
-}
-
-void CEList::add(int32_t ce)
-{
-    if (listSize >= listMax) {
-        listMax *= 2;
-
-        int32_t *newCEs = new int32_t[listMax];
-
-        uprv_memcpy(newCEs, ces, listSize * sizeof(int32_t));
-        delete[] ces;
-        ces = newCEs;
-    }
-
-    ces[listSize++] = ce;
-}
-
-int32_t CEList::get(int32_t index) const
-{
-    if (index >= 0 && index < listSize) {
-        return ces[index];
-    }
-
-    return -1;
-}
-
-UBool CEList::matchesAt(int32_t offset, const CEList *other) const
-{
-    if (listSize - offset < other->size()) {
-        return FALSE;
-    }
-
-    for (int32_t i = offset, j = 0; j < other->size(); i += 1, j += 1) {
-        if (ces[i] != other->get(j)) {
-            return FALSE;
-        }
-    }
-
-    return TRUE;
-}
-
-int32_t CEList::size() const
-{
-    return listSize;
-}
-
-class StringList
-{
-public:
-    StringList();
-    ~StringList();
-
-    void add(const UnicodeString *string);
-    void add(const UChar *chars, int32_t count);
-    const UnicodeString *get(int32_t index) const;
-    int32_t size() const;
-
-private:
-    UnicodeString *strings;
-    int32_t listMax;
-    int32_t listSize;
-};
-
-StringList::StringList()
-    : strings(NULL), listMax(16), listSize(0)
-{
-    strings = new UnicodeString [listMax];
-}
-
-StringList::~StringList()
-{
-    delete[] strings;
-}
-
-void StringList::add(const UnicodeString *string)
-{
-    if (listSize >= listMax) {
-        listMax *= 2;
-
-        UnicodeString *newStrings = new UnicodeString[listMax];
-
-        uprv_memcpy(newStrings, strings, listSize * sizeof(UnicodeString));
-
-        delete[] strings;
-        strings = newStrings;
-    }
-
-    // The ctor initialized all the strings in
-    // the array to empty strings, so this
-    // is the same as copying the source string.
-    strings[listSize++].append(*string);
-}
-
-void StringList::add(const UChar *chars, int32_t count)
-{
-    const UnicodeString string(chars, count);
-
-    add(&string);
-}
-
-const UnicodeString *StringList::get(int32_t index) const
-{
-    if (index >= 0 && index < listSize) {
-        return &strings[index];
-    }
-
-    return NULL;
-}
-
-int32_t StringList::size() const
-{
-    return listSize;
-}
-
-class CEToStringsMap
-{
-public:
-
-    CEToStringsMap();
-    ~CEToStringsMap();
-
-    void put(int32_t ce, UnicodeString *string);
-    StringList *getStringList(int32_t ce) const;
-
-private:
-    static void deleteStringList(void *obj);
-    void putStringList(int32_t ce, StringList *stringList);
-    UHashtable *map;
-};
-
-CEToStringsMap::CEToStringsMap()
-{
-    UErrorCode status = U_ZERO_ERROR;
-
-    map = uhash_open(uhash_hashLong, uhash_compareLong,
-                     uhash_compareCaselessUnicodeString,
-                     &status);
-
-    uhash_setValueDeleter(map, deleteStringList);
-}
-
-CEToStringsMap::~CEToStringsMap()
-{
-    uhash_close(map);
-}
-
-void CEToStringsMap::put(int32_t ce, UnicodeString *string)
-{
-    StringList *strings = getStringList(ce);
-
-    if (strings == NULL) {
-        strings = new StringList();
-        putStringList(ce, strings);
-    }
-
-    strings->add(string);
-}
-
-StringList *CEToStringsMap::getStringList(int32_t ce) const
-{
-    return (StringList *) uhash_iget(map, ce);
-}
-
-void CEToStringsMap::putStringList(int32_t ce, StringList *stringList)
-{
-    UErrorCode status = U_ZERO_ERROR;
-
-    uhash_iput(map, ce, (void *) stringList, &status);
-}
-
-void CEToStringsMap::deleteStringList(void *obj)
-{
-    StringList *strings = (StringList *) obj;
-
-    delete strings;
-}
-
-class StringToCEsMap
-{
-public:
-    StringToCEsMap();
-    ~StringToCEsMap();
-
-    void put(const UnicodeString *string, const CEList *ces);
-    const CEList *get(const UnicodeString *string);
-
-private:
-
-    static void deleteCEList(void *obj);
-    static void deleteUnicodeStringKey(void *obj);
-
-    UHashtable *map;
-};
-
-StringToCEsMap::StringToCEsMap()
-{
-    UErrorCode status = U_ZERO_ERROR;
-
-    map = uhash_open(uhash_hashCaselessUnicodeString,
-                     uhash_compareCaselessUnicodeString,
-                     uhash_compareLong,
-                     &status);
-
-    uhash_setValueDeleter(map, deleteCEList);
-    uhash_setKeyDeleter(map, deleteUnicodeStringKey);
-}
-
-StringToCEsMap::~StringToCEsMap()
-{
-    uhash_close(map);
-}
-
-void StringToCEsMap::put(const UnicodeString *string, const CEList *ces)
-{
-    UErrorCode status = U_ZERO_ERROR;
-
-    uhash_put(map, (void *) string, (void *) ces, &status);
-}
-
-const CEList *StringToCEsMap::get(const UnicodeString *string)
-{
-    return (const CEList *) uhash_get(map, string);
-}
-
-void StringToCEsMap::deleteCEList(void *obj)
-{
-    CEList *list = (CEList *) obj;
-
-    delete list;
-}
-
-void StringToCEsMap::deleteUnicodeStringKey(void *obj)
-{
-    UnicodeString *key = (UnicodeString *) obj;
-
-    delete key;
-}
-
-static void buildData(UCollator *coll, USet *charsToTest, StringToCEsMap *charsToCEList, CEToStringsMap *ceToCharsStartingWith)
-{
-    int32_t itemCount = uset_getItemCount(charsToTest);
-    UErrorCode status = U_ZERO_ERROR;
-
-    for(int32_t item = 0; item < itemCount; item += 1) {
-        UChar32 start = 0, end = 0;
-        UChar buffer[16];
-        int32_t len = uset_getItem(charsToTest, item, &start, &end,
-                                   buffer, 16, &status);
-
-        if (len == 0) {
-            for (UChar32 ch = start; ch <= end; ch += 1) {
-                UnicodeString *st = new UnicodeString(ch);
-                CEList *ceList = new CEList(coll, *st);
-
-                charsToCEList->put(st, ceList);
-                ceToCharsStartingWith->put(ceList->get(0), st);
-            }
-        } else if (len > 0) {
-            UnicodeString *st = new UnicodeString(buffer, len);
-            CEList *ceList = new CEList(coll, *st);
-
-            charsToCEList->put(st, ceList);
-            ceToCharsStartingWith->put(ceList->get(0), st);
-        } else {
-            // shouldn't happen...
-        }
-    }
-}
-
 static UnicodeString &escape(const UnicodeString &string, UnicodeString &buffer)
 {
     for(int32_t i = 0; i < string.length(); i += 1) {
@@ -1035,67 +720,252 @@ static UnicodeString &escape(const UnicodeString &string, UnicodeString &buffer)
 
     return buffer;
 }
+#endif
 
-static int32_t minLengthInChars(const CEList *ceList, int32_t offset, StringToCEsMap *charsToCEList, CEToStringsMap *ceToCharsStartingWith,
-                                UnicodeString &debug)
+void SSearchTest::sharpSTest()
 {
-    // find out shortest string for the longest sequence of ces.
-    // needs to be refined to use dynamic programming, but will be roughly right
-       int32_t totalStringLength = 0;
-       
-    while (offset < ceList->size()) {
-        int32_t ce = ceList->get(offset);
-        int32_t bestLength = INT32_MIN;
-        const UnicodeString *bestString = NULL;
-        int32_t bestCeLength = 0;
-        const StringList *strings = ceToCharsStartingWith->getStringList(ce);
-        int32_t stringCount = strings->size();
-      
-        for (int32_t s = 0; s < stringCount; s += 1) {
-            const UnicodeString *string = strings->get(s);
-            const CEList *ceList2 = charsToCEList->get(string);
-
-            if (ceList->matchesAt(offset, ceList2)) {
-                int32_t length = ceList2->size() - string->length();
-
-                if (bestLength < length) {
-                    bestLength = length;
-                    bestCeLength = ceList2->size();
-                    bestString = string;
-                }
-            }
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator *coll = NULL;
+    UnicodeString lp  = "fuss";
+    UnicodeString sp = "fu\\u00DF";
+    UnicodeString targets[]  = {"fu\\u00DF", "fu\\u00DFball", "1fu\\u00DFball", "12fu\\u00DFball", "123fu\\u00DFball", "1234fu\\u00DFball",
+                                "ffu\\u00DF", "fufu\\u00DF", "fusfu\\u00DF",
+                                "fuss", "ffuss", "fufuss", "fusfuss", "1fuss", "12fuss", "123fuss", "1234fuss", "fu\\u00DF", "1fu\\u00DF", "12fu\\u00DF", "123fu\\u00DF", "1234fu\\u00DF"};
+    int32_t start = -1, end = -1;
+
+    coll = ucol_openFromShortString("LEN_S1", FALSE, NULL, &status);
+    TEST_ASSERT_SUCCESS(status);
+
+    UnicodeString lpUnescaped = lp.unescape();
+    UnicodeString spUnescaped = sp.unescape();
+
+    LocalUStringSearchPointer ussLong(usearch_openFromCollator(lpUnescaped.getBuffer(), lpUnescaped.length(),
+                                                           lpUnescaped.getBuffer(), lpUnescaped.length(),   // actual test data will be set later
+                                                           coll,
+                                                           NULL,     // the break iterator
+                                                           &status));
+
+    LocalUStringSearchPointer ussShort(usearch_openFromCollator(spUnescaped.getBuffer(), spUnescaped.length(),
+                                                           spUnescaped.getBuffer(), spUnescaped.length(),   // actual test data will be set later
+                                                           coll,
+                                                           NULL,     // the break iterator
+                                                           &status));
+    TEST_ASSERT_SUCCESS(status);
+
+    for (uint32_t t = 0; t < (sizeof(targets)/sizeof(targets[0])); t += 1) {
+        UBool bFound;
+        UnicodeString target = targets[t].unescape();
+
+        start = end = -1;
+        usearch_setText(ussLong.getAlias(), target.getBuffer(), target.length(), &status);
+        bFound = usearch_search(ussLong.getAlias(), 0, &start, &end, &status);
+        TEST_ASSERT_SUCCESS(status);
+        if (bFound) {
+            logln("Test %d: found long pattern at [%d, %d].", t, start, end);
+        } else {
+            dataerrln("Test %d: did not find long pattern.", t);
+        }
+
+        usearch_setText(ussShort.getAlias(), target.getBuffer(), target.length(), &status);
+        bFound = usearch_search(ussShort.getAlias(), 0, &start, &end, &status);
+        TEST_ASSERT_SUCCESS(status);
+        if (bFound) {
+            logln("Test %d: found long pattern at [%d, %d].", t, start, end);
+        } else {
+            dataerrln("Test %d: did not find long pattern.", t);
         }
-      
-        totalStringLength += bestString->length();
-        escape(*bestString, debug).append("/");
-        offset += bestCeLength;
     }
 
-    debug.append((UChar)0x0000);
-    return totalStringLength;
+    ucol_close(coll);
 }
 
-static void minLengthTest(UCollator *coll, StringToCEsMap *charsToCEList, CEToStringsMap *ceToCharsStartingWith)
+void SSearchTest::goodSuffixTest()
 {
-    UnicodeString examples[] = {"fuss", "fiss", "affliss", "VII"};
-    UnicodeString debug;
-    int32_t nExamples = sizeof(examples) / sizeof(examples[0]);
+    UErrorCode status = U_ZERO_ERROR;
+    UCollator *coll = NULL;
+    UnicodeString pat = /*"gcagagag"*/ "fxeld";
+    UnicodeString target = /*"gcatcgcagagagtatacagtacg"*/ "cloveldfxeld";
+    int32_t start = -1, end = -1;
+    UBool bFound;
+
+    coll = ucol_open(NULL, &status);
+    TEST_ASSERT_SUCCESS(status);
 
-    for (int32_t s = 0; s < nExamples; s += 1) {
-        CEList *ceList = new CEList(coll, examples[s]);
+    LocalUStringSearchPointer ss(usearch_openFromCollator(pat.getBuffer(), pat.length(),
+                                                          target.getBuffer(), target.length(),
+                                                          coll,
+                                                          NULL,     // the break iterator
+                                                          &status));
+    TEST_ASSERT_SUCCESS(status);
 
-      //infoln("%S:", examples[s].getTerminatedBuffer());
+    bFound = usearch_search(ss.getAlias(), 0, &start, &end, &status);
+    TEST_ASSERT_SUCCESS(status);
+    if (bFound) {
+        logln("Found pattern at [%d, %d].", start, end);
+    } else {
+        dataerrln("Did not find pattern.");
+    }
 
-        for(int32_t i = 0; i < examples[s].length(); i += 1) {
-            debug.remove();
+    ucol_close(coll);
+}
 
-            int32_t minLength = minLengthInChars(ceList, i, charsToCEList, ceToCharsStartingWith, debug);
-          //infoln("\t%d\t%S", minLength, debug.getTerminatedBuffer());
-        }
+//
+//  searchTime()    A quick and dirty performance test for string search.
+//                  Probably  doesn't really belong as part of intltest, but it
+//                  does check that the search succeeds, and gets the right result,
+//                  so it serves as a functionality test also.
+//
+//                  To run as a perf test, up the loop count, select by commenting
+//                  and uncommenting in the code the operation to be measured,
+//                  rebuild, and measure the running time of this test alone.
+//
+//                     time LD_LIBRARY_PATH=whatever  ./intltest  collate/SSearchTest/searchTime
+//
+void SSearchTest::searchTime() {
+    static const char *longishText =
+"Whylom, as olde stories tellen us,\n"
+"Ther was a duk that highte Theseus:\n"
+"Of Athenes he was lord and governour,\n"
+"And in his tyme swich a conquerour,\n"
+"That gretter was ther noon under the sonne.\n"
+"Ful many a riche contree hadde he wonne;\n"
+"What with his wisdom and his chivalrye,\n"
+"He conquered al the regne of Femenye,\n"
+"That whylom was y-cleped Scithia;\n"
+"And weddede the quene Ipolita,\n"
+"And broghte hir hoom with him in his contree\n"
+"With muchel glorie and greet solempnitee,\n"
+"And eek hir yonge suster Emelye.\n"
+"And thus with victorie and with melodye\n"
+"Lete I this noble duk to Athenes ryde,\n"
+"And al his hoost, in armes, him bisyde.\n"
+"And certes, if it nere to long to here,\n"
+"I wolde han told yow fully the manere,\n"
+"How wonnen was the regne of Femenye\n"
+"By Theseus, and by his chivalrye;\n"
+"And of the grete bataille for the nones\n"
+"Bitwixen Athen's and Amazones;\n"
+"And how asseged was Ipolita,\n"
+"The faire hardy quene of Scithia;\n"
+"And of the feste that was at hir weddinge,\n"
+"And of the tempest at hir hoom-cominge;\n"
+"But al that thing I moot as now forbere.\n"
+"I have, God woot, a large feeld to ere,\n"
+"And wayke been the oxen in my plough.\n"
+"The remenant of the tale is long y-nough.\n"
+"I wol nat letten eek noon of this route;\n"
+"Lat every felawe telle his tale aboute,\n"
+"And lat see now who shal the soper winne;\n"
+"And ther I lefte, I wol ageyn biginne.\n"
+"This duk, of whom I make mencioun,\n"
+"When he was come almost unto the toun,\n"
+"In al his wele and in his moste pryde,\n"
+"He was war, as he caste his eye asyde,\n"
+"Wher that ther kneled in the hye weye\n"
+"A companye of ladies, tweye and tweye,\n"
+"Ech after other, clad in clothes blake; \n"
+"But swich a cry and swich a wo they make,\n"
+"That in this world nis creature livinge,\n"
+"That herde swich another weymentinge;\n"
+"And of this cry they nolde never stenten,\n"
+"Til they the reynes of his brydel henten.\n"
+"'What folk ben ye, that at myn hoomcominge\n"
+"Perturben so my feste with cryinge'?\n"
+"Quod Theseus, 'have ye so greet envye\n"
+"Of myn honour, that thus compleyne and crye? \n"
+"Or who hath yow misboden, or offended?\n"
+"And telleth me if it may been amended;\n"
+"And why that ye ben clothed thus in blak'?\n"
+"The eldest lady of hem alle spak,\n"
+"When she hadde swowned with a deedly chere,\n"
+"That it was routhe for to seen and here,\n"
+"And seyde: 'Lord, to whom Fortune hath yiven\n"
+"Victorie, and as a conquerour to liven,\n"
+"Noght greveth us your glorie and your honour;\n"
+"But we biseken mercy and socour.\n"
+"Have mercy on our wo and our distresse.\n"
+"Som drope of pitee, thurgh thy gentilesse,\n"
+"Up-on us wrecched wommen lat thou falle.\n"
+"For certes, lord, ther nis noon of us alle,\n"
+"That she nath been a duchesse or a quene;\n"
+"Now be we caitifs, as it is wel sene:\n"
+"Thanked be Fortune, and hir false wheel,\n"
+"That noon estat assureth to be weel.\n"
+"And certes, lord, t'abyden your presence,\n"
+"Here in the temple of the goddesse Clemence\n"
+"We han ben waytinge al this fourtenight;\n"
+"Now help us, lord, sith it is in thy might.\n"
+"I wrecche, which that wepe and waille thus,\n"
+"Was whylom wyf to king Capaneus,\n"
+"That starf at Thebes, cursed be that day!\n"
+"And alle we, that been in this array,\n"
+"And maken al this lamentacioun,\n"
+"We losten alle our housbondes at that toun,\n"
+"Whyl that the sege ther-aboute lay.\n"
+"And yet now th'olde Creon, weylaway!\n"
+"The lord is now of Thebes the citee, \n"
+"Fulfild of ire and of iniquitee,\n"
+"He, for despyt, and for his tirannye,\n"
+"To do the dede bodyes vileinye,\n"
+"Of alle our lordes, whiche that ben slawe,\n"
+"Hath alle the bodyes on an heep y-drawe,\n"
+"And wol nat suffren hem, by noon assent,\n"
+"Neither to been y-buried nor y-brent,\n"
+"But maketh houndes ete hem in despyt. zet'\n";
+
+const char *cPattern = "maketh houndes ete hem";
+//const char *cPattern = "Whylom";
+//const char *cPattern = "zet";
+    const char *testId = "searchTime()";   // for error macros.
+    UnicodeString target = longishText;
+    UErrorCode status = U_ZERO_ERROR;
+
+
+    LocalUCollatorPointer collator(ucol_open("en", &status));
+    //ucol_setStrength(collator.getAlias(), collatorStrength);
+    //ucol_setAttribute(collator.getAlias(), UCOL_NORMALIZATION_MODE, normalize, &status);
+    UnicodeString uPattern = cPattern;
+    LocalUStringSearchPointer uss(usearch_openFromCollator(uPattern.getBuffer(), uPattern.length(),
+                                                           target.getBuffer(), target.length(),
+                                                           collator.getAlias(),
+                                                           NULL,     // the break iterator
+                                                           &status));
+    TEST_ASSERT_SUCCESS(status);
+
+//  int32_t foundStart;
+//  int32_t foundEnd;
+    UBool   found;
+
+    // Find the match position usgin strstr
+    const char *pm = strstr(longishText, cPattern);
+    TEST_ASSERT_M(pm!=NULL, "No pattern match with strstr");
+    int32_t  refMatchPos = (int32_t)(pm - longishText);
+    int32_t  icuMatchPos;
+    int32_t  icuMatchEnd;
+    usearch_search(uss.getAlias(), 0, &icuMatchPos, &icuMatchEnd, &status);
+    TEST_ASSERT_SUCCESS(status);
+    TEST_ASSERT_M(refMatchPos == icuMatchPos, "strstr and icu give different match positions.");
+
+    int32_t i;
+    // int32_t j=0;
+
+    // Try loopcounts around 100000 to some millions, depending on the operation,
+    //   to get runtimes of at least several seconds.
+    for (i=0; i<10000; i++) {
+        found = usearch_search(uss.getAlias(), 0, &icuMatchPos, &icuMatchEnd, &status);
+        //TEST_ASSERT_SUCCESS(status);
+        //TEST_ASSERT(found);
 
-      //infoln();
-        delete ceList;
+        // usearch_setOffset(uss.getAlias(), 0, &status);
+        // icuMatchPos = usearch_next(uss.getAlias(), &status);
+
+         // The i+j stuff is to confuse the optimizer and get it to actually leave the
+         //   call to strstr in place.
+         //pm = strstr(longishText+j, cPattern);
+         //j = (j + i)%5;
     }
+
+    //printf("%ld, %d\n", pm-longishText, j);
 }
 
 //----------------------------------------------------------------------------------------
@@ -1171,7 +1041,7 @@ void SetMonkey::append(UnicodeString &test, UnicodeString &alternate)
 class StringSetMonkey : public Monkey
 {
 public:
-    StringSetMonkey(const USet *theSet, UCollator *theCollator, StringToCEsMap *theCharsToCEList, CEToStringsMap *theCeToCharsStartingWith);
+    StringSetMonkey(const USet *theSet, UCollator *theCollator, CollData *theCollData);
     ~StringSetMonkey();
 
     void append(UnicodeString &testCase, UnicodeString &alternate);
@@ -1180,13 +1050,12 @@ private:
     UnicodeString &generateAlternative(const UnicodeString &testCase, UnicodeString &alternate);
 
     const USet *set;
-    UCollator      *coll;
-    StringToCEsMap *charsToCEList;
-    CEToStringsMap *ceToCharsStartingWith;
+    UCollator  *coll;
+    CollData   *collData;
 };
 
-StringSetMonkey::StringSetMonkey(const USet *theSet, UCollator *theCollator, StringToCEsMap *theCharsToCEList, CEToStringsMap *theCeToCharsStartingWith)
-: Monkey(), set(theSet), coll(theCollator), charsToCEList(theCharsToCEList), ceToCharsStartingWith(theCeToCharsStartingWith)
+StringSetMonkey::StringSetMonkey(const USet *theSet, UCollator *theCollator, CollData *theCollData)
+: Monkey(), set(theSet), coll(theCollator), collData(theCollData)
 {
     // ook.
 }
@@ -1228,7 +1097,8 @@ UnicodeString &StringSetMonkey::generateAlternative(const UnicodeString &testCas
 {
     // find out shortest string for the longest sequence of ces.
     // needs to be refined to use dynamic programming, but will be roughly right
-    CEList ceList(coll, testCase);
+    UErrorCode status = U_ZERO_ERROR;
+    CEList ceList(coll, testCase, status);
     UnicodeString alt;
     int32_t offset = 0;
 
@@ -1238,7 +1108,7 @@ UnicodeString &StringSetMonkey::generateAlternative(const UnicodeString &testCas
 
     while (offset < ceList.size()) {
         int32_t ce = ceList.get(offset);
-        const StringList *strings = ceToCharsStartingWith->getStringList(ce);
+        const StringList *strings = collData->getStringList(ce);
 
         if (strings == NULL) {
             return alternate.append(testCase);
@@ -1246,10 +1116,11 @@ UnicodeString &StringSetMonkey::generateAlternative(const UnicodeString &testCas
 
         int32_t stringCount = strings->size();
         int32_t tries = 0;
-      
+
         // find random string that generates the same CEList
-        const CEList *ceList2;
-        const UnicodeString *string;
+        const CEList *ceList2 = NULL;
+        const UnicodeString *string = NULL;
+              UBool matches = FALSE;
 
         do {
             int32_t s = m_rand() % stringCount;
@@ -1260,14 +1131,20 @@ UnicodeString &StringSetMonkey::generateAlternative(const UnicodeString &testCas
             }
 
             string = strings->get(s);
-            ceList2 = charsToCEList->get(string);
-        } while (! ceList.matchesAt(offset, ceList2));
+            ceList2 = collData->getCEList(string);
+            matches = ceList.matchesAt(offset, ceList2);
+
+            if (! matches) {
+                collData->freeCEList((CEList *) ceList2);
+            }
+        } while (! matches);
 
         alt.append(*string);
         offset += ceList2->size();
+        collData->freeCEList(ceList2);
     }
 
-    const CEList altCEs(coll, alt);
+    const CEList altCEs(coll, alt, status);
 
     if (ceList.matchesAt(0, &altCEs)) {
         return alternate.append(alt);
@@ -1279,6 +1156,7 @@ UnicodeString &StringSetMonkey::generateAlternative(const UnicodeString &testCas
 static void generateTestCase(UCollator *coll, Monkey *monkeys[], int32_t monkeyCount, UnicodeString &testCase, UnicodeString &alternate)
 {
     int32_t pieces = (m_rand() % 4) + 1;
+    UErrorCode status = U_ZERO_ERROR;
     UBool matches;
 
     do {
@@ -1292,91 +1170,13 @@ static void generateTestCase(UCollator *coll, Monkey *monkeys[], int32_t monkeyC
             monkeys[monkey]->append(testCase, alternate);
         }
 
-        const CEList ceTest(coll, testCase);
-        const CEList ceAlt(coll, alternate);
+        const CEList ceTest(coll, testCase, status);
+        const CEList ceAlt(coll, alternate, status);
 
         matches = ceTest.matchesAt(0, &ceAlt);
     } while (! matches);
 }
 
-static inline USet *uset_openEmpty()
-{
-    return uset_open(1, 0);
-}
-
-//
-//  Find the next acceptable boundary following the specified starting index
-//     in the target text being searched.
-//      TODO:  refine what is an acceptable boundary.  For the moment,
-//             choose the next position not within a combining sequence.
-//
-static int32_t nextBoundaryAfter(const UnicodeString &string, int32_t startIndex) {
-    const UChar *text = string.getBuffer();
-    int32_t textLen   = string.length();
-    
-    if (startIndex >= textLen) {
-        return startIndex;
-    }
-
-    UChar32  c;
-    int32_t  i = startIndex;
-
-    U16_NEXT(text, i, textLen, c);
-    
-    // If we are on a control character, stop without looking for combining marks.
-    //    Control characters do not combine.
-    int32_t gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
-    if (gcProperty==U_GCB_CONTROL || gcProperty==U_GCB_LF || gcProperty==U_GCB_CR) {
-        return i;
-    }
-    
-    // The initial character was not a control, and can thus accept trailing
-    //   combining characters.  Advance over however many of them there are.
-    int32_t  indexOfLastCharChecked;
-
-    for (;;) {
-        indexOfLastCharChecked = i;
-
-        if (i>=textLen) {
-            break;
-        }
-
-        U16_NEXT(text, i, textLen, c);
-        gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
-
-        if (gcProperty != U_GCB_EXTEND && gcProperty != U_GCB_SPACING_MARK) {
-            break;
-        }
-    }
-
-    return indexOfLastCharChecked;
-}
-static UBool isInCombiningSequence(const UnicodeString &string, int32_t index) {
-    const UChar *text = string.getBuffer();
-    int32_t textLen   = string.length();
-    
-    if (index>=textLen || index<=0) {
-        return FALSE;
-    }
-  
-    // If the character at the current index is not a GRAPHEME_EXTEND
-    //    then we can not be within a combining sequence.
-    UChar32  c;
-    U16_GET(text, 0, index, textLen, c);
-    int32_t gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
-    if (gcProperty != U_GCB_EXTEND && gcProperty != U_GCB_SPACING_MARK) {
-        return FALSE;
-    }
-    
-    // We are at a combining mark.  If the preceding character is anything
-    //   except a CONTROL, CR or LF, we are in a combining sequence.
-    U16_PREV(text, 0, index, c);    
-    gcProperty = u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
-
-    return !(gcProperty==U_GCB_CONTROL || gcProperty==U_GCB_LF || gcProperty==U_GCB_CR);
-}      
-        
 static UBool simpleSearch(UCollator *coll, const UnicodeString &target, int32_t offset, const UnicodeString &pattern, int32_t &matchStart, int32_t &matchEnd)
 {
     UErrorCode      status = U_ZERO_ERROR;
@@ -1384,11 +1184,13 @@ static UBool simpleSearch(UCollator *coll, const UnicodeString &target, int32_t
     OrderList       patternOrders(coll, pattern);
     int32_t         targetSize  = targetOrders.size() - 1;
     int32_t         patternSize = patternOrders.size() - 1;
-    UBreakIterator *charBreakIterator = ubrk_open(UBRK_CHARACTER, ucol_getLocale(coll, ULOC_VALID_LOCALE, &status), 
-                                                                                     target.getBuffer(), target.length(), &status);
+    UBreakIterator *charBreakIterator = ubrk_open(UBRK_CHARACTER, ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, &status),
+                                                  target.getBuffer(), target.length(), &status);
 
     if (patternSize == 0) {
-        matchStart = matchEnd = 0;
+        // Searching for an empty pattern always fails
+        matchStart = matchEnd = -1;
+        ubrk_close(charBreakIterator);
         return FALSE;
     }
 
@@ -1429,10 +1231,20 @@ static UBool simpleSearch(UCollator *coll, const UnicodeString &target, int32_t
             // that's after the last CE in the match, use that index
             // as the end of the match.
             if (minLimit < maxLimit) {
-                int32_t nba = ubrk_following(charBreakIterator, minLimit);
-
-                if (nba >= targetOrders.getHighOffset(i + patternSize - 1)) {
-                    mend = nba;
+                // When the last CE's low index is same with its high index, the CE is likely
+                // a part of expansion. In this case, the index is located just after the
+                // character corresponding to the CEs compared above. If the index is right
+                // at the break boundary, move the position to the next boundary will result
+                // incorrect match length when there are ignorable characters exist between
+                // the position and the next character produces CE(s). See ticket#8482.
+                if (minLimit == targetOrders.getHighOffset(i + patternSize - 1) && ubrk_isBoundary(charBreakIterator, minLimit)) {
+                    mend = minLimit;
+                } else {
+                    int32_t nba = ubrk_following(charBreakIterator, minLimit);
+
+                    if (nba >= targetOrders.getHighOffset(i + patternSize - 1)) {
+                        mend = nba;
+                    }
                 }
             }
 
@@ -1475,7 +1287,7 @@ static int32_t  getIntParam(UnicodeString name, UnicodeString &params, int32_t d
         }
 
         params.extract(m.start(1, status), paramLength, valString, sizeof(valString));
-        val = strtol(valString,  NULL, 10);
+        val = uprv_strtol(valString,  NULL, 10);
 
         // Delete this parameter from the params string.
         m.reset();
@@ -1500,23 +1312,18 @@ int32_t SSearchTest::monkeyTestCase(UCollator *coll, const UnicodeString &testCa
   //int32_t expectedStart = prefix.length(), expectedEnd = prefix.length() + altPattern.length();
     int32_t expectedStart = -1, expectedEnd = -1;
     int32_t notFoundCount = 0;
-    UStringSearch *uss = usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
-                                testCase.getBuffer(), testCase.length(),
-                                coll,
-                                NULL,     // the break iterator
-                                &status);
+    LocalUStringSearchPointer uss(usearch_openFromCollator(pattern.getBuffer(), pattern.length(),
+                                                           testCase.getBuffer(), testCase.length(),
+                                                           coll,
+                                                           NULL,     // the break iterator
+                                                           &status));
 
     // **** TODO: find *all* matches, not just first one ****
     simpleSearch(coll, testCase, 0, pattern, expectedStart, expectedEnd);
 
-#if 0
-    usearch_search(uss, 0, &actualStart, &actualEnd, &status);
-#else
-    actualStart = usearch_next(uss, &status);
-    actualEnd   = actualStart + usearch_getMatchedLength(uss);
-#endif
+    usearch_search(uss.getAlias(), 0, &actualStart, &actualEnd, &status);
 
-    if (actualStart != expectedStart || actualEnd != expectedEnd) {
+    if (expectedStart >= 0 && (actualStart != expectedStart || actualEnd != expectedEnd)) {
         errln("Search for <pattern> in <%s> failed: expected [%d, %d], got [%d, %d]\n"
               "    strength=%s seed=%d",
               name, expectedStart, expectedEnd, actualStart, actualEnd, strength, seed);
@@ -1529,17 +1336,11 @@ int32_t SSearchTest::monkeyTestCase(UCollator *coll, const UnicodeString &testCa
     // **** TODO: find *all* matches, not just first one ****
     simpleSearch(coll, testCase, 0, altPattern, expectedStart, expectedEnd);
 
-    usearch_setPattern(uss, altPattern.getBuffer(), altPattern.length(), &status);
+    usearch_setPattern(uss.getAlias(), altPattern.getBuffer(), altPattern.length(), &status);
 
-#if 0
-    usearch_search(uss, 0, &actualStart, &actualEnd, &status);
-#else
-    usearch_reset(uss);
-    actualStart = usearch_next(uss, &status);
-    actualEnd   = actualStart + usearch_getMatchedLength(uss);
-#endif
+    usearch_search(uss.getAlias(), 0, &actualStart, &actualEnd, &status);
 
-    if (actualStart != expectedStart || actualEnd != expectedEnd) {
+    if (expectedStart >= 0 && (actualStart != expectedStart || actualEnd != expectedEnd)) {
         errln("Search for <alt_pattern> in <%s> failed: expected [%d, %d], got [%d, %d]\n"
               "    strength=%s seed=%d",
               name, expectedStart, expectedEnd, actualStart, actualEnd, strength, seed);
@@ -1549,8 +1350,6 @@ int32_t SSearchTest::monkeyTestCase(UCollator *coll, const UnicodeString &testCa
         notFoundCount += 1;
     }
 
-    usearch_close(uss);
-
     return notFoundCount;
 }
 #endif
@@ -1559,33 +1358,27 @@ void SSearchTest::monkeyTest(char *params)
 {
     // ook!
     UErrorCode status = U_ZERO_ERROR;
-    U_STRING_DECL(test_pattern, "[[:assigned:]-[:ideographic:]-[:hangul:]-[:c:]]", 47);
-    U_STRING_INIT(test_pattern, "[[:assigned:]-[:ideographic:]-[:hangul:]-[:c:]]", 47);
-    UCollator *coll = ucol_open(NULL, &status);
+  //UCollator *coll = ucol_open(NULL, &status);
+    UCollator *coll = ucol_openFromShortString("S1", FALSE, NULL, &status);
+
     if (U_FAILURE(status)) {
-        errln("Failed to create collator in MonkeyTest!");
+        errcheckln(status, "Failed to create collator in MonkeyTest! - %s", u_errorName(status));
         return;
     }
-    USet *charsToTest  = uset_openPattern(test_pattern, 47, &status);
+
+    CollData  *monkeyData = new CollData(coll, status);
+
     USet *expansions   = uset_openEmpty();
     USet *contractions = uset_openEmpty();
-    StringToCEsMap *charsToCEList = new StringToCEsMap();
-    CEToStringsMap *ceToCharsStartingWith = new CEToStringsMap();
 
     ucol_getContractionsAndExpansions(coll, contractions, expansions, FALSE, &status);
 
-    uset_addAll(charsToTest, contractions);
-    uset_addAll(charsToTest, expansions);
-
-    // TODO: set strength to UCOL_PRIMARY, change CEList to use strength?
-    buildData(coll, charsToTest, charsToCEList, ceToCharsStartingWith);
-
     U_STRING_DECL(letter_pattern, "[[:letter:]-[:ideographic:]-[:hangul:]]", 39);
     U_STRING_INIT(letter_pattern, "[[:letter:]-[:ideographic:]-[:hangul:]]", 39);
     USet *letters = uset_openPattern(letter_pattern, 39, &status);
     SetMonkey letterMonkey(letters);
-    StringSetMonkey contractionMonkey(contractions, coll, charsToCEList, ceToCharsStartingWith);
-    StringSetMonkey expansionMonkey(expansions, coll, charsToCEList, ceToCharsStartingWith);
+    StringSetMonkey contractionMonkey(contractions, coll, monkeyData);
+    StringSetMonkey expansionMonkey(expansions, coll, monkeyData);
     UnicodeString testCase;
     UnicodeString alternate;
     UnicodeString pattern, altPattern;
@@ -1603,14 +1396,14 @@ void SSearchTest::monkeyTest(char *params)
         &contractionMonkey,
         &expansionMonkey};
     int32_t monkeyCount = sizeof(monkeys) / sizeof(monkeys[0]);
-    int32_t nonMatchCount = 0;
+    // int32_t nonMatchCount = 0;
 
     UCollationStrength strengths[] = {UCOL_PRIMARY, UCOL_SECONDARY, UCOL_TERTIARY};
     const char *strengthNames[] = {"primary", "secondary", "tertiary"};
     int32_t strengthCount = sizeof(strengths) / sizeof(strengths[0]);
     int32_t loopCount = quick? 1000 : 10000;
     int32_t firstStrength = 0;
-    int32_t lastStrength  = strengthCount - 1;
+    int32_t lastStrength  = strengthCount - 1; //*/ 0;
 
     if (params != NULL) {
 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
@@ -1651,13 +1444,14 @@ void SSearchTest::monkeyTest(char *params)
     for(int32_t s = firstStrength; s <= lastStrength; s += 1) {
         int32_t notFoundCount = 0;
 
+        logln("Setting strength to %s.", strengthNames[s]);
         ucol_setStrength(coll, strengths[s]);
 
         // TODO: try alternate prefix and suffix too?
         // TODO: alterntaes are only equal at primary strength. Is this OK?
-        for(int32_t t = 0; t < 10000; t += 1) {
+        for(int32_t t = 0; t < loopCount; t += 1) {
             uint32_t seed = m_seed;
-            int32_t  nmc = 0;
+            // int32_t  nmc = 0;
 
             generateTestCase(coll, monkeys, monkeyCount, pattern, altPattern);
             generateTestCase(coll, monkeys, monkeyCount, prefix,  altPrefix);
@@ -1681,25 +1475,22 @@ void SSearchTest::monkeyTest(char *params)
             testCase.remove();
             testCase.append(pattern);
             testCase.append(suffix);
-            
+
             // pattern + suffix
             notFoundCount += monkeyTestCase(coll, testCase, pattern, altPattern, "pattern + suffix", strengthNames[s], seed);
         }
 
-        logln("For strength %s the not found count is %d.", strengthNames[s], notFoundCount);
+       logln("For strength %s the not found count is %d.", strengthNames[s], notFoundCount);
     }
 
-    delete ceToCharsStartingWith;
-    delete charsToCEList;
-
     uset_close(contractions);
     uset_close(expansions);
-    uset_close(charsToTest);
     uset_close(letters);
-    
+    delete monkeyData;
+
     ucol_close(coll);
 }
 
-#endif        
-        
+#endif
+
 #endif