test/jdk/jdk/internal/misc/JavaLangAccess/NewUnsafeString.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44042 jdk/test/jdk/internal/misc/JavaLangAccess/NewUnsafeString.java@2040b44fe636
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     1
/*
44042
2040b44fe636 8176187: jdk/internal/misc/JavaLangAccess/NewUnsafeString.java is not run
amlu
parents: 32834
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     4
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     8
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    13
 * accompanied this code).
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    14
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    18
 *
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    21
 * questions.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    22
 */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    23
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    24
import java.util.Objects;
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17426
diff changeset
    25
import java.util.Comparator;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 30820
diff changeset
    26
import jdk.internal.misc.JavaLangAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 30820
diff changeset
    27
import jdk.internal.misc.SharedSecrets;
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    28
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    29
/*
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    30
 * @test
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 23010
diff changeset
    31
 * @bug 8013528
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    32
 * @summary Test JavaLangAccess.newUnsafeString
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 30820
diff changeset
    33
 * @modules java.base/jdk.internal.misc
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    34
 * @compile -XDignore.symbol.file NewUnsafeString.java
44042
2040b44fe636 8176187: jdk/internal/misc/JavaLangAccess/NewUnsafeString.java is not run
amlu
parents: 32834
diff changeset
    35
 * @run main NewUnsafeString
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    36
 */
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    37
public class NewUnsafeString {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    38
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    39
    static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    40
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    41
    public static void testNewUnsafeString() {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    42
        String benchmark = "exemplar";
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    43
        String constructorCopy = new String(benchmark);
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    44
        char[] jlaChars = benchmark.toCharArray();
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    45
        String jlaCopy = jla.newStringUnsafe(jlaChars);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    46
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    47
        if (benchmark == constructorCopy) {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    48
            throw new Error("should be different instances");
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    49
        }
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    50
        if (!benchmark.equals(constructorCopy)) {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    51
            throw new Error("Copy not equal");
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    52
        }
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17426
diff changeset
    53
        if (0 != Objects.compare(benchmark, constructorCopy, Comparator.naturalOrder())) {
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    54
            throw new Error("Copy not equal");
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    55
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    56
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    57
        if (benchmark == jlaCopy) {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    58
            throw new Error("should be different instances");
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    59
        }
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    60
        if (!benchmark.equals(jlaCopy)) {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    61
            throw new Error("Copy not equal");
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    62
        }
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17426
diff changeset
    63
        if (0 != Objects.compare(benchmark, jlaCopy, Comparator.naturalOrder())) {
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    64
            throw new Error("Copy not equal");
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    65
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    66
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    67
        if (constructorCopy == jlaCopy) {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    68
            throw new Error("should be different instances");
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    69
        }
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    70
        if (!constructorCopy.equals(jlaCopy)) {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    71
            throw new Error("Copy not equal");
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    72
        }
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents: 17426
diff changeset
    73
        if (0 != Objects.compare(constructorCopy, jlaCopy, Comparator.naturalOrder())) {
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    74
            throw new Error("Copy not equal");
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    75
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    76
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    77
        // The following is extremely "evil". Never ever do this in non-test code.
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    78
        jlaChars[0] = 'X';
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    79
        if (!"Xxemplar".equals(jlaCopy)) {
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    80
            throw new Error("jla.newStringUnsafe did not use provided string");
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    81
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    82
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    83
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    84
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    85
    public static void main(String[] args) {
17426
45db19517346 8013528: Provide SharedSecrets access to String(char[], boolean) constructor
mduigou
parents: 12859
diff changeset
    86
        testNewUnsafeString();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    87
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents:
diff changeset
    88
}