This is my class, how I assure that the method insert() is working correctly?
public class HeapQueue<P extends Comparable<? super P>, V>
implements PriorityQueue<P, V> {
private final ArrayList<Pair<P, V>> pairs = new ArrayList<>();
private static class Pair<P extends Comparable<? super P>, V>
implements Comparable<Pair<P, V>> {
private final P priority;
private final V value;
public Pair(P priority, V value) {
this.priority = priority;
this.value = value;
}
@Override
public int compareTo(Pair<P, V> o) {
return priority.compareTo(o.priority);
}
}
I tried this test, but I can't assure the object Pair<> is correct.
@Test
public void insertPrioritatAltaTest() {
HeapQueue hq = new HeapQueue();
Priority p1 = null;
try {
p1 = new Priority("Alta");
} catch (InvalidInputError iie) {
}
int a = 1;
hq.insert(p1, a);
assertEquals(hq.size(),1);
}
I tried creating getters to extract the value of Pairs, but Netbeans warnered me that I was passing a private thing through a public API and moreover I wasn't obtaining it. I don't think declaring a public class Pair<> is the correct way to test this. Neither using the method extract() which I haven't tested it yet.
It's often easiest to test your class by using it how a user would use it, and not worrying too much about the internal state.
I think you were on the right track with
I tried creating getters to extract the value of Pairs
Although it sounds like you were getting an error because you were returning an instance of your private class Pair<P,V>
. I'm not sure from your question exactly how the interface expects your class to behave, but it seems like you will want a public V pop()
or similar function to be able to remove and return the next item in your PriorityQueue
. Note that in this example, you would return a V
type, not a Pair
.
If that's the behavior you're looking for, your test would be similar to:
@Test
public void insertPrioritatAltaTest() {
HeapQueue hq = new HeapQueue();
hq.insert(new Priority("A"), 1);
hq.insert(new Priority("C"), 2);
hq.insert(new Priority("B"), 3);
assertEquals(hq.pop(), 1);
assertEquals(hq.pop(), 3);
assertEquals(hq.pop(), 2);
}