0

I need to implement a sparse graph and do some junit tests on it.

This is my graph class:

package graphs;
import java.util.Collection;
import java.util.HashMap;
public class SparseGraph<V, E> implements Graph<V, E> {
    HashMap<V, HashMap<V, E>> node;
    public SparseGraph() {
        node = new HashMap<>();
    }
    @Override
    public boolean addVertex(V vertex) {
        if (hasVertex(vertex)) {
            return false;
        }
        else {
            node.put(vertex, new HashMap());
        }
        return true;
    }
    @Override
    public boolean addEdge(V vertex1, V vertex2, E data) {
        if (!hasVertex(vertex1) || !hasVertex(vertex2)) {
            return false;
        }
        if (hasEdge(vertex1, vertex2) && getData(vertex1, vertex2).equals(data)) {
            return false;
        }
        else if (hasEdge(vertex1, vertex2) && !getData(vertex1, vertex2).equals(data)) {
            node.get(vertex1).put(vertex2, data);
            return true;
        }
        else {
            (node.get(vertex1)).put(vertex2, data);
        }
        return true;
    }
    @Override
    public boolean hasVertex(V vertex) {
        if (vertex == null) return false;
        return (node.containsKey(vertex));
    }
    @Override
    public boolean hasEdge(V vertex1, V vertex2) {
        if (!hasVertex(vertex1) || !hasVertex(vertex2)) return false;
        if (node.get(vertex1).containsKey(vertex2)) {
            return true;
        }
        return false;
    }
    @Override
    public E getData(V vertex1, V vertex2) {
        if (!hasVertex(vertex1) || !hasVertex(vertex2)) return null;
        return node.get(vertex1).get(vertex2);
    }
    @Override
    public Collection<V> getVertices() {
        return node.keySet();
    }
    @Override
    public Collection<V> getNeighbors(V vertex) {
        if (!hasVertex(vertex))
            return null;
        else
            return node.get(vertex).keySet();
    }
}// end class

and this is my test:

public void testGetVertices() {
    SparseGraph instance = new SparseGraph();
    instance.addVertex("A");
    instance.addVertex("B");
    instance.addVertex("C");
    instance.addVertex("D");
    instance.addVertex("E");
    //assertEquals("[D, E, A, B, C]",instance.getVertices());
}

My question is, how can I create a test that checks if all vertices are present in my Collection?

I tried comparing a string with the keySet, but the error was that is expected a string but method assertEquals gets a keySet.

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Don Diego
  • 1,309
  • 3
  • 23
  • 46

3 Answers3

2

There are many ways of testing this. The most basic one is to do a contains for each element but JUnit has more elegant ways. You can use assertThat() together with the Hamcrest

assertThat(instance.getVertices(), containsInAnyOrder("A", "B", "C", "D", "E"));

use following imports:

import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.Matchers.containsInAnyOrder;
Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
  • Should be "assertThat". (I cannot edit, because the edit is less than six characters.) – Jan Jun 15 '15 at 12:15
  • 1
    [`hasItem`](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/core/IsCollectionContaining.html#hasItem%28T%29) checks if at least one of the specified items is within the result, [`contains`](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#contains%28java.util.List%29) or [`containsInAnyOrder`](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#containsInAnyOrder%28T...%29) will check for all items – Roman Vottner Jun 15 '15 at 12:17
  • @RomanVottner good point; this will also remove the size check – Liviu Stirb Jun 15 '15 at 12:19
  • @user1121883, thanks fow your answer! Anyway, just to know, is there a way to check it without using Hamcrest? If not, how can I use it? I'm using Eclipse (and I'm a beginner =) ) – Don Diego Jun 15 '15 at 12:26
  • @Diego just import: import static org.junit.Assert.assertThat; import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.Matchers.containsInAnyOrder; – Liviu Stirb Jun 15 '15 at 12:28
  • @user1121883 thanks! Anyway I tried importing everithing but I get an error importing this: import static org.hamcrest.Matchers.containsInAnyOrder; it says "cannot be resolved" – Don Diego Jun 15 '15 at 12:45
  • @Diego what version of junit do you use? – Liviu Stirb Jun 15 '15 at 12:46
  • @user1121883 junit v4.11 – Don Diego Jun 15 '15 at 12:55
  • @Diego can you add to hamcrest to your classpath – Liviu Stirb Jun 15 '15 at 13:05
0

I'd iterate over the graph and check to see if each one appears in a List of expected values.

duffymo
  • 305,152
  • 44
  • 369
  • 561
-1

A JUnit-Test with plain Java would look like this:

assertEquals(5, instance.getVertices().size());
assertTrue(instance.getVertices().hasVertex("A"));
assertTrue(instance.getVertices().hasVertex("B"));
assertTrue(instance.getVertices().hasVertex("C"));
assertTrue(instance.getVertices().hasVertex("D"));
assertTrue(instance.getVertices().hasVertex("E"));
Jan
  • 2,060
  • 2
  • 29
  • 34