add Arrays test for equals and hashCode, handle corner cases with null

This commit is contained in:
Joshua Warner 2012-07-24 11:31:47 -06:00
parent b034d11221
commit fffde5f445
2 changed files with 28 additions and 1 deletions

View File

@ -43,15 +43,25 @@ public class Arrays {
}
public static int hashCode(Object[] array) {
if(array == null) {
return 9023;
}
int hc = 823347;
for(Object o : array) {
hc += o.hashCode();
hc += o != null ? o.hashCode() : 54267;
hc *= 3;
}
return hc;
}
public static boolean equals(Object[] a, Object[] b) {
if(a == b) {
return true;
}
if(a == null || b == null) {
return false;
}
if(a.length != b.length) {
return false;
}

View File

@ -77,5 +77,22 @@ public class Arrays {
array[0] = Integer.valueOf(42);
expect(array[0].intValue() == 42);
}
{ Object[] a = new Object[3];
Object[] b = new Object[3];
expect(java.util.Arrays.equals(a, b));
a[0] = new Object();
expect(! java.util.Arrays.equals(a, b));
expect(! java.util.Arrays.equals(b, new Object[4]));
expect(! java.util.Arrays.equals(b, null));
expect(! java.util.Arrays.equals(a, b));
expect(java.util.Arrays.equals(null, null));
b[0] = a[0];
expect(java.util.Arrays.equals(a, b));
java.util.Arrays.hashCode(a);
java.util.Arrays.hashCode(null);
}
}
}