Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 95616e31 authored by Matías Hernández's avatar Matías Hernández
Browse files

Implement toString() for ArrayMap keySet() and values()

Also add ArrayMapTest to presubmit (it wasn't being run anywhere) and disable a previously failing concurrency test.

Bug: 399310530
Test: atest ArrayMapTest
Flag: EXEMPT Minor debugging-related change
Change-Id: Iae3fe48e91963411f744d9be2570aa0140b13145
parent bb13b093
Loading
Loading
Loading
Loading
+35 −2
Original line number Diff line number Diff line
@@ -355,7 +355,12 @@ abstract class MapCollections<K, V> {
            }
            return result;
        }
    };

        @Override
        public String toString() {
            return toStringHelper(0, this, "KeySet");
        }
    }

    final class ValuesCollection implements Collection<V> {

@@ -456,7 +461,12 @@ abstract class MapCollections<K, V> {
        public <T> T[] toArray(T[] array) {
            return toArrayHelper(array, 1);
        }
    };

        @Override
        public String toString() {
            return toStringHelper(1, this, "ValuesCollection");
        }
    }

    public static <K, V> boolean containsAllHelper(Map<K, V> map, Collection<?> collection) {
        Iterator<?> it = collection.iterator();
@@ -513,6 +523,29 @@ abstract class MapCollections<K, V> {
        return array;
    }

    private String toStringHelper(int offset, Object thing, String thingName) {
        int size = colGetSize();
        if (size == 0) {
            return "[]";
        }

        StringBuilder buffer = new StringBuilder(size * 14);
        buffer.append('[');
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            Object entry = colGetEntry(i, offset);
            if (entry != thing) {
                buffer.append(entry);
            } else {
                buffer.append("(this ").append(thingName).append(")");
            }
        }
        buffer.append(']');
        return buffer.toString();
    }

    public static <T> boolean equalsSetHelper(Set<T> set, Object object) {
        if (set == object) {
            return true;
+51 −0
Original line number Diff line number Diff line
@@ -14,14 +14,18 @@

package android.util;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.fail;

import android.platform.test.annotations.IgnoreUnderRavenwood;
import android.platform.test.annotations.Presubmit;
import android.platform.test.ravenwood.RavenwoodRule;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -32,6 +36,7 @@ import java.util.ConcurrentModificationException;
 * Unit tests for ArrayMap that don't belong in CTS.
 */
@LargeTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class ArrayMapTest {
    private static final String TAG = "ArrayMapTest";
@@ -51,6 +56,7 @@ public class ArrayMapTest {
     * @throws Exception
     */
    @Test
    @Ignore("Failing; b/399137661")
    @IgnoreUnderRavenwood(reason = "Long test runtime")
    public void testConcurrentModificationException() throws Exception {
        final int TEST_LEN_MS = 5000;
@@ -118,4 +124,49 @@ public class ArrayMapTest {
            }
        }
    }

    @Test
    public void testToString() {
        map.put("1", "One");
        map.put("2", "Two");
        map.put("3", "Five");
        map.put("3", "Three");

        assertThat(map.toString()).isEqualTo("{1=One, 2=Two, 3=Three}");
        assertThat(map.keySet().toString()).isEqualTo("[1, 2, 3]");
        assertThat(map.values().toString()).isEqualTo("[One, Two, Three]");
    }

    @Test
    public void testToStringRecursive() {
        ArrayMap<Object, Object> weird = new ArrayMap<>();
        weird.put("1", weird);

        assertThat(weird.toString()).isEqualTo("{1=(this Map)}");
        assertThat(weird.keySet().toString()).isEqualTo("[1]");
        assertThat(weird.values().toString()).isEqualTo("[{1=(this Map)}]");
    }

    @Test
    public void testToStringRecursiveKeySet() {
        ArrayMap<Object, Object> weird = new ArrayMap<>();
        weird.put("1", weird.keySet());
        weird.put(weird.keySet(), "2");

        assertThat(weird.toString()).isEqualTo("{1=[1, (this KeySet)], [1, (this KeySet)]=2}");
        assertThat(weird.keySet().toString()).isEqualTo("[1, (this KeySet)]");
        assertThat(weird.values().toString()).isEqualTo("[[1, (this KeySet)], 2]");
    }

    @Test
    public void testToStringRecursiveValues() {
        ArrayMap<Object, Object> weird = new ArrayMap<>();
        weird.put("1", weird.values());
        weird.put(weird.values(), "2");

        assertThat(weird.toString()).isEqualTo(
                "{1=[(this ValuesCollection), 2], [(this ValuesCollection), 2]=2}");
        assertThat(weird.keySet().toString()).isEqualTo("[1, [(this ValuesCollection), 2]]");
        assertThat(weird.values().toString()).isEqualTo("[(this ValuesCollection), 2]");
    }
}