From fffde5f44552e05e278637f3ee9c20f16b655e30 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Tue, 24 Jul 2012 11:31:47 -0600 Subject: [PATCH] add Arrays test for equals and hashCode, handle corner cases with null --- classpath/java/util/Arrays.java | 12 +++++++++++- test/Arrays.java | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/classpath/java/util/Arrays.java b/classpath/java/util/Arrays.java index 1d77d88307..c32cc1fe5f 100644 --- a/classpath/java/util/Arrays.java +++ b/classpath/java/util/Arrays.java @@ -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; } diff --git a/test/Arrays.java b/test/Arrays.java index 96f5f598f0..27acd2e3ff 100644 --- a/test/Arrays.java +++ b/test/Arrays.java @@ -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); + } } }