package de.lmu.ifi.dbs.elki.utilities.datastructures.heap; /* This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures Copyright (C) 2012 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ import static org.junit.Assert.assertEquals; import java.util.Collections; import org.junit.Test; import de.lmu.ifi.dbs.elki.utilities.datastructures.heap.Heap; import de.lmu.ifi.dbs.elki.utilities.datastructures.heap.TiedTopBoundedHeap; /** * Test the in-memory bounded heap class. * * @author Erich Schubert */ public class TestTiedTopBoundedHeap { /** * Test bounded heap */ @Test public void testTiedTopBoundedHeap() { Integer[] data = { 5, 3, 4, 2, 7, 1, 9, 8, 10, 6, 5 }; Integer[] asc = { 5, 5, 6, 7, 8, 9, 10 }; Integer[] desc = { 5, 5, 4, 3, 2, 1 }; Heap hasc = new TiedTopBoundedHeap<>(asc.length - 1); Heap hdesc = new TiedTopBoundedHeap<>(desc.length - 1, Collections.reverseOrder()); for(Integer i : data) { hasc.add(i); hdesc.add(i); } // LoggingUtil.warning("Heap: "+hasc.toString()+ " -- "+hdesc.toString()); assertEquals("Ascending heap size doesn't match", asc.length, hasc.size()); assertEquals("Descending heap size doesn't match", desc.length, hdesc.size()); for(int i = 0; i < asc.length; i++) { final Integer gota = hasc.poll(); assertEquals("Objects sorted incorrectly at ascending position " + i, asc[i], gota); } for(int i = 0; i < desc.length; i++) { final Integer gotd = hdesc.poll(); assertEquals("Objects sorted incorrectly at descending position " + i, desc[i], gotd); } } /** * Test bounded heap */ @Test public void testTiedTopBoundedHeapTrival() { Heap heap1 = new TiedTopBoundedHeap<>(1); Heap heap2 = new TiedTopBoundedHeap<>(1); Heap heap3 = new TiedTopBoundedHeap<>(1); Heap heap4 = new TiedTopBoundedHeap<>(1); Heap heap5 = new TiedTopBoundedHeap<>(1); heap2.add(2); heap4.add(0); for(int i = 0; i < 10; i++) { heap1.add(1); heap2.add(1); heap3.add(1); heap4.add(1); heap5.add(1); } heap3.add(2); heap5.add(0); assertEquals("First heap size doesn't match", 10, heap1.size()); assertEquals("Second heap size doesn't match", 1, heap2.size()); assertEquals("Third heap size doesn't match", 1, heap3.size()); assertEquals("Fourth heap size doesn't match", 10, heap4.size()); assertEquals("Fifth heap size doesn't match", 10, heap5.size()); } }