public void testBufferEmpty() { resetEmpty(); Buffer buffer = (Buffer) collection; assertEquals(0, buffer.size()); assertEquals(true, buffer.isEmpty()); try { buffer.get(); fail(); } catch (BufferUnderflowException ex) {} try { buffer.remove(); fail(); } catch (BufferUnderflowException ex) {} }
public Collection makeConfirmedFullCollection() { ArrayList list = new ArrayList(); list.addAll(Arrays.asList(getFullElements())); return list; }
public Object[] getFullElements() { return getFullNonNullStringElements(); }
public PriorityBuffer serializeAndRestore(PriorityBuffer h) { PriorityBuffer h1 = null; try { byte[] objekt = writeExternalFormToBytes(h); h1 = (PriorityBuffer) readExternalFormFromBytes(objekt); } catch (IOException e) { e.printStackTrace(); fail(e.toString()); } catch (ClassNotFoundException e) { e.printStackTrace(); fail(e.toString()); } return h1; }
heap.add("f"); assertTrue("heap should not be empty after adds", !heap.isEmpty()); assertEquals( "get using default constructor should return minimum value in the binary heap", String.valueOf((char) ('a' + i)), heap.get()); assertEquals( "remove using default constructor should return minimum value in the binary heap", String.valueOf((char) ('a' + i)), assertTrue("heap should not be empty before all elements are removed", !heap.isEmpty()); } else { assertTrue("heap should be empty after all elements are removed", heap.isEmpty()); fail("NoSuchElementException should be thrown if get is called after all elements are removed"); } catch (BufferUnderflowException ex) {} fail("NoSuchElementException should be thrown if remove is called after all elements are removed"); } catch (BufferUnderflowException ex) {}
h.add(new Integer(randGenerator.nextInt(heapSize))); assertTrue(h.size() == heapSize); PriorityBuffer h1 = serializeAndRestore(h); assertTrue(h1.size() == heapSize); Iterator hit = h.iterator(); while (hit.hasNext()) { Integer n = (Integer) hit.next(); assertTrue(h1.contains(n)); checkOrder(h1);
/** * Illustrates bad internal heap state reported in Bugzilla PR #235818. */ public void testAddRemove() { resetEmpty(); PriorityBuffer heap = (PriorityBuffer) collection; heap.add(new Integer(0)); heap.add(new Integer(2)); heap.add(new Integer(4)); heap.add(new Integer(3)); heap.add(new Integer(8)); heap.add(new Integer(10)); heap.add(new Integer(12)); heap.add(new Integer(3)); confirmed.addAll(heap); // System.out.println(heap); Object obj = new Integer(10); heap.remove(obj); confirmed.remove(obj); // System.out.println(heap); verify(); }
/** * Pops all elements from the heap and verifies that the elements come off * in the correct order. NOTE: this method empties the heap. */ protected void checkOrder(PriorityBuffer h) { Integer lastNum = null; Integer num = null; while (!h.isEmpty()) { num = (Integer) h.remove(); if (h.ascendingOrder) { assertTrue(lastNum == null || num.intValue() >= lastNum.intValue()); } else { // max heap assertTrue(lastNum == null || num.intValue() <= lastNum.intValue()); } lastNum = num; num = null; } }
public Object[] getOtherElements() { return getOtherNonNullStringElements(); }
h.add(new Integer(randGenerator.nextInt(heapSize))); checkOrder(h);
assertTrue("heap should be empty after create", heap.isEmpty()); fail("NoSuchElementException should be thrown if get is called before any elements are added"); } catch (BufferUnderflowException ex) {} fail("NoSuchElementException should be thrown if remove is called before any elements are added"); } catch (BufferUnderflowException ex) {} assertTrue("heap should not be empty after adds", !heap.isEmpty()); assertEquals( "get using default constructor should return minimum value in the binary heap", String.valueOf((char) ('n' - i)), heap.get()); assertEquals( "remove using default constructor should return minimum value in the binary heap", String.valueOf((char) ('n' - i)), assertTrue("heap should not be empty before all elements are removed", !heap.isEmpty()); } else { assertTrue("heap should be empty after all elements are removed", heap.isEmpty()); fail("NoSuchElementException should be thrown if get is called after all elements are removed"); } catch (BufferUnderflowException ex) {} fail("NoSuchElementException should be thrown if remove is called after all elements are removed"); } catch (BufferUnderflowException ex) {}
public void verify() { super.verify(); PriorityBuffer heap = (PriorityBuffer) collection; Comparator c = heap.comparator; if (c == null) { c = ComparatorUtils.naturalComparator(); } if (!heap.ascendingOrder) { c = ComparatorUtils.reversedComparator(c); } Object[] tree = heap.elements; for (int i = 1; i <= heap.size; i++) { Object parent = tree[i]; if (i * 2 <= heap.size) { assertTrue("Parent is less than or equal to its left child", c.compare(parent, tree[i * 2]) <= 0); } if (i * 2 + 1 < heap.size) { assertTrue("Parent is less than or equal to its right child", c.compare(parent, tree[i * 2 + 1]) <= 0); } } }