Package org.apache.commons.collections

Examples of org.apache.commons.collections.Bag


            // expected
        }
    }
   
    public void testIteratorFailNoMore() {
        Bag bag = makeBag();
        bag.add("A");
        bag.add("A");
        bag.add("B");
        Iterator it = bag.iterator();
        it.next();
        it.next();
        it.next();
        try {
            it.next();
View Full Code Here


            // expected
        }
    }
   
    public void testIteratorFailDoubleRemove() {
        Bag bag = makeBag();
        bag.add("A");
        bag.add("A");
        bag.add("B");
        Iterator it = bag.iterator();
        it.next();
        it.next();
        assertEquals(3, bag.size());
        it.remove();
        assertEquals(2, bag.size());
        try {
            it.remove();
            fail("Should throw IllegalStateException");
        } catch (IllegalStateException ex) {
            // expected
        }
        assertEquals(2, bag.size());
        it.next();
        it.remove();
        assertEquals(1, bag.size());
    }
View Full Code Here

        it.remove();
        assertEquals(1, bag.size());
    }
   
    public void testToArray() {
        Bag bag = makeBag();
        bag.add("A");
        bag.add("A");
        bag.add("B");
        bag.add("B");
        bag.add("C");
        Object[] array = bag.toArray();
        int a = 0, b = 0, c = 0;
        for (int i = 0; i < array.length; i++) {
            a += (array[i].equals("A") ? 1 : 0);
            b += (array[i].equals("B") ? 1 : 0);
            c += (array[i].equals("C") ? 1 : 0);
View Full Code Here

        assertEquals(2, b);
        assertEquals(1, c);
    }

    public void testToArrayPopulate() {
        Bag bag = makeBag();
        bag.add("A");
        bag.add("A");
        bag.add("B");
        bag.add("B");
        bag.add("C");
        String[] array = (String[]) bag.toArray(new String[0]);
        int a = 0, b = 0, c = 0;
        for (int i = 0; i < array.length; i++) {
            a += (array[i].equals("A") ? 1 : 0);
            b += (array[i].equals("B") ? 1 : 0);
            c += (array[i].equals("C") ? 1 : 0);
View Full Code Here

        assertEquals(1, c);
    }

    //-----------------------------------------------------------------------
    public void testEquals() {
        Bag bag = makeBag();
        Bag bag2 = makeBag();
        assertEquals(true, bag.equals(bag2));
        bag.add("A");
        assertEquals(false, bag.equals(bag2));
        bag2.add("A");
        assertEquals(true, bag.equals(bag2));
        bag.add("A");
        bag.add("B");
        bag.add("B");
        bag.add("C");
        bag2.add("A");
        bag2.add("B");
        bag2.add("B");
        bag2.add("C");
        assertEquals(true, bag.equals(bag2));
    }
View Full Code Here

        bag2.add("C");
        assertEquals(true, bag.equals(bag2));
    }

    public void testEqualsHashBag() {
        Bag bag = makeBag();
        Bag bag2 = new HashBag();
        assertEquals(true, bag.equals(bag2));
        bag.add("A");
        assertEquals(false, bag.equals(bag2));
        bag2.add("A");
        assertEquals(true, bag.equals(bag2));
        bag.add("A");
        bag.add("B");
        bag.add("B");
        bag.add("C");
        bag2.add("A");
        bag2.add("B");
        bag2.add("B");
        bag2.add("C");
        assertEquals(true, bag.equals(bag2));
    }
View Full Code Here

        bag2.add("C");
        assertEquals(true, bag.equals(bag2));
    }

    public void testHashCode() {
        Bag bag = makeBag();
        Bag bag2 = makeBag();
        assertEquals(0, bag.hashCode());
        assertEquals(0, bag2.hashCode());
        assertEquals(bag.hashCode(), bag2.hashCode());
        bag.add("A");
        bag.add("A");
        bag.add("B");
        bag.add("B");
        bag.add("C");
        bag2.add("A");
        bag2.add("A");
        bag2.add("B");
        bag2.add("B");
        bag2.add("C");
        assertEquals(bag.hashCode(), bag2.hashCode());
       
        int total = 0;
        total += ("A".hashCode() ^ 2);
        total += ("B".hashCode() ^ 2);
        total += ("C".hashCode() ^ 1);
        assertEquals(total, bag.hashCode());
        assertEquals(total, bag2.hashCode());
    }
View Full Code Here

    }
   
    //--------------------------------------------------------------------------

    public void testlegalAddRemove() {
        Bag bag = makeTestBag();
        assertEquals(0, bag.size());
        Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
        for (int i = 0; i < els.length; i++) {
            bag.add(els[i]);
            assertEquals(i + 1, bag.size());
            assertEquals(true, bag.contains(els[i]));
        }
        Set set = ((PredicatedBag) bag).uniqueSet();
        assertTrue("Unique set contains the first element",set.contains(els[0]));
        assertEquals(true, bag.remove(els[0]));
        set = ((PredicatedBag) bag).uniqueSet();
        assertTrue("Unique set now does not contain the first element",
            !set.contains(els[0]));
    }
View Full Code Here

        assertTrue("Unique set now does not contain the first element",
            !set.contains(els[0]));
    }
    public void testIllegalAdd() {
        Bag bag = makeTestBag();
        Integer i = new Integer(3);
        try {
            bag.add(i);
            fail("Integer should fail string predicate.");
        } catch (IllegalArgumentException e) {
            // expected
        }
        assertTrue("Collection shouldn't contain illegal element",
         !bag.contains(i));  
    }
View Full Code Here

        elements.add("one");
        elements.add("two");
        elements.add(new Integer(3));
        elements.add("four");
        try {
            Bag bag = decorateBag(elements, stringPredicate());
            fail("Bag contains an element that should fail the predicate.");
        } catch (IllegalArgumentException e) {
            // expected
        }
        try {
            Bag bag = decorateBag(new HashBag(), null);
            fail("Expectiing IllegalArgumentException for null predicate.");
        } catch (IllegalArgumentException e) {
            // expected
        }             
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.collections.Bag

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.