From b3aaaf8d10036dd9ce315f252398d60851b4c5d3 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 25 Feb 2018 22:23:27 +0100 Subject: [PATCH] Update BinaryHeap tests. --- src/test/org/insa/utility/BinaryHeapTest.java | 102 ++++++++++-------- 1 file changed, 57 insertions(+), 45 deletions(-) diff --git a/src/test/org/insa/utility/BinaryHeapTest.java b/src/test/org/insa/utility/BinaryHeapTest.java index 88abc19..5a23ce5 100644 --- a/src/test/org/insa/utility/BinaryHeapTest.java +++ b/src/test/org/insa/utility/BinaryHeapTest.java @@ -1,68 +1,80 @@ package org.insa.utility; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import java.util.Arrays; import java.util.stream.IntStream; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; public class BinaryHeapTest { - - private BinaryHeap rangeHeap1; - - static IntStream dataRange1() { - return IntStream.range(0, 20); - } - - @BeforeAll - static void initAll() { - - } + + private int[] data1 = IntStream.range(0, 20).toArray(); + private int[] data2 = { 8, 1, 6, 3, 4, 5, 9 }; + + private BinaryHeap heap1, heap2; @BeforeEach void init() { - // Create the range heap - this.rangeHeap1 = new BinaryHeap(); - dataRange1().forEach((int x) -> rangeHeap1.insert(x)); + // Create the range heap + this.heap1 = new BinaryHeap(); + this.heap2 = new BinaryHeap(); + + for (int v: data1) + this.heap1.insert(v); + for (int v: data2) + this.heap2.insert(v); } @Test void testInsert() { - BinaryHeap heap = new BinaryHeap(); - int size = 0; - for (int x: dataRange1().toArray()) { - heap.insert(x); - size += 1; - assertEquals(heap.size(), size); - } + BinaryHeap heap = new BinaryHeap(); + int size = 0; + for (int x: data1) { + heap.insert(x); + size += 1; + assertEquals(heap.size(), size); + } + assertEquals(data1.length, heap.size()); + + heap = new BinaryHeap<>(); + size = 0; + for (int x: data2) { + heap.insert(x); + size += 1; + assertEquals(heap.size(), size); + } + assertEquals(data2.length, heap.size()); } - + @Test void testDeleteMin() { - int[] range1 = dataRange1().toArray(); - int size = range1.length; - assertEquals(rangeHeap1.size(), size); - for (int x: range1) { - assertEquals(rangeHeap1.deleteMin().intValue(), x); - size -= 1; - assertEquals(rangeHeap1.size(), size); - } + // range 1 (sorted) + int[] range1 = data1; + int size = range1.length; + assertEquals(heap1.size(), size); + for (int x: range1) { + assertEquals(heap1.deleteMin().intValue(), x); + size -= 1; + assertEquals(heap1.size(), size); + } + assertEquals(heap1.size(), 0); + assertTrue(heap1.isEmpty()); + + // range 2 (was not sorted) + int[] range2 = Arrays.copyOf(data2, data2.length); + Arrays.sort(range2); + size = range2.length; + assertEquals(heap2.size(), size); + for (int x: range2) { + assertEquals(heap2.deleteMin().intValue(), x); + size -= 1; + assertEquals(heap2.size(), size); + } + assertEquals(heap2.size(), 0); + assertTrue(heap2.isEmpty()); } - @AfterEach - void tearDown() { - } - - @AfterAll - static void tearDownAll() { - } - }