1 /*
2  * Copyright (c) 1997, 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 /* @test
25    @bug 4010528 4010529
26    @summary Math.min and Math.max should treat negative zero as strictly
27             less than positive zero
28  */
29 package test.java.lang.Math;
30 
31 import org.testng.annotations.Test;
32 import org.testng.Assert;
33 
34 public class MinMax {
35 
test(String what, float result, float correctResult)36     static void test(String what, float result, float correctResult) {
37         final String resultString = Float.toString(result);
38         final String correctString = Float.toString(correctResult);
39         final String message = what + ": got " + result + ", expected " + correctResult;
40         Assert.assertEquals(resultString, correctString, message);
41     }
42 
test(String what, double result, double correctResult)43     static void test(String what, double result, double correctResult) {
44         final String resultString = Double.toString(result);
45         final String correctString = Double.toString(correctResult);
46         final String message = what + ": got " + result + ", expected " + correctResult;
47         Assert.assertEquals(resultString, correctString, message);
48     }
49 
50     @Test
testMinMax()51     public void testMinMax() {
52         float fnz = -0.0f;
53         float fpz = +0.0f;
54 
55         test("Math.min(fnz, fnz)", Math.min(fnz, fnz), fnz);
56         test("Math.min(fnz, fpz)", Math.min(fnz, fpz), fnz);
57         test("Math.min(fpz, fnz)", Math.min(fpz, fnz), fnz);
58         test("Math.min(fpz, fpz)", Math.min(fpz, fpz), fpz);
59 
60         test("Math.min(-1.0f, fnz)", Math.min(-1.0f, fnz), -1.0f);
61         test("Math.min(-1.0f, fpz)", Math.min(-1.0f, fpz), -1.0f);
62         test("Math.min(+1.0f, fnz)", Math.min(+1.0f, fnz), fnz);
63         test("Math.min(+1.0f, fpz)", Math.min(+1.0f, fpz), fpz);
64         test("Math.min(-1.0f, +1.0f)", Math.min(-1.0f, +1.0f), -1.0f);
65         test("Math.min(fnz, -1.0f)", Math.min(fnz, -1.0f), -1.0f);
66         test("Math.min(fpz, -1.0f)", Math.min(fpz, -1.0f), -1.0f);
67         test("Math.min(fnz, +1.0f)", Math.min(fnz, +1.0f), fnz);
68         test("Math.min(fpz, +1.0f)", Math.min(fpz, +1.0f), fpz);
69         test("Math.min(+1.0f, -1.0f)", Math.min(+1.0f, -1.0f), -1.0f);
70 
71         test("Math.max(fnz, fnz)", Math.max(fnz, fnz), fnz);
72         test("Math.max(fnz, fpz)", Math.max(fnz, fpz), fpz);
73         test("Math.max(fpz, fnz)", Math.max(fpz, fnz), fpz);
74         test("Math.max(fpz, fpz)", Math.max(fpz, fpz), fpz);
75 
76         test("Math.max(-1.0f, fnz)", Math.max(-1.0f, fnz), fnz);
77         test("Math.max(-1.0f, fpz)", Math.max(-1.0f, fpz), fpz);
78         test("Math.max(+1.0f, fnz)", Math.max(+1.0f, fnz), +1.0f);
79         test("Math.max(+1.0f, fpz)", Math.max(+1.0f, fpz), +1.0f);
80         test("Math.max(-1.0f, +1.0f)", Math.max(-1.0f, +1.0f), +1.0f);
81         test("Math.max(fnz, -1.0f)", Math.max(fnz, -1.0f), fnz);
82         test("Math.max(fpz, -1.0f)", Math.max(fpz, -1.0f), fpz);
83         test("Math.max(fnz, +1.0f)", Math.max(fnz, +1.0f), +1.0f);
84         test("Math.max(fpz, +1.0f)", Math.max(fpz, +1.0f), +1.0f);
85         test("Math.max(+1.0f, -1.0f)", Math.max(+1.0f, -1.0f), +1.0f);
86 
87         double dnz = -0.0d;
88         double dpz = +0.0d;
89 
90         test("Math.min(dnz, dnz)", Math.min(dnz, dnz), dnz);
91         test("Math.min(dnz, dpz)", Math.min(dnz, dpz), dnz);
92         test("Math.min(dpz, dnz)", Math.min(dpz, dnz), dnz);
93         test("Math.min(dpz, dpz)", Math.min(dpz, dpz), dpz);
94 
95         test("Math.min(-1.0d, dnz)", Math.min(-1.0d, dnz), -1.0d);
96         test("Math.min(-1.0d, dpz)", Math.min(-1.0d, dpz), -1.0d);
97         test("Math.min(+1.0d, dnz)", Math.min(+1.0d, dnz), dnz);
98         test("Math.min(+1.0d, dpz)", Math.min(+1.0d, dpz), dpz);
99         test("Math.min(-1.0d, +1.0d)", Math.min(-1.0d, +1.0d), -1.0d);
100         test("Math.min(dnz, -1.0d)", Math.min(dnz, -1.0d), -1.0d);
101         test("Math.min(dpz, -1.0d)", Math.min(dpz, -1.0d), -1.0d);
102         test("Math.min(dnz, +1.0d)", Math.min(dnz, +1.0d), dnz);
103         test("Math.min(dpz, +1.0d)", Math.min(dpz, +1.0d), dpz);
104         test("Math.min(+1.0d, -1.0d)", Math.min(+1.0d, -1.0d), -1.0d);
105 
106         test("Math.max(dnz, dnz)", Math.max(dnz, dnz), dnz);
107         test("Math.max(dnz, dpz)", Math.max(dnz, dpz), dpz);
108         test("Math.max(dpz, dnz)", Math.max(dpz, dnz), dpz);
109         test("Math.max(dpz, dpz)", Math.max(dpz, dpz), dpz);
110 
111         test("Math.max(-1.0d, dnz)", Math.max(-1.0d, dnz), dnz);
112         test("Math.max(-1.0d, dpz)", Math.max(-1.0d, dpz), dpz);
113         test("Math.max(+1.0d, dnz)", Math.max(+1.0d, dnz), +1.0d);
114         test("Math.max(+1.0d, dpz)", Math.max(+1.0d, dpz), +1.0d);
115         test("Math.max(-1.0d, +1.0d)", Math.max(-1.0d, +1.0d), +1.0d);
116         test("Math.max(dnz, -1.0d)", Math.max(dnz, -1.0d), dnz);
117         test("Math.max(dpz, -1.0d)", Math.max(dpz, -1.0d), dpz);
118         test("Math.max(dnz, +1.0d)", Math.max(dnz, +1.0d), +1.0d);
119         test("Math.max(dpz, +1.0d)", Math.max(dpz, +1.0d), +1.0d);
120         test("Math.max(+1.0d, -1.0d)", Math.max(+1.0d, -1.0d), +1.0d);
121 
122     }
123 
124 }
125