< prev index next >

test/jdk/java/text/Collator/Regression.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

@@ -28,10 +28,11 @@
   *      4087243 4092260 4095316 4101940 4103436 4114076 4114077 4124632 4132736
   *      4133509 4139572 4141640 4179126 4179686 4244884 4663220
   * @library /java/text/testlib
   * @summary Regression tests for Collation and associated classes
   * @modules jdk.localedata
+  * @run junit Regression
   */
  /*
  (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  (C) Copyright IBM Corp. 1996 - All Rights Reserved
  

@@ -45,33 +46,35 @@
  
  import java.text.*;
  import java.util.Locale;
  import java.util.Vector;
  
+ import org.junit.jupiter.api.Test;
  
- public class Regression extends CollatorTest {
+ import static org.junit.jupiter.api.Assertions.fail;
  
-     public static void main(String[] args) throws Exception {
-         new Regression().run(args);
-     }
+ 
+ public class Regression {
  
      // CollationElementIterator.reset() doesn't work
      //
+     @Test
      public void Test4048446() {
          CollationElementIterator i1 = en_us.getCollationElementIterator(test1);
          CollationElementIterator i2 = en_us.getCollationElementIterator(test1);
  
          while ( i1.next() != CollationElementIterator.NULLORDER ) {
          }
          i1.reset();
  
-         assertEqual(i1, i2);
+         TestUtils.compareCollationElementIters(i1, i2);
      }
  
  
      // Collator -> rules -> Collator round-trip broken for expanding characters
      //
+     @Test
      public void Test4051866() throws ParseException {
          // Build a collator containing expanding characters
          RuleBasedCollator c1 = new RuleBasedCollator("< o "
                                                      +"& oe ,o\u3080"
                                                      +"& oe ,\u1530 ,O"

@@ -82,26 +85,28 @@
          // Build another using the rules from  the first
          RuleBasedCollator c2 = new RuleBasedCollator(c1.getRules());
  
          // Make sure they're the same
          if (!c1.getRules().equals(c2.getRules())) {
-             errln("Rules are not equal");
+             fail("Rules are not equal");
          }
      }
  
      // Collator thinks "black-bird" == "black"
      //
+     @Test
      public void Test4053636() {
          if (en_us.equals("black-bird","black")) {
-             errln("black-bird == black");
+             fail("black-bird == black");
          }
      }
  
  
      // CollationElementIterator will not work correctly if the associated
      // Collator object's mode is changed
      //
+     @Test
      public void Test4054238() {
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();
  
          c.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
          CollationElementIterator i1 = en_us.getCollationElementIterator(test3);

@@ -109,22 +114,23 @@
          c.setDecomposition(Collator.NO_DECOMPOSITION);
          CollationElementIterator i2 = en_us.getCollationElementIterator(test3);
  
          // At this point, BOTH iterators should use NO_DECOMPOSITION, since the
          // collator itself is in that mode
-         assertEqual(i1, i2);
+         TestUtils.compareCollationElementIters(i1, i2);
      }
  
      // Collator.IDENTICAL documented but not implemented
      //
+     @Test
      public void Test4054734() {
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();
          try {
              c.setStrength(Collator.IDENTICAL);
          }
          catch (Exception e) {
-             errln("Caught " + e.toString() + " setting Collator.IDENTICAL");
+             fail("Caught " + e.toString() + " setting Collator.IDENTICAL");
          }
  
          String[] decomp = {
              "\u0001",   "<",    "\u0002",
              "\u0001",   "=",    "\u0001",

@@ -141,10 +147,11 @@
          compareArray(c, nodecomp);
      }
  
      // Full Decomposition mode not implemented
      //
+     @Test
      public void Test4054736() {
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();
          c.setDecomposition(Collator.FULL_DECOMPOSITION);
  
          String[] tests = {

@@ -154,10 +161,11 @@
          compareArray(c, tests);
      }
  
      // Collator.getInstance() causes an ArrayIndexOutofBoundsException for Korean
      //
+     @Test
      public void Test4058613() {
          // Creating a default collator doesn't work when Korean is the default
          // locale
  
          Locale oldDefault = Locale.getDefault();

@@ -167,43 +175,45 @@
              Collator c = Collator.getInstance();
  
              // Since the fix to this bug was to turn of decomposition for Korean collators,
              // ensure that's what we got
              if (c.getDecomposition() != Collator.NO_DECOMPOSITION) {
-               errln("Decomposition is not set to NO_DECOMPOSITION");
+               fail("Decomposition is not set to NO_DECOMPOSITION");
              }
          }
          finally {
              Locale.setDefault(oldDefault);
          }
      }
  
      // RuleBasedCollator.getRules does not return the exact pattern as input
      // for expanding character sequences
      //
+     @Test
      public void Test4059820() {
          RuleBasedCollator c = null;
          try {
              c = new RuleBasedCollator("< a < b , c/a < d < z");
          } catch (ParseException e) {
-             errln("Exception building collator: " + e.toString());
+             fail("Exception building collator: " + e.toString());
              return;
          }
          if ( c.getRules().indexOf("c/a") == -1) {
-             errln("returned rules do not contain 'c/a'");
+             fail("returned rules do not contain 'c/a'");
          }
      }
  
      // MergeCollation::fixEntry broken for "& H < \u0131, \u0130, i, I"
      //
+     @Test
      public void Test4060154() {
          RuleBasedCollator c = null;
          try {
              c = new RuleBasedCollator("< g, G < h, H < i, I < j, J"
                                        + " & H < \u0131, \u0130, i, I" );
          } catch (ParseException e) {
-             errln("Exception building collator: " + e.toString());
+             fail("Exception building collator: " + e.toString());
              return;
          }
          c.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
  
          String[] tertiary = {

@@ -225,10 +235,11 @@
          compareArray(c, secondary);
      };
  
      // Secondary/Tertiary comparison incorrect in French Secondary
      //
+     @Test
      public void Test4062418() throws ParseException {
          RuleBasedCollator c = (RuleBasedCollator) Collator.getInstance(Locale.FRANCE);
          c.setStrength(Collator.SECONDARY);
  
          String[] tests = {

@@ -238,20 +249,22 @@
          compareArray(c, tests);
      }
  
      // Collator.compare() method broken if either string contains spaces
      //
+     @Test
      public void Test4065540() {
          if (en_us.compare("abcd e", "abcd f") == 0) {
-             errln("'abcd e' == 'abcd f'");
+             fail("'abcd e' == 'abcd f'");
          }
      }
  
      // Unicode characters need to be recursively decomposed to get the
      // correct result. For example,
      // u1EB1 -> \u0103 + \u0300 -> a + \u0306 + \u0300.
      //
+     @Test
      public void Test4066189() {
          String test1 = "\u1EB1";
          String test2 = "a\u0306\u0300";
  
          RuleBasedCollator c1 = (RuleBasedCollator) en_us.clone();

@@ -260,15 +273,16 @@
  
          RuleBasedCollator c2 = (RuleBasedCollator) en_us.clone();
          c2.setDecomposition(Collator.NO_DECOMPOSITION);
          CollationElementIterator i2 = en_us.getCollationElementIterator(test2);
  
-         assertEqual(i1, i2);
+         TestUtils.compareCollationElementIters(i1, i2);
      }
  
      // French secondary collation checking at the end of compare iteration fails
      //
+     @Test
      public void Test4066696() {
          RuleBasedCollator c = (RuleBasedCollator) Collator.getInstance(Locale.FRANCE);
          c.setStrength(Collator.SECONDARY);
  
          String[] tests = {

@@ -279,52 +293,56 @@
      }
  
  
      // Bad canonicalization of same-class combining characters
      //
+     @Test
      public void Test4076676() {
          // These combining characters are all in the same class, so they should not
          // be reordered, and they should compare as unequal.
          String s1 = "A\u0301\u0302\u0300";
          String s2 = "A\u0302\u0300\u0301";
  
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();
          c.setStrength(Collator.TERTIARY);
  
          if (c.compare(s1,s2) == 0) {
-             errln("Same-class combining chars were reordered");
+             fail("Same-class combining chars were reordered");
          }
      }
  
  
      // RuleBasedCollator.equals(null) throws NullPointerException
      //
+     @Test
      public void Test4079231() {
          try {
              if (en_us.equals(null)) {
-                 errln("en_us.equals(null) returned true");
+                 fail("en_us.equals(null) returned true");
              }
          }
          catch (Exception e) {
-             errln("en_us.equals(null) threw " + e.toString());
+             fail("en_us.equals(null) threw " + e.toString());
          }
      }
  
      // RuleBasedCollator breaks on "< a < bb" rule
      //
+     @Test
      public void Test4078588() throws ParseException {
          RuleBasedCollator rbc=new RuleBasedCollator("< a < bb");
  
          int result = rbc.compare("a","bb");
  
          if (result != -1) {
-             errln("Compare(a,bb) returned " + result + "; expected -1");
+             fail("Compare(a,bb) returned " + result + "; expected -1");
          }
      }
  
      // Combining characters in different classes not reordered properly.
      //
+     @Test
      public void Test4081866() throws ParseException {
          // These combining characters are all in different classes,
          // so they should be reordered and the strings should compare as equal.
          String s1 = "A\u0300\u0316\u0327\u0315";
          String s2 = "A\u0327\u0316\u0315\u0300";

@@ -336,16 +354,17 @@
          // (as a result of fixing bug 4114077), we must set it explicitly
          // when we're testing reordering behavior.  -- lwerner, 5/5/98
          c.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
  
          if (c.compare(s1,s2) != 0) {
-             errln("Combining chars were not reordered");
+             fail("Combining chars were not reordered");
          }
      }
  
      // string comparison errors in Scandinavian collators
      //
+     @Test
      public void Test4087241() {
          RuleBasedCollator c = (RuleBasedCollator) Collator.getInstance(
                                                          Locale.of("da", "DK"));
          c.setStrength(Collator.SECONDARY);
  

@@ -358,10 +377,11 @@
          compareArray(c, tests);
      }
  
      // CollationKey takes ignorable strings into account when it shouldn't
      //
+     @Test
      public void Test4087243() {
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();
          c.setStrength(Collator.TERTIARY);
  
          String[] tests = {

@@ -372,10 +392,11 @@
      }
  
      // Mu/micro conflict
      // Micro symbol and greek lowercase letter Mu should sort identically
      //
+     @Test
      public void Test4092260() {
          Collator c = Collator.getInstance(Locale.of("el"));
  
          // will only be equal when FULL_DECOMPOSITION is used
          c.setDecomposition(Collator.FULL_DECOMPOSITION);

@@ -399,27 +420,29 @@
          };
  
          compareArray(c, tests);
      }
  
+     @Test
      public void Test4101940() {
          try {
              RuleBasedCollator c = new RuleBasedCollator("< a < b");
              CollationElementIterator i = c.getCollationElementIterator("");
              i.reset();
  
              if (i.next() != i.NULLORDER) {
-                 errln("next did not return NULLORDER");
+                 fail("next did not return NULLORDER");
              }
          }
          catch (Exception e) {
-             errln("Caught " + e );
+             fail("Caught " + e );
          }
      }
  
      // Collator.compare not handling spaces properly
      //
+     @Test
      public void Test4103436() {
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();
          c.setStrength(Collator.TERTIARY);
  
          String[] tests = {

@@ -430,10 +453,11 @@
          compareArray(c, tests);
      }
  
      // Collation not Unicode conformant with Hangul syllables
      //
+     @Test
      public void Test4114076() {
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();
          c.setStrength(Collator.TERTIARY);
  
          //

@@ -455,22 +479,24 @@
      }
  
  
      // Collator.getCollationKey was hanging on certain character sequences
      //
+     @Test
      public void Test4124632() throws Exception {
          Collator coll = Collator.getInstance(Locale.JAPAN);
  
          try {
              coll.getCollationKey("A\u0308bc");
          } catch (OutOfMemoryError e) {
-             errln("Ran out of memory -- probably an infinite loop");
+             fail("Ran out of memory -- probably an infinite loop");
          }
      }
  
      // sort order of french words with multiple accents has errors
      //
+     @Test
      public void Test4132736() {
          Collator c = Collator.getInstance(Locale.FRANCE);
  
          String[] test1 = {
              "e\u0300e\u0301",   "<",    "e\u0301e\u0300",

@@ -479,10 +505,11 @@
          compareArray(c, test1);
      }
  
      // The sorting using java.text.CollationKey is not in the exact order
      //
+     @Test
      public void Test4133509() {
          String[] test1 = {
              "Exception",    "<",    "ExceptionInInitializerError",
              "Graphics",     "<",    "GraphicsEnvironment",
              "String",       "<",    "StringBuffer",

@@ -490,10 +517,11 @@
          compareArray(en_us, test1);
      }
  
      // Collation with decomposition off doesn't work for Europe
      //
+     @Test
      public void Test4114077() {
          // Ensure that we get the same results with decomposition off
          // as we do with it on....
  
          RuleBasedCollator c = (RuleBasedCollator) en_us.clone();

@@ -517,10 +545,11 @@
          compareArray(c, test2);
      }
  
      // Support for Swedish gone in 1.1.6 (Can't create Swedish collator)
      //
+     @Test
      public void Test4141640() {
          //
          // Rather than just creating a Swedish collator, we might as well
          // try to instantiate one for every locale available on the system
          // in order to prevent this sort of bug from cropping up in the future

@@ -529,18 +558,19 @@
  
          for (int i = 0; i < locales.length; i++) {
              try {
                  Collator c = Collator.getInstance(locales[i]);
              } catch (Exception e) {
-                 errln("Caught " + e + " creating collator for " + locales[i]);
+                 fail("Caught " + e + " creating collator for " + locales[i]);
              }
          }
      }
  
      // getCollationKey throws exception for spanish text
      // Cannot reproduce this bug on 1.2, however it DOES fail on 1.1.6
      //
+     @Test
      public void Test4139572() {
          //
          // Code pasted straight from the bug report
          //
          // create spanish locale and collator

@@ -551,25 +581,26 @@
          col.getCollationKey("Nombre De Objeto");
      }
  
      // RuleBasedCollator doesn't use getCollationElementIterator internally
      //
+     @Test
      public void Test4146160() throws ParseException {
          //
          // Use a custom collator class whose getCollationElementIterator
          // methods increment a count....
          //
          My4146160Collator.count = 0;
          new My4146160Collator().getCollationKey("1");
          if (My4146160Collator.count < 1) {
-             errln("getCollationElementIterator not called");
+             fail("getCollationElementIterator not called");
          }
  
          My4146160Collator.count = 0;
          new My4146160Collator().compare("1", "2");
          if (My4146160Collator.count < 1) {
-             errln("getCollationElementIterator not called");
+             fail("getCollationElementIterator not called");
          }
      }
  
      static class My4146160Collator extends RuleBasedCollator {
          public My4146160Collator() throws ParseException {

@@ -590,10 +621,11 @@
          public static int count = 0;
      };
  
      // CollationElementIterator.previous broken for expanding char sequences
      //
+     @Test
      public void Test4179686() throws ParseException {
  
          // Create a collator with a few expanding character sequences in it....
          RuleBasedCollator coll = new RuleBasedCollator(en_us.getRules()
                                                      + " & ae ; \u00e4 & AE ; \u00c4"

@@ -615,18 +647,19 @@
          int index = elements.size() - 1;
          while ((elem = iter.previous()) != iter.NULLORDER) {
              int expect = ((Integer)elements.elementAt(index)).intValue();
  
              if (elem != expect) {
-                 errln("Mismatch at index " + index
+                 fail("Mismatch at index " + index
                        + ": got " + Integer.toString(elem,16)
                        + ", expected " + Integer.toString(expect,16));
              }
              index--;
          }
      }
  
+     @Test
      public void Test4244884() throws ParseException {
          RuleBasedCollator coll = (RuleBasedCollator)Collator.getInstance(Locale.US);
          coll = new RuleBasedCollator(coll.getRules()
                  + " & C < ch , cH , Ch , CH < cat < crunchy");
  

@@ -642,17 +675,18 @@
              "dog"
          };
  
          for (int i = 1; i < testStrings.length; i++) {
              if (coll.compare(testStrings[i - 1], testStrings[i]) >= 0) {
-                 errln("error: \"" + testStrings[i - 1]
+                 fail("error: \"" + testStrings[i - 1]
                      + "\" is greater than or equal to \"" + testStrings[i]
                      + "\".");
              }
          }
      }
  
+     @Test
      public void Test4179216() throws ParseException {
          // you can position a CollationElementIterator in the middle of
          // a contracting character sequence, yielding a bogus collation
          // element
          RuleBasedCollator coll = (RuleBasedCollator)Collator.getInstance(Locale.US);

@@ -671,11 +705,11 @@
  
          iter.setOffset(5);
          int elt5 = CollationElementIterator.primaryOrder(iter.next());
  
          if (elt4 != elt0 || elt5 != elt0)
-             errln("The collation elements at positions 0 (" + elt0 + "), 4 ("
+             fail("The collation elements at positions 0 (" + elt0 + "), 4 ("
                      + elt4 + "), and 5 (" + elt5 + ") don't match.");
  
          // test that the "cat" combination works properly
          iter.setOffset(14);
          int elt14 = CollationElementIterator.primaryOrder(iter.next());

@@ -695,11 +729,11 @@
          iter.setOffset(19);
          int elt19 = CollationElementIterator.primaryOrder(iter.next());
  
          if (elt14 != elt15 || elt14 != elt16 || elt14 != elt17
                  || elt14 != elt18 || elt14 != elt19)
-             errln("\"cat\" elements don't match: elt14 = " + elt14 + ", elt15 = "
+             fail("\"cat\" elements don't match: elt14 = " + elt14 + ", elt15 = "
              + elt15 + ", elt16 = " + elt16 + ", elt17 = " + elt17
              + ", elt18 = " + elt18 + ", elt19 = " + elt19);
  
          // now generate a complete list of the collation elements,
          // first using next() and then using setOffset(), and

@@ -733,18 +767,19 @@
              setOffsetElements[count++] = testText.substring(lastPos, iter.getOffset());
              i = iter.getOffset();
          }
          for (int i = 0; i < nextElements.length; i++) {
              if (nextElements[i].equals(setOffsetElements[i])) {
-                 logln(nextElements[i]);
+                 System.out.println(nextElements[i]);
              } else {
-                 errln("Error: next() yielded " + nextElements[i] + ", but setOffset() yielded "
+                 fail("Error: next() yielded " + nextElements[i] + ", but setOffset() yielded "
                      + setOffsetElements[i]);
              }
          }
      }
  
+     @Test
      public void Test4216006() throws Exception {
          // rule parser barfs on "<\u00e0=a\u0300", and on other cases
          // where the same token (after normalization) appears twice in a row
          boolean caughtException = false;
          try {

@@ -767,10 +802,11 @@
          };
  
          compareArray(collator, tests);
      }
  
+     @Test
      public void Test4171974() {
          // test French accent ordering more thoroughly
          String[] frenchList = {
              "\u0075\u0075",     // u u
              "\u00fc\u0075",     // u-umlaut u

@@ -798,14 +834,14 @@
              "\u016b\u1e7b",     // u-macron u-macron-umlaut
              "\u1e7b\u1e7b"      // u-macron-umlaut u-macron-umlaut
          };
          Collator french = Collator.getInstance(Locale.FRENCH);
  
-         logln("Testing French order...");
+         System.out.println("Testing French order...");
          checkListOrder(frenchList, french);
  
-         logln("Testing French order without decomposition...");
+         System.out.println("Testing French order without decomposition...");
          french.setDecomposition(Collator.NO_DECOMPOSITION);
          checkListOrder(frenchList, french);
  
          String[] englishList = {
              "\u0075\u0075",     // u u

@@ -834,52 +870,53 @@
              "\u1e7b\u016b",     // u-macron-umlaut u-macron
              "\u1e7b\u1e7b"      // u-macron-umlaut u-macron-umlaut
          };
          Collator english = Collator.getInstance(Locale.ENGLISH);
  
-         logln("Testing English order...");
+         System.out.println("Testing English order...");
          checkListOrder(englishList, english);
  
-         logln("Testing English order without decomposition...");
+         System.out.println("Testing English order without decomposition...");
          english.setDecomposition(Collator.NO_DECOMPOSITION);
          checkListOrder(englishList, english);
      }
  
      private void checkListOrder(String[] sortedList, Collator c) {
          // this function uses the specified Collator to make sure the
          // passed-in list is already sorted into ascending order
          for (int i = 0; i < sortedList.length - 1; i++) {
              if (c.compare(sortedList[i], sortedList[i + 1]) >= 0) {
-                 errln("List out of order at element #" + i + ": "
-                         + prettify(sortedList[i]) + " >= "
-                         + prettify(sortedList[i + 1]));
+                 fail("List out of order at element #" + i + ": "
+                         + TestUtils.prettify(sortedList[i]) + " >= "
+                         + TestUtils.prettify(sortedList[i + 1]));
              }
          }
      }
  
      // CollationElementIterator set doesn't work propertly with next/prev
+     @Test
      public void Test4663220() {
          RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance(Locale.US);
          CharacterIterator stringIter = new StringCharacterIterator("fox");
          CollationElementIterator iter = collator.getCollationElementIterator(stringIter);
  
          int[] elements_next = new int[3];
-         logln("calling next:");
+         System.out.println("calling next:");
          for (int i = 0; i < 3; ++i) {
-             logln("[" + i + "] " + (elements_next[i] = iter.next()));
+             System.out.println("[" + i + "] " + (elements_next[i] = iter.next()));
          }
  
          int[] elements_fwd = new int[3];
-         logln("calling set/next:");
+         System.out.println("calling set/next:");
          for (int i = 0; i < 3; ++i) {
              iter.setOffset(i);
-             logln("[" + i + "] " + (elements_fwd[i] = iter.next()));
+             System.out.println("[" + i + "] " + (elements_fwd[i] = iter.next()));
          }
  
          for (int i = 0; i < 3; ++i) {
              if (elements_next[i] != elements_fwd[i]) {
-                 errln("mismatch at position " + i +
+                 fail("mismatch at position " + i +
                        ": " + elements_next[i] +
                        " != " + elements_fwd[i]);
              }
          }
      }

@@ -902,27 +939,27 @@
              }
  
              int result = c.compare(tests[i], tests[i+2]);
              if (sign(result) != sign(expect))
              {
-                 errln( i/3 + ": compare(" + prettify(tests[i])
-                                     + " , " + prettify(tests[i+2])
+                 fail( i/3 + ": compare(" + TestUtils.prettify(tests[i])
+                                     + " , " + TestUtils.prettify(tests[i+2])
                                      + ") got " + result + "; expected " + expect);
              }
              else
              {
                  // Collator.compare worked OK; now try the collation keys
                  CollationKey k1 = c.getCollationKey(tests[i]);
                  CollationKey k2 = c.getCollationKey(tests[i+2]);
  
                  result = k1.compareTo(k2);
                  if (sign(result) != sign(expect)) {
-                     errln( i/3 + ": key(" + prettify(tests[i])
-                                         + ").compareTo(key(" + prettify(tests[i+2])
+                     fail( i/3 + ": key(" + TestUtils.prettify(tests[i])
+                                         + ").compareTo(key(" + TestUtils.prettify(tests[i+2])
                                          + ")) got " + result + "; expected " + expect);
  
-                     errln("  " + prettify(k1) + " vs. " + prettify(k2));
+                     fail("  " + TestUtils.prettifyCKey(k1) + " vs. " + TestUtils.prettifyCKey(k2));
                  }
              }
          }
      }
  
< prev index next >