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

Code cleanup of some older exercises #60

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 79 additions & 82 deletions GAD 2 - Binaere Suche/tests/binarysearch/BinSeaTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,120 +9,117 @@

public class BinSeaTester {

/**
* Tests from examples from exercise 1
*/
@Test
public void aufgabe1() {
/**
* Tests from examples from exercise 1.
*/
@Test
public void aufgabe1() {
int[] array = new int[]{2, 7, 7, 42, 69, 1337, 2000, 9001};

int[] array = new int[] { 2, 7, 7, 42, 69, 1337, 2000, 9001};
TestResult result = new TestResult();

TestResult result = new TestResult();
int index = gad.binarysearch.BinSea.search(array, 7, result);

int index = gad.binarysearch.BinSea.search(array,7,result);
List<Integer> resList = result.getSteps();
assertEquals(7, array[index], "Found wrong index");
assertFalse(resList.size() < 2, "Logged not enough Steps.");
assertFalse(resList.size() > 2, "Logged too many Steps.");
assertArrayEquals(new Integer[]{3, 1}, resList.toArray(new Integer[resList.size()]), "Logged wrong steps");

List<Integer> resList = result.getSteps();
assertEquals(7, array[index], "Found wrong index");
assertFalse(resList.size()<2,"Logged not enough Steps.");
assertFalse(resList.size()>2,"Logged too many Steps.");
assertArrayEquals(new Integer[]{3,1},resList.toArray(new Integer[resList.size()]), "Logged wrong steps");
result = new TestResult();

result = new TestResult();
index = gad.binarysearch.BinSea.search(array, 100, result);

index = gad.binarysearch.BinSea.search(array,100,result);
resList = result.getSteps();
assertTrue(index == 4 || index == 5, "Found wrong index");
assertFalse(resList.size() < 2, "Logged not enough Steps.");
assertFalse(resList.size() > 2, "Logged too many Steps.");
assertArrayEquals(new Integer[]{3, 5}, resList.toArray(new Integer[resList.size()]), "Logged wrong steps");

resList = result.getSteps();
assertTrue(index == 4 || index == 5, "Found wrong index");
assertFalse(resList.size()<2,"Logged not enough Steps.");
assertFalse(resList.size()>2,"Logged too many Steps.");
assertArrayEquals(new Integer[]{3,5},resList.toArray(new Integer[resList.size()]), "Logged wrong steps");
}

}
/**
* Tests from examples from exercise 2.
*/
@Test
public void aufgabe2() {
int[] array = new int[]{2, 7, 7, 42, 69, 1337, 2000, 9001};

/**
* Tests from examples from exercise 2
*/
@Test
public void aufgabe2() {
TestResult result = new TestResult();

int[] array = new int[] { 2, 7, 7, 42, 69, 1337, 2000, 9001};
int index = gad.binarysearch.BinSea.search(array, 7, false, result);

TestResult result = new TestResult();
List<Integer> resList = result.getSteps();

int index = gad.binarysearch.BinSea.search(array,7, false, result);
assertEquals(2, index, "Found wrong index");
assertFalse(resList.size() < 2, "Logged not enough Steps.");
assertFalse(resList.size() > 2, "Logged too many Steps.");
assertArrayEquals(new Integer[]{3, 1}, resList.toArray(new Integer[resList.size()]), "Logged wrong steps");

List<Integer> resList = result.getSteps();
result = new TestResult();

assertEquals(2,index, "Found wrong index");
assertFalse(resList.size()<2,"Logged not enough Steps.");
assertFalse(resList.size()>2,"Logged too many Steps.");
assertArrayEquals(new Integer[]{3,1},resList.toArray(new Integer[resList.size()]), "Logged wrong steps");
index = gad.binarysearch.BinSea.search(array, 100, true, result);

result = new TestResult();
resList = result.getSteps();

index = gad.binarysearch.BinSea.search(array,100, true,result);
assertEquals(5, index, "Found wrong index");
assertFalse(resList.size() < 2, "Logged not enough Steps.");
assertFalse(resList.size() > 2, "Logged too many Steps.");
assertArrayEquals(new Integer[]{3, 5}, resList.toArray(new Integer[resList.size()]), "Logged wrong steps");

resList = result.getSteps();
result = new TestResult();

assertEquals(5,index, "Found wrong index");
assertFalse(resList.size()<2,"Logged not enough Steps.");
assertFalse(resList.size()>2,"Logged too many Steps.");
assertArrayEquals(new Integer[]{3,5},resList.toArray(new Integer[resList.size()]), "Logged wrong steps");
index = gad.binarysearch.BinSea.search(array, 1, false, result);

result = new TestResult();
resList = result.getSteps();

index = gad.binarysearch.BinSea.search(array, 1, false,result);
assertEquals(-1, index, "Found wrong index");
assertFalse(resList.size() < 2, "Logged not enough Steps.");
assertFalse(resList.size() > 2, "Logged too many Steps.");
assertArrayEquals(new Integer[]{3, 1}, resList.toArray(new Integer[resList.size()]), "Logged wrong steps");
}

resList = result.getSteps();
/**
* Tests from examples from exercise 3.
*/
@Test
public void aufgabe3() {
int[] array = new int[]{2, 7, 7, 42, 69, 1337, 2000, 9001};

assertEquals(-1,index, "Found wrong index");
assertFalse(resList.size()<2,"Logged not enough Steps.");
assertFalse(resList.size()>2,"Logged too many Steps.");
assertArrayEquals(new Integer[]{3,1},resList.toArray(new Integer[resList.size()]), "Logged wrong steps");
}
TestResult highResult = new TestResult();
TestResult lowResult = new TestResult();

/**
* Tests from examples from exercise 3
*/
@Test
public void aufgabe3() {
Interval interval = gad.binarysearch.BinSea.search(
array, Interval.fromArrayIndices(7, 1500), lowResult, highResult);

int[] array = new int[] { 2, 7, 7, 42, 69, 1337, 2000, 9001};
List<Integer> resList = lowResult.getSteps();

TestResult highResult = new TestResult();
TestResult lowResult = new TestResult();
assertEquals(Interval.fromArrayIndices(1, 5), interval, "Found wrong Interval");
assertFalse(resList.size() < 2, "Logged not enough Steps.");
assertFalse(resList.size() > 2, "Logged too many Steps.");
assertArrayEquals(new Integer[]{3, 1}, resList.toArray(new Integer[resList.size()]), "Logged wrong steps");

Interval interval = gad.binarysearch.BinSea.search(array,Interval.fromArrayIndices(7,1500),lowResult,highResult);
resList = highResult.getSteps();

List<Integer> resList = lowResult.getSteps();
assertFalse(resList.size() > 3, "Logged too manny steps");
assertFalse(resList.size() < 3, "Logged not enough steps");
assertArrayEquals(new Integer[]{3, 5, 6}, resList.toArray(new Integer[3]), "Logged wrong steps");

assertEquals(Interval.fromArrayIndices(1,5),interval, "Found wrong Interval");
assertFalse(resList.size()<2,"Logged not enough Steps.");
assertFalse(resList.size()>2,"Logged too many Steps.");
assertArrayEquals(new Integer[]{3,1},resList.toArray(new Integer[resList.size()]), "Logged wrong steps");

resList = highResult.getSteps();
highResult = new TestResult();
lowResult = new TestResult();

assertFalse(resList.size()>3, "Logged too manny steps");
assertFalse(resList.size()<3, "Logged not enough steps");
assertArrayEquals(new Integer[]{3,5,6}, resList.toArray(new Integer[3]), "Logged wrong steps");
interval = gad.binarysearch.BinSea.search(array, Interval.fromArrayIndices(9002, 10000), lowResult, highResult);

resList = lowResult.getSteps();

highResult = new TestResult();
lowResult = new TestResult();
assertEquals(Interval.EmptyInterval.getEmptyInterval(), interval, "Found wrong interval");
assertFalse(resList.size() > 3, "Logged too manny steps");
assertFalse(resList.size() < 3, "Logged not enough steps");
assertArrayEquals(new Integer[]{3, 5, 6}, resList.toArray(new Integer[3]), "Logged wrong steps");

interval = gad.binarysearch.BinSea.search(array, Interval.fromArrayIndices(9002,10000), lowResult, highResult);
resList = highResult.getSteps();

resList = lowResult.getSteps();

assertEquals(Interval.EmptyInterval.getEmptyInterval(), interval, "Found wrong interval");
assertFalse(resList.size()>3, "Logged too manny steps");
assertFalse(resList.size()<3, "Logged not enough steps");
assertArrayEquals(new Integer[]{3,5,6}, resList.toArray(new Integer[3]), "Logged wrong steps");

resList = highResult.getSteps();

assertEquals(0, resList.size(), "Logged too many steps");

}
assertEquals(0, resList.size(), "Logged too many steps");
}
}
22 changes: 11 additions & 11 deletions GAD 2 - Binaere Suche/tests/binarysearch/TestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

public class TestResult implements Result {

private List<Integer> steps;
private List<Integer> steps;

public TestResult() {
this.steps = new ArrayList<>();
}
public TestResult() {
this.steps = new ArrayList<>();
}

@Override
public void addStep(int index) {
steps.add(index);
}
@Override
public void addStep(int index) {
steps.add(index);
}

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