-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapTester.java
More file actions
35 lines (32 loc) · 822 Bytes
/
HeapTester.java
File metadata and controls
35 lines (32 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import java.util.*;
public class HeapTester {
private void permute (int[] array) {
final Random random = new Random();
for (int i = array.length - 1; i >= 0; i--) {
final int j = random.nextInt(i+1);
final int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
@Test
public void testShuffled () {
final int N = 1000;
final int[] numbers = new int[N];
for (int i = 0; i < N; i++) {
numbers[i] = i;
}
permute(numbers);
final HeapImpl<Integer> heap = new HeapImpl<Integer>();
for (int i = 0; i < N; i++) {
heap.add(numbers[i]);
}
assertEquals(N, heap.size());
for (int i = N-1; i >= 0; i--) {
assertEquals((Integer) i, heap.removeFirst());
}
assertEquals(0, heap.size());
}
}