Commit ab7823cc authored by Daniel Schlör's avatar Daniel Schlör
Browse files

added more test

parent 40f94902
......@@ -5,19 +5,28 @@ import static org.junit.Assert.*;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import de.uniwue.ls6.datastructure.Instance;
public class InstanceTest {
Random r = new Random();
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void testInstanceRandomConstLbl() {
// set and test label and array size with random values
int lbl = Math.abs(r.nextInt(10000));
int row = Math.abs(r.nextInt(10000));
int col = Math.abs(r.nextInt(10000));
......@@ -32,6 +41,9 @@ public class InstanceTest {
int lbl = 31337;
int row = 42;
int col = 5;
// set and test label and array size with constant values
Instance tInst = new Instance(col, row, lbl);
assertEquals(lbl, tInst.getLabel());
assertEquals(col, tInst.getFeatureArray().length);
......@@ -42,6 +54,8 @@ public class InstanceTest {
public void testInstanceSetFeatures() {
int rows = 10;
int cols = 5;
// try and set single features
Instance tInstance = new Instance(cols, rows);
tInstance.setFeatureAt(0, 0, 1);
assertEquals(1, tInstance.getFeatureArray()[0][0]);
......@@ -61,30 +75,40 @@ public class InstanceTest {
int cols = 5;
Instance tInstance = new Instance(cols, rows);
int[][] test = new int[cols][rows];
// check for whole array feature setting
for (int col = 0; col < cols; col++)
for (int row = 0; row < rows; row++) {
int val = r.nextInt();
test[col][row] = val;
// set feature
tInstance.setFeatureAt(col, row, val);
// and check if it is in array
assertEquals(val, tInstance.getFeatureArray()[col][row]);
}
assertArrayEquals(test, tInstance.getFeatureArray());
}
@Test(expected = IndexOutOfBoundsException.class)
public void testBoundary1() {
int rows = 10;
int cols = 5;
Instance tInstance = new Instance(cols, rows);
// setting features at a col which should not exist
exception.expect(ArrayIndexOutOfBoundsException.class);
tInstance.setFeatureAt(cols, rows -1, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testBoundary2() {
int rows = 10;
int cols = 5;
Instance tInstance = new Instance(cols, rows);
// setting features at a row which should not exist
exception.expect(ArrayIndexOutOfBoundsException.class);
tInstance.setFeatureAt(cols - 1, rows, 1);
}
......@@ -99,6 +123,8 @@ public class InstanceTest {
int rows = 10;
int cols = 5;
Instance tInstance = new Instance(cols, rows);
// add a point and check if found
tInstance.setFeatureAt(1, 3, 1337);
assertTrue(tInstance.containsFeature(pointlist));
......@@ -106,10 +132,31 @@ public class InstanceTest {
pointlist = new ArrayList<Point>();
pointlist.add(p);
// a point not added shouldn't be contained
assertFalse(tInstance.containsFeature(pointlist));
// but when added
tInstance.setFeatureAt(3, 0, 42);
assertTrue(tInstance.containsFeature(pointlist));
// try with another point
pointlist.add(new Point(3, 17));
assertFalse(tInstance.containsFeature(pointlist));
tInstance.setFeatureAt(3, 1, 17);
assertTrue(tInstance.containsFeature(pointlist));
// now check point contained only by column which it has been added to
for (int col = 0; col < cols; col++) {
p = new Point(col, 1337);
pointlist = new ArrayList<Point>();
pointlist.add(p);
if (col != 1) {
assertFalse(tInstance.containsFeature(pointlist));
} else {
assertTrue(tInstance.containsFeature(pointlist));
}
}
}
}
Supports Markdown
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