test/jdk/jdk/internal/misc/JavaLangAccess/NewUnsafeString.java
branchihse-testmakefiles-branch
changeset 55967 e2c47e2db0a5
parent 55966 07d921cdd7a6
parent 48079 eaa406097d93
equal deleted inserted replaced
55966:07d921cdd7a6 55967:e2c47e2db0a5
     1 /*
       
     2  * Copyright (c) 2012, 2017, 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 import java.util.Objects;
       
    25 import java.util.Comparator;
       
    26 import jdk.internal.misc.JavaLangAccess;
       
    27 import jdk.internal.misc.SharedSecrets;
       
    28 
       
    29 /*
       
    30  * @test
       
    31  * @bug 8013528
       
    32  * @summary Test JavaLangAccess.newUnsafeString
       
    33  * @modules java.base/jdk.internal.misc
       
    34  * @compile -XDignore.symbol.file NewUnsafeString.java
       
    35  * @run main NewUnsafeString
       
    36  */
       
    37 public class NewUnsafeString {
       
    38 
       
    39     static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
       
    40 
       
    41     public static void testNewUnsafeString() {
       
    42         String benchmark = "exemplar";
       
    43         String constructorCopy = new String(benchmark);
       
    44         char[] jlaChars = benchmark.toCharArray();
       
    45         String jlaCopy = jla.newStringUnsafe(jlaChars);
       
    46 
       
    47         if (benchmark == constructorCopy) {
       
    48             throw new Error("should be different instances");
       
    49         }
       
    50         if (!benchmark.equals(constructorCopy)) {
       
    51             throw new Error("Copy not equal");
       
    52         }
       
    53         if (0 != Objects.compare(benchmark, constructorCopy, Comparator.naturalOrder())) {
       
    54             throw new Error("Copy not equal");
       
    55         }
       
    56 
       
    57         if (benchmark == jlaCopy) {
       
    58             throw new Error("should be different instances");
       
    59         }
       
    60         if (!benchmark.equals(jlaCopy)) {
       
    61             throw new Error("Copy not equal");
       
    62         }
       
    63         if (0 != Objects.compare(benchmark, jlaCopy, Comparator.naturalOrder())) {
       
    64             throw new Error("Copy not equal");
       
    65         }
       
    66 
       
    67         if (constructorCopy == jlaCopy) {
       
    68             throw new Error("should be different instances");
       
    69         }
       
    70         if (!constructorCopy.equals(jlaCopy)) {
       
    71             throw new Error("Copy not equal");
       
    72         }
       
    73         if (0 != Objects.compare(constructorCopy, jlaCopy, Comparator.naturalOrder())) {
       
    74             throw new Error("Copy not equal");
       
    75         }
       
    76 
       
    77         // The following is extremely "evil". Never ever do this in non-test code.
       
    78         jlaChars[0] = 'X';
       
    79         if (!"Xxemplar".equals(jlaCopy)) {
       
    80             throw new Error("jla.newStringUnsafe did not use provided string");
       
    81         }
       
    82 
       
    83     }
       
    84 
       
    85     public static void main(String[] args) {
       
    86         testNewUnsafeString();
       
    87     }
       
    88 }