testing
This commit is contained in:
parent
ac1cc19a88
commit
0c95c28bc4
BIN
bin/test/uebung/uebung09/collection/SetTest.class
Normal file
BIN
bin/test/uebung/uebung09/collection/SetTest.class
Normal file
Binary file not shown.
BIN
bin/uebung09/collection/BigSet$1.class
Normal file
BIN
bin/uebung09/collection/BigSet$1.class
Normal file
Binary file not shown.
BIN
bin/uebung09/collection/BigSet.class
Normal file
BIN
bin/uebung09/collection/BigSet.class
Normal file
Binary file not shown.
BIN
bin/uebung09/collection/EmptySet$1.class
Normal file
BIN
bin/uebung09/collection/EmptySet$1.class
Normal file
Binary file not shown.
BIN
bin/uebung09/collection/EmptySet.class
Normal file
BIN
bin/uebung09/collection/EmptySet.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
bin/uebung09/collection/SingeltonSet$1.class
Normal file
BIN
bin/uebung09/collection/SingeltonSet$1.class
Normal file
Binary file not shown.
BIN
bin/uebung09/collection/SingeltonSet.class
Normal file
BIN
bin/uebung09/collection/SingeltonSet.class
Normal file
Binary file not shown.
76
src/test/uebung/uebung09/collection/SetTest.java
Normal file
76
src/test/uebung/uebung09/collection/SetTest.java
Normal file
@ -0,0 +1,76 @@
|
||||
package test.uebung.uebung09.collection;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import uebung09.collection.Set;
|
||||
import uebung09.collection.SetFactory;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class SetTest {
|
||||
|
||||
static String a = "test";
|
||||
static String b = "test2";
|
||||
static String c = "test3";
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void EmptySetTest(){
|
||||
|
||||
Set<String> test = SetFactory.create();
|
||||
|
||||
Iterator<String> i = test.iterator();
|
||||
|
||||
assertFalse(i.hasNext());
|
||||
assertThrows(NoSuchElementException.class, i::next);
|
||||
assertFalse(test.contains(a));
|
||||
assertEquals(0,test.size());
|
||||
assertTrue(test.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SingeltonSetTest(){
|
||||
|
||||
Set<String> test = SetFactory.create(a);
|
||||
|
||||
Iterator<String> i = test.iterator();
|
||||
|
||||
assertTrue(i.hasNext());
|
||||
assertTrue(a == i.next());
|
||||
assertFalse(i.hasNext());
|
||||
assertThrows(NoSuchElementException.class, i::next);
|
||||
assertTrue(test.contains(a));
|
||||
assertEquals(1,test.size());
|
||||
assertFalse(test.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void BigSetTest() {
|
||||
|
||||
Set<String> test = SetFactory.create(a,b,c);
|
||||
|
||||
Iterator<String> i = test.iterator();
|
||||
|
||||
assertTrue(i.hasNext());
|
||||
assertTrue(a == i.next());
|
||||
assertTrue(i.hasNext());
|
||||
assertTrue(a == i.next());
|
||||
assertTrue(i.hasNext());
|
||||
assertTrue(a == i.next());
|
||||
assertFalse(i.hasNext());
|
||||
assertThrows(NoSuchElementException.class, i::next);
|
||||
assertTrue(test.contains(a));
|
||||
assertEquals(1,test.size());
|
||||
assertFalse(test.isEmpty());
|
||||
}
|
||||
}
|
94
src/uebung09/collection/BigSet.java
Normal file
94
src/uebung09/collection/BigSet.java
Normal file
@ -0,0 +1,94 @@
|
||||
package uebung09.collection;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
class BigSet<T> implements Set<T>{
|
||||
|
||||
private T item;
|
||||
private Set<T> rest;
|
||||
|
||||
public BigSet(T item, Set<T> rest){
|
||||
//TODO
|
||||
this.item = item;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
|
||||
private boolean returned = false;
|
||||
|
||||
private Iterator<T> restI = rest.iterator();
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return restI.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (!returned) {
|
||||
returned = true;
|
||||
return item;
|
||||
} else {
|
||||
return restI.next();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
int count = 1;
|
||||
count += rest.size();
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object el) {
|
||||
if(el == item) {
|
||||
return true;
|
||||
} else {
|
||||
return rest.contains(el);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> union(Set<T> other) {
|
||||
if (other == EmptySet.getInstance()) {
|
||||
return this;
|
||||
} else if (other instanceof SingeltonSet<T> && !contains(other.iterator().next())) {
|
||||
return new BigSet<T>(other.iterator().next(), this);
|
||||
} else {
|
||||
return new BigSet<T>(item, rest.union(other));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> add(T element) {
|
||||
if (contains(element)) {
|
||||
return this;
|
||||
} else {
|
||||
return new BigSet<T>(element, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> intersection(Set<T> other) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'intersection'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean subsetOf(Set<?> other) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'subsetOf'");
|
||||
}
|
||||
}
|
71
src/uebung09/collection/EmptySet.java
Normal file
71
src/uebung09/collection/EmptySet.java
Normal file
@ -0,0 +1,71 @@
|
||||
package uebung09.collection;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
class EmptySet<T> implements Set<T> {
|
||||
|
||||
private static EmptySet<?> INSTANCE;
|
||||
|
||||
private EmptySet() {}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> EmptySet<T> getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new EmptySet<>();
|
||||
}
|
||||
return (EmptySet<T>) INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object el) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> union(Set<T> other) {
|
||||
return other;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> add(T element) {
|
||||
return new SingeltonSet<T>(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> intersection(Set<T> other) {
|
||||
return other;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean subsetOf(Set<?> other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -38,10 +38,10 @@ public interface Set<E> extends Iterable<E> {
|
||||
/**
|
||||
* returns the set resulting from adding the specified element to this set.
|
||||
*
|
||||
* @param element an element (must not be null)
|
||||
* @param elem an element (must not be null)
|
||||
* @return the set resulting from adding the specified element to this set.
|
||||
*/
|
||||
Set<E> add(E element);
|
||||
Set<E> add(E elem);
|
||||
|
||||
/**
|
||||
* Returns the intersection of this set and the specified set.
|
||||
|
@ -10,9 +10,8 @@ public class SetFactory {
|
||||
* @param <T> the element type of the returned set.
|
||||
* @return the empty set.
|
||||
*/
|
||||
static <T> Set<T> create() {
|
||||
// TODO implement
|
||||
return null;
|
||||
public static <T> Set<T> create() {
|
||||
return EmptySet.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -22,9 +21,8 @@ public class SetFactory {
|
||||
* @param <T> the element type of the returned set.
|
||||
* @return the singleton set containing the specified element.
|
||||
*/
|
||||
static <T> Set<T> create(T element) {
|
||||
// TODO implement
|
||||
return null;
|
||||
public static <T> Set<T> create(T element) {
|
||||
return new SingeltonSet<T>(element);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,8 +32,14 @@ public class SetFactory {
|
||||
* @param <T> the element type of the returned set.
|
||||
* @return a set containing the specified elements.
|
||||
*/
|
||||
static <T> Set<T> create(T... elems) {
|
||||
// TODO implement
|
||||
return null;
|
||||
public static <T> Set<T> create(T... elems) {
|
||||
// BigSet<T> rtn = new BigSet<T>(elems[1], new SingeltonSet<>(elems[1]));
|
||||
Set<T> set = EmptySet.getInstance();
|
||||
|
||||
for(T elem : elems) {
|
||||
set = set.add(elem);
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
77
src/uebung09/collection/SingeltonSet.java
Normal file
77
src/uebung09/collection/SingeltonSet.java
Normal file
@ -0,0 +1,77 @@
|
||||
package uebung09.collection;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
class SingeltonSet<T> implements Set<T> {
|
||||
|
||||
private T item;
|
||||
|
||||
public SingeltonSet(T item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
|
||||
boolean returned = false;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !returned;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (!returned) {
|
||||
returned = true;
|
||||
return item;
|
||||
} else {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object el) {
|
||||
return el == item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> union(Set<T> other) {
|
||||
//TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> add(T element) {
|
||||
if (element == item) {
|
||||
return this;
|
||||
} else {
|
||||
return new BigSet<T>(item, new SingeltonSet<T>(element));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> intersection(Set<T> other) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'intersection'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean subsetOf(Set<?> other) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user