Skip to content

Commit

Permalink
Add build test for Math.max and Math.min
Browse files Browse the repository at this point in the history
- tests float corner cases with +/-0
- NaN cases
- confirms respective OMR changes

Signed-off-by: Matthew Hall <[email protected]>
  • Loading branch information
matthewhall2 committed Dec 6, 2024
1 parent f7446d9 commit f1e4af6
Show file tree
Hide file tree
Showing 2 changed files with 382 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
package jit.test.recognizedMethod;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import java.util.Random;
import org.testng.asserts.SoftAssert;
import static jit.test.recognizedMethod.TestMathUtils.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.AssertJUnit;



@Test(singleThreaded=true)
public class TestJavaLangMath {

/**
Expand All @@ -39,10 +48,10 @@ public class TestJavaLangMath {
*
* Subsequent tree simplification passes will attempt to reduce this constant
* operation to a <code>dsqrt</code> IL by performing the square root at compile
* time. The transformation will be performed when the function get executed
* time. The transformation will be performed when the function get executed
* twice, therefore, the "invocationCount=2" is needed. However we must ensure the
* result of the square root done by the compiler at compile time will be exactly
* the same as the result had it been done by the Java runtime at runtime. This
* result of the square root done by the compiler at compile time will be exactly
* the same as the result had it been done by the Java runtime at runtime. This
* test validates the results are the same.
*/
@Test(groups = {"level.sanity"}, invocationCount=2)
Expand All @@ -55,4 +64,82 @@ public void test_java_lang_Math_sqrt() {
AssertJUnit.assertEquals(Double.POSITIVE_INFINITY, Math.sqrt(Double.POSITIVE_INFINITY));
AssertJUnit.assertTrue(Double.isNaN(Math.sqrt(Double.NaN)));
}

@Test(groups = {"level.sanity"}, invocationCount=2, dataProvider="zeroProviderFD", dataProviderClass=TestMathUtils.class)
public void test_java_lang_Math_min_zeros_FD(Number a, Number b, boolean isFirstArg) {
if (a instanceof Float) {
float f1 = a.floatValue();
float f2 = b.floatValue();
assertEquals(Math.min(f1, f2), isFirstArg ? f1 : f2);
} else {
double f1 = a.doubleValue();
double f2 = b.doubleValue();
assertEquals(Math.min(f1, f2), isFirstArg ? f1 : f2);
}
}

@Test(groups = {"level.sanity"}, invocationCount=2, dataProvider="zeroProviderFD", dataProviderClass=TestMathUtils.class)
public void test_java_lang_Math_max_zeros_FD(Number a, Number b, boolean isFirstArg) {
if (a instanceof Float) {
float f1 = a.floatValue();
float f2 = b.floatValue();
assertEquals(Math.max(f1, f2), isFirstArg ? f2 : f1);
} else {
double f1 = a.doubleValue();
double f2 = b.doubleValue();
assertEquals(Math.max(f1, f2), isFirstArg ? f2 : f1);
}
}

@Test(groups = {"level.sanity"}, invocationCount=2, dataProvider="nanProviderFD", dataProviderClass=TestMathUtils.class)
public void test_java_lang_Math_min_nan_FD(Number a, Number b, Number expected) {
if (a instanceof Float) {
float f1 = a.floatValue();
float f2 = b.floatValue();
AssertJUnit.assertTrue(Float.isNaN(Math.min(f1, f2)));
} else {
double f1 = a.doubleValue();
double f2 = b.doubleValue();
AssertJUnit.assertTrue(Double.isNaN(Math.min(f1, f2)));
}
}

@Test(groups = {"level.sanity"}, invocationCount=2, dataProvider="nanProviderFD", dataProviderClass=TestMathUtils.class)
public void test_java_lang_Math_max_nan_FD(Number a, Number b, Number expected) {
if (a instanceof Float) {
float f1 = a.floatValue();
float f2 = b.floatValue();
AssertJUnit.assertTrue(Float.isNaN(Math.max(f1, f2)));
} else {
double f1 = a.doubleValue();
double f2 = b.doubleValue();
AssertJUnit.assertTrue(Double.isNaN(Math.max(f1, f2)));
}
}

@Test(groups = {"level.sanity"}, invocationCount=2, dataProvider="normalNumberProviderFD", dataProviderClass=TestMathUtils.class)
public void test_java_lang_Math_min_normal_FD(Number a, Number b){
if (a instanceof Float) {
float f1 = a.floatValue();
float f2 = b.floatValue();
AssertJUnit.assertEquals(Math.min(f1, f2), f1 <= f2 ? f1 : f2);
} else {
double f1 = a.doubleValue();
double f2 = b.doubleValue();
AssertJUnit.assertEquals(Math.min(f1, f2), f1 <= f2 ? f1 : f2);
}
}

@Test(groups = {"level.sanity"}, invocationCount=2, dataProvider="normalNumberProviderFD", dataProviderClass=TestMathUtils.class)
public void test_java_lang_Math_max_normal_FD(Number a, Number b){
if (a instanceof Float) {
float f1 = a.floatValue();
float f2 = b.floatValue();
AssertJUnit.assertEquals(Math.max(f1, f2), f1 >= f2 ? f1 : f2);
} else {
double f1 = a.doubleValue();
double f2 = b.doubleValue();
AssertJUnit.assertEquals(Math.max(f1, f2), f1 >= f2 ? f1 : f2);
}
}
}
Loading

0 comments on commit f1e4af6

Please sign in to comment.