Skip to content

Commit 7f2cb1e

Browse files
awalter17imagejan
authored andcommitted
Add tests for modifying multiple rows/columns
1 parent ae714af commit 7f2cb1e

File tree

7 files changed

+1131
-0
lines changed

7 files changed

+1131
-0
lines changed

src/test/java/net/imagej/table/DefaultBoolTableTest.java

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertSame;
3636

37+
import java.util.List;
38+
3739
import org.junit.Test;
3840

3941
/**
@@ -133,6 +135,78 @@ public void testInsertColumn() {
133135
checkTableModifiedColumn(table, values, 1);
134136
}
135137

138+
@Test
139+
public void testAppendColumns() {
140+
final BoolTable table = createTable();
141+
final boolean[][] values =
142+
{
143+
{ true, true, true, true, false, true, false, true },
144+
{ true, false, true, false, false, true, false, true },
145+
{ false, true, true, true, false, true, false, false },
146+
{ true, false, false, true, false, false, false, true } };
147+
148+
final String[] headers = { "Header4", "Header5", "Header6", "Header7" };
149+
final List<BoolColumn> col = table.appendColumns(headers);
150+
col.get(0).fill(values[0]);
151+
col.get(1).fill(values[1]);
152+
col.get(2).fill(values[2]);
153+
col.get(3).fill(values[3]);
154+
155+
// Test appending a column
156+
assertEquals(table.getColumnCount(), 7);
157+
assertEquals(table.get(3).getHeader(), "Header4");
158+
assertEquals(table.get(4).getHeader(), "Header5");
159+
assertEquals(table.get(5).getHeader(), "Header6");
160+
assertEquals(table.get(6).getHeader(), "Header7");
161+
162+
checkTableModifiedColumns(table, values, 3, 6);
163+
}
164+
165+
@Test
166+
public void testRemoveColumns() {
167+
final BoolTable table = createTable();
168+
169+
final List<BoolColumn> col = table.removeColumns(1, 2);
170+
171+
// Test removing a column
172+
for (int q = 0; q < col.size(); q++) {
173+
for (int i = 0; i < col.get(0).size(); i++) {
174+
assertEquals(col.get(q).getValue(i), DATA[i][q + 1]);
175+
}
176+
}
177+
assertEquals(table.getColumnCount(), 1);
178+
179+
checkTableModifiedColumns(table, null, 1, 3);
180+
}
181+
182+
@Test
183+
public void testInsertColumns() {
184+
final BoolTable table = createTable();
185+
final boolean[][] values =
186+
{
187+
{ true, true, true, true, false, true, false, true },
188+
{ true, false, true, false, false, true, false, true },
189+
{ false, true, true, true, false, true, false, false },
190+
{ true, false, false, true, false, false, false, true } };
191+
192+
193+
final String[] headers = { "Header4", "Header5", "Header6", "Header7" };
194+
final List<BoolColumn> col = table.insertColumns(0, headers);
195+
col.get(0).fill(values[0]);
196+
col.get(1).fill(values[1]);
197+
col.get(2).fill(values[2]);
198+
col.get(3).fill(values[3]);
199+
200+
// Test appending a column
201+
assertEquals(table.getColumnCount(), 7);
202+
assertEquals(table.get(0).getHeader(), "Header4");
203+
assertEquals(table.get(1).getHeader(), "Header5");
204+
assertEquals(table.get(2).getHeader(), "Header6");
205+
assertEquals(table.get(3).getHeader(), "Header7");
206+
207+
checkTableModifiedColumns(table, values, 0, 3);
208+
}
209+
136210
@Test
137211
public void testAppendRow() {
138212
final BoolTable table = createTable();
@@ -178,6 +252,55 @@ public void testInsertRow() {
178252
checkTableModifiedRow(table, values, 6);
179253
}
180254

255+
@Test
256+
public void testAppendRows() {
257+
final BoolTable table = createTable();
258+
final boolean[][] values =
259+
{ { true, true, false }, { false, true, false }, { true, false, true },
260+
{ true, false, false }, { false, false, false } };
261+
262+
// Test appending a row
263+
table.appendRows(5);
264+
assertEquals(table.getRowCount(), 13);
265+
for (int r = 0; r < values.length; r++) {
266+
for (int c = 0; c < values[0].length; c++) {
267+
table.setValue(c, r + 8, values[r][c]);
268+
assertEquals(table.getValue(c, r + 8), values[r][c]);
269+
}
270+
}
271+
272+
checkTableModifiedRows(table, values, 8, 12);
273+
}
274+
275+
@Test
276+
public void testRemoveRows() {
277+
final BoolTable table = createTable();
278+
table.removeRows(4, 3);
279+
assertEquals(table.getRowCount(), 5);
280+
281+
checkTableModifiedRows(table, null, 4, 6);
282+
}
283+
284+
@Test
285+
public void testInsertRows() {
286+
final BoolTable table = createTable();
287+
final boolean[][] values =
288+
{ { true, true, false }, { false, true, false }, { true, false, true },
289+
{ true, false, false }, { false, false, false } };
290+
291+
table.insertRows(5, 5);
292+
293+
assertEquals(table.getRowCount(), 13);
294+
for (int r = 0; r < values.length; r++) {
295+
for (int c = 0; c < values[0].length; c++) {
296+
table.setValue(c, r + 5, values[r][c]);
297+
assertEquals(table.getValue(c, r + 5), values[r][c]);
298+
}
299+
}
300+
301+
checkTableModifiedRows(table, values, 5, 9);
302+
}
303+
181304
// TODO - Add more tests.
182305

183306
// -- Helper methods --
@@ -240,4 +363,47 @@ else if ( r >= mod && values == null ) {
240363
}
241364
}
242365

366+
private void checkTableModifiedColumns(final BoolTable table,
367+
final boolean[][] values, final int startMod, final int endMod)
368+
{
369+
for (int r = 0; r < table.getRowCount(); r++) {
370+
for (int c = 0; c < table.getColumnCount(); c++) {
371+
if (c >= startMod && c <= endMod && values != null) {
372+
assertEquals(table.getValue(c, r), values[c - startMod][r]);
373+
}
374+
else if (c > endMod && values != null) {
375+
assertEquals(table.getValue(c, r), DATA[r][c - values.length]);
376+
}
377+
else if (c >= startMod && values == null) {
378+
assertEquals(table.getValue(c, r), DATA[r][c + (endMod - startMod)]);
379+
}
380+
else {
381+
assertEquals(table.getValue(c, r), DATA[r][c]);
382+
}
383+
}
384+
}
385+
}
386+
387+
private void checkTableModifiedRows(final BoolTable table,
388+
final boolean[][] values, final int startMod, final int endMod)
389+
{
390+
for (int r = 0; r < table.getRowCount(); r++) {
391+
for (int c = 0; c < table.getColumnCount(); c++) {
392+
if (r >= startMod && r <= endMod && values != null) {
393+
assertEquals(table.getValue(c, r), values[r - startMod][c]);
394+
}
395+
else if (r > endMod && values != null) {
396+
assertEquals(table.getValue(c, r), DATA[r - values.length][c]);
397+
}
398+
else if (r >= startMod && values == null) {
399+
assertEquals(table.getValue(c, r),
400+
DATA[r + (endMod - startMod + 1)][c]);
401+
}
402+
else {
403+
assertEquals(table.getValue(c, r), DATA[r][c]);
404+
}
405+
}
406+
}
407+
}
408+
243409
}

src/test/java/net/imagej/table/DefaultByteTableTest.java

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertSame;
3636

37+
import java.util.List;
38+
3739
import org.junit.Test;
3840

3941
/**
@@ -130,6 +132,70 @@ public void testInsertColumn() {
130132
checkTableModifiedColumn(table, values, 0);
131133
}
132134

135+
@Test
136+
public void testAppendColumns() {
137+
final ByteTable table = createTable();
138+
final byte[][] values =
139+
{
140+
{ 17, 23, -12, 0, -93, -7, 127 },
141+
{ -100, 54, 93, -2, 111, -86, -74 },
142+
{ -12, 120, 6, 8, -4, -121, 13 } };
143+
144+
final String[] headers = { "Header3", "Header4", "Header5" };
145+
final List<ByteColumn> col = table.appendColumns(headers);
146+
col.get(0).fill(values[0]);
147+
col.get(1).fill(values[1]);
148+
col.get(2).fill(values[2]);
149+
150+
// Test appending a column
151+
assertEquals(table.getColumnCount(), 5);
152+
assertEquals(table.get(2).getHeader(), "Header3");
153+
assertEquals(table.get(3).getHeader(), "Header4");
154+
assertEquals(table.get(4).getHeader(), "Header5");
155+
156+
checkTableModifiedColumns(table, values, 2, 4);
157+
}
158+
159+
@Test
160+
public void testRemoveColumns() {
161+
final ByteTable table = createTable();
162+
163+
final List<ByteColumn> col = table.removeColumns(0, 2);
164+
165+
// Test removing a column
166+
for (int q = 0; q < col.size(); q++) {
167+
for (int i = 0; i < col.get(0).size(); i++) {
168+
assertEquals(col.get(q).getValue(i), DATA[i][q]);
169+
}
170+
}
171+
assertEquals(table.getColumnCount(), 0);
172+
173+
checkTableModifiedColumns(table, null, 0, 1);
174+
}
175+
176+
@Test
177+
public void testInsertColumns() {
178+
final ByteTable table = createTable();
179+
final byte[][] values =
180+
{
181+
{ 17, 23, -12, 0, -93, -7, 127 },
182+
{ -100, 54, 93, -2, 111, -86, -74 },
183+
{ -12, 120, 6, 8, -4, -121, 13 } };
184+
185+
final String[] headers = { "Header3", "Header4", "Header5" };
186+
final List<ByteColumn> col = table.insertColumns(1, headers);
187+
col.get(0).fill(values[0]);
188+
col.get(1).fill(values[1]);
189+
col.get(2).fill(values[2]);
190+
191+
assertEquals(table.getColumnCount(), 5);
192+
assertEquals(table.get(1).getHeader(), "Header3");
193+
assertEquals(table.get(2).getHeader(), "Header4");
194+
assertEquals(table.get(3).getHeader(), "Header5");
195+
196+
checkTableModifiedColumns(table, values, 1, 3);
197+
}
198+
133199
@Test
134200
public void testAppendRow() {
135201
final ByteTable table = createTable();
@@ -176,6 +242,55 @@ public void testInsertRow() {
176242
checkTableModifiedRow(table, values, 5);
177243
}
178244

245+
@Test
246+
public void testAppendRows() {
247+
final ByteTable table = createTable();
248+
final byte[][] values =
249+
{ { 79, 8 }, { 100, -12 }, { 54, 36 }, { -100, -86 }, { -43, 60 },
250+
{ 92, -122 } };
251+
252+
// Test appending a row
253+
table.appendRows(6);
254+
assertEquals(table.getRowCount(), 13);
255+
for (int r = 0; r < values.length; r++) {
256+
for (int c = 0; c < values[0].length; c++) {
257+
table.setValue(c, r + 7, values[r][c]);
258+
assertEquals(table.getValue(c, r + 7), values[r][c]);
259+
}
260+
}
261+
262+
checkTableModifiedRows(table, values, 7, 12);
263+
}
264+
265+
@Test
266+
public void testRemoveRows() {
267+
final ByteTable table = createTable();
268+
table.removeRows(2, 3);
269+
assertEquals(table.getRowCount(), 4);
270+
271+
checkTableModifiedRows(table, null, 2, 4);
272+
}
273+
274+
@Test
275+
public void testInsertRows() {
276+
final ByteTable table = createTable();
277+
final byte[][] values =
278+
{ { 79, 8 }, { 100, -12 }, { 54, 36 }, { -100, -86 }, { -43, 60 },
279+
{ 92, -122 } };
280+
281+
table.insertRows(4, 6);
282+
283+
assertEquals(table.getRowCount(), 13);
284+
for (int r = 0; r < values.length; r++) {
285+
for (int c = 0; c < values[0].length; c++) {
286+
table.setValue(c, r + 4, values[r][c]);
287+
assertEquals(table.getValue(c, r + 4), values[r][c]);
288+
}
289+
}
290+
291+
checkTableModifiedRows(table, values, 4, 9);
292+
}
293+
179294
// TODO - Add more tests.
180295

181296
// -- Helper methods --
@@ -238,4 +353,47 @@ else if ( r >= mod && values == null ) {
238353
}
239354
}
240355

356+
private void checkTableModifiedColumns(final ByteTable table,
357+
final byte[][] values, final int startMod, final int endMod)
358+
{
359+
for (int r = 0; r < table.getRowCount(); r++) {
360+
for (int c = 0; c < table.getColumnCount(); c++) {
361+
if (c >= startMod && c <= endMod && values != null) {
362+
assertEquals(table.getValue(c, r), values[c - startMod][r]);
363+
}
364+
else if (c > endMod && values != null) {
365+
assertEquals(table.getValue(c, r), DATA[r][c - values.length]);
366+
}
367+
else if (c >= startMod && values == null) {
368+
assertEquals(table.getValue(c, r), DATA[r][c + (endMod - startMod)]);
369+
}
370+
else {
371+
assertEquals(table.getValue(c, r), DATA[r][c]);
372+
}
373+
}
374+
}
375+
}
376+
377+
private void checkTableModifiedRows(final ByteTable table,
378+
final byte[][] values, final int startMod, final int endMod)
379+
{
380+
for (int r = 0; r < table.getRowCount(); r++) {
381+
for (int c = 0; c < table.getColumnCount(); c++) {
382+
if (r >= startMod && r <= endMod && values != null) {
383+
assertEquals(table.getValue(c, r), values[r - startMod][c]);
384+
}
385+
else if (r > endMod && values != null) {
386+
assertEquals(table.getValue(c, r), DATA[r - values.length][c]);
387+
}
388+
else if (r >= startMod && values == null) {
389+
assertEquals(table.getValue(c, r),
390+
DATA[r + (endMod - startMod + 1)][c]);
391+
}
392+
else {
393+
assertEquals(table.getValue(c, r), DATA[r][c]);
394+
}
395+
}
396+
}
397+
}
398+
241399
}

0 commit comments

Comments
 (0)