< prev index next >

test/jdk/internal/misc/JavaLangAccess/NewUnsafeString.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2013, 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  */
  36 public class NewUnsafeString {
  37 
  38     static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
  39 
  40     public static void testNewUnsafeString() {
  41         String benchmark = "exemplar";
  42         String constructorCopy = new String(benchmark);
  43         char[] jlaChars = benchmark.toCharArray();
  44         String jlaCopy = jla.newStringUnsafe(jlaChars);
  45 
  46         if (benchmark == constructorCopy) {
  47             throw new Error("should be different instances");
  48         }
  49         if (!benchmark.equals(constructorCopy)) {
  50             throw new Error("Copy not equal");
  51         }
  52         if (0 != Objects.compare(benchmark, constructorCopy, Comparator.naturalOrder())) {
  53             throw new Error("Copy not equal");
  54         }


   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         }


< prev index next >