ICU-8639 Merging #8624 ucol_getSortKey+strncmp and ucol_strcoll give different compar...
authorYoshito Umaoka <y.umaoka@gmail.com>
Wed, 13 Jul 2011 16:27:07 +0000 (16:27 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 13 Jul 2011 16:27:07 +0000 (16:27 +0000)
X-SVN-Rev: 30330

main/classes/collate/src/com/ibm/icu/text/RuleBasedCollator.java
main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationRegressionTest.java

index 727ee3462457d89a22762be835080bfc15f90a15..9d2810cfbc21c81092980d6ba26548c18380dcf4 100644 (file)
@@ -4146,7 +4146,7 @@ public final class RuleBasedCollator extends Collator {
         }
 
         // Set the compression values
-        int total3 = m_top3_ - COMMON_BOTTOM_3_ - 1;
+        int total3 = m_top3_ - m_bottom3_ - 1;
         // we multilply double with int, but need only int
         m_topCount3_ = (int) (PROPORTION_3_ * total3);
         m_bottomCount3_ = total3 - m_topCount3_;
index f7c637516e0b27937ba222fec4a244d39f951fe8..cc4fe20e46c0e41b8737d671d8e562df7feb1be5 100644 (file)
@@ -1182,7 +1182,63 @@ public class CollationRegressionTest extends TestFmwk {
         return sb;
     }
 
-    
+    /*
+     * Test case for ticket#8624
+     * Bad collation key with upper first option.
+     */
+    public void TestCaseFirstCompression() {
+        RuleBasedCollator col = (RuleBasedCollator)Collator.getInstance(Locale.US);
+
+        // Default
+        caseFirstCompressionSub(col, "default");
+
+        // Upper first
+        col.setUpperCaseFirst(true);
+        caseFirstCompressionSub(col, "upper first");
+
+        // Lower first
+        col.setLowerCaseFirst(true);
+        caseFirstCompressionSub(col, "lower first");
+    }
+
+    /*
+     * Compare two strings - "aaa...A" and "aaa...a" with
+     * Collation#compare and CollationKey#compareTo, called from
+     * TestCaseFirstCompression.
+     */
+    private void caseFirstCompressionSub(RuleBasedCollator col, String opt) {
+        final int maxLength = 50;
+
+        StringBuilder buf1 = new StringBuilder();
+        StringBuilder buf2 = new StringBuilder();
+        String str1, str2;
+
+        for (int n = 1; n <= maxLength; n++) {
+            buf1.setLength(0);
+            buf2.setLength(0);
+
+            for (int i = 0; i < n - 1; i++) {
+                buf1.append('a');
+                buf2.append('a');
+            }
+            buf1.append('A');
+            buf2.append('a');
+
+            str1 = buf1.toString();
+            str2 = buf2.toString();
+
+            CollationKey key1 = col.getCollationKey(str1);
+            CollationKey key2 = col.getCollationKey(str2);
+
+            int cmpKey = key1.compareTo(key2);
+            int cmpCol = col.compare(str1, str2);
+
+            if ((cmpKey < 0 && cmpCol >= 0) || (cmpKey > 0 && cmpCol <= 0) || (cmpKey == 0 && cmpCol != 0)) {
+                errln("Inconsistent comparison(" + opt + "): str1=" + str1 + ", str2=" + str2 + ", cmpKey=" + cmpKey + " , cmpCol=" + cmpCol);
+            }
+        }
+    }
+
     /* RuleBasedCollator not subclassable
      * @bug 4146160
     //