Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/JavaLangAccess/NewUnsafeString.java
86409 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Objects;24import java.util.Comparator;25import sun.misc.JavaLangAccess;26import sun.misc.SharedSecrets;2728/*29* @test30* @summary Test JavaLangAccess.newUnsafeString31* @bug 801352832* @compile -XDignore.symbol.file NewUnsafeString.java33*/34public class NewUnsafeString {3536static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();3738public static void testNewUnsafeString() {39String benchmark = "exemplar";40String constructorCopy = new String(benchmark);41char[] jlaChars = benchmark.toCharArray();42String jlaCopy = jla.newStringUnsafe(jlaChars);4344if (benchmark == constructorCopy) {45throw new Error("should be different instances");46}47if (!benchmark.equals(constructorCopy)) {48throw new Error("Copy not equal");49}50if (0 != Objects.compare(benchmark, constructorCopy, Comparator.naturalOrder())) {51throw new Error("Copy not equal");52}5354if (benchmark == jlaCopy) {55throw new Error("should be different instances");56}57if (!benchmark.equals(jlaCopy)) {58throw new Error("Copy not equal");59}60if (0 != Objects.compare(benchmark, jlaCopy, Comparator.naturalOrder())) {61throw new Error("Copy not equal");62}6364if (constructorCopy == jlaCopy) {65throw new Error("should be different instances");66}67if (!constructorCopy.equals(jlaCopy)) {68throw new Error("Copy not equal");69}70if (0 != Objects.compare(constructorCopy, jlaCopy, Comparator.naturalOrder())) {71throw new Error("Copy not equal");72}7374// The following is extremely "evil". Never ever do this in non-test code.75jlaChars[0] = 'X';76if (!"Xxemplar".equals(jlaCopy)) {77throw new Error("jla.newStringUnsafe did not use provided string");78}7980}8182public static void main(String[] args) {83testNewUnsafeString();84}85}868788