package edd.util; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import org.junit.Test; public class BagTest { @Test public void testConstructors() { Collection b1 = new Bag<>(); assertEquals(0, b1.size()); assertEquals("[]", b1.toString()); Collection b2 = new Bag<>(Arrays.asList("pepe", "pecas", "pica", "papas", "con", "un", "pico")); assertEquals(7, b2.size()); assertEquals("[pepe, pecas, pica, papas, con, un, pico]", b2.toString()); } @Test public void testAdd() { Collection b1 = new Bag<>(); assertEquals(0, b1.size()); assertTrue(b1.add(4)); assertEquals(1, b1.size()); assertTrue(b1.add(8)); assertEquals(2, b1.size()); assertTrue(b1.add(15)); assertEquals(3, b1.size()); assertTrue(b1.add(16)); assertEquals(4, b1.size()); assertTrue(b1.add(23)); assertEquals(5, b1.size()); assertTrue(b1.add(42)); assertEquals(6, b1.size()); assertEquals("[4, 8, 15, 16, 23, 42]", b1.toString()); Collection b2 = new Bag<>(); b2.addAll(Arrays.asList("pepe", "pecas", "pica", "papas", "con", "un", "pico")); assertEquals(7, b2.size()); assertEquals("[pepe, pecas, pica, papas, con, un, pico]", b2.toString()); } @Test public void testClear() { Collection b1 = new Bag<>(); assertEquals(0, b1.size()); assertEquals("[]", b1.toString()); b1.clear(); assertEquals(0, b1.size()); assertEquals("[]", b1.toString()); Collection b2 = new Bag<>(Arrays.asList("pepe", "pecas", "pica", "papas", "con", "un", "pico")); assertEquals(7, b2.size()); assertEquals("[pepe, pecas, pica, papas, con, un, pico]", b2.toString()); b2.clear(); assertEquals(0, b2.size()); assertEquals("[]", b2.toString()); } @Test public void testContains() { Collection b1 = new Bag<>(Arrays.asList(4, 8, 15, 16, 23, 42)); Collection b2 = new Bag<>(Arrays.asList(8, 15, 23)); assertTrue(b1.contains(4)); assertTrue(b1.contains(15)); assertTrue(b1.contains(42)); assertFalse(b1.contains(0)); assertFalse(b1.contains(41)); assertFalse(b1.contains(108)); assertTrue(b1.containsAll(b2)); assertFalse(b2.containsAll(b1)); b2.add(108); assertFalse(b1.containsAll(b2)); assertFalse(b2.containsAll(b1)); } @Test public void testIsEmpty() { Collection b1 = new Bag<>(); assertTrue(b1.isEmpty()); b1.add("hola"); assertFalse(b1.isEmpty()); Collection b2 = new Bag<>(Arrays.asList(4, 8, 15, 16, 23, 42)); assertFalse(b2.isEmpty()); b2.clear(); assertTrue(b2.isEmpty()); } @Test public void testIterator() { List lst1 = Arrays.asList(4, 8, 15, 16, 23, 42); Collection b1 = new Bag<>(lst1); Iterator it1 = b1.iterator(); assertEquals(6, b1.size()); assertEquals("[4, 8, 15, 16, 23, 42]", b1.toString()); for (int i = 0, n = b1.size(); i < n; i++) { assertTrue(it1.hasNext()); assertEquals(lst1.get(i), it1.next()); if (i == 3) { it1.remove(); } } assertFalse(it1.hasNext()); try { it1.next(); fail(); } catch (NoSuchElementException e) { } assertEquals(5, b1.size()); assertEquals("[4, 8, 15, 23, 42]", b1.toString()); Collection b2 = new Bag<>(); Iterator it2 = b2.iterator(); assertEquals(0, b2.size()); assertEquals("[]", b2.toString()); assertFalse(it2.hasNext()); try { it2.next(); fail(); } catch (NoSuchElementException e) { } Collection b3 = new Bag<>(Arrays.asList('a', 'b', 'c')); StringBuilder sb = new StringBuilder(); for (char i: b3) { for (char j: b3) { sb.append(i); sb.append(j); sb.append(' '); } } assertEquals("aa ab ac ba bb bc ca cb cc ", sb.toString()); } @Test public void testRemove() { Collection b1 = new Bag<>(Arrays.asList(4, 8, 15, 16, 23, 4, 42)); Collection b2 = new Bag<>(Arrays.asList(8, 15, 23, 108)); assertTrue(b1.remove(4)); assertTrue(b1.remove(4)); assertFalse(b1.remove(4)); assertTrue(b1.remove(42)); assertFalse(b1.remove(42)); assertFalse(b1.remove(108)); assertEquals("[8, 15, 16, 23]", b1.toString()); assertTrue(b1.removeAll(b2)); assertEquals(1, b1.size()); assertEquals("[16]", b1.toString()); assertFalse(b1.removeAll(b2)); assertEquals(1, b1.size()); assertEquals("[16]", b1.toString()); assertFalse(b2.removeAll(b1)); assertEquals("[8, 15, 23, 108]", b2.toString()); } @Test public void testRetainAll() { Collection b1 = new Bag<>(Arrays.asList(4, 8, 15, 16, 23, 4, 42)); Collection b2 = new Bag<>(Arrays.asList(8, 15, 23, 108)); assertTrue(b1.retainAll(b2)); assertEquals("[8, 15, 23]", b1.toString()); assertFalse(b1.retainAll(b2)); Collection b3 = new Bag<>(Arrays.asList("Fili", "Kili", "Oin", "Gloin", "Thorin", "Dwalin", "Balin")); Collection b4 = new Bag<>(Arrays.asList("Bifur", "Bofur", "Bombur", "Dori", "Nori", "Ori")); assertTrue(b3.retainAll(b4)); assertTrue(b3.isEmpty()); assertFalse(b3.retainAll(b4)); } @Test public void testToArray() { Integer[] ai = { 4, 8, 15, 16, 23, 42 }; Collection b1 = new Bag<>(Arrays.asList(ai)); assertArrayEquals(ai, b1.toArray()); String[] as = new String[0]; Collection b2 = new Bag<>(Arrays.asList(as)); assertArrayEquals(as, b2.toArray()); } }