From 84a9c18a14767272ad9931a3dd6d357bec3725e6 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 13 Apr 2018 11:36:53 +0200 Subject: [PATCH] Praktikum 2: Offiziele Testklasse --- .../praktikum/praktikum2/CharCollection.java | 9 +- .../hs/java2/tests/CharCollectionTest2.java | 113 ++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 src/test/java/de/joethei/hs/java2/tests/CharCollectionTest2.java diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java index bf0a25f..af6a951 100644 --- a/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java @@ -82,10 +82,10 @@ public class CharCollection implements Cloneable{ } public boolean isSubset(CharCollection cc) { - ArrayList list2 = new ArrayList<>(this.list); + ArrayList list = new ArrayList<>(this.list); for(char c:cc.list) { - if(!list2.contains(c)) return false; - else list2.remove(list2.indexOf(c)); + if(!list.contains(c)) return false; + else list.remove(list.indexOf(c)); } return true; } @@ -99,7 +99,8 @@ public class CharCollection implements Cloneable{ public boolean equals(Object o) { if(o instanceof CharCollection) { CharCollection c = (CharCollection) o; - return this.list.containsAll(c.list); + if(this.size() != c.size()) return false; + return this.isSubset(c); }return false; } diff --git a/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest2.java b/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest2.java new file mode 100644 index 0000000..28a5c1e --- /dev/null +++ b/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest2.java @@ -0,0 +1,113 @@ +package de.joethei.hs.java2.tests; + +import de.joethei.hs.java2.praktikum.praktikum2.CharCollection; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +public class CharCollectionTest2 { + + CharCollection a, b, c, d; + + @Before + public void setUp() + { + a = new CharCollection('A', 'N', 'A', 'N', 'A', 'S'); + b = new CharCollection('H', 'O', 'C', 'H', 'S', 'C', 'H', 'U', 'L', 'E'); + c = new CharCollection("AAANNS"); + d = new CharCollection(); + } + + @Test + public void Laenge(){ + assertEquals(6,a.size()); + assertEquals(10,b.size()); + assertEquals(6,c.size()); + assertEquals(0,d.size()); + } + + @Test + public void Anzahl(){ + assertEquals(2,a.count('N')); + assertEquals(0,a.count('X')); + assertEquals(3,b.count('H')); + assertEquals(1,c.count('S')); + assertEquals(0,d.count('H')); + } + + @Test + public void Verschieden(){ + assertEquals(3,a.different()); + assertEquals(7,b.different()); + assertEquals(3,c.different()); + assertEquals(0,d.different()); + } + + @Test + public void Haeufigster(){ + assertEquals('A',a.top()); + assertEquals('H',b.top()); + assertEquals('A',c.top()); + assertEquals(0,d.top()); + } + + @Test + public void Vergleich(){ + assertTrue(a.equals(c)); + assertTrue(b.equals(new CharCollection("CCEHHHLOSU"))); + assertFalse(b.equals(new CharCollection("CEHLOSU"))); + assertTrue(d.equals(new CharCollection(""))); + } + + @Test + public void Ausgabe(){ + assertEquals(a.toString().length(),c.toString().length()); + assertEquals(b.toString().length(),new CharCollection("CCEHHHLOSU").toString().length()); + assertEquals("()",d.toString()); + + char[] aa = a.toString().toCharArray(); + char[] ca = c.toString().toCharArray(); + Arrays.sort(aa); + Arrays.sort(ca); + assertTrue(Arrays.equals(aa, ca)); + + char[] ba = b.toString().toCharArray(); + char[] b2a = "(C, C, H, H, H, L, O, E, S, U)".toCharArray(); + Arrays.sort(ba); + Arrays.sort(b2a); + assertTrue(Arrays.equals(ba, b2a)); + } + + @Test + public void Mindestens(){ + assertTrue(a.moreThan(1).equals(new CharCollection("AAANN"))); + assertTrue(a.moreThan(2).equals(new CharCollection("AAA"))); + assertEquals("(A, A, A)",a.moreThan(2).toString()); + assertTrue(b.moreThan(1).equals(new CharCollection("CCHHH"))); + assertTrue(b.moreThan(3).equals(new CharCollection(""))); + assertEquals("()",b.moreThan(3).toString()); + } + + @Test + public void Differenz(){ + assertTrue(a.except(new CharCollection("NASE")).equals(new CharCollection("AAN"))); + assertTrue(a.except(new CharCollection("KIWI")).equals(new CharCollection("ANANAS"))); + assertTrue(a.except(new CharCollection()).equals(new CharCollection("ANANAS"))); + assertTrue(a.except(new CharCollection("ANANAS")).equals(new CharCollection())); + assertTrue(b.except(new CharCollection("CHHO")).equals(new CharCollection("SCHULE"))); + assertTrue(c.except(new CharCollection("HOSIANNA")).equals(new CharCollection('A'))); + assertTrue(d.except(new CharCollection("ABCD")).equals(new CharCollection())); + } + + @Test + public void Untermenge(){ + assertTrue(a.isSubset(new CharCollection("ANANAS"))); + assertTrue(a.isSubset(new CharCollection("NASA"))); + assertTrue(a.isSubset(new CharCollection(""))); + assertFalse(a.isSubset(new CharCollection("NASE"))); + assertFalse(a.isSubset(new CharCollection("AAAA"))); + } +}