FileDocCategorySizeDatePackage
OpReorderTest.javaAPI DocAndroid 5.1 API18171Thu Mar 12 22:22:56 GMT 2015android.support.v7.widget

OpReorderTest

public class OpReorderTest extends TestCase

Fields Summary
private static final String
TAG
List
mUpdateOps
List
mAddedItems
List
mRemovedItems
Set
mRecycledOps
static Random
random
OpReorderer
mOpReorderer
int
itemCount
int
updatedItemCount
Constructors Summary
Methods Summary
android.support.v7.widget.AdapterHelper.UpdateOpadd(int start, int count)

        updatedItemCount += count;
        return record(new UpdateOp(ADD, start, count));
    
private voidapply(java.util.List items, java.util.List updateOps)

        for (UpdateOp op : updateOps) {
            switch (op.cmd) {
                case UpdateOp.ADD:
                    for (int i = 0; i < op.itemCount; i++) {
                        final Item newItem = Item.create();
                        mAddedItems.add(newItem);
                        items.add(op.positionStart + i, newItem);
                    }
                    break;
                case UpdateOp.REMOVE:
                    for (int i = 0; i < op.itemCount; i++) {
                        mRemovedItems.add(items.remove(op.positionStart));
                    }
                    break;
                case UpdateOp.MOVE:
                    items.add(op.itemCount, items.remove(op.positionStart));
                    break;
                case UpdateOp.UPDATE:
                    for (int i = 0; i < op.itemCount; i++) {
                        final int index = op.positionStart + i;
                        items.get(index).version = items.get(index).version + 1;
                    }
                    break;
            }
        }
    
private voidassertAllMovesAtTheEnd(java.util.List ops)

        boolean foundMove = false;
        for (UpdateOp op : ops) {
            if (op.cmd == MOVE) {
                foundMove = true;
            } else {
                assertFalse(foundMove);
            }
        }
    
private voidassertHasTheSameItems(java.util.List items, java.util.List clones)

        String log = "has the same items\n" + toString(items) + "--\n" + toString(clones);
        assertEquals(log, items.size(), clones.size());
        for (Item item : items) {
            for (Item clone : clones) {
                if (item.id == clone.id && item.version == clone.version) {
                    clones.remove(clone);
                    break;
                }
            }
        }
        assertEquals(log, 0, clones.size());
    
private voidassertListsIdentical(java.util.List items, java.util.List clones)

        String log = "is identical\n" + toString(items) + "--\n" + toString(clones);
        assertEquals(items.size(), clones.size());
        for (int i = 0; i < items.size(); i++) {
            Item.assertIdentical(log, items.get(i), clones.get(i));
        }
    
private voidassertRecycledOpsAreNotReused(java.util.List items)

        for (Item item : items) {
            assertFalse(mRecycledOps.contains(item));
        }
    
voidcleanState()

        mUpdateOps = new ArrayList<UpdateOp>();
        mAddedItems = new ArrayList<Item>();
        mRemovedItems = new ArrayList<Item>();
        mRecycledOps = new HashSet<UpdateOp>();
        Item.idCounter = 0;
    
android.support.v7.widget.AdapterHelper.UpdateOpmv(int from, int to)

        return record(new UpdateOp(MOVE, from, to));
    
intnextInt(java.util.Random random, int n)

        if (n == 0) {
            return 0;
        }
        return random.nextInt(n);
    
private java.lang.StringopsToString(java.util.List updateOps)

        StringBuilder sb = new StringBuilder();
        for (UpdateOp op : updateOps) {
            sb.append("\n").append(op.toString());
        }
        return sb.append("\n").toString();
    
public voidorderedRandom(int ops)

        for (int op : ops) {
            randOp(op);
        }
    
voidprocess()

        List<Item> items = new ArrayList<Item>(itemCount);
        for (int i = 0; i < itemCount; i++) {
            items.add(Item.create());
        }
        List<Item> clones = new ArrayList<Item>(itemCount);
        for (int i = 0; i < itemCount; i++) {
            clones.add(Item.clone(items.get(i)));
        }
        List<UpdateOp> rewritten = rewriteOps(mUpdateOps);

        assertAllMovesAtTheEnd(rewritten);

        apply(items, mUpdateOps);
        List<Item> originalAdded = mAddedItems;
        List<Item> originalRemoved = mRemovedItems;
        if (originalAdded.size() > 0) {
            Item.idCounter = originalAdded.get(0).id;
        }
        mAddedItems = new ArrayList<Item>();
        mRemovedItems = new ArrayList<Item>();
        apply(clones, rewritten);

        // now check equality
        assertListsIdentical(items, clones);
        assertHasTheSameItems(originalAdded, mAddedItems);
        assertHasTheSameItems(originalRemoved, mRemovedItems);

        assertRecycledOpsAreNotReused(items);
        assertRecycledOpsAreNotReused(clones);
    
