Update BinaryHeap tests.
This commit is contained in:
parent
20a64255aa
commit
b3aaaf8d10
1 changed files with 57 additions and 45 deletions
|
@ -1,68 +1,80 @@
|
||||||
package org.insa.utility;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.stream.IntStream;
|
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.BeforeEach;
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
|
||||||
|
|
||||||
public class BinaryHeapTest {
|
public class BinaryHeapTest {
|
||||||
|
|
||||||
private BinaryHeap<Integer> rangeHeap1;
|
private int[] data1 = IntStream.range(0, 20).toArray();
|
||||||
|
private int[] data2 = { 8, 1, 6, 3, 4, 5, 9 };
|
||||||
|
|
||||||
static IntStream dataRange1() {
|
private BinaryHeap<Integer> heap1, heap2;
|
||||||
return IntStream.range(0, 20);
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
static void initAll() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void init() {
|
void init() {
|
||||||
// Create the range heap
|
// Create the range heap
|
||||||
this.rangeHeap1 = new BinaryHeap<Integer>();
|
this.heap1 = new BinaryHeap<Integer>();
|
||||||
dataRange1().forEach((int x) -> rangeHeap1.insert(x));
|
this.heap2 = new BinaryHeap<Integer>();
|
||||||
|
|
||||||
|
for (int v: data1)
|
||||||
|
this.heap1.insert(v);
|
||||||
|
for (int v: data2)
|
||||||
|
this.heap2.insert(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testInsert() {
|
void testInsert() {
|
||||||
BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
|
BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
|
||||||
int size = 0;
|
int size = 0;
|
||||||
for (int x: dataRange1().toArray()) {
|
for (int x: data1) {
|
||||||
heap.insert(x);
|
heap.insert(x);
|
||||||
size += 1;
|
size += 1;
|
||||||
assertEquals(heap.size(), size);
|
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
|
@Test
|
||||||
void testDeleteMin() {
|
void testDeleteMin() {
|
||||||
int[] range1 = dataRange1().toArray();
|
// range 1 (sorted)
|
||||||
int size = range1.length;
|
int[] range1 = data1;
|
||||||
assertEquals(rangeHeap1.size(), size);
|
int size = range1.length;
|
||||||
for (int x: range1) {
|
assertEquals(heap1.size(), size);
|
||||||
assertEquals(rangeHeap1.deleteMin().intValue(), x);
|
for (int x: range1) {
|
||||||
size -= 1;
|
assertEquals(heap1.deleteMin().intValue(), x);
|
||||||
assertEquals(rangeHeap1.size(), size);
|
size -= 1;
|
||||||
}
|
assertEquals(heap1.size(), size);
|
||||||
}
|
}
|
||||||
|
assertEquals(heap1.size(), 0);
|
||||||
|
assertTrue(heap1.isEmpty());
|
||||||
|
|
||||||
@AfterEach
|
// range 2 (was not sorted)
|
||||||
void tearDown() {
|
int[] range2 = Arrays.copyOf(data2, data2.length);
|
||||||
}
|
Arrays.sort(range2);
|
||||||
|
size = range2.length;
|
||||||
@AfterAll
|
assertEquals(heap2.size(), size);
|
||||||
static void tearDownAll() {
|
for (int x: range2) {
|
||||||
|
assertEquals(heap2.deleteMin().intValue(), x);
|
||||||
|
size -= 1;
|
||||||
|
assertEquals(heap2.size(), size);
|
||||||
|
}
|
||||||
|
assertEquals(heap2.size(), 0);
|
||||||
|
assertTrue(heap2.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue