1 /* 2 * Copyright (c) 2011, 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 /* 25 * @test 26 * @summary <rdar://problem/3569904> 142: threaded append double to String causes divide by zero on dual G5 (-Xint wo 27 * @summary com.apple.junit.java.vm 28 * @run junit R3569904ThreadedAppendDoubleDivByZeroTest 29 */ 30 31 import junit.framework.*; 32 import java.util.Timer; 33 import java.util.TimerTask; 34 35 public class R3569904ThreadedAppendDoubleDivByZeroTest extends TestCase { 36 static volatile boolean continueTest = true; 37 38 public static Test suite() { 39 return new TestSuite(R3569904ThreadedAppendDoubleDivByZeroTest.class); 40 } 41 42 public static void main(String argv[]) { 43 junit.textui.TestRunner.run(suite()); 44 } 45 46 public void testDivByZeroException() throws Exception { 47 String doubleBits = "0100000011001011111010111100110101110000010111111001000011100000"; 48 long doubleLong = 0l; 49 for (int i = 0; i < doubleBits.length(); i++) { 50 if (doubleBits.substring(i, i+1).equals("1")) { 51 doubleLong += 1l << (63-i); 52 } 53 } 54 double d = Double.longBitsToDouble(doubleLong); 55 // System.out.println("double value is "+d); 56 57 int numThreads = 2; 58 TestThread[] threads = new TestThread[numThreads]; 59 for (int i = 0; i < numThreads; i++) { 60 threads[i] = new R3569904ThreadedAppendDoubleDivByZeroTest.TestThread(d); 61 threads[i].start(); 62 } 63 Timer t = new Timer(true); 64 TimerTask task = new TimerTask() { 65 public void run() { 66 R3569904ThreadedAppendDoubleDivByZeroTest.continueTest = false; 67 } 68 }; 69 t.schedule(task, 2000); 70 71 for (int i = 0; i < numThreads; i++) { 72 threads[i].join(); 73 assertFalse("Thread got an ArithmeticException", threads[i].gotArithmeticException); 74 } 75 } 76 77 static class TestThread extends Thread { 78 boolean gotArithmeticException = false; 79 80 TestThread(double val) { 81 this.val = val; 82 } 83 84 double val; 85 86 public void run() { 87 while(R3569904ThreadedAppendDoubleDivByZeroTest.continueTest){ 88 doit(val); 89 } 90 } 91 92 public void doit(double v) { 93 try 94 { 95 String s = ""; 96 s+=v; 97 } catch (ArithmeticException ae) { 98 ae.printStackTrace(); 99 gotArithmeticException = true; 100 return; 101 } 102 } 103 } 104 }