voidrandOp(int cmd)

        switch (cmd) {
            case REMOVE:
                if (updatedItemCount > 1) {
                    int s = nextInt(random, updatedItemCount - 1);
                    int len = Math.max(1, nextInt(random, updatedItemCount - s));
                    rm(s, len);
                }
                break;
            case ADD:
                int s = updatedItemCount == 0 ? 0 : nextInt(random, updatedItemCount);
                add(s, nextInt(random, 50));
                break;
            case MOVE:
                if (updatedItemCount >= 2) {
                    int from = nextInt(random, updatedItemCount);
                    int to;
                    do {
                        to = nextInt(random, updatedItemCount);
                    } while (to == from);
                    mv(from, to);
                }
                break;
            case UPDATE:
                if (updatedItemCount > 1) {
                    s = nextInt(random, updatedItemCount - 1);
                    int len = Math.max(1, nextInt(random, updatedItemCount - s));
                    up(s, len);
                }
                break;
        }
    
android.support.v7.widget.AdapterHelper.UpdateOprecord(android.support.v7.widget.AdapterHelper.UpdateOp op)

        mUpdateOps.add(op);
        return op;
    
private java.util.ListrewriteOps(java.util.List updateOps)

        List<UpdateOp> copy = new ArrayList<UpdateOp>();
        for (UpdateOp op : updateOps) {
            copy.add(new UpdateOp(op.cmd, op.positionStart, op.itemCount));
        }
        mOpReorderer.reorderOps(copy);
        return copy;
    
android.support.v7.widget.AdapterHelper.UpdateOprm(int start, int count)

        updatedItemCount -= count;
        return record(new UpdateOp(REMOVE, start, count));
    
protected voidsetUp()

        super.setUp();
        cleanState();
    
public voidsetup(int count)


        
        itemCount = count;
        updatedItemCount = itemCount;
    
private voidswapMoveRemove(java.util.List list, int move)

        mOpReorderer.swapMoveRemove(list, move, list.get(move), move + 1, list.get(move + 1));
    
private voidswapMoveUpdate(java.util.List list, int move)

        mOpReorderer.swapMoveUpdate(list, move, list.get(move), move + 1, list.get(move + 1));
    
public voidtest1()

        setup(10);
        mv(3, 5);
        rm(3, 4);
        process();
    
public voidtest2()

        setup(5);
        mv(1, 3);
        rm(1, 1);
        process();
    
public voidtest3()

        setup(5);
        mv(0, 4);
        rm(2, 1);
        process();
    
public voidtest4()

        setup(5);
        mv(3, 0);
        rm(3, 1);
        process();
    
public voidtest5()

        setup(10);
        mv(8, 1);
        rm(6, 3);
        process();
    
public voidtest6()

        setup(5);
        mv(1, 3);
        rm(0, 3);
        process();
    
public voidtest7()

        setup(5);
        mv(3, 4);
        rm(3, 1);
        process();
    
public voidtest8()

        setup(5);
        mv(4, 3);
        rm(3, 1);
        process();
    
public voidtest9()

        setup(5);
        mv(2, 0);
        rm(2, 2);
        process();
    
public voidtestMoveRemove()

        setup(10);
        mv(3, 8);
        rm(3, 5);
        process();
    
public voidtestMoveRemoved()

        setup(10);
        mv(3, 8);
        rm(7, 3);
        process();
    
public voidtestRandom()

        for (int i = 0; i < 150; i++) {
            try {
                cleanState();
                setup(50);
                for (int j = 0; j < 50; j++) {
                    randOp(nextInt(random, nextInt(random, 4)));
                }
                Log.d(TAG, "running random test " + i);
                process();
            } catch (Throwable t) {
                throw new Exception(t.getMessage() + "\n" + opsToString(mUpdateOps));
            }
        }
    
public voidtestRandomMoveAdd()

        for (int i = 0; i < 1000; i++) {
            try {
                cleanState();
                setup(5);
                orderedRandom(MOVE, ADD);
                process();
            } catch (Throwable t) {
                throw new Exception(t.getMessage() + "\n" + opsToString(mUpdateOps));
            }
        }
    
public voidtestRandomMoveRemove()

        for (int i = 0; i < 1000; i++) {
            try {
                cleanState();
                setup(5);
                orderedRandom(MOVE, REMOVE);
                process();
            } catch (Throwable t) {
                throw new Exception(t.getMessage() + "\n" + opsToString(mUpdateOps));
            }
        }
    
public voidtestRandomMoveUpdate()

        for (int i = 0; i < 1000; i++) {
            try {
                cleanState();
                setup(5);
                orderedRandom(MOVE, UPDATE);
                process();
            } catch (Throwable t) {
                throw new Exception(t.getMessage() + "\n" + opsToString(mUpdateOps));
            }
        }
    
