Add test coverage for listIndex

Fixes #6226
This commit is contained in:
Jordan Sissel 2016-11-03 17:53:37 -07:00 committed by Suyog Rao
parent 01f7ab708d
commit 28116ee80f
2 changed files with 41 additions and 1 deletions

View file

@ -188,7 +188,7 @@ public class Accessors {
* @param size the size of the list.
* @return the positive integer offset for the list given by index i.
*/
private static int listIndex(int i, int size) {
public static int listIndex(int i, int size) {
if (i >= size || i < -size) {
throw new IndexOutOfBoundsException("Index " + i + " is out of bounds for a list with size " + size);
}

View file

@ -1,5 +1,11 @@
package org.logstash;
import org.junit.experimental.theories.DataPoint;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.junit.Test;
import static org.junit.Assert.*;
@ -207,4 +213,38 @@ public class AccessorsTest {
assertEquals(accessors.get("[foo][bar]"), null);
assertEquals(accessors.get("[foo]"), "boom");
}
@RunWith(Theories.class)
public static class TestListIndexFailureCases {
private static final int size = 10;
@DataPoint
public static final int tooLarge = size;
@DataPoint
public static final int tooLarge1 = size+1;
@DataPoint
public static final int tooLargeNegative = -size - 1;
@Rule
public ExpectedException exception = ExpectedException.none();
@Theory
public void testListIndexOutOfBounds(int i) {
exception.expect(IndexOutOfBoundsException.class);
Accessors.listIndex(i, size);
}
}
public static class TestListIndex {
public void testListIndexOutOfBounds() {
assertEquals(Accessors.listIndex(0, 10), 0);
assertEquals(Accessors.listIndex(1, 10), 1);
assertEquals(Accessors.listIndex(9, 10), 9);
assertEquals(Accessors.listIndex(-1, 10), 9);
assertEquals(Accessors.listIndex(-9, 10), 1);
assertEquals(Accessors.listIndex(-10, 10), 0);
}
}
}