Skip to content
Snippets Groups Projects
Commit 455cdac6 authored by Powers, Peter M.'s avatar Powers, Peter M.
Browse files

intervalTable tests; other cleaning and comments

parent 25b05917
No related branches found
No related tags found
1 merge request!197Data tests
...@@ -271,7 +271,8 @@ public final class IntervalArray { ...@@ -271,7 +271,8 @@ public final class IntervalArray {
} }
/** /**
* Add supplied values to this builder. * Add supplied values to this builder. Assumes that supplied value array is
* the same size or smaller than the internal builder data array.
* *
* @param values to add * @param values to add
* @throws IndexOutOfBoundsException if values overrun array * @throws IndexOutOfBoundsException if values overrun array
...@@ -286,7 +287,9 @@ public final class IntervalArray { ...@@ -286,7 +287,9 @@ public final class IntervalArray {
} }
/** /**
* Add the y-values of the supplied sequence to this builder. * Add the y-values of the supplied sequence to this builder. Assumes that
* supplied sequence is the same size or smaller than the internal builder
* data array.
* *
* @param sequence to add * @param sequence to add
* @throws IndexOutOfBoundsException if values overrun array * @throws IndexOutOfBoundsException if values overrun array
...@@ -312,6 +315,15 @@ public final class IntervalArray { ...@@ -312,6 +315,15 @@ public final class IntervalArray {
return this; return this;
} }
/*
* Check hash codes of row arrays in case fromModel or copyOf has been used,
* otherwise check array equality.
*/
IntervalArray validateArray(IntervalArray that) {
checkArgument(Arrays.equals(this.rows, that.rows));
return that;
}
/** /**
* Add each value-pair of the supplied sequence to the appropriate interval. * Add each value-pair of the supplied sequence to the appropriate interval.
* *
...@@ -338,15 +350,6 @@ public final class IntervalArray { ...@@ -338,15 +350,6 @@ public final class IntervalArray {
return this; return this;
} }
/*
* Check hash codes of row arrays in case fromModel or copyOf has been used,
* otherwise check array equality.
*/
IntervalArray validateArray(IntervalArray that) {
checkArgument(Arrays.equals(this.rows, that.rows));
return that;
}
/* /*
* Data is not copied on build() so we dereference data arrays to prevent * Data is not copied on build() so we dereference data arrays to prevent
* lingering builders from further modifying data. * lingering builders from further modifying data.
......
...@@ -378,7 +378,9 @@ public final class IntervalTable { ...@@ -378,7 +378,9 @@ public final class IntervalTable {
} }
/** /**
* Add to the values in the specified row. * Add to the values in the specified row. Assumes that supplied value array
* is the same size or smaller than the columns of the internal builder data
* array.
* *
* @param row key * @param row key
* @param values to add * @param values to add
...@@ -396,7 +398,8 @@ public final class IntervalTable { ...@@ -396,7 +398,8 @@ public final class IntervalTable {
/** /**
* Add the y-values of the supplied sequence to the values in the specified * Add the y-values of the supplied sequence to the values in the specified
* row. * row. Assumes that supplied sequence is the same size or smaller than the
* columns of the internal builder data array.
* *
* @param row key * @param row key
* @param sequence to add * @param sequence to add
...@@ -423,6 +426,16 @@ public final class IntervalTable { ...@@ -423,6 +426,16 @@ public final class IntervalTable {
return this; return this;
} }
/*
* Checks hash codes of row and column arrays in case fromModel or copyOf
* has been used, otherwise check array equality.
*/
IntervalTable validateTable(IntervalTable that) {
checkArgument(Arrays.equals(this.rows, that.rows));
checkArgument(Arrays.equals(this.columns, that.columns));
return that;
}
/** /**
* Multiply ({@code scale}) all values in this builder. * Multiply ({@code scale}) all values in this builder.
* @param scale factor * @param scale factor
...@@ -438,16 +451,6 @@ public final class IntervalTable { ...@@ -438,16 +451,6 @@ public final class IntervalTable {
return this; return this;
} }
/*
* Checks hash codes of row and column arrays in case fromModel or copyOf
* has been used, otherwise check array equality.
*/
IntervalTable validateTable(IntervalTable that) {
checkArgument(Arrays.equals(this.rows, that.rows) &&
Arrays.equals(this.columns, that.columns));
return that;
}
/* /*
* Data is not copied on build() so we dereference data arrays to prevent * Data is not copied on build() so we dereference data arrays to prevent
* lingering builders from further modifying data. * lingering builders from further modifying data.
......
...@@ -127,6 +127,18 @@ class IntervalTableTests { ...@@ -127,6 +127,18 @@ class IntervalTableTests {
assertTrue(expected.equals(actual)); assertTrue(expected.equals(actual));
} }
@Test
final void builderInitTests() {
// test branch where columns have been initialized but not rows.
Builder builder = new Builder()
.columns(COL_MIN, COL_MAX, Δ)
.rows(ROW_MIN, ROW_MAX, Δ);
Arrays.stream(ROWS).forEach(row -> builder.add(row, COLS));
IntervalTable table = builder.build();
assertEquals(table.rows(), TABLE.rows());
assertEquals(table.columns(), TABLE.columns());
}
@Test @Test
final void builderCopyOfTests() { final void builderCopyOfTests() {
IntervalTable actual = Builder.copyOf(TABLE).build(); IntervalTable actual = Builder.copyOf(TABLE).build();
...@@ -174,9 +186,33 @@ class IntervalTableTests { ...@@ -174,9 +186,33 @@ class IntervalTableTests {
assertEquals(expected, actualCol); assertEquals(expected, actualCol);
} }
@Test
final void builderSetTests() {
int index = 0;
double expected = 1.25;
IntervalTable actual = Builder.copyOf(TABLE)
.set(ROWS[index], COLS[index], expected)
.build();
assertEquals(expected, actual.get(ROWS[index], COLS[index]), 0);
assertEquals(expected, actual.get(index, index), 0);
}
@Test @Test
final void builderAddTests() { final void builderAddTests() {
// add(double row, double column, double value) and
// add(int row, int column, double value)
int index = 0; int index = 0;
double addValue = 1.25;
double expect = 5.75;
IntervalTable actual = Builder.copyOf(TABLE)
.add(ROWS[index], COLS[index], addValue)
.build();
assertEquals(expect, actual.get(ROWS[index], COLS[index]), 0);
assertEquals(expect, actual.get(index, index), 0);
double[] expected = Arrays.stream(COLS).map(x -> x + x).toArray(); double[] expected = Arrays.stream(COLS).map(x -> x + x).toArray();
// add(double row, double[] values) // add(double row, double[] values)
...@@ -192,6 +228,43 @@ class IntervalTableTests { ...@@ -192,6 +228,43 @@ class IntervalTableTests {
.build(); .build();
assertArrayEquals(expected, actualB.row(index).yValues().toArray(), 0); assertArrayEquals(expected, actualB.row(index).yValues().toArray(), 0);
// add(IntervalTable)
IntervalTable tableToAdd = Builder.copyOf(TABLE).build();
IntervalTable actualD = Builder.copyOf(TABLE)
.add(tableToAdd)
.build();
assertEquals(9.0, actualD.get(0, 0), 0.0);
}
@Test
final void builderSetData() {
double[][] data = { { 5, 4, 3, 2 }, { 6, 7, 8, 9 }, { 1, 0, -1, -2 } };
IntervalTable ia = Builder.copyOf(TABLE)
.data(data)
.build();
// really should be assertSame, but data is not visible
assertArrayEquals(data[0], ia.row(0).yValues().toArray());
assertArrayEquals(data[1], ia.row(1).yValues().toArray());
assertArrayEquals(data[2], ia.row(2).yValues().toArray());
}
@Test
final void builderMultiplyTests() {
IntervalTable actual = Builder.copyOf(TABLE)
.multiply(3.0)
.build();
assertEquals(13.5, actual.get(0, 0));
assertEquals(22.5, actual.get(3, 3));
}
@Test
final void builderBuiltTests() {
assertThrows(IllegalStateException.class, () -> {
Builder b = Builder.copyOf(TABLE);
b.build();
b.build();
});
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment