Skip to content

Commit

Permalink
Added new methods for comparing float and double arrays with delta - F…
Browse files Browse the repository at this point in the history
  • Loading branch information
atulagrawal committed Jul 2, 2018
1 parent 6a53ca2 commit 4748fc8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-1803: Added new methods for comparing float and double arrays with delta (Atul Agrawal)
Fixed: GITHUB-1661: Fixed Assert logic for two dimensional arrays (Atul Agrawal)
Fixed: GITHUB-1734: Added unit tests for NaN, Max, Min, Positive and Negative infinity (Atul Agrawal)
Fixed: GITHUB-1740: Bumped up artifact version of dependencies for java 8 support (Atul Agrawal)
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/testng/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,42 @@ public static void assertEquals(float[] actual, float[] expected, String message
}
}

/**
* Asserts that two arrays contain the equal elements concerning a delta in the same order. If they do not, an
* AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param delta the absolute tolerable difference between the actual and expected values
*/
public static void assertEquals(float[] actual, float[] expected, float delta) {
assertEquals(actual, expected, delta,"");
}

/**
* Asserts that two arrays contain the equal elements concerning a delta in the same order. If
* they do not, an
* AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param delta the absolute tolerable difference between the actual and expected values
* @param message the assertion error message
*/
public static void assertEquals(float[] actual, float[] expected, float delta, String message) {
if (checkRefEqualityAndLength(actual, expected, message)) return;

for (int i = 0; i < expected.length; i++) {
assertEquals(actual[i], expected[i], delta,
String.format(
ARRAY_MISMATCH_TEMPLATE,
i,
Float.toString(expected[i]),
Float.toString(actual[i]),
message));
}
}

/**
* Asserts that two arrays contain the same elements in the same order. If they do not, an
* AssertionError is thrown.
Expand Down Expand Up @@ -412,6 +448,41 @@ public static void assertEquals(double[] actual, double[] expected, String messa
}
}

/**
* Asserts that two arrays contain the equal elements concerning a delta in the same order. If they do not, an
* AssertionError is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param delta the absolute tolerable difference between the actual and expected values
*/
public static void assertEquals(double[] actual, double[] expected, double delta) {
assertEquals(actual, expected, delta, "");
}

/**
* Asserts that two arrays contain the equal elements concerning a delta in the same order. If they do not, an
* AssertionError, with the given message, is thrown.
*
* @param actual the actual value
* @param expected the expected value
* @param delta the absolute tolerable difference between the actual and expected values
* @param message the assertion error message
*/
public static void assertEquals(double[] actual, double[] expected, double delta, String message) {
if (checkRefEqualityAndLength(actual, expected, message)) return;

for (int i = 0; i < expected.length; i++) {
assertEquals(actual[i], expected[i], delta,
String.format(
ARRAY_MISMATCH_TEMPLATE,
i,
Double.toString(expected[i]),
Double.toString(actual[i]),
message));
}
}

/**
* Asserts that two arrays contain the same elements in the same order. If they do not, an
* AssertionError is thrown.
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/testng/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ public void compareFloatArrays() {
Assert.assertEquals(actual, expected);
}

@Test
public void compareFloatArraysWithDelta() {
float[] actual = {0.1f, 0.2f, 0.3f, 0.4f};
float[] expected = {0.5f, 0.7f, 0.1f, 0.2f};
Assert.assertEquals(actual, expected,0.5f);
}

@Test(expectedExceptions = AssertionError.class)
public void compareUnEqualFloatArraysWithDelta() {
float[] actual = {0.1f, 0.2f, 0.3f, 0.4f};
float[] expected = {0.5f, 0.7f, 0.1f, 0.2f};
Assert.assertEquals(actual, expected,0.1f);
}

@Test
public void compareFloatArraysWithNaNValues() {
Assert.assertEquals(new float[] { Float.NaN }, new float[] { Float.NaN });
Expand Down Expand Up @@ -200,6 +214,20 @@ public void compareDoubleWithNaNValues() {
Assert.assertEquals( (double) Double.NaN , (double) Double.NaN);
}

@Test
public void compareDoubleArraysWithDelta() {
double[] actual = {0.1d,0.2d, 0.3d, 0.4d};
double[] expected = {0.5d,0.7d, 0.1d, 0.2d};
Assert.assertEquals(actual, expected, 0.5d);
}

@Test(expectedExceptions = AssertionError.class)
public void compareUnEqualDoubleArraysWithDelta() {
double[] actual = {0.1d, 0.2d, 0.3d, 0.4d};
double[] expected = {0.5d, 0.7d, 0.1d, 0.2d};
Assert.assertEquals(actual, expected, 0.1d);
}

@Test(expectedExceptions = AssertionError.class)
public void assertEqualsMapShouldFail() {
Map<String, String> mapActual = new HashMap<String, String>() {{
Expand Down

0 comments on commit 4748fc8

Please sign in to comment.