From cfe533f03ba14e8d74fc4bd294c71978d650d305 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 28 Sep 2022 10:34:51 +0200 Subject: [PATCH] Add tests for the `MySortedList` class --- .../java/ch/nuth/zhaw/exbox/ADS2_4_test.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 app/src/test/java/ch/nuth/zhaw/exbox/ADS2_4_test.java diff --git a/app/src/test/java/ch/nuth/zhaw/exbox/ADS2_4_test.java b/app/src/test/java/ch/nuth/zhaw/exbox/ADS2_4_test.java new file mode 100644 index 0000000..2e9f7a1 --- /dev/null +++ b/app/src/test/java/ch/nuth/zhaw/exbox/ADS2_4_test.java @@ -0,0 +1,80 @@ +package ch.nuth.zhaw.exbox; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @(#)ListTest.java + * + * + * @author + * @version 1.00 2017/8/30 + */ +public class ADS2_4_test { + MySortedList list; + + @BeforeEach + public void setUp() { + list = new MySortedList(); + } + + @Test + public void testAdd() { + list.clear(); + list.add("A"); + Object o = list.get(0); + assertEquals("A", o); + } + + @Test + public void testAdd2() { + list.clear(); + list.add("B"); + list.add("A"); + assertEquals("A", list.get(0)); + assertEquals("B", list.get(1)); + } + + @Test + public void testAdd3() { + list.clear(); + list.add("C"); + list.add("B"); + list.add("A"); + assertEquals("A", list.get(0)); + assertEquals("B", list.get(1)); + assertEquals("C", list.get(2)); + } + + @Test + public void testMixed() { + List list2 = new LinkedList<>(); + for (int i = 0; i < 100; i++) { + Character c = (char) ('A' + (Math.random()*26)); + int op = (int)(Math.random()*2); + switch (op) { + case 0: + list.add(c); + list2.add(c); + break; + case 1: + list.remove(c); + list2.remove(c); + break; + } + } + Collections.sort(list2); + assertEquals(list2.size(), list.size()); + for (int i = 0; i < list.size(); i++) { + char c1 = (char)list.get(i); + char c2 = (char)list2.get(i); + assertEquals(c1, c2); + } + } +}