Commit b8e89679 authored by Daniel Le Berre's avatar Daniel Le Berre

Fixed another sonar violation (+ added a test).

parent b81e2bc5
......@@ -3,10 +3,11 @@ package org.sat4j.pb.tools;
import java.util.BitSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
public class CombinationIterator implements Iterable<Set<Integer>>,
Iterator<Set<Integer>> {
public class CombinationIterator
implements Iterable<Set<Integer>>, Iterator<Set<Integer>> {
/* contains a combination of indexes ; ascending order */
private int indexes[] = null;
......@@ -90,6 +91,9 @@ public class CombinationIterator implements Iterable<Set<Integer>>,
}
public Set<Integer> next() {
if (!this.hasNext) {
throw new NoSuchElementException();
}
Set<Integer> nextSet = new HashSet<Integer>();
for (Integer i : this.indexes) {
nextSet.add(this.items[i]);
......
package org.sat4j.pb;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.NoSuchElementException;
import org.junit.Before;
import org.junit.Test;
import org.sat4j.pb.tools.CombinationIterator;
public class CombinationIteratorTest {
private CombinationIterator iterator;
@Before
public void setup() {
iterator = new CombinationIterator(2, 3);
}
@Test
public void testExpectedBehavior() {
assertTrue(iterator.hasNext());
assertNotNull(iterator.next());
assertTrue(iterator.hasNext());
assertNotNull(iterator.next());
assertTrue(iterator.hasNext());
assertNotNull(iterator.next());
assertFalse(iterator.hasNext());
}
@Test(expected = NoSuchElementException.class)
public void testExceptionWhenNoMoreSolutions() {
assertTrue(iterator.hasNext());
assertNotNull(iterator.next());
assertTrue(iterator.hasNext());
assertNotNull(iterator.next());
assertTrue(iterator.hasNext());
assertNotNull(iterator.next());
assertFalse(iterator.hasNext());
assertNotNull(iterator.next());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment