Test6959129.java revision 13431:fa2686ded3a7
1116874Ssmkelly/*
2116874Ssmkelly * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3117185Ssmkelly * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4126383Sphk *
5126383Sphk * This code is free software; you can redistribute it and/or modify it
6116874Ssmkelly * under the terms of the GNU General Public License version 2 only, as
7242519Sdelphij * published by the Free Software Foundation.
8242519Sdelphij *
9126383Sphk * This code is distributed in the hope that it will be useful, but WITHOUT
10116874Ssmkelly * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11126383Sphk * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12126383Sphk * version 2 for more details (a copy is included in the LICENSE file that
13126383Sphk * 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 */
23package org.graalvm.compiler.jtt.hotspot;
24
25import org.graalvm.compiler.jtt.JTTTest;
26import org.junit.Rule;
27import org.junit.Test;
28import org.junit.rules.TestRule;
29import org.junit.rules.Timeout;
30
31public class Test6959129 extends JTTTest {
32
33    @Rule public TestRule timeout = NotOnDebug.create(Timeout.seconds(20));
34
35    public static long test() {
36        int min = Integer.MAX_VALUE - 30000;
37        int max = Integer.MAX_VALUE;
38        return maxMoves(min, max);
39    }
40
41    /**
42     * Imperative implementation that returns the length hailstone moves for a given number.
43     */
44    public static long hailstoneLengthImp(long n2) {
45        long n = n2;
46        long moves = 0;
47        while (n != 1) {
48            if (n <= 1) {
49                throw new IllegalStateException();
50            }
51            if (isEven(n)) {
52                n = n / 2;
53            } else {
54                n = 3 * n + 1;
55            }
56            ++moves;
57        }
58        return moves;
59    }
60
61    private static boolean isEven(long n) {
62        return n % 2 == 0;
63    }
64
65    /**
66     * Returns the maximum length of the hailstone sequence for numbers between min to max.
67     *
68     * For rec1 - Assume that min is bigger than max.
69     */
70    public static long maxMoves(int min, int max) {
71        long maxmoves = 0;
72        for (int n = min; n <= max; n++) {
73            long moves = hailstoneLengthImp(n);
74            if (moves > maxmoves) {
75                maxmoves = moves;
76            }
77        }
78        return maxmoves;
79    }
80
81    @Test
82    public void run0() throws Throwable {
83        initializeForTimeout();
84        runTest("test");
85    }
86
87}
88