Compare commits

...

9 Commits

  1. 7
      SimpleActivityStack/.classpath
  2. 17
      SimpleActivityStack/.project
  3. BIN
      SimpleActivityStack/2018-WS Testat-90.pdf
  4. 41
      SimpleActivityStack/src/solution/SimpleActivityStack.java
  5. 14
      SimpleActivityStack/src/solution/SimpleActivityStackTest.java
  6. 64
      SimpleActivityStack/src/solution/SimpleStack.java
  7. 13
      SimpleActivityStack/src/solution/SimpleStackTest.java
  8. 58
      SimpleActivityStack/src/testat/ASimpleActivityStackTest.java
  9. 41
      SimpleActivityStack/src/testat/ASimpleStackTest.java
  10. 289
      SimpleActivityStack/src/testat/ASimpleStackTestBase.java
  11. 11
      SimpleActivityStack/src/testat/IAction.java
  12. 66
      SimpleActivityStack/src/testat/ISimpleStack.java
  13. 56
      SimpleActivityStack/src/testat/SimpleActivity.java
  14. 7
      SimplePriorityQueue/.checkstyle
  15. 15
      SimplePriorityQueue/.classpath
  16. 17
      SimplePriorityQueue/.project

7
SimpleActivityStack/.classpath

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
SimpleActivityStack/.project

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SimpleActivityStack</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

BIN
SimpleActivityStack/2018-WS Testat-90.pdf

Binary file not shown.

41
SimpleActivityStack/src/solution/SimpleActivityStack.java

@ -0,0 +1,41 @@
package solution;
import testat.SimpleActivity;
public class SimpleActivityStack extends SimpleStack<SimpleActivity> {
@Override
public int push(SimpleActivity activity) {
if (activity == null) {
return 0;
}
if (size() > 0) {
super.get(size() - 1).passivate();
}
activity.activate();
return super.push(activity);
}
@Override
public SimpleActivity pop() {
SimpleActivity activity = super.pop();
if (activity != null) {
activity.passivate();
activity.destroy();
if (size() > 0) {
super.get(size() - 1).activate();
}
}
return activity;
}
@Override
public SimpleActivity remove(int i) {
if (i == super.size() - 1) {
return this.pop();
}
super.get(i).destroy();
return super.remove(i);
}
}

14
SimpleActivityStack/src/solution/SimpleActivityStackTest.java

@ -0,0 +1,14 @@
package solution;
import testat.ASimpleActivityStackTest;
import testat.ISimpleStack;
import testat.SimpleActivity;
public class SimpleActivityStackTest extends ASimpleActivityStackTest {
@Override
protected ISimpleStack<SimpleActivity> getInstance() {
return new SimpleActivityStack();
}
}

64
SimpleActivityStack/src/solution/SimpleStack.java

@ -0,0 +1,64 @@
package solution;
import java.util.LinkedList;
import java.util.List;
import testat.IAction;
import testat.ISimpleStack;
public class SimpleStack<T> implements ISimpleStack<T> {
private List<T> list;
public SimpleStack() {
super();
list = new LinkedList<T>();
}
@Override
public int push(T t) {
if (t != null) {
if (list.contains(t)) {
list.remove(t);
}
list.add(t);
return list.size();
}
return 0;
}
@Override
public T pop() {
if (list.size() == 0) {
return null;
}
return list.remove(list.size() - 1);
}
@Override
public T remove(int i) {
return list.remove(i);
}
@Override
public int size() {
return list.size();
}
@Override
public boolean contains(Object o) {
return list.contains(o);
}
@Override
public T get(int i) {
return list.get(i);
}
@Override
public void forAll(IAction<T> action) {
if (action != null) {
list.forEach(t -> action.execute(t));
}
}
}

13
SimpleActivityStack/src/solution/SimpleStackTest.java

@ -0,0 +1,13 @@
package solution;
import testat.ASimpleStackTest;
import testat.ISimpleStack;
public class SimpleStackTest extends ASimpleStackTest {
@Override
protected ISimpleStack<String> getInstance() {
return new SimpleStack<>();
}
}

58
SimpleActivityStack/src/testat/ASimpleActivityStackTest.java

