MultiSet - Enable assertions.
I have a class for my MultiSet<E>, and an associated test-program.
My class is a follows:
Code :
public class MultiSet<E> extends AbstractCollection<E>
{
private int size = 0;
private Map<E, Integer> values = new HashMap<E, Integer>();
public MultiSet()
{
}
public MultiSet(Collection<E> c)
{
addAll(c);
}
@Override
public boolean add(E e)
{
Integer i = values.get(e);
if (i == null)
values.put(e, 1);
else
values.put(e, i + 1);
return true;
}
@Override
public boolean remove(Object o)
{
Integer i = values.remove(o);
return i != null;
}
public Iterator<E> iterator()
{
return new Iterator<E>()
{
private Iterator<E> iterator = values.keySet().iterator();
private int remaining = 0;
private E current = null;
public boolean hasNext()
{
return remaining > 0 || iterator.hasNext();
}
public E next()
{
if (remaining == 0)
{
current = iterator.next();
remaining = values.get(current);
}
remaining--;
return current;
}
public void remove()
{
throw new UnsupportedOperationException();
}
};
}
@Override
public boolean equals(Object object)
{
if (this == object) return true;
if (object == null) return false;
if (this.getClass() != object.getClass()) return false;
MultiSet<?> o = (MultiSet<?>) object;
return o.values.equals(values);
}
@Override
public int hashCode()
{
return values.hashCode()*163 + new Integer(size).hashCode()*389;
}
@Override
public String toString()
{
String res = "";
for (E e : values.keySet())
{
res = res + "[" + e.toString() + ":" + values.get(e) + "]";
}
return getClass().getName() + res;
}
public int size()
{
return values.size();
}
}
And the test program:
Code :
public class MultiSetTest {
public static void main(String[] args) {
MultiSet<String> a = new MultiSet<String>();
MultiSet<String> b = new MultiSet<String>();
a.add("Foo");
a.add("Bar");
a.add("Foo");
System.out.println("a:" + a); // test toString
b.add("Bar");
b.add("Foo");
b.add("Bar");
b.add("Foo");
System.out.println("b:" + b);
assert !a.equals(b) : "Failed test 1!"; // test equals
assert b.remove("Bar") : "Failed test 2!"; // test remove
assert a.equals(b) : "Failed test 3!";
for(String s : a) { // test iterator
assert b.remove(s) : "Failed test 4!";
}
assert b.size() == 0 : "Failed test 5!";
Set<String> baseSet = new HashSet<String>(a);
assert baseSet.size()==2 : "Failed test 6!";
b = new MultiSet<String>(a);
assert a.equals(b) : "Failed test 7!";
try {
assert false;
System.out.println("Please enable assertions!");
}
catch(AssertionError e) {
System.out.println("Success!");
}
}
}
To me it seems to be working, but if my MultiSet-class was working proberly, then the output should be: "Success!"
Instead i get: "Please enable assertions!".
Where/how do i enable assertions?
Re: MultiSet - Enable assertions.
Google is your friend, learn to embrace it as you will get results far quicker than posting here.
Use the forums when Google doesn't return anything relevant :D.
Back on topic: To enable assertions all you need to do is add -ea to the command line arguments / program arguments before running your program.
If you're running from the command line, usage is as such: java -ea TestHarness
Or if you're using an IDE, such as eclipse, you will find the textfields for adding -ea in the area of run configurations.
1 Attachment(s)
Re: MultiSet - Enable assertions.
Attachment 893
Is that correct?
I wrote "-ea" at Program arguments for my Test-class,
and when I run the program I still get the same fail-output :/
Re: MultiSet - Enable assertions.
Apologies, should be under VM arguments in the IDE
Re: MultiSet - Enable assertions.
Oh wait.
It has to be VM arguments, ofcourse.
Now; "Failed test 3!" xD
So i guess there's a problem with my equals then :/
Re: MultiSet - Enable assertions.
Do any of you see what the problem is?
Re: MultiSet - Enable assertions.
well A is not equal to B, so it will fail :P
Re: MultiSet - Enable assertions.
I found the problem.
I modified my Add and remove-methods. No problems with the equals-method :)
It works - the output is: Success.
Success!!!
And thank you for your help! ;)