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