Skip to content

Commit 9d2467b

Browse files
Sonia Zaldana Callesshipilev
authored andcommitted
8313621: test/jdk/jdk/internal/math/FloatingDecimal/TestFloatingDecimal should use RandomFactory
Backport-of: 864a876ebfdf745b0e1f875c3e3668380f2498a7
1 parent a84f19f commit 9d2467b

File tree

1 file changed

+41
-108
lines changed

1 file changed

+41
-108
lines changed

test/jdk/jdk/internal/math/FloatingDecimal/TestFloatingDecimal.java

Lines changed: 41 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,12 @@
2424
import java.util.Random;
2525
import jdk.internal.math.FloatingDecimal;
2626

27+
import jdk.test.lib.RandomFactory;
28+
29+
import org.junit.jupiter.api.Test;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
2733
/*
2834
OldFloatingDecimalForTest
2935
@@ -57,48 +63,35 @@ public class jdk.internal.math.FloatingDecimal {
5763
/**
5864
* @test
5965
* @bug 7032154
60-
* @summary unit tests of FloatingDecimal
66+
* @summary unit tests of FloatingDecimal (use -Dseed=X to set PRANDOM seed)
6167
* @modules java.base/jdk.internal.math
68+
* @library ..
69+
* @library /test/lib
6270
* @library /java/lang/Math
71+
* @build jdk.test.lib.RandomFactory
6372
* @build DoubleConsts FloatConsts
64-
* @run main TestFloatingDecimal
73+
* @run junit TestFloatingDecimal
6574
* @author Brian Burkhalter
6675
* @key randomness
6776
*/
6877
public class TestFloatingDecimal {
69-
private static enum ResultType {
70-
RESULT_EXCEPTION,
71-
RESULT_PRINT
72-
}
78+
private static final int NUM_RANDOM_TESTS = 100_000;
7379

74-
private static final ResultType RESULT_TYPE = ResultType.RESULT_PRINT;
75-
private static final int NUM_RANDOM_TESTS = 100000;
76-
77-
private static final Random RANDOM = new Random();
78-
79-
private static void result(String message) {
80-
switch (RESULT_TYPE) {
81-
case RESULT_EXCEPTION:
82-
throw new RuntimeException(message);
83-
case RESULT_PRINT:
84-
System.err.println(message);
85-
break;
86-
default:
87-
assert false;
88-
}
89-
}
80+
private static final Random RANDOM = RandomFactory.getRandom();
9081

9182
private static int check(String test, Object expected, Object actual) {
9283
int failures = 0;
9384
if(!actual.equals(expected)) {
9485
failures++;
95-
result("Test "+test+" expected "+expected+" but obtained "+actual);
86+
System.err.println("Test " + test +
87+
" expected " + expected +
88+
" but obtained " + actual);
9689
}
9790
return failures;
9891
}
9992

100-
private static int testAppendToDouble() {
101-
System.out.println(" testAppendToDouble");
93+
@Test
94+
public void testAppendToDouble() {
10295
int failures = 0;
10396

10497
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
@@ -119,11 +112,11 @@ private static int testAppendToDouble() {
119112
}
120113
}
121114

122-
return failures;
115+
assertEquals(0, failures);
123116
}
124117

125-
private static int testAppendToFloat() {
126-
System.out.println(" testAppendToFloat");
118+
@Test
119+
public void testAppendToFloat() {
127120
int failures = 0;
128121

129122
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
@@ -144,21 +137,11 @@ private static int testAppendToFloat() {
144137
}
145138
}
146139

147-
return failures;
140+
assertEquals(0, failures);
148141
}
149142

150-
private static int testAppendTo() {
151-
System.out.println("testAppendTo");
152-
int failures = 0;
153-
154-
failures += testAppendToDouble();
155-
failures += testAppendToFloat();
156-
157-
return failures;
158-
}
159-
160-
private static int testParseDouble() {
161-
System.out.println(" testParseDouble");
143+
@Test
144+
public void testParseDouble() {
162145
int failures = 0;
163146

164147
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
@@ -177,11 +160,11 @@ private static int testParseDouble() {
177160
}
178161
}
179162

180-
return failures;
163+
assertEquals(0, failures);
181164
}
182165

183-
private static int testParseFloat() {
184-
System.out.println(" testParseFloat");
166+
@Test
167+
public void testParseFloat() {
185168
int failures = 0;
186169

187170
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
@@ -200,21 +183,11 @@ private static int testParseFloat() {
200183
}
201184
}
202185

203-
return failures;
204-
}
205-
206-
private static int testParse() {
207-
System.out.println("testParse");
208-
int failures = 0;
209-
210-
failures += testParseDouble();
211-
failures += testParseFloat();
212-
213-
return failures;
186+
assertEquals(0, failures);
214187
}
215188

216-
private static int testToJavaFormatStringDoubleFixed() {
217-
System.out.println(" testToJavaFormatStringDoubleFixed");
189+
@Test
190+
public void testToJavaFormatStringDoubleFixed() {
218191
int failures = 0;
219192

220193
double[] d = new double [] {
@@ -228,11 +201,11 @@ private static int testToJavaFormatStringDoubleFixed() {
228201
failures += check("testToJavaFormatStringDoubleFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(d[i]));
229202
}
230203

231-
return failures;
204+
assertEquals(0, failures);
232205
}
233206

234-
private static int testToJavaFormatStringDoubleRandom() {
235-
System.out.println(" testToJavaFormatStringDoubleRandom");
207+
@Test
208+
public void testToJavaFormatStringDoubleRandom() {
236209
int failures = 0;
237210

238211
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
@@ -247,19 +220,11 @@ private static int testToJavaFormatStringDoubleRandom() {
247220
}
248221
}
249222

250-
return failures;
223+
assertEquals(0, failures);
251224
}
252225

253-
private static int testToJavaFormatStringDouble() {
254-
System.out.println(" testToJavaFormatStringDouble");
255-
int failures = 0;
256-
failures += testToJavaFormatStringDoubleFixed();
257-
failures += testToJavaFormatStringDoubleRandom();
258-
return failures;
259-
}
260-
261-
private static int testToJavaFormatStringFloatFixed() {
262-
System.out.println(" testToJavaFormatStringFloatFixed");
226+
@Test
227+
public void testToJavaFormatStringFloatFixed() {
263228
int failures = 0;
264229

265230
float[] f = new float[] {
@@ -273,11 +238,11 @@ private static int testToJavaFormatStringFloatFixed() {
273238
failures += check("testToJavaFormatStringFloatFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(f[i]));
274239
}
275240

276-
return failures;
241+
assertEquals(0, failures);
277242
}
278243

279-
private static int testToJavaFormatStringFloatRandom() {
280-
System.out.println(" testToJavaFormatStringFloatRandom");
244+
@Test
245+
public void testToJavaFormatStringFloatRandom() {
281246
int failures = 0;
282247

283248
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
@@ -292,38 +257,6 @@ private static int testToJavaFormatStringFloatRandom() {
292257
}
293258
}
294259

295-
return failures;
296-
}
297-
298-
private static int testToJavaFormatStringFloat() {
299-
System.out.println(" testToJavaFormatStringFloat");
300-
int failures = 0;
301-
302-
failures += testToJavaFormatStringFloatFixed();
303-
failures += testToJavaFormatStringFloatRandom();
304-
305-
return failures;
306-
}
307-
308-
private static int testToJavaFormatString() {
309-
System.out.println("testToJavaFormatString");
310-
int failures = 0;
311-
312-
failures += testToJavaFormatStringDouble();
313-
failures += testToJavaFormatStringFloat();
314-
315-
return failures;
316-
}
317-
318-
public static void main(String[] args) {
319-
int failures = 0;
320-
321-
failures += testAppendTo();
322-
failures += testParse();
323-
failures += testToJavaFormatString();
324-
325-
if (failures != 0) {
326-
throw new RuntimeException("" + failures + " failures while testing FloatingDecimal");
327-
}
260+
assertEquals(0, failures);
328261
}
329262
}

0 commit comments

Comments
 (0)