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