test/hotspot/jtreg/runtime/Thread/Fibonacci.java
author shade
Thu, 30 Nov 2017 17:13:33 +0100
branchepsilon-gc-branch
changeset 55934 912c55e702d6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29318
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     1
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
29318
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     4
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     8
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    14
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    18
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    21
 * questions.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    22
 */
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    23
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    24
/**
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    25
 * @test
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    26
 * @summary Calculates Fibonacci numbers "recursively" via threads and compares
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    27
 *     the result with the classical calculation.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    28
 *     This test is skipped on 32-bit Windows: limited virtual space on Win-32
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    29
 *     make this test inherently unstable on Windows with 32-bit VM data model.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    30
 * @requires !(os.family == "windows" & sun.arch.data.model == "32")
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 34634
diff changeset
    31
 * @modules java.base/jdk.internal.misc
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38152
diff changeset
    32
 * @library /test/lib
34634
5b74061eb1bb 8144629: runtime/thread/Fibonacci.java test should ran in othervm mode
ddmitriev
parents: 30604
diff changeset
    33
 * @run main/othervm Fibonacci 15
29318
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    34
 */
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    35
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 29318
diff changeset
    36
import jdk.test.lib.Asserts;
29318
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    37
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    38
public class Fibonacci extends Thread {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    39
    private int index;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    40
    private int value;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    41
    private Fibonacci left;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    42
    private Fibonacci right;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    43
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    44
    public Fibonacci(int i) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    45
        index = i;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    46
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    47
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    48
    private int getValue() {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    49
        return value;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    50
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    51
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    52
    @Override
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    53
    public void run() {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    54
        if (index == 0 || index == 1) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    55
            // base cases, 0 Fibonacci number = 0, 1 Fibonacci number = 1
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    56
            value = index;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    57
        } else {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    58
            // inductive cases
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    59
            left = new Fibonacci(index - 2);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    60
            right = new Fibonacci(index - 1);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    61
            left.start();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    62
            right.start();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    63
            try {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    64
                left.join();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    65
                right.join();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    66
            } catch (InterruptedException e) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    67
                throw new Error("InterruptedException for index " + index, e);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    68
            }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    69
            // compute and terminate
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    70
            value = left.getValue() + right.getValue();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    71
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    72
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    73
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    74
    public static int traditionalFibonacci(int n) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    75
        int n1 = 0, n2 = 1, nn = 0;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    76
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    77
        if (n == 0 || n == 1) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    78
           nn = n;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    79
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    80
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    81
        for (int i = 1; i < n; ++i) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    82
            nn = n2 + n1;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    83
            n1 = n2;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    84
            n2 = nn;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    85
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    86
        return nn;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    87
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    88
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    89
    public static void main(String[] args) throws Error,AssertionError {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    90
        int expected;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    91
        int number;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    92
        Fibonacci recursiveFibonacci;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    93
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    94
        if (args.length != 1) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    95
            throw new Error("Error: args.length must be 1");
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    96
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    97
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    98
        number = Integer.parseInt(args[0]);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    99
        recursiveFibonacci = new Fibonacci(number);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   100
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   101
        recursiveFibonacci.start();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   102
        try {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   103
            recursiveFibonacci.join();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   104
        } catch (InterruptedException e) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   105
            throw new Error("InterruptedException in main thread", e);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   106
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   107
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   108
        expected = traditionalFibonacci(number);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   109
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   110
        System.out.println("Fibonacci[" + number + "] = " + expected);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   111
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   112
        Asserts.assertEQ(recursiveFibonacci.getValue(), expected,
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   113
                          "Unexpected calculated value: " + recursiveFibonacci.getValue() + " expected " + expected );
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   114
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   115
}