]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/perf/collationperf/collperf.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / perf / collationperf / collperf.cpp
index a518254d6ee0156d206fd51d9c8b23c20736d1a4..33e3e6c696d2493bc6261c05d4f6a6a349a8dd6b 100644 (file)
@@ -1,8 +1,12 @@
-/********************************************************************
+/***********************************************************************
+ * © 2016 and later: Unicode, Inc. and others.
+ * License & terms of use: http://www.unicode.org/copyright.html#License
+ ***********************************************************************
+ ***********************************************************************
  * COPYRIGHT:
- * Copyright (C) 2001-2008 IBM, Inc.   All Rights Reserved.
+ * Copyright (C) 2001-2012 IBM, Inc.   All Rights Reserved.
  *
- ********************************************************************/
+ ***********************************************************************/
 /********************************************************************************
 *
 * File CALLCOLL.C
@@ -81,7 +85,7 @@ const char gUsageString[] =
 //  Stubs for Windows API functions when building on UNIXes.
 //
 typedef int DWORD;
-inline int CompareStringW(DWORD, DWORD, UChar *, int, UChar *, int) {return 0;};
+inline int CompareStringW(DWORD, DWORD, UChar *, int, UChar *, int) {return 0;}
 #include <sys/time.h>
 unsigned long timeGetTime() {
     struct timeval t;
@@ -89,8 +93,8 @@ unsigned long timeGetTime() {
     unsigned long val = t.tv_sec * 1000;  // Let it overflow.  Who cares.
     val += t.tv_usec / 1000;
     return val;
-};
-inline int LCMapStringW(DWORD, DWORD, UChar *, int, UChar *, int) {return 0;};
+}
+inline int LCMapStringW(DWORD, DWORD, UChar *, int, UChar *, int) {return 0;}
 const int LCMAP_SORTKEY = 0;
 #define MAKELCID(a,b) 0
 const int SORT_DEFAULT = 0;
@@ -103,7 +107,7 @@ const int SORT_DEFAULT = 0;
 //     These global variables are set according to the options specified
 //     on the command line by the user.
 char * opt_fName      = 0;
-char * opt_locale     = "en_US";
+const char * opt_locale     = "en_US";
 int    opt_langid     = 0;         // Defaults to value corresponding to opt_locale.
 char * opt_rules      = 0;
 UBool  opt_help       = FALSE;
@@ -368,9 +372,8 @@ int ICURandomCmp(const void *a, const void *b) {
 void doKeyGen()
 {
     int  line;
-    int  loops;
+    int  loops = 0;
     int  iLoop;
-    int  t;
     int  len=-1;
 
     // Adjust loop count to compensate for file size.   Should be order n
@@ -388,9 +391,9 @@ void doKeyGen()
                     len = gFileLines[line].len;
                 }
                 for (iLoop=0; iLoop < opt_iLoopCount; iLoop++) {
-                    t=LCMapStringW(gWinLCID, LCMAP_SORTKEY,
+                    LCMapStringW(gWinLCID, LCMAP_SORTKEY,
                         gFileLines[line].name, len,
-                        (unsigned short *)gFileLines[line].winSortKey, 5000);    // TODO  something with length.
+                        (UChar *)gFileLines[line].winSortKey, 5000);    // TODO  something with length.
                 }
             }
         }
@@ -403,7 +406,7 @@ void doKeyGen()
                     len = gFileLines[line].len;
                 }
                 for (iLoop=0; iLoop < opt_iLoopCount; iLoop++) {
-                    t = ucol_getSortKey(gCol, gFileLines[line].name, len, (unsigned char *)gFileLines[line].icuSortKey, 5000);
+                    ucol_getSortKey(gCol, gFileLines[line].name, len, (unsigned char *)gFileLines[line].icuSortKey, 5000);
                 }
             }
         }
@@ -413,7 +416,7 @@ void doKeyGen()
         for (loops=0; loops<adj_loopCount; loops++) {
             for (line=0; line < gNumFileLines; line++) {
                 for (iLoop=0; iLoop < opt_iLoopCount; iLoop++) {
-                t = strxfrm(gFileLines[line].unixSortKey, gFileLines[line].unixName, 5000);
+                    strxfrm(gFileLines[line].unixSortKey, gFileLines[line].unixName, 5000);
                 }
             }
         }
@@ -465,15 +468,15 @@ void doBinarySearch()
 
     gCount = 0;
     int  line;
-    int  loops;
-    int  iLoop;
-    unsigned long elapsedTime;
+    int  loops = 0;
+    int  iLoop = 0;
+    unsigned long elapsedTime = 0;
 
     // Adjust loop count to compensate for file size.   Should be order n (lookups) * log n  (compares/lookup)
     // Accurate timings do not depend on this being perfect.  The correction is just to try to
     //   get total running times of about the right order, so the that user doesn't need to
     //   manually adjust the loop count for every different file size.
-    double dLoopCount = double(opt_loopCount) * 3000. / (log10(gNumFileLines) * double(gNumFileLines));
+    double dLoopCount = double(opt_loopCount) * 3000. / (log10((double)gNumFileLines) * double(gNumFileLines));
     if (opt_usekeys) dLoopCount *= 5;
     int adj_loopCount = int(dLoopCount);
     if (adj_loopCount < 1) adj_loopCount = 1;
@@ -490,7 +493,7 @@ void doBinarySearch()
             //if (opt_strcmp && opt_win) {pf = (PF)wcscmp;}   // Damn the difference between int32_t and int
                                                             //   which forces the use of a cast here.
             
-            int r;
+            int r = 0;
             for (loops=0; loops<adj_loopCount; loops++) {
                 
                 for (line=0; line < gNumFileLines; line++) {
@@ -523,7 +526,7 @@ void doBinarySearch()
         if (opt_icu)
         {
             unsigned long startTime = timeGetTime();
-            UCollationResult  r;
+            UCollationResult  r = UCOL_EQUAL;
             for (loops=0; loops<adj_loopCount; loops++) {
                 
                 for (line=0; line < gNumFileLines; line++) {
@@ -540,7 +543,7 @@ void doBinarySearch()
                         if (newGuess == guess)
                             break;
                         guess = newGuess;
-                        int ri;
+                        int ri = 0;
                         if (opt_usekeys) {
                             for (iLoop=0; iLoop < opt_iLoopCount; iLoop++) {
                                 ri = strcmp((gSortedLines[line])->icuSortKey, (gSortedLines[guess])->icuSortKey);
@@ -574,7 +577,7 @@ void doBinarySearch()
         if (opt_win)
         {
             unsigned long startTime = timeGetTime();
-            int r;
+            int r = 0;
             for (loops=0; loops<adj_loopCount; loops++) {
                 
                 for (line=0; line < gNumFileLines; line++) {
@@ -630,7 +633,7 @@ void doBinarySearch()
         if (opt_unix)
         {
             unsigned long startTime = timeGetTime();
-            int r;
+            int r = 0;
             for (loops=0; loops<adj_loopCount; loops++) {
                 
                 for (line=0; line < gNumFileLines; line++) {
@@ -699,7 +702,7 @@ void doQSort() {
     Line **sortBuf = new Line *[gNumFileLines];
 
     // Adjust loop count to compensate for file size.   QSort should be n log(n)
-    double dLoopCount = double(opt_loopCount) * 3000. / (log10(gNumFileLines) * double(gNumFileLines));
+    double dLoopCount = double(opt_loopCount) * 3000. / (log10((double)gNumFileLines) * double(gNumFileLines));
     if (opt_usekeys) dLoopCount *= 5;
     int adj_loopCount = int(dLoopCount);
     if (adj_loopCount < 1) adj_loopCount = 1;
@@ -766,7 +769,7 @@ void doQSort() {
     } else {
         printf("%d, ", ns);
     }
-};
+}
 
 
 
@@ -808,6 +811,8 @@ void doKeyHist() {
                 (float)accumulatedLen[i] / (float)(numKeysOfSize[i] * i));
         }
     }
+    delete []accumulatedLen;
+    delete []numKeysOfSize ;
 }
 
 //---------------------------------------------------------------------------------------
@@ -851,7 +856,7 @@ void doForwardIterTest(UBool haslen) {
         count ++;
     }
     unsigned long elapsedTime = timeGetTime() - startTime;
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
 
     // empty loop recalculation
     count = 0;
@@ -867,7 +872,7 @@ void doForwardIterTest(UBool haslen) {
         count ++;
     }
     elapsedTime -= (timeGetTime() - startTime);
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
 
     ucol_closeElements(iter);
 
@@ -939,7 +944,7 @@ void doForwardIterTest(UBool haslen) {
     }
 
     elapsedTime = timeGetTime() - startTime;
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
     
     // empty loop recalculation
     int tempgCount = 0;
@@ -964,7 +969,7 @@ void doForwardIterTest(UBool haslen) {
         count ++;
     }
     elapsedTime -= (timeGetTime() - startTime);
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
 
     ucol_closeElements(iter);
 
@@ -1015,7 +1020,7 @@ void doBackwardIterTest(UBool haslen) {
     }
     unsigned long elapsedTime = timeGetTime() - startTime;
 
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
 
     // empty loop recalculation
     count = 0;
@@ -1032,7 +1037,7 @@ void doBackwardIterTest(UBool haslen) {
     }
     elapsedTime -= (timeGetTime() - startTime);
 
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
     ucol_closeElements(iter);
 
     int ns = (int)(float(1000000) * (float)elapsedTime / (float)gCount);
@@ -1102,7 +1107,7 @@ void doBackwardIterTest(UBool haslen) {
     }
 
     elapsedTime = timeGetTime() - startTime;
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
     
     // empty loop recalculation
     count = 0;
@@ -1127,7 +1132,7 @@ void doBackwardIterTest(UBool haslen) {
         count ++;
     }
     elapsedTime -= (timeGetTime() - startTime);
-    printf("elapsedTime %d\n", elapsedTime);
+    printf("elapsedTime %ld\n", elapsedTime);
     ucol_closeElements(iter);
 
     printf("gCount %d\n", gCount);
@@ -1163,7 +1168,7 @@ void  UnixConvert() {
 
     cvrtr = ucnv_open("utf-8", &status);    // we are just doing UTF-8 locales for now.
     if (U_FAILURE(status)) {
-        fprintf(stderr, "ICU Converter open failed.: %d\n", &status);
+        fprintf(stderr, "ICU Converter open failed.: %s\n", u_errorName(status));
         exit(-1);
     }
 
@@ -1211,8 +1216,8 @@ public:
     UBool   error() {return fError;};
     
 private:
-    UCharFile (const UCharFile &other) {};                         // No copy constructor.
-    UCharFile & operator = (const UCharFile &other) {return *this;};   // No assignment op
+    UCharFile (const UCharFile & /*other*/) {};                         // No copy constructor.
+    UCharFile & operator = (const UCharFile &/*other*/) {return *this;};   // No assignment op
 
     FILE         *fFile;
     const char   *fName;
