1 /*
   2  * Copyright (c) 2008, 2018, 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  *
  27  * @summary converted from VM Testbase jit/series.
  28  * VM Testbase keywords: [jit, quick]
  29  *
  30  * @library /vmTestbase
  31  *          /test/lib
  32  * @run driver jdk.test.lib.FileInstaller . .
  33  * @build jit.series.series
  34  * @run driver ExecDriver --java jit.series.series
  35  */
  36 
  37 package jit.series;
  38 
  39 import nsk.share.TestFailure;
  40 import nsk.share.GoldChecker;
  41 
  42 class series {
  43 
  44   public static final GoldChecker goldChecker = new GoldChecker( "series" );
  45 
  46   private double arithmeticSeries(double i)
  47   {
  48     if (i == 0.0D)
  49       return 0.0D;
  50     else
  51       return i + arithmeticSeries(i - 1.0D);
  52   }
  53 
  54   private float arithmeticSeries(float i)
  55   {
  56     if (i == 0.0F)
  57       return 0.0F;
  58     else
  59       return i + arithmeticSeries(i - 1.0F);
  60   }
  61 
  62   private long arithmeticSeries(long i)
  63   {
  64     if (i == 0L)
  65       return 0L;
  66     else
  67       return i + arithmeticSeries(i - 1L);
  68   }
  69 
  70   private int arithmeticSeries(int i)
  71   {
  72     if (i == 0)
  73       return 0;
  74     else
  75       return i + arithmeticSeries(i - 1);
  76   }
  77 
  78   private double fake(double i)
  79   {
  80     if (i == 0.0D)
  81       return 0.0D;
  82     else
  83       return i + mul100(i - 1.0D);
  84   }
  85   private double mul100(double i)
  86   {
  87     return i * 100.0D;
  88   }
  89 
  90   private float fake(float i)
  91   {
  92     if (i == 0.0F)
  93       return 0.0F;
  94     else
  95       return i + mul100(i - 1.0F);
  96   }
  97   private float mul100(float i)
  98   {
  99     return i * 100F;
 100   }
 101 
 102   private long fake(long i)
 103   {
 104     if (i == 0L)
 105       return 0L;
 106     else
 107       return i + mul100(i - 1L);
 108   }
 109   private long mul100(long i)
 110   {
 111     return i * 100L;
 112   }
 113 
 114   private int fake(int i)
 115   {
 116     if (i == 0)
 117       return 0;
 118     else
 119       return i + mul100(i - 1);
 120   }
 121   private int mul100(int i)
 122   {
 123     return i * 100;
 124   }
 125 
 126   int runit()
 127   {
 128      double dres;
 129      float fres;
 130      long lres;
 131      int res;
 132      int failures = 0;
 133 
 134      res = arithmeticSeries(50);
 135      series.goldChecker.println("1: "+Math.abs(res - 1275));
 136      if (res != 1275) {
 137       series.goldChecker.println(" *** Fail test 1: expected = 1275, computed = " +
 138 res);
 139       failures++;
 140      }
 141      res = fake(50);
 142      series.goldChecker.println("2: "+Math.abs(res - 4950));
 143      if (res != 4950) {
 144       series.goldChecker.println(" *** Fail test 2: expected = 4950, computed = " +
 145 res);
 146       failures++;
 147      }
 148      lres = arithmeticSeries(50L);
 149      series.goldChecker.println("3: "+Math.abs(lres - 1275L));
 150      if (lres != 1275L) {
 151       series.goldChecker.println(" *** Fail test 3: expected = 1275, computed = " +
 152 res);
 153       failures++;
 154      }
 155      lres = fake(50L);
 156      series.goldChecker.println("4: "+Math.abs(lres - 4950L));
 157      if (lres != 4950L) {
 158       series.goldChecker.println(" *** Fail test 4: expected = 4950, computed = " +
 159 res);
 160       failures++;
 161      }
 162      dres = arithmeticSeries(50.0D);
 163      series.goldChecker.println("5: "+Math.abs(dres - 1275.0D));
 164      if (Math.abs(dres - 1275.0D) > 1.0E-09D) {
 165       series.goldChecker.println(" *** Fail test 5: expected = 1275, computed = " +
 166 dres);
 167       failures++;
 168      }
 169      dres = fake(50.0D);
 170      series.goldChecker.println("6: "+Math.abs(dres - 4950.0D));
 171      if (Math.abs(dres - 4950.0D) > 5.0E-09D) {
 172       series.goldChecker.println(" *** Fail test 6: expected = 4950, computed = " +
 173 dres);
 174       failures++;
 175      }
 176      fres = arithmeticSeries(50.0F);
 177      series.goldChecker.println("7: "+Math.abs(fres - 1275.0F));
 178      if (Math.abs(fres - 1275.0F) > 1.0E-04F) {
 179       series.goldChecker.println(" *** Fail test 7: expected = 1275, computed = " +
 180 fres);
 181       failures++;
 182      }
 183      fres = fake(50.0F);
 184      series.goldChecker.println("8: "+Math.abs(fres - 4950.0F));
 185      if (Math.abs(fres - 4950.0F) > 5.0E-04F) {
 186       series.goldChecker.println(" *** Fail test 8: expected = 4950, computed = " +
 187 fres);
 188       failures++;
 189      }
 190      return(failures);
 191 }
 192 
 193   static public void main(String args[])
 194   {
 195       series patObj = new series();
 196       if (patObj.runit()!=0)
 197          throw new TestFailure("Test failed.");;
 198                                                series.goldChecker.check();
 199   }
 200 }