FileDocCategorySizeDatePackage
LongSparseLongArrayTest.javaAPI DocAndroid 5.1 API3723Thu Mar 12 22:22:12 GMT 2015android.util

LongSparseLongArrayTest

public class LongSparseLongArrayTest extends TestCase
Tests for {@link LongSparseLongArray}.

Fields Summary
private static final String
TAG
Constructors Summary
Methods Summary
private static EgetKeyAtIndex(java.util.Map map, int index)

        final Iterator<E> keys = map.keySet().iterator();
        for (int i = 0; i < index; i++) {
            keys.next();
        }
        return keys.next();
    
public voidtestFuzz()

        final Random r = new Random();

        final HashMap<Long, Long> map = new HashMap<Long, Long>();
        final LongSparseLongArray array = new LongSparseLongArray(r.nextInt(128));

        for (int i = 0; i < 10240; i++) {
            if (r.nextBoolean()) {
                final long key = r.nextLong();
                final long value = r.nextLong();
                map.put(key, value);
                array.put(key, value);
            }
            if (r.nextBoolean() && map.size() > 0) {
                final int index = r.nextInt(map.size());
                final long key = getKeyAtIndex(map, index);
                map.remove(key);
                array.delete(key);
            }
        }

        Log.d(TAG, "verifying a map with " + map.size() + " entries");

        for (Map.Entry<Long, Long> e : map.entrySet()) {
            final long key = e.getKey();
            final long value = e.getValue();
            assertEquals(value, array.get(key));
        }
    
public voidtestMiddleInsert()

        final LongSparseLongArray array = new LongSparseLongArray(5);
        for (int i = 0; i < 48; i++) {
            final long value = 1 << i;
            array.put(value, value);
        }
        final long special = (1 << 24) + 5;
        array.put(special, 1024);
        for (int i = 0; i < 48; i++) {
            final long value = 1 << i;
            assertEquals(value, array.get(value, -1));
            assertEquals(-1, array.get(-value, -1));
        }
        assertEquals(1024, array.get(special, -1));
    
public voidtestSimplePut()


         
        final LongSparseLongArray array = new LongSparseLongArray(5);
        for (int i = 0; i < 48; i++) {
            final long value = 1 << i;
            array.put(value, value);
        }
        for (int i = 0; i < 48; i++) {
            final long value = 1 << i;
            assertEquals(value, array.get(value, -1));
            assertEquals(-1, array.get(-value, -1));
        }
    
public voidtestSimplePutBackwards()

        final LongSparseLongArray array = new LongSparseLongArray(5);
        for (int i = 47; i >= 0; i--) {
            final long value = 1 << i;
            array.put(value, value);
        }
        for (int i = 0; i < 48; i++) {
            final long value = 1 << i;
            assertEquals(value, array.get(value, -1));
            assertEquals(-1, array.get(-value, -1));
        }