test/jdk/java/lang/String/ICCBasher.java
changeset 47216 71c04702a3d5
parent 30046 cf2c86e1819e
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1998, 2002, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 4152868
       
    27  * @summary test Case Insensitive Comparator in String
       
    28  * @key randomness
       
    29  */
       
    30 
       
    31 import java.util.*;
       
    32 
       
    33 public class ICCBasher {
       
    34 
       
    35     static final int TEST_SIZE = 20;
       
    36     static final int STRING_SIZE = 5;
       
    37     static final int CHAR_VALUE_LIMIT = 128;
       
    38 
       
    39     public static void main(String[] args) throws Exception {
       
    40         LinkedList L1 = new LinkedList();
       
    41         LinkedList L2 = new LinkedList();
       
    42         LinkedList L3 = new LinkedList();
       
    43         LinkedList L4 = new LinkedList();
       
    44 
       
    45         // First generate L1 and L2 with random lower case chars
       
    46         //System.out.println("Generate L1 and L2");
       
    47         Random generator = new Random();
       
    48         int achar=0;
       
    49         StringBuffer entryBuffer = new StringBuffer(10);
       
    50         String snippet = null;
       
    51         for (int x=0; x<TEST_SIZE * 2; x++) {
       
    52             for(int y=0; y<STRING_SIZE; y++) {
       
    53                 achar = generator.nextInt(CHAR_VALUE_LIMIT);
       
    54                 char test = (char)(achar);
       
    55                 entryBuffer.append(test);
       
    56             }
       
    57             snippet = entryBuffer.toString();
       
    58             snippet.toLowerCase();
       
    59             if (x < TEST_SIZE)
       
    60                 L1.add(snippet);
       
    61             else
       
    62                 L2.add(snippet);
       
    63         }
       
    64 
       
    65         // Concatenate L1 and L2 to form L3
       
    66         //System.out.println("Generate L3");
       
    67         for (int x=0; x<TEST_SIZE; x++) {
       
    68             String entry = (String)L1.get(x) + (String)L2.get(x);
       
    69             L3.add(entry);
       
    70         }
       
    71 
       
    72         // Randomly toUpper L1 and L2
       
    73         //System.out.println("Modify L1 and L2");
       
    74         for (int x=0; x<TEST_SIZE; x++) {
       
    75             achar = generator.nextInt();
       
    76             if (achar > 0) {
       
    77                 String mod = (String)L1.get(x);
       
    78                 mod = mod.toUpperCase();
       
    79                 L1.set(x, mod);
       
    80             }
       
    81             achar = generator.nextInt();
       
    82             if (achar > 0) {
       
    83                 String mod = (String)L2.get(x);
       
    84                 mod = mod.toUpperCase();
       
    85                 L2.set(x, mod);
       
    86             }
       
    87         }
       
    88 
       
    89         // Concatenate L1 and L2 to form L4
       
    90         //System.out.println("Generate L4");
       
    91         for (int x=0; x<TEST_SIZE; x++) {
       
    92             String entry = (String)L1.get(x) + (String)L2.get(x);
       
    93             L4.add(entry);
       
    94         }
       
    95 
       
    96         // Sort L3 and L4 using case insensitive comparator
       
    97         //System.out.println("Sort L3 and L4");
       
    98         Collections.sort(L3, String.CASE_INSENSITIVE_ORDER);
       
    99         Collections.sort(L4, String.CASE_INSENSITIVE_ORDER);
       
   100 
       
   101         // Check to see that order of L3 and L4 are identical
       
   102         // ignoring case considerations
       
   103         //System.out.println("Check order of L3 and L4");
       
   104         for (int x=0; x<TEST_SIZE; x++) {
       
   105             String one = (String)L3.get(x);
       
   106             String two = (String)L4.get(x);
       
   107             if (!one.equalsIgnoreCase(two))
       
   108                 throw new RuntimeException("Case Insensitive Sort Failure.");
       
   109         }
       
   110 
       
   111     }
       
   112 }