@ -0,0 +1,58 @@
package testat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
/**
*
* @author cm
*
*/
public abstract class ASimpleActivityStackTest extends ASimpleStackTestBase<SimpleActivity> {
@Override
protected void init() {
s1 = new SimpleActivity("s1");
s2 = new SimpleActivity("s2");
s3 = new SimpleActivity("s3");
s4 = new SimpleActivity("s4");
s5 = new SimpleActivity("s5");
s6 = new SimpleActivity("s6", "negativ", false);
s7 = new SimpleActivity("s7", "negativ", false);
s8 = new SimpleActivity("s8", "negativ", false);
s9 = new SimpleActivity("s9", "negativ", false);
s10 = new SimpleActivity("s10", "negativ", false);
act1 = new IAction<SimpleActivity>() {
@Override
public void execute(SimpleActivity e) {
temp1 = temp1 || e.isActive();
}
};
act2 = new IAction<SimpleActivity>() {
@Override
public void execute(SimpleActivity e) {
temp2 = temp2 && e.isActive();
}
};
}
@Test
public void testPushActivity_1() {
sstack.push(s1);
assertEquals(true, sstack.contains(s1));
assertEquals(true, sstack.get(0).isActive());
}
@Test
public void testPushActivity_2() {
sstack.push(s6);
assertEquals(true, sstack.contains(s6));
assertEquals(true, sstack.get(0).isActive());
}
}

41
SimpleActivityStack/src/testat/ASimpleStackTest.java

@ -0,0 +1,41 @@
package testat;
/**
*
* @author cm
*
*/
public abstract class ASimpleStackTest extends ASimpleStackTestBase<String> {
@Override
protected void init() {
s1 = "T-s1";
s2 = "T-s2";
s3 = "T-s3";
s4 = "T-s4";
s5 = "T-s5";
s6 = "T-s6";
s7 = "T-s7";
s8 = "T-s8";
s9 = "T-s9";
s10 = "T-s10";
act1 = new IAction<String>() {
@Override
public void execute(String e) {
temp1 = temp1 || e.contains("T");
}
};
act2 = new IAction<String>() {
@Override
public void execute(String e) {
temp2 = temp2 && e.contains("T");
}
};
}
}

289
SimpleActivityStack/src/testat/ASimpleStackTestBase.java

@ -0,0 +1,289 @@
package testat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.EmptyStackException;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
*
* @author Christian
*
*/
public abstract class ASimpleStackTestBase<T> {
protected ISimpleStack<T> sstack;
protected boolean temp1 = false, temp2=true;
protected abstract ISimpleStack<T> getInstance();
protected IAction<T> act1, act2;
protected T s1;
protected T s2;
protected T s3;
protected T s4;
protected T s5;
protected T s6;
protected T s7;
protected T s8;
protected T s9;
protected T s10;
abstract protected void init() ;
@BeforeAll
static void setUpBeforeClass() throws Exception {
}
@BeforeEach
void setUp() throws Exception {
init();
sstack = getInstance();
}
@Test
public void testPush_1_Null() {
assertEquals(0, sstack.push(null));
}
@Test
public void testPush_2_Single() {
assertEquals(1, sstack.push(s1));
}
@Test
public void testPush_3_Multiple() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
sstack.push(s6);
sstack.push(s7);
assertEquals(8, sstack.push(s8));
}
@Test
public void testSize_1_Initial() {
assertEquals(0, sstack.size());
}
@Test
public void testSize_2_AfterPush() {
sstack.push(s1);
assertEquals(1, sstack.size());
}
@Test
public void testSize_3_AfterPushs() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
assertEquals(5, sstack.size());
}
@Test
public void testGet_1_Simple() {
sstack.push(s1);
assertEquals(s1, sstack.get(0));
}
@Test
public void testGet_2_Multiple() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
assertEquals(s3, sstack.get(2));
}
@Test
public void testGet_3_ErrorHandling_1() {
sstack.push(s1);
sstack.push(s2);
try {
sstack.get(-1);
} catch (Exception e) {
assertEquals(true, e instanceof IndexOutOfBoundsException);
assertEquals(false, e instanceof EmptyStackException);
} finally {
assertEquals(2, sstack.size());
}
}
@Test
public void testGet_4_ErrorHandling_2() {
sstack.push(s1);
sstack.push(s2);
try {
sstack.get(5);
} catch (Exception e) {
assertEquals(true, e instanceof IndexOutOfBoundsException);
} finally {
assertEquals(2, sstack.size());
}
}
@Test
public void testPop_1_Initial() {
assertEquals(null, sstack.pop());
}
@Test
public void testPop_2_Single() {
sstack.push(s1);
assertEquals(s1, sstack.pop());
}
@Test
public void testPop_3_Multiple() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
assertEquals(s5, sstack.pop());
assertEquals(s4, sstack.pop());
assertEquals(s3, sstack.pop());
}
@Test
public void testPush_4_Existing() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
sstack.push(s2);
assertEquals(s2, sstack.pop());
}
@Test
public void testPush_5_AfterPop() {
// TODO Test Push after Pop
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
sstack.pop();
sstack.push(s7);
assertEquals(6, sstack.push(s8));
}
@Test
public void testGet_5_AfterPop() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
sstack.pop();
sstack.pop();
sstack.pop();
sstack.push(s6);
sstack.push(s7);
assertEquals(s6, sstack.get(2));
}
@Test
public void testRemove_1_One() {
sstack.push(s1);
assertEquals(s1, sstack.remove(0));
}
@Test
public void testRemove_2_Index() {
sstack.push(s1);
sstack.push(s2);
try {
sstack.remove(-1);
} catch (Exception e) {
assertEquals(true, e instanceof IndexOutOfBoundsException);
} finally {
assertEquals(2, sstack.size());
}
try {
sstack.remove(5);
} catch (Exception e) {
assertEquals(true, e instanceof IndexOutOfBoundsException);
} finally {
assertEquals(2, sstack.size());
}
}
@Test
public void testRemove_4_Multiple() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.push(s4);
sstack.push(s5);
assertEquals(s3, sstack.remove(2));
assertEquals(4, sstack.size());
}
@Test
public void testContains_1() {
assertEquals(false, sstack.contains(s2));
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
assertEquals(false, sstack.contains(s4));
assertEquals(true, sstack.contains(s2));
assertEquals(true, sstack.contains(s3));
assertEquals(s3, sstack.pop());
assertEquals(false, sstack.contains(s3));
}
@Test
public void testForAll_1() {
assertEquals(false, temp1);
assertEquals(true, temp2);
this.sstack.forAll(act1);
this.sstack.forAll(act2);
assertEquals(true, temp1);
assertEquals(true, temp2);
}
@Test
public void testSize_4_AfterRemove_1() {
sstack.push(s1);
sstack.pop();
assertEquals(0, sstack.size());
}
@Test
public void testSize_4_AfterRemove_2() {
sstack.push(s1);
sstack.push(s2);
sstack.push(s3);
sstack.pop();
assertEquals(2, sstack.size());
}
}

