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

Corrected Instance class, added first Test

parent e41abe03
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
......@@ -18,12 +18,12 @@ public class Instance {
private int[][] featureArray;
private int label;
public Instance(int nrRows, int nrCols) {
public Instance(int nrCols, int nrRows) {
super();
featureArray = new int[nrCols][nrRows];
}
public Instance(int nrRows, int nrCols, int label) {
public Instance(int nrCols, int nrRows, int label) {
super();
featureArray = new int[nrCols][nrRows];
this.label = label;
......@@ -51,9 +51,9 @@ public class Instance {
outer: for (Point p : features) {
for (int row = 0; row < featureArray.length; row++) {
for (int row = 0; row < featureArray[0].length; row++) {
if (featureArray[row][p.x] == p.y) {
if (featureArray[p.x][row] == p.y) {
continue outer;
}
}
......
package de.uniwue.ls6.datastructure.test;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import org.mockito.Mockito;
import de.uniwue.ls6.datastructure.Instance;
public class InstanceTest {
Random r = new Random();
@Test
public void testInstanceRandomConstLbl() {
int lbl = Math.abs(r.nextInt(10000));
int row = Math.abs(r.nextInt(10000));
int col = Math.abs(r.nextInt(10000));
Instance tInst = new Instance(col, row, lbl);
assertEquals(lbl, tInst.getLabel());
assertEquals(col, tInst.getFeatureArray().length);
assertEquals(row, tInst.getFeatureArray()[0].length);
}
@Test
public void testInstanceFixedConstLbl() {
int lbl = 31337;
int row = 42;
int col = 5;
Instance tInst = new Instance(col, row, lbl);
assertEquals(lbl, tInst.getLabel());
assertEquals(col, tInst.getFeatureArray().length);
assertEquals(row, tInst.getFeatureArray()[0].length);
}
@Test
public void testInstanceSetFeatures() {
int rows = 10;
int cols = 5;
Instance tInstance = new Instance(cols, rows);
tInstance.setFeatureAt(0, 0, 1);
assertEquals(1, tInstance.getFeatureArray()[0][0]);
assertEquals(0, tInstance.getFeatureArray()[0][1]);
tInstance.setFeatureAt(0, 1, 17);
assertEquals(17, tInstance.getFeatureArray()[0][1]);
tInstance.setFeatureAt(1, 0, 2);
assertEquals(2, tInstance.getFeatureArray()[1][0]);
assertEquals(0, tInstance.getFeatureArray()[1][1]);
tInstance.setFeatureAt(1, 1, 18);
assertEquals(18, tInstance.getFeatureArray()[1][1]);
}
@Test
public void testInstanceSetAllFeatures() {
int rows = 10;
int cols = 5;
Instance tInstance = new Instance(cols, rows);
int[][] test = new int[cols][rows];
for (int col = 0; col < cols; col++)
for (int row = 0; row < rows; row++) {
int val = r.nextInt();
test[col][row] = val;
tInstance.setFeatureAt(col, row, val);
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);
tInstance.setFeatureAt(cols, rows -1, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testBoundary2() {
int rows = 10;
int cols = 5;
Instance tInstance = new Instance(cols, rows);
tInstance.setFeatureAt(cols - 1, rows, 1);
}
@Test
public void testContainsFeature() {
List<Point> pointlist;
Point p;
p = new Point(1, 1337);
pointlist = new ArrayList<Point>();
pointlist.add(p);
int rows = 10;
int cols = 5;
Instance tInstance = new Instance(cols, rows);
tInstance.setFeatureAt(1, 3, 1337);
assertTrue(tInstance.containsFeature(pointlist));
p = new Point(3, 42);
pointlist = new ArrayList<Point>();
pointlist.add(p);
assertFalse(tInstance.containsFeature(pointlist));
tInstance.setFeatureAt(3, 0, 42);
assertTrue(tInstance.containsFeature(pointlist));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
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