Erster JUnitTest

main
student 2026-04-01 12:41:08 +02:00
parent 0dc80b3b47
commit 3cfcc9d9c1
3 changed files with 61 additions and 6 deletions

View File

@ -1,6 +1,7 @@
public class Knoten {
private static final Exception IndexOutOfBoundsException = null;
private int wert;
private Knoten nachfolger;
@ -19,13 +20,41 @@ public class Knoten {
public void auslesenAnPos(int pos) {
if(pos >0 && this.nachfolger != null) {
nachfolger.auslesenAnPos(pos--);
public int auslesenAnPos(int pos) throws Exception {
pos--;
if(nachfolger==null && pos>1) {
throw IndexOutOfBoundsException;
}
if(pos >0 ) {
nachfolger.auslesenAnPos(pos);
}
else {
return this.wert;
}
return 0;
}
public void loeschenAnPos(int pos) throws Exception{
if(this.nachfolger==null) {
throw IndexOutOfBoundsException;
}
if(pos>1) {
this.nachfolger = nachfolger.nachfolger;
}else {
System.out.println(this.wert);
pos --;
}
}
public int getWert() {return this.wert;}
// End of Class
}

View File

@ -11,9 +11,9 @@ public class LinkedList {
kopf.einfuegenAmEnde(wert);
}
public void auslesenAnPos(int pos) throws Exception {
public int auslesenAnPos(int pos) throws Exception {
kopf.auslesenAnPos(pos);
return kopf.auslesenAnPos(pos);
}
public void loeschenAnPos(int pos) throws Exception {

View File

@ -0,0 +1,26 @@
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Assert;
import org.junit.function.ThrowingRunnable;
import org.junit.jupiter.api.Test;
class LinkedListTest {
private static final ThrowingRunnable IndexOutOfBoundsException = null;
@Test
void test() throws Exception {
LinkedList L = new LinkedList(11);
L.einfuegenAmEnde(10);
L.einfuegenAmEnde(9);
L.einfuegenAmEnde(8);
L.einfuegenAmEnde(7);
LinkedList R = new LinkedList(10);
R.einfuegenAmEnde(3);
Assert.assertEquals(3, R.auslesenAnPos(2));
}
}