Skip to content

Commit 8f59230

Browse files
committed
Avoid a potentially expensive reflection-based loop in assertArrayEquals, in the usual case where the arrays are in fact exactly equal.
1 parent 950702c commit 8f59230

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Diff for: src/main/java/org/junit/internal/ComparisonCriteria.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.junit.internal;
22

33
import java.lang.reflect.Array;
4+
import java.util.Arrays;
45

56
import org.junit.Assert;
67

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

0 commit comments

Comments
 (0)