Skip to content

Commit

Permalink
Merge pull request junit-team#918 from eamonnmcmanus/master
Browse files Browse the repository at this point in the history
Avoid a potentially expensive reflection-based loop in assertArrayEquals...
  • Loading branch information
David Saff committed Sep 11, 2014
2 parents afdbe56 + 8f59230 commit c5a2e04
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/org/junit/internal/ComparisonCriteria.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.junit.internal;

import java.lang.reflect.Array;
import java.util.Arrays;

import org.junit.Assert;

Expand All @@ -24,7 +25,11 @@ public abstract class ComparisonCriteria {
*/
public void arrayEquals(String message, Object expecteds, Object actuals)
throws ArrayComparisonFailure {
if (expecteds == actuals) {
if (expecteds == actuals
|| Arrays.deepEquals(new Object[] {expecteds}, new Object[] {actuals})) {
// The reflection-based loop below is potentially very slow, especially for primitive
// arrays. The deepEquals check allows us to circumvent it in the usual case where
// the arrays are exactly equal.
return;
}
String header = message == null ? "" : message + ": ";
Expand Down

0 comments on commit c5a2e04

Please sign in to comment.