@@ -1338,7 +1343,7 @@ UChar UCharFile::get() {
             // Convert the bytes from the temp array to a Unicode char.
             i = 0;
             uint32_t  cp;
-            UTF8_NEXT_CHAR_UNSAFE(bytes, i, cp);
+            U8_NEXT_UNSAFE(bytes, i, cp);
             c = (UChar)cp;
             
             if (cp >= 0x10000) {
@@ -1353,6 +1358,10 @@ UChar UCharFile::get() {
             }
             break;
         };
+    default:
+        c = 0xFFFD; /* Error, unspecified codepage*/
+        fprintf(stderr, "UCharFile: Error: unknown fEncoding\n");
+        exit(1);
     }
     return c;
 }
@@ -1371,6 +1380,7 @@ UCollator *openRulesCollator() {
 
     int  bufLen = 10000;
     UChar *buf = (UChar *)malloc(bufLen * sizeof(UChar));
+    UChar *tmp;
     int i = 0;
 
     for(;;) {
@@ -1383,8 +1393,13 @@ UCollator *openRulesCollator() {
         }
         i++;
         if (i >= bufLen) {
+            tmp = buf;
             bufLen += 10000;
             buf = (UChar *)realloc(buf, bufLen);
+            if (buf == NULL) {
+                free(tmp);
+                return 0;
+            }
         }
     }
     buf[i] = 0;
@@ -1597,13 +1612,13 @@ int main(int argc, const char** argv) {
     //  Pre-compute ICU sort keys for the lines of the file.
     //
     int line;
-    int t;
+    int32_t t;
 
     for (line=0; line<gNumFileLines; line++) {
          t = ucol_getSortKey(gCol, gFileLines[line].name, -1, (unsigned char *)buf, sizeof(buf));
          gFileLines[line].icuSortKey  = new char[t];
 
-         if (t > sizeof(buf)) {
+         if (t > (int32_t)sizeof(buf)) {
              t = ucol_getSortKey(gCol, gFileLines[line].name, -1, (unsigned char *)gFileLines[line].icuSortKey , t);
          }
          else
@@ -1620,8 +1635,8 @@ int main(int argc, const char** argv) {
     for (line=0; line<gNumFileLines; line++) {
          t=LCMapStringW(gWinLCID, LCMAP_SORTKEY, gFileLines[line].name, -1, buf, sizeof(buf));
          gFileLines[line].winSortKey  = new char[t];
-         if (t > sizeof(buf)) {
-             t = LCMapStringW(gWinLCID, LCMAP_SORTKEY, gFileLines[line].name, -1, (unsigned short *)(gFileLines[line].winSortKey), t);
+         if (t > (int32_t)sizeof(buf)) {
+             t = LCMapStringW(gWinLCID, LCMAP_SORTKEY, gFileLines[line].name, -1, (UChar *)(gFileLines[line].winSortKey), t);
          }
          else
          {
@@ -1636,7 +1651,7 @@ int main(int argc, const char** argv) {
         for (line=0; line<gNumFileLines; line++) {
             t=strxfrm((char *)buf,  gFileLines[line].unixName,  sizeof(buf));
             gFileLines[line].unixSortKey  = new char[t];
-            if (t > sizeof(buf)) {
+            if (t > (int32_t)sizeof(buf)) {
                 t = strxfrm(gFileLines[line].unixSortKey,  gFileLines[line].unixName,  sizeof(buf));
             }
             else