public voidtestSwapMoveRemove_1()

        mv(10, 15);
        rm(2, 3);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(mv(7, 12), mUpdateOps.get(1));
        assertEquals(rm(2, 3), mUpdateOps.get(0));
    
public voidtestSwapMoveRemove_10()

        mv(1, 3);
        rm(0, 3);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(2, 2), mUpdateOps.get(0));
        assertEquals(rm(0, 1), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_11()

        mv(3, 8);
        rm(7, 3);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(3, 1), mUpdateOps.get(0));
        assertEquals(rm(7, 2), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_12()

        mv(1, 3);
        rm(2, 1);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(3, 1), mUpdateOps.get(0));
        assertEquals(mv(1, 2), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_13()

        mv(1, 3);
        rm(1, 2);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(1, mUpdateOps.size());
        assertEquals(rm(2, 2), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_14()

        mv(4, 2);
        rm(3, 1);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(2, 1), mUpdateOps.get(0));
        assertEquals(mv(2, 3), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_15()

        mv(4, 2);
        rm(3, 2);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(1, mUpdateOps.size());
        assertEquals(rm(2, 2), mUpdateOps.get(0));
    
public voidtestSwapMoveRemove_16()

        mv(2, 3);
        rm(1, 2);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(3, 1), mUpdateOps.get(0));
        assertEquals(rm(1, 1), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_2()

        mv(3, 8);
        rm(4, 2);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(5, 2), mUpdateOps.get(0));
        assertEquals(mv(3, 6), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_3()

        mv(3, 8);
        rm(3, 2);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(4, 2), mUpdateOps.get(0));
        assertEquals(mv(3, 6), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_4()

        mv(3, 8);
        rm(2, 3);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(3, mUpdateOps.size());
        assertEquals(rm(4, 2), mUpdateOps.get(0));
        assertEquals(rm(2, 1), mUpdateOps.get(1));
        assertEquals(mv(2, 5), mUpdateOps.get(2));
    
public voidtestSwapMoveRemove_5()

        mv(3, 0);
        rm(2, 3);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(3, mUpdateOps.size());
        assertEquals(rm(4, 1), mUpdateOps.get(0));
        assertEquals(rm(1, 2), mUpdateOps.get(1));
        assertEquals(mv(1, 0), mUpdateOps.get(2));
    
public voidtestSwapMoveRemove_6()

        mv(3, 10);
        rm(2, 3);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(3, mUpdateOps.size());
        assertEquals(rm(4, 2), mUpdateOps.get(0));
        assertEquals(rm(2, 1), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_7()

        mv(3, 2);
        rm(6, 2);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(rm(6, 2), mUpdateOps.get(0));
        assertEquals(mv(3, 2), mUpdateOps.get(1));
    
public voidtestSwapMoveRemove_8()

        mv(3, 4);
        rm(3, 1);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(1, mUpdateOps.size());
        assertEquals(rm(4, 1), mUpdateOps.get(0));
    
public voidtestSwapMoveRemove_9()

        mv(3, 4);
        rm(4, 1);
        swapMoveRemove(mUpdateOps, 0);
        assertEquals(1, mUpdateOps.size());
        assertEquals(rm(3, 1), mUpdateOps.get(0));
    
public voidtestSwapMoveUpdate_0()

        mv(1, 3);
        up(1, 2);
        swapMoveUpdate(mUpdateOps, 0);
        assertEquals(2, mUpdateOps.size());
        assertEquals(up(2, 2), mUpdateOps.get(0));
        assertEquals(mv(1, 3), mUpdateOps.get(1));
    
public voidtestSwapMoveUpdate_1()

        mv(0, 2);
        up(0, 4);
        swapMoveUpdate(mUpdateOps, 0);
        assertEquals(3, mUpdateOps.size());
        assertEquals(up(0, 1), mUpdateOps.get(0));
        assertEquals(up(1, 3), mUpdateOps.get(1));
        assertEquals(mv(0, 2), mUpdateOps.get(2));
    
public voidtestSwapMoveUpdate_2()

        mv(2, 0);
        up(1, 3);
        swapMoveUpdate(mUpdateOps, 0);
        assertEquals(3, mUpdateOps.size());
        assertEquals(up(3, 1), mUpdateOps.get(0));
        assertEquals(up(0, 2), mUpdateOps.get(1));
        assertEquals(mv(2, 0), mUpdateOps.get(2));
    
private java.lang.StringtoString(java.util.List items)

        StringBuilder sb = new StringBuilder();
        for (Item item : items) {
            sb.append(item.toString()).append("\n");
        }
        return sb.toString();
    
android.support.v7.widget.AdapterHelper.UpdateOpup(int start, int count)

        return record(new UpdateOp(UPDATE, start, count));