diff --git a/GAD 8 - Binomial Heap/tests/binomilia/BinomialHeapTester.java b/GAD 8 - Binomial Heap/tests/binomilia/BinomialHeapTester.java index da4539a..c9953b6 100644 --- a/GAD 8 - Binomial Heap/tests/binomilia/BinomialHeapTester.java +++ b/GAD 8 - Binomial Heap/tests/binomilia/BinomialHeapTester.java @@ -24,110 +24,108 @@ public class BinomialHeapTester { - private BinomialHeap heap; - private StudentResult result; - - @BeforeEach - void init() { - - heap = new BinomialHeap(); - result = new StudentResult(); - } - - @Test - void testIllegal() { - assertThrows(RuntimeException.class, () -> heap.min(), - "Please throw an exception when heap is empty, since there cant be a minimal element in an empty heap. (NoSuchElementExeption would be cool)"); - assertThrows(RuntimeException.class, () -> heap.deleteMin(result), - "Please throw an exception when heap is empty, since deleteMin() is illegal on an empty heap. (NoSuchElementExeption would be cool)"); - } - - @Test - void testMin() { - for (int i = 0; i < 500; i++) { - heap.insert(i, result); - assertEquals(0, heap.min(), "Wrong value for min!"); - } - } - - @Test - void testDeleteMin() { - List nums = new ArrayList<>(); - for (int i = 0; i < 500; i++) { - heap.insert(i, result); - nums.add(i); - } - - for (int x : nums) { - assertEquals(x, heap.min(), "Wrong value for min!"); - int deleted = heap.deleteMin(result); - assertEquals(x, deleted, - "Either did not delete the smallest number or the expected number wasnt in the heap"); - } - } - - @Test - void testBigNumbers() { - List nums = new ArrayList<>(); - for (int i = 0; i < 1000; i++) { - int num = new Random().nextInt(); - nums.add(num); - heap.insert(num, result); - int minList = nums.stream().min((x, y) -> Integer.compare(x, y)).get(); - int minHeap = heap.min(); - assertEquals(minList, minHeap, "Wrong minimum " + i); - } - for (int i = 0; i < 500; i++) { - int num = new Random().nextInt(); - nums.add(num); - heap.insert(num, result); - Integer min = nums.stream().min((x, y) -> Integer.compare(x, y)).get(); - nums.remove(min); - assertEquals(min, heap.deleteMin(result), "Wrong number removed " + i); - } - } - - @Test - void testAmountOFTrees() { - List nums = new ArrayList<>(); - TreeCounterResult myResult = new TreeCounterResult(); - - for (int i = 0; i < 500; i++) { - int num = new Random().nextInt(); - nums.add(num); - heap.insert(num, myResult); - int expectedSize = Integer.bitCount(nums.size()); - int trees = myResult.getSize(); - assertEquals(expectedSize, trees, - "Wrong amount of Trees in heap. If only this test fails, check if there is an Integer-Overflow, since these Numbers are pretty big."); - } - } - - @Test - void zwischenSchritte() { - StepCounterResult myResult = new StepCounterResult(); - - for (int i = 0; i < 500; i++) { - int num = new Random().nextInt(); - heap.insert(num, myResult); - for (int j = 1; j < myResult.getSteps().size(); j++) { - if (myResult.getSteps().get(j) + 1 != myResult.getSteps().get(j - 1)) { - fail("Es wurden nicht genau zwei Bäume zu einem gemergt. Stattdessen wurden in einem Zwischenschritt " - + (myResult.getSteps().get(j - 1) - myResult.getSteps().get(j)) - + " vorhandene Bäume entfernt, anstatt genau einem"); - } - } - } - - for (int i = 0; i < 500; i++) { - heap.deleteMin(myResult); - for (int j = 1; j < myResult.getSteps().size(); j++) { - if (myResult.getSteps().get(j) + 1 != myResult.getSteps().get(j - 1)) { - fail("Es wurden nicht genau zwei Bäume zu einem gemergt. Stattdessen wurden in einem Zwischenschritt " - + (myResult.getSteps().get(j - 1) - myResult.getSteps().get(j)) - + " vorhandene Bäume entfernt, anstatt genau einem"); - } - } - } - } + private BinomialHeap heap; + private StudentResult result; + + @BeforeEach + void init() { + + heap = new BinomialHeap(); + result = new StudentResult(); + } + + @Test + void testIllegal() { + assertThrows(RuntimeException.class, () -> heap.min(), "Please throw an exception when heap is empty, since " + + "there cant be a minimal element in an empty heap. (NoSuchElementExeption would be cool)"); + assertThrows(RuntimeException.class, () -> heap.deleteMin(result), "Please throw an exception when heap is " + + "empty, since deleteMin() is illegal on an empty heap. (NoSuchElementExeption would be cool)"); + } + + @Test + void testMin() { + for (int i = 0; i < 500; i++) { + heap.insert(i, result); + assertEquals(0, heap.min(), "Wrong value for min!"); + } + } + + @Test + void testDeleteMin() { + List nums = new ArrayList<>(); + for (int i = 0; i < 500; i++) { + heap.insert(i, result); + nums.add(i); + } + + for (int x : nums) { + assertEquals(x, heap.min(), "Wrong value for min!"); + int deleted = heap.deleteMin(result); + assertEquals(x, deleted, "Either did not delete the smallest number or the expected number wasnt in the " + + "heap"); + } + } + + @Test + void testBigNumbers() { + List nums = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + int num = new Random().nextInt(); + nums.add(num); + heap.insert(num, result); + int minList = nums.stream().min((x, y) -> Integer.compare(x, y)).get(); + int minHeap = heap.min(); + assertEquals(minList, minHeap, "Wrong minimum " + i); + } + for (int i = 0; i < 500; i++) { + int num = new Random().nextInt(); + nums.add(num); + heap.insert(num, result); + Integer min = nums.stream().min((x, y) -> Integer.compare(x, y)).get(); + nums.remove(min); + assertEquals(min, heap.deleteMin(result), "Wrong number removed " + i); + } + } + + @Test + void testAmountOFTrees() { + List nums = new ArrayList<>(); + TreeCounterResult myResult = new TreeCounterResult(); + + for (int i = 0; i < 500; i++) { + int num = new Random().nextInt(); + nums.add(num); + heap.insert(num, myResult); + int expectedSize = Integer.bitCount(nums.size()); + int trees = myResult.getSize(); + assertEquals(expectedSize, trees, "Wrong amount of Trees in heap. If only this test fails, check if there" + + " is an Integer-Overflow, since these Numbers are pretty big."); + } + } + + @Test + void zwischenSchritte() { + StepCounterResult myResult = new StepCounterResult(); + + for (int i = 0; i < 500; i++) { + int num = new Random().nextInt(); + heap.insert(num, myResult); + for (int j = 1; j < myResult.getSteps().size(); j++) { + if (myResult.getSteps().get(j) + 1 != myResult.getSteps().get(j - 1)) { + fail("Es wurden nicht genau zwei Bäume zu einem gemergt. Stattdessen wurden in einem " + + "Zwischenschritt " + (myResult.getSteps().get(j - 1) - myResult.getSteps().get(j)) + " " + + "vorhandene Bäume entfernt, anstatt genau einem"); + } + } + } + + for (int i = 0; i < 500; i++) { + heap.deleteMin(myResult); + for (int j = 1; j < myResult.getSteps().size(); j++) { + if (myResult.getSteps().get(j) + 1 != myResult.getSteps().get(j - 1)) { + fail("Es wurden nicht genau zwei Bäume zu einem gemergt. Stattdessen wurden in einem Zwischenschritt " + (myResult.getSteps().get(j - 1) - myResult.getSteps().get(j)) + " vorhandene Bäume entfernt, anstatt genau einem"); + } + } + } + } }