From 839a5f15d6a5dc1a01f5055303690ae8a63ad0b5 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 4 Oct 2026 15:43:45 +0200 Subject: listen: add simple test class --- Info/12/Listen/Funktionen/Test.java | 164 ++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 Info/12/Listen/Funktionen/Test.java (limited to 'Info') diff --git a/Info/12/Listen/Funktionen/Test.java b/Info/12/Listen/Funktionen/Test.java new file mode 100644 index 00000000..7f2f0427 --- /dev/null +++ b/Info/12/Listen/Funktionen/Test.java @@ -0,0 +1,164 @@ +@Test +class TestAdd { + @Test + void addToEmptyList() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.add(s1); + assertEquals(pl.first, s1, "Erster Song ist nicht s1"); + assertEquals(pl.last, s1, "Letzter Song ist nicht s1"); + assertEquals(s1.next, null, "s1 hat einen Nachfolger"); + } + + @Test + void addToSingleElementList() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; pl.last = s1; + Song s2 = new Song("2"); + pl.add(s2); + assertEquals(pl.first, s1, "Erster Song ist nicht s1"); + assertEquals(pl.last, s2, "Letzter Song ist nicht s2"); + assertEquals(s1.next, s2, "s1 hat nicht s2 als Nachfolger"); + assertEquals(s2.next, null, "s2 hat einen Nachfolger"); + } +} + +@Test +class TestPop { + @Test + void popFromEmptyList() { + Playlist pl = new Playlist(); + Song s = pl.pop(); + assertEquals(s, null, "Es wurde ein Song zurueckgegeben"); + } + + @Test + void popFromSingleElementList() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; pl.last = s1; + Song popped = pl.pop(); + + assertEquals(pl.first, null, "Erster Song ist nicht null"); + assertEquals(pl.last, null, "Letzter Song ist nicht null"); + assertEquals(popped, s1, "Entfernter Song ist nicht s1"); + } +} + +@Test +class TestGetLength { + @Test + void getLengthEmptyList() { + Playlist pl = new Playlist(); + assertEquals(pl.getLength(), 0, "Length ist nicht 0"); + } + + @Test + void getLengthSingleElementList() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; pl.last = s1; + + assertEquals(pl.getLength(), 1, "Length ist nicht 1"); + } + @Test + void getLengthTwoElementList() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; + Song s2 = new Song("2"); + s1.next = s2; + pl.last = s2; + + assertEquals(pl.getLength(), 2, "Length ist nicht 2"); + } +} + +@Test +class TestFind { + @Test + void findEmptyList() { + Playlist pl = new Playlist(); + assertEquals(pl.find("1"), null, "Song gefunden"); + } + + @Test + void findNotIncludedElement() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; + Song s2 = new Song("2"); + s1.next = s2; + pl.last = s2; + + assertEquals(pl.find("3"), null, "Song gefunden"); + } + + @Test + void findFirstElement() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; + Song s2 = new Song("2"); + s1.next = s2; + pl.last = s2; + + assertEquals(pl.find("1"), s1, "s1 nicht gefunden"); + } + + @Test + void findLastElement() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; + Song s2 = new Song("2"); + s1.next = s2; + pl.last = s2; + + assertEquals(pl.find("2"), s2, "s2 nicht gefunden"); + } +} + +@Test +class TestRemove { + @Test + void removeNotIncludedElement() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; + Song s2 = new Song("2"); + s1.next = s2; + pl.last = s2; + pl.remove("3"); + assertEquals(pl.first, s1, "Erstes Lied geloescht"); + assertEquals(pl.last, s2, "Letztes Lied geloescht"); + assertEquals(s1.next, s2, "s1 Nachfolger nicht s2"); + } + + @Test + void removeFirstElement() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; + Song s2 = new Song("2"); + s1.next = s2; + pl.last = s2; + pl.remove("1"); + assertEquals(pl.first, s2, "Erstes Lied nicht s2"); + } + + @Test + void findLastElement() { + Playlist pl = new Playlist(); + Song s1 = new Song("1"); + pl.first = s1; + Song s2 = new Song("2"); + s1.next = s2; + pl.last = s2; + pl.remove("2"); + assertEquals(pl.first, s1, "Erster Song nicht s1"); + assertEquals(pl.last, s1, "Letzter Song nicht s1"); + assertEquals(s1.next, null, "s1 hat einen Nachfolger"); + } +} -- cgit v1.2.3