Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/StringIntern/StringIntern.java
41155 views
/*1* Copyright (c) 2007, 2020, 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*/222324/*25* @test26* @key stress randomness27*28* @summary converted from VM Testbase gc/gctests/StringIntern.29* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]30*31* @library /vmTestbase32* /test/lib33* @run main/othervm gc.gctests.StringIntern.StringIntern34*/3536package gc.gctests.StringIntern;3738import nsk.share.test.*;39import nsk.share.gc.*;4041/**42* Test that strings returned by String.intern() can be collected.43*44* Create strings consisting of random characters, call String.intern().45* Check that intern() contract.46*/47public class StringIntern extends ThreadedGCTest {48private int maxLength = 1000;49private int checkCount = 100;5051private class StringGenerator implements Runnable {52private StringBuffer sb = new StringBuffer();5354private void generateRandomBuffer() {55int length = LocalRandom.nextInt(maxLength);56for (int i = 0; i < length; ++i)57sb.append((char) LocalRandom.nextInt(Integer.MAX_VALUE));58}5960private String getString() {61return sb.toString();62}6364public void run() {65generateRandomBuffer();66for (int i = 0; i < checkCount; ++i) {67String s1 = getString();68String s2 = getString();69if (s1.intern() != s2.intern()) {70log.error("Test failed on: " + s1);71setFailed(true);72}73}74}75}7677protected Runnable createRunnable(int i) {78return new StringGenerator();79}8081public static void main(String[] args) {82GC.runTest(new StringIntern(), args);83}84}858687