001/*
002 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.jtt.hotspot;
024
025import org.junit.*;
026
027import com.oracle.graal.jtt.*;
028
029public class Test6959129 extends JTTTest {
030
031    public static long test() {
032        int min = Integer.MAX_VALUE - 30000;
033        int max = Integer.MAX_VALUE;
034        return maxMoves(min, max);
035    }
036
037    /**
038     * Imperative implementation that returns the length hailstone moves for a given number.
039     */
040    public static long hailstoneLengthImp(long n2) {
041        long n = n2;
042        long moves = 0;
043        while (n != 1) {
044            if (n <= 1) {
045                throw new IllegalStateException();
046            }
047            if (isEven(n)) {
048                n = n / 2;
049            } else {
050                n = 3 * n + 1;
051            }
052            ++moves;
053        }
054        return moves;
055    }
056
057    private static boolean isEven(long n) {
058        return n % 2 == 0;
059    }
060
061    /**
062     * Returns the maximum length of the hailstone sequence for numbers between min to max.
063     *
064     * For rec1 - Assume that min is bigger than max.
065     */
066    public static long maxMoves(int min, int max) {
067        long maxmoves = 0;
068        for (int n = min; n <= max; n++) {
069            long moves = hailstoneLengthImp(n);
070            if (moves > maxmoves) {
071                maxmoves = moves;
072            }
073        }
074        return maxmoves;
075    }
076
077    @Test(timeout = 20000)
078    public void run0() throws Throwable {
079        runTest("test");
080    }
081
082}