diff --git a/src/main/java/git/snippet/heap/Code_Heap.java b/src/main/java/git/snippet/heap/Code_Heap.java index 33d08fffc70e868974011ec03283811d7c329d88..f8ccfed980f3922451e8e2b2a89de5ebd82216f3 100644 --- a/src/main/java/git/snippet/heap/Code_Heap.java +++ b/src/main/java/git/snippet/heap/Code_Heap.java @@ -27,93 +27,6 @@ import java.util.List; // heapify和heapInsert都是logN级别的复杂度,因为N个节点的二叉树高度是logN public class Code_Heap { - public static void testHeap() { - int value = 1000; - int limit = 100; - int testTimes = 1000000; - for (int i = 0; i < testTimes; i++) { - int curLimit = (int) (Math.random() * limit) + 1; - MaxHeap my = new MaxHeap(curLimit); - RightMaxHeap test = new RightMaxHeap(curLimit); - int curOpTimes = (int) (Math.random() * limit); - for (int j = 0; j < curOpTimes; j++) { - if (my.isEmpty() != test.isEmpty()) { - System.out.println("Oops!"); - } - if (my.isFull() != test.isFull()) { - System.out.println("Oops!"); - } - if (my.isEmpty()) { - int curValue = (int) (Math.random() * value); - my.push(curValue); - test.push(curValue); - } else if (my.isFull()) { - if (my.pop() != test.pop()) { - System.out.println("Oops!"); - } - } else { - if (Math.random() < 0.5) { - int curValue = (int) (Math.random() * value); - my.push(curValue); - test.push(curValue); - } else { - if (my.pop() != test.pop()) { - System.out.println("Oops!"); - } - } - } - } - } - System.out.println("finish!"); - } - - // 加强堆测试 - public static void testGreaterHeap() { - int value = 1000; - int limit = 100; - int testTimes = 1000000; - for (int i = 0; i < testTimes; i++) { - int curLimit = (int) (Math.random() * limit) + 1; - HeapGreater my = - new HeapGreater<>( - new Comparator() { - @Override - public int compare(Integer o1, Integer o2) { - return o2 - o1; - } - }); - RightMaxHeap test = new RightMaxHeap(curLimit); - int curOpTimes = (int) (Math.random() * limit); - for (int j = 0; j < curOpTimes; j++) { - if (test.isEmpty()) { - int curValue = (int) (Math.random() * value); - my.push(curValue); - test.push(curValue); - } else if (test.isFull()) { - if (my.pop() != test.pop()) { - System.out.println("Oops!"); - } - } else { - if (Math.random() < 0.5) { - int curValue = (int) (Math.random() * value); - my.push(curValue); - test.push(curValue); - } else { - if (my.pop() != test.pop()) { - System.out.println("Oops!"); - } - } - } - } - } - System.out.println("finish!"); - } - - public static void main(String[] args) { - testHeap(); - testGreaterHeap(); - } - public static class MaxHeap { private final int[] heap; // private final int limit; limit == heap.length diff --git a/src/test/java/git/snippet/heap/Code_HeapTest.java b/src/test/java/git/snippet/heap/Code_HeapTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8d831f42b068d1896e56d9a98c6226b2b9e069a5 --- /dev/null +++ b/src/test/java/git/snippet/heap/Code_HeapTest.java @@ -0,0 +1,88 @@ +package git.snippet.heap; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("堆结构测试") +public class Code_HeapTest { + @Test + @DisplayName("堆结构测试") + public void testHeap() { + int value = 1000; + int limit = 100; + int testTimes = 1000000; + for (int i = 0; i < testTimes; i++) { + int curLimit = (int) (Math.random() * limit) + 1; + Code_Heap.MaxHeap my = new Code_Heap.MaxHeap(curLimit); + Code_Heap.RightMaxHeap test = new Code_Heap.RightMaxHeap(curLimit); + int curOpTimes = (int) (Math.random() * limit); + for (int j = 0; j < curOpTimes; j++) { + if (my.isEmpty() != test.isEmpty()) { + Assertions.fail(); + } + if (my.isFull() != test.isFull()) { + Assertions.fail(); + } + if (my.isEmpty()) { + int curValue = (int) (Math.random() * value); + my.push(curValue); + test.push(curValue); + } else if (my.isFull()) { + if (my.pop() != test.pop()) { + Assertions.fail(); + } + } else { + if (Math.random() < 0.5) { + int curValue = (int) (Math.random() * value); + my.push(curValue); + test.push(curValue); + } else { + if (my.pop() != test.pop()) { + Assertions.fail(); + } + } + } + } + } + } + + // 加强堆测试 + @Test + @DisplayName("加强堆结构测试") + public void testGreaterHeap() { + int value = 1000; + int limit = 100; + int testTimes = 1000000; + for (int i = 0; i < testTimes; i++) { + int curLimit = (int) (Math.random() * limit) + 1; + Code_Heap.HeapGreater my = + new Code_Heap.HeapGreater<>( + (o1, o2) -> o2 - o1); + Code_Heap.RightMaxHeap test = new Code_Heap.RightMaxHeap(curLimit); + int curOpTimes = (int) (Math.random() * limit); + for (int j = 0; j < curOpTimes; j++) { + if (test.isEmpty()) { + int curValue = (int) (Math.random() * value); + my.push(curValue); + test.push(curValue); + } else if (test.isFull()) { + if (my.pop() != test.pop()) { + Assertions.fail(); + } + } else { + if (Math.random() < 0.5) { + int curValue = (int) (Math.random() * value); + my.push(curValue); + test.push(curValue); + } else { + if (my.pop() != test.pop()) { + Assertions.fail(); + } + } + } + } + } + } + +} \ No newline at end of file