Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

fixed all requests #57

Merged
merged 9 commits into from
Jun 23, 2021
Merged
131 changes: 131 additions & 0 deletions GAD 8 - Binomial Heap/tests/binomilia/BinomialHeapTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package tests.binomilia;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import gad.binomilia.*;

/**
*@author Mihir Mahajan
*
* These tests only test if your BinomialHeap/BinomialTree itself works.
* They don't test the logging. Still, they may not work if the logging
* is incorrect!
*
* I can not guarantee the correctness or accuracy of these tests. I
* hope they help, enjoy!
*/

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<Integer> 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<Integer> 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<Integer> 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");
}
}
}
}
}
74 changes: 74 additions & 0 deletions GAD 8 - Binomial Heap/tests/binomilia/StepCounterResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tests.binomilia;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import gad.binomilia.*;

public class StepCounterResult implements Result {
private List<Integer> steps = new ArrayList<>();

@Override
public void startInsert(int value, Collection<BinomialTreeNode> heap) {
steps.clear();
}

@Override
public void startInsert(int value, BinomialTreeNode[] heap) {
steps.clear();
}

@Override
public void startDeleteMin(Collection<BinomialTreeNode> heap) {
steps.clear();
}

@Override
public void startDeleteMin(BinomialTreeNode[] heap) {
steps.clear();
}

@Override
public void logIntermediateStep(Collection<BinomialTreeNode> heap) {
// heap.size() should return the amount of trees in the heap. So it should
// return the size of the Collection where u save the trees. If you do it
// another way, this test may fail
steps.add(heap.size());
}

@Override
public void logIntermediateStep(BinomialTreeNode[] heap) {
logIntermediateStep(Arrays.stream(heap).toList());

}

@Override
public void logIntermediateStep(BinomialTreeNode tree) {
logIntermediateStep(List.of(tree));

}

@Override
public void addToIntermediateStep(Collection<BinomialTreeNode> heap) {
int tmp = steps.remove(steps.size() - 1);
tmp += heap.size();
steps.add(tmp);
}

@Override
public void addToIntermediateStep(BinomialTreeNode[] heap) {
addToIntermediateStep(Arrays.stream(heap).toList());
}

@Override
public void addToIntermediateStep(BinomialTreeNode tree) {
addToIntermediateStep(List.of(tree));
}

public List<Integer> getSteps() {
return steps;
}

}
68 changes: 68 additions & 0 deletions GAD 8 - Binomial Heap/tests/binomilia/TreeCounterResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package tests.binomilia;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import gad.binomilia.*;

public class TreeCounterResult implements Result {

private int size;

@Override
public void startInsert(int value, Collection<BinomialTreeNode> heap) {
}

@Override
public void startInsert(int value, BinomialTreeNode[] heap) {
}

@Override
public void startDeleteMin(Collection<BinomialTreeNode> heap) {
}

@Override
public void startDeleteMin(BinomialTreeNode[] heap) {
}

@Override
public void logIntermediateStep(Collection<BinomialTreeNode> heap) {
// heap.size() should return the amount of trees in the heap. So it should
// return the size of the Collection where u save the trees. If you do it
// another way, this test may fail
size = heap.size();
}

@Override
public void logIntermediateStep(BinomialTreeNode[] heap) {
logIntermediateStep(Arrays.stream(heap).toList());

}

@Override
public void logIntermediateStep(BinomialTreeNode tree) {
logIntermediateStep(List.of(tree));

}

@Override
public void addToIntermediateStep(Collection<BinomialTreeNode> heap) {
size += heap.size();
}

@Override
public void addToIntermediateStep(BinomialTreeNode[] heap) {
addToIntermediateStep(Arrays.stream(heap).toList());
}

@Override
public void addToIntermediateStep(BinomialTreeNode tree) {
addToIntermediateStep(List.of(tree));
}

public int getSize() {
return size;
}

}