9446
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 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 |
* @bug 6959129
|
|
27 |
* @summary COMPARISON WITH INTEGER.MAX_INT DOES NOT WORK CORRECTLY IN THE CLIENT VM.
|
|
28 |
*
|
40059
|
29 |
* @run main/othervm -ea compiler.c2.Test6959129
|
9446
|
30 |
*/
|
|
31 |
|
40059
|
32 |
package compiler.c2;
|
|
33 |
|
9446
|
34 |
public class Test6959129 {
|
|
35 |
|
|
36 |
public static void main(String[] args) {
|
|
37 |
long start = System.currentTimeMillis();
|
|
38 |
int min = Integer.MAX_VALUE-30000;
|
|
39 |
int max = Integer.MAX_VALUE;
|
|
40 |
long maxmoves = 0;
|
|
41 |
try {
|
|
42 |
maxmoves = maxMoves(min, max);
|
|
43 |
} catch (AssertionError e) {
|
|
44 |
System.out.println("Passed");
|
|
45 |
System.exit(95);
|
|
46 |
}
|
|
47 |
System.out.println("maxMove:" + maxmoves);
|
|
48 |
System.out.println("FAILED");
|
|
49 |
System.exit(97);
|
|
50 |
}
|
|
51 |
/**
|
|
52 |
* Imperative implementation that returns the length hailstone moves
|
|
53 |
* for a given number.
|
|
54 |
*/
|
|
55 |
public static long hailstoneLengthImp(long n) {
|
|
56 |
long moves = 0;
|
|
57 |
while (n != 1) {
|
|
58 |
assert n > 1;
|
|
59 |
if (isEven(n)) {
|
|
60 |
n = n / 2;
|
|
61 |
} else {
|
|
62 |
n = 3 * n + 1;
|
|
63 |
}
|
|
64 |
++moves;
|
|
65 |
}
|
|
66 |
return moves;
|
|
67 |
}
|
|
68 |
|
|
69 |
private static boolean isEven(long n) {
|
|
70 |
return n % 2 == 0;
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Returns the maximum length of the hailstone sequence for numbers
|
|
75 |
* between min to max.
|
|
76 |
*
|
|
77 |
* For rec1 - Assume that min is bigger than max.
|
|
78 |
*/
|
|
79 |
public static long maxMoves(int min, int max) {
|
|
80 |
long maxmoves = 0;
|
|
81 |
for (int n = min; n <= max; n++) {
|
|
82 |
if ((n & 1023) == 0) System.out.println(n);
|
|
83 |
long moves = hailstoneLengthImp(n);
|
|
84 |
if (moves > maxmoves) {
|
|
85 |
maxmoves = moves;
|
|
86 |
}
|
|
87 |
}
|
|
88 |
return maxmoves;
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|