From 21ab6f49910a6f319bc7b9d3964086cb1ffe09d0 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Mon, 10 Jun 2013 15:59:15 -0700 Subject: ArrayMap is great, so how about ArraySet! Also a few little tweaks to the ArrayMap implementation. Note that these are fairly parallel implementations; I looked at what I could abstract out as a base class, but there isn't really all that much without making the resulting code more general and thus slower. If we kept the data structure for ArraySet the same as map, where the array has two values per entry, then they could probably share a lot of code. I couldn't really bring myself to do that at this point, though... Change-Id: I9e0dd8baa8e1febcc1033ecef61623ad59ce4aae --- .../android/test/activity/ArrayMapTests.java | 206 +++++++++++++++------ 1 file changed, 150 insertions(+), 56 deletions(-) (limited to 'tests') diff --git a/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java b/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java index 7d82cd3..9b54927 100644 --- a/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java +++ b/tests/ActivityTests/src/com/google/android/test/activity/ArrayMapTests.java @@ -17,10 +17,12 @@ package com.google.android.test.activity; import android.util.ArrayMap; +import android.util.ArraySet; import android.util.Log; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -137,41 +139,35 @@ public class ArrayMapTests { } } - int index = 0; - for (Object entry : array.entrySet()) { - Object key = ((Map.Entry)entry).getKey(); - Object value = ((Map.Entry)entry).getValue(); - Object realKey = array.keyAt(index); - Object realValue = array.valueAt(index); - if (!compare(key, realKey)) { - Log.e("test", "Bad entry iterator: expected key " + realKey + ", got " + key - + " at index " + index); - return false; - } - if (!compare(value, realValue)) { - Log.e("test", "Bad entry iterator: expected value " + realValue + ", got " + value - + " at index " + index); + return true; + } + + private static boolean compareSets(HashSet set, ArraySet array) { + if (set.size() != array.size()) { + Log.e("test", "Bad size: expected " + set.size() + ", got " + array.size()); + return false; + } + + for (Object entry : set) { + if (!array.contains(entry)) { + Log.e("test", "Bad value: expected " + entry + " not found in ArraySet"); return false; } - index++; } - index = 0; - for (Object key : array.keySet()) { - Object realKey = array.keyAt(index); - if (!compare(key, realKey)) { - Log.e("test", "Bad key iterator: expected key " + realKey + ", got " + key - + " at index " + index); + for (int i=0; i mapSet = map.entrySet(); for (Map.Entry entry : mapSet) { @@ -247,6 +243,17 @@ public class ArrayMapTests { } } + private static void dump(Set set, ArraySet array) { + Log.e("test", "HashSet of " + set.size() + " entries:"); + for (Object entry : set) { + Log.e("test", " " + entry); + } + Log.e("test", "ArraySet of " + array.size() + " entries:"); + for (int i=0; i mapSet = map1.entrySet(); @@ -260,60 +267,93 @@ public class ArrayMapTests { } public static void run() { - HashMap mHashMap = new HashMap(); - ArrayMap mArrayMap = new ArrayMap(); + HashMap hashMap = new HashMap(); + ArrayMap arrayMap = new ArrayMap(); + HashSet hashSet = new HashSet(); + ArraySet arraySet = new ArraySet(); for (int i=0; i it = mArrayMap.keySet().iterator(); + Iterator it = arrayMap.keySet().iterator(); + while (it.hasNext()) { + if (it.next().equals(lookup)) { + it.remove(); + } + } + if (arrayMap.containsKey(lookup)) { + Log.e("test", "Bad map iterator: didn't remove test key"); + dump(hashMap, arrayMap); + } + + arraySet.add(new ControlledHash(50000)); + it = arraySet.iterator(); while (it.hasNext()) { if (it.next().equals(lookup)) { it.remove(); } } - if (mArrayMap.containsKey(lookup)) { - Log.e("test", "Bad iterator: didn't remove test key"); - dump(mHashMap, mArrayMap); + if (arraySet.contains(lookup)) { + Log.e("test", "Bad set iterator: didn't remove test key"); + dump(hashSet, arraySet); + } + + if (!equalsMapTest()) { + return; } - if (!equalsTest()) { + if (!equalsSetTest()) { return; } - // copy constructor test + // map copy constructor test ArrayMap newMap = new ArrayMap(); for (int i = 0; i < 10; ++i) { newMap.put(i, String.valueOf(i)); @@ -322,15 +362,31 @@ public class ArrayMapTests { if (!compare(mapCopy, newMap)) { Log.e("test", "ArrayMap copy constructor failure: expected " + newMap + ", got " + mapCopy); - dump(mHashMap, mArrayMap); + dump(newMap, mapCopy); + return; + } + + // set copy constructor test + ArraySet newSet = new ArraySet(); + for (int i = 0; i < 10; ++i) { + newSet.add(i); + } + ArraySet setCopy = new ArraySet(newSet); + if (!compare(setCopy, newSet)) { + Log.e("test", "ArraySet copy constructor failure: expected " + + newSet + ", got " + setCopy); + dump(newSet, setCopy); return; } Log.e("test", "Test successful; printing final map."); - dump(mHashMap, mArrayMap); + dump(hashMap, arrayMap); + + Log.e("test", "Test successful; printing final set."); + dump(hashSet, arraySet); } - private static boolean equalsTest() { + private static boolean equalsMapTest() { ArrayMap map1 = new ArrayMap(); ArrayMap map2 = new ArrayMap(); HashMap map3 = new HashMap(); @@ -368,4 +424,42 @@ public class ArrayMapTests { return true; } + + private static boolean equalsSetTest() { + ArraySet set1 = new ArraySet(); + ArraySet set2 = new ArraySet(); + HashSet set3 = new HashSet(); + if (!compare(set1, set2) || !compare(set1, set3) || !compare(set3, set2)) { + Log.e("test", "ArraySet equals failure for empty sets " + set1 + ", " + + set2 + ", " + set3); + return false; + } + + for (int i = 0; i < 10; ++i) { + set1.add(i); + set2.add(i); + set3.add(i); + } + if (!compare(set1, set2) || !compare(set1, set3) || !compare(set3, set2)) { + Log.e("test", "ArraySet equals failure for populated sets " + set1 + ", " + + set2 + ", " + set3); + return false; + } + + set1.remove(0); + if (compare(set1, set2) || compare(set1, set3) || compare(set3, set1)) { + Log.e("test", "ArraSet equals failure for set size " + set1 + ", " + + set2 + ", " + set3); + return false; + } + + set1.add(-1); + if (compare(set1, set2) || compare(set1, set3) || compare(set3, set1)) { + Log.e("test", "ArraySet equals failure for set contents " + set1 + ", " + + set2 + ", " + set3); + return false; + } + + return true; + } } -- cgit v1.1