Skip to content

Commit

Permalink
Fixing ArrayExample.iterator and BinaryFeaturesExample.iterator so th…
Browse files Browse the repository at this point in the history
…ey throw NoSuchElementException rather than ArrayIndexOutOfBoundsException when exhausted. (#138)
  • Loading branch information
Craigacp authored Apr 27, 2021
1 parent 0cbf236 commit 42ff2a0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Core/src/main/java/org/tribuo/impl/ArrayExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Set;
Expand Down Expand Up @@ -570,6 +571,9 @@ public boolean hasNext() {

@Override
public Feature next() {
if (!hasNext()) {
throw new NoSuchElementException("Iterator exhausted at position " + pos);
}
Feature f = new Feature(featureNames[pos],featureValues[pos]);
pos++;
return f;
Expand Down
4 changes: 4 additions & 0 deletions Core/src/main/java/org/tribuo/impl/BinaryFeaturesExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Set;
Expand Down Expand Up @@ -502,6 +503,9 @@ public boolean hasNext() {

@Override
public Feature next() {
if (!hasNext()) {
throw new NoSuchElementException("Iterator exhausted at position " + pos);
}
Feature f = new Feature(featureNames[pos], 1.0);
pos++;
return f;
Expand Down
52 changes: 52 additions & 0 deletions Core/src/test/java/org/tribuo/ExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -261,6 +262,57 @@ public void invalidListExampleTest() {
// This example should be valid
assertTrue(test.validateExample());
}

@Test
public void exampleIterators() {
MockOutput output = new MockOutput("UNK");
List<Feature> features = new ArrayList<>();
features.add(new Feature("A",1.0));
features.add(new Feature("C",1.0));
features.add(new Feature("B",1.0));

ArrayExample<MockOutput> array = new ArrayExample<>(output,features);
assertEquals(3,array.size());

Iterator<Feature> arrayItr = array.iterator();
assertTrue(arrayItr.hasNext());
assertEquals(features.get(0),arrayItr.next());
assertTrue(arrayItr.hasNext());
// Features are lexicographically sorted inside examples
assertEquals(features.get(2),arrayItr.next());
assertTrue(arrayItr.hasNext());
assertEquals(features.get(1),arrayItr.next());
assertFalse(arrayItr.hasNext());
assertThrows(NoSuchElementException.class, arrayItr::next);

ListExample<MockOutput> list = new ListExample<>(output,features);
assertEquals(3,list.size());

Iterator<Feature> listItr = list.iterator();
assertTrue(listItr.hasNext());
assertEquals(features.get(0),listItr.next());
assertTrue(listItr.hasNext());
// Features are lexicographically sorted inside examples
assertEquals(features.get(2),listItr.next());
assertTrue(listItr.hasNext());
assertEquals(features.get(1),listItr.next());
assertFalse(listItr.hasNext());
assertThrows(NoSuchElementException.class, listItr::next);

BinaryFeaturesExample<MockOutput> binary = new BinaryFeaturesExample<>(output,features);
assertEquals(3,binary.size());

Iterator<Feature> binaryItr = binary.iterator();
assertTrue(binaryItr.hasNext());
assertEquals(features.get(0),binaryItr.next());
assertTrue(binaryItr.hasNext());
// Features are lexicographically sorted inside examples
assertEquals(features.get(2),binaryItr.next());
assertTrue(binaryItr.hasNext());
assertEquals(features.get(1),binaryItr.next());
assertFalse(binaryItr.hasNext());
assertThrows(NoSuchElementException.class, binaryItr::next);
}

@Test
public void testBinaryFeaturesExample() {
Expand Down

0 comments on commit 42ff2a0

Please sign in to comment.