11
SimpleActivityStack/src/testat/IAction.java

@ -0,0 +1,11 @@
package testat;
/**
*
* @author cm
*
* @param <E>
*/
public interface IAction<E> {
public void execute(E e);
}

66
SimpleActivityStack/src/testat/ISimpleStack.java

@ -0,0 +1,66 @@
package testat;
/**
*
* @author cm
* not provided
*
* @param <T>
*/
public interface ISimpleStack<T> {
/**
* Nimmt ein Objekt auf, das nicht null ist und noch nicht im Stack enthalten
* ist. Rückgabe: Anzahl an Elemente im Stack nach der Aufnahme oder 0, falls t
* == null ist das Objekt bereits enthalten, wird es ganz oben auf den Stapel
* gelegt. Hinweis: Verwenden Sie entsprechende Operationen der Collection/des
* Array und nicht pop/push
*
* @param t
* @return
*/
public int push(T t);
/**
* Entfernt das oberste Objekt und gibt es zurück. Liefert null, falls der Stack
* leer ist
*/
public T pop();
/**
* gibt einen Verweise auf das i-te Objekt zurück. Das Objekt bleibt im //
* Stack. Falls i nicht existiert, führt dies zur IndexOutOfBoundsException. //
* Das unterste Element hat Index 0, das oberste Index size()-1
*
* @param i index
* @return
*/
public T get(int i);
/**
* Wie get(i), nur wird das Objekt aus dem Stack entfernt
*
* @param i
* @return
*/
public T remove(int i);
/**
* Anzahl an Objekte im Stack
*
*/
public int size();
/**
* true, falls das Objekt im Stack enthalten ist
*
*/
public boolean contains(Object o);
/**
* Wendet action.execute auf alle Objekte im Stack an
*
* @param action
*/
public void forAll(IAction<T> action);
}

56
SimpleActivityStack/src/testat/SimpleActivity.java

@ -0,0 +1,56 @@
package testat;
/**
*
* @author cm
*
*/
public class SimpleActivity {
private String desc;
private String name;
private boolean isActive;
public SimpleActivity(String name, String desc, boolean activityState) {
this.name = name;
this.setDesc(desc);
this.setActive(activityState);
}
public SimpleActivity(String name) {
this.name = name;
this.setActive(true);
}
public void activate() {
this.setActive(true);
}
public void destroy() {
System.out.println(this.name + " is destroyed");
}
public void passivate() {
this.setActive(false);
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
}

7
SimplePriorityQueue/.checkstyle

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="Google Checks" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>

15
SimplePriorityQueue/.classpath

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

17
SimplePriorityQueue/.project

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SimplePriorityQueue</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Loading…
Cancel
Save