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 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_MaxHeap my = new Code_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()) { 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(); } } } } } } }