Path: blob/master/test/jdk/java/text/Normalizer/ThreadSafeTest.java
41149 views
/*1* Copyright (c) 2018, 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*/2223/*24* @test25* @bug 4221795 8032446 817427026* @summary Confirm that java.text.Normalizer and sun.text.Normalizer are27* thread-safe.28* @modules java.base/sun.text java.base/jdk.internal.icu.text29* @compile -XDignore.symbol.file ThreadSafeTest.java30* @run main/othervm -esa ThreadSafeTest 5 1031*/3233// Usage: java ThreadSafeTest [threadsFactor [duration]]34public class ThreadSafeTest {3536static volatile boolean runrun = true;37static volatile boolean error = false;3839public static void main(String[] args) throws Exception {40int threadsFactor = 5;41if (args.length > 0) {42threadsFactor = Math.max(Integer.parseInt(args[0]), 5);43}44int duration = 180;45if (args.length > 1) {46duration = Math.max(5, Integer.parseInt(args[1]));47}48int nProcessors = Runtime.getRuntime().availableProcessors();49int nTasks = nProcessors * threadsFactor;50Thread[] tasks = new Thread[nTasks];5152System.out.println("Testing with " + nTasks + " threads on " +53nProcessors + " processors for " + duration +54" seconds.");5556for (int i = 0; i < nTasks; i++) {57tasks[i] = new Thread(new Worker());58}59for (int i = 0; i < nTasks; i++) {60tasks[i].start();61}6263try {64for (int i = 0; runrun && i < duration; i++) {65Thread.sleep(1000); // 1 second66}67runrun = false;68for (int i = 0; i < nTasks; i++) {69tasks[i].join();70}71}72catch (InterruptedException e) {73}7475if (error) {76throw new RuntimeException("Normalizer is not thread-safe.");77}78}7980static void testJavaNormalize(int num, java.text.Normalizer.Form form) {81String got = java.text.Normalizer.normalize(data[num][0], form);82if (!got.equals(data[num][1])) {83System.err.println("java.text.Normalizer.normalize(" +84form.toString() + ") failed.");85error = true;86}87}8889static void testSunNormalize(int num, java.text.Normalizer.Form form,90int option) {91String got = sun.text.Normalizer.normalize(data[num][0], form, option);92if (!got.equals(data[num][1])) {93System.err.println("sun.text.Normalizer.normalize(" +94form.toString() + ", " +95Integer.toHexString(option) + ") failed.");96error = true;97}98}99100static void testIsNormalized(int num, java.text.Normalizer.Form form) {101boolean normalized = java.text.Normalizer.isNormalized(data[num][1], form);102if (!normalized) {103System.err.println("java.text.Normalizer.isNormalized(" +104form.toString() + ") failed.");105error = true;106}107}108109static class Worker implements Runnable {110public void run() {111while (runrun) {112testJavaNormalize(0, java.text.Normalizer.Form.NFKC);113testSunNormalize(1, java.text.Normalizer.Form.NFC,114sun.text.Normalizer.UNICODE_3_2);115testJavaNormalize(2, java.text.Normalizer.Form.NFKD);116testSunNormalize(3, java.text.Normalizer.Form.NFC,117jdk.internal.icu.text.NormalizerBase.UNICODE_LATEST);118testJavaNormalize(4, java.text.Normalizer.Form.NFD);119120testIsNormalized(0, java.text.Normalizer.Form.NFKC);121testIsNormalized(2, java.text.Normalizer.Form.NFKD);122testIsNormalized(4, java.text.Normalizer.Form.NFD);123124if (error) {125runrun = false;126return;127}128}129}130}131132static final String[][] data = {133/* From: To: */134{"A\u0300\u0316", "\u00C0\u0316"},135{"\u0071\u0307\u0323\u0072", "\u0071\u0323\u0307\u0072"},136{"\u0f77", "\u0fb2\u0f71\u0f80"},137{"D\u0307\u0328\u0323", "\u1e0c\u0328\u0307"},138{"\u0f71\u0f72\u0f73\u0f74\u0f75",139"\u0F71\u0F71\u0F71\u0F72\u0F72\u0F74\u0F74"},140};141}142143144