Skip to content

Commit 3e7f248

Browse files
awalter17imagejan
authored andcommitted
Add tests for remove/append a row/column to tables
1 parent 459436c commit 3e7f248

8 files changed

Lines changed: 825 additions & 1 deletion

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,66 @@ public void testGetColumnType() {
8888
assertEquals(col.getType(), Boolean.class);
8989
}
9090

91+
@Test
92+
public void testAppendColumn() {
93+
final BoolTable table = createTable();
94+
final Boolean[] values =
95+
{ true, true, true, true, false, true, false, true };
96+
97+
final BoolColumn col = table.appendColumn("Header4");
98+
col.fill(values);
99+
100+
// Test appending a column
101+
assertEquals(table.getColumnCount(), 4);
102+
assertEquals(table.get(3).getHeader(), "Header4");
103+
104+
checkTableModifiedColumn(table, values, 3);
105+
}
106+
107+
@Test
108+
public void testRemoveColumn() {
109+
final BoolTable table = createTable();
110+
final BoolColumn col = table.removeColumn(2);
111+
112+
// Test removing a column
113+
for (int i = 0; i < col.size(); i++) {
114+
assertEquals(col.getValue(i), DATA[i][2]);
115+
}
116+
assertEquals(table.getColumnCount(), 2);
117+
118+
checkTableModifiedColumn(table, null, 2);
119+
}
120+
121+
@Test
122+
public void testAppendRow() {
123+
final BoolTable table = createTable();
124+
final boolean[] values = { true, true, false };
125+
126+
// Test appending a row
127+
table.appendRow();
128+
assertEquals(table.getRowCount(), 9);
129+
for (int i = 0; i < values.length; i++) {
130+
table.setValue(i, 8, values[i]);
131+
assertEquals(table.getValue(i, 8), values[i]);
132+
}
133+
134+
checkTableModifiedRow(table, values, 8);
135+
}
136+
137+
@Test
138+
public void testRemoveRow() {
139+
final BoolTable table = createTable();
140+
141+
table.removeRow(1);
142+
143+
assertEquals(table.getRowCount(), 7);
144+
for (int i = 0; i < table.getColumnCount(); i++) {
145+
assertEquals(table.getValue(i, 1), DATA[2][i]);
146+
}
147+
148+
checkTableModifiedRow(table, null, 1);
149+
}
150+
91151
// TODO - Add more tests.
92152

93153
// -- Helper methods --
@@ -108,4 +168,46 @@ private BoolTable createTable() {
108168
return table;
109169
}
110170

171+
private void checkTableModifiedColumn(final BoolTable table,
172+
final Boolean[] values, final int mod)
173+
{
174+
for (int r = 0; r < table.getRowCount(); r++) {
175+
for (int c = 0; c < table.getColumnCount(); c++) {
176+
if ( c == mod && values != null ) {
177+
assertEquals(table.getValue(c, r), values[r]);
178+
}
179+
else if ( c > mod && values != null ) {
180+
assertEquals(table.getValue(c, r), DATA[r][c - 1]);
181+
}
182+
else if ( c >= mod && values == null ) {
183+
assertEquals(table.getValue(c, r), DATA[r][c + 1]);
184+
}
185+
else {
186+
assertEquals(table.getValue(c, r), DATA[r][c]);
187+
}
188+
}
189+
}
190+
}
191+
192+
private void checkTableModifiedRow(final BoolTable table,
193+
final boolean[] values, final int mod)
194+
{
195+
for (int r = 0; r < table.getRowCount(); r++) {
196+
for (int c = 0; c < table.getColumnCount(); c++) {
197+
if ( r == mod && values != null ) {
198+
assertEquals(table.getValue(c, r), values[c]);
199+
}
200+
else if ( r > mod && values != null) {
201+
assertEquals(table.getValue(c, r), DATA[r-1][c]);
202+
}
203+
else if ( r >= mod && values == null ) {
204+
assertEquals(table.getValue(c, r), DATA[r+1][c]);
205+
}
206+
else {
207+
assertEquals(table.getValue(c, r), DATA[r][c]);
208+
}
209+
}
210+
}
211+
}
212+
111213
}

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,66 @@ public void testGetColumnType() {
8787
assertEquals(col.getType(), Byte.class);
8888
}
8989

90+
@Test
91+
public void testAppendColumn() {
92+
final ByteTable table = createTable();
93+
final Byte[] values = { 17, 23, -12, 0, -93, -7, 127 };
94+
95+
final ByteColumn col = table.appendColumn("Header3");
96+
col.fill(values);
97+
98+
// Test appending a column
99+
assertEquals(table.getColumnCount(), 3);
100+
assertEquals(table.get(2).getHeader(), "Header3");
101+
102+
checkTableModifiedColumn(table, values, 2);
103+
}
104+
105+
@Test
106+
public void testRemoveColumn() {
107+
final ByteTable table = createTable();
108+
final ByteColumn col = table.removeColumn(1);
109+
110+
// Test removing a column
111+
for (int i = 0; i < col.size(); i++) {
112+
assertEquals(col.getValue(i), DATA[i][1]);
113+
}
114+
assertEquals(table.getColumnCount(), 1);
115+
116+
checkTableModifiedColumn(table, null, 1);
117+
}
118+
119+
@Test
120+
public void testAppendRow() {
121+
final ByteTable table = createTable();
122+
final byte[] values = { 79, 8 };
123+
124+
// Test appending a row
125+
table.appendRow();
126+
assertEquals(table.getRowCount(), 8);
127+
for (int i = 0; i < values.length; i++) {
128+
table.setValue(i, 7, values[i]);
129+
assertEquals(table.getValue(i, 7), values[i]);
130+
}
131+
132+
checkTableModifiedRow(table, values, 7);
133+
}
134+
135+
@Test
136+
public void testRemoveRow() {
137+
final ByteTable table = createTable();
138+
139+
// Test removing a row
140+
table.removeRow(2);
141+
142+
assertEquals(table.getRowCount(), 6);
143+
for (int i = 0; i < table.getColumnCount(); i++) {
144+
assertEquals(table.getValue(i, 2), DATA[3][i]);
145+
}
146+
147+
checkTableModifiedRow(table, null, 2);
148+
}
149+
90150
// TODO - Add more tests.
91151

92152
// -- Helper methods --
@@ -107,4 +167,46 @@ private ByteTable createTable() {
107167
return table;
108168
}
109169

170+
private void checkTableModifiedColumn(final ByteTable table,
171+
final Byte[] values, final int mod)
172+
{
173+
for (int r = 0; r < table.getRowCount(); r++) {
174+
for (int c = 0; c < table.getColumnCount(); c++) {
175+
if ( c == mod && values != null ) {
176+
assertEquals(table.getValue(c, r), values[r].byteValue());
177+
}
178+
else if ( c > mod && values != null ) {
179+
assertEquals(table.getValue(c, r), DATA[r][c - 1]);
180+
}
181+
else if ( c >= mod && values == null ) {
182+
assertEquals(table.getValue(c, r), DATA[r][c + 1]);
183+
}
184+
else {
185+
assertEquals(table.getValue(c, r), DATA[r][c]);
186+
}
187+
}
188+
}
189+
}
190+
191+
private void checkTableModifiedRow(final ByteTable table,
192+
final byte[] values, final int mod)
193+
{
194+
for (int r = 0; r < table.getRowCount(); r++) {
195+
for (int c = 0; c < table.getColumnCount(); c++) {
196+
if ( r == mod && values != null ) {
197+
assertEquals(table.getValue(c, r), values[c]);
198+
}
199+
else if ( r > mod && values != null) {
200+
assertEquals(table.getValue(c, r), DATA[r-1][c]);
201+
}
202+
else if ( r >= mod && values == null ) {
203+
assertEquals(table.getValue(c, r), DATA[r+1][c]);
204+
}
205+
else {
206+
assertEquals(table.getValue(c, r), DATA[r][c]);
207+
}
208+
}
209+
}
210+
}
211+
110212
}

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,66 @@ public void testGetColumnType() {
8989
assertEquals(col.getType(), Character.class);
9090
}
9191

92+
@Test
93+
public void testAppendColumn() {
94+
final CharTable table = createTable();
95+
final Character[] values = { '2', 'W', '!', '*', 'o', 'E', ' ', 'A', '\t' };
96+
97+
final CharColumn col = table.appendColumn("Header4");
98+
col.fill(values);
99+
100+
// Test appending a column
101+
assertEquals(table.getColumnCount(), 4);
102+
assertEquals(table.get(3).getHeader(), "Header4");
103+
104+
checkTableModifiedColumn(table, values, 3);
105+
}
106+
107+
@Test
108+
public void testRemoveColumn() {
109+
final CharTable table = createTable();
110+
111+
final CharColumn col = table.removeColumn(2);
112+
113+
// Test removing a column
114+
for (int i = 0; i < col.size(); i++) {
115+
assertEquals(col.getValue(i), DATA[i][2]);
116+
}
117+
assertEquals(table.getColumnCount(), 2);
118+
119+
checkTableModifiedColumn(table, null, 2);
120+
}
121+
122+
@Test
123+
public void testAppendRow() {
124+
final CharTable table = createTable();
125+
final char[] values = { '\t', '\uffff', '\u0000' };
126+
127+
// Test appending a row
128+
table.appendRow();
129+
assertEquals(table.getRowCount(), 10);
130+
for (int i = 0; i < values.length; i++) {
131+
table.setValue(i, 9, values[i]);
132+
assertEquals(table.getValue(i, 9), values[i]);
133+
}
134+
135+
checkTableModifiedRow(table, values, 9);
136+
}
137+
138+
@Test
139+
public void testRemoveRow() {
140+
final CharTable table = createTable();
141+
142+
// Test removing a row
143+
table.removeRow(7);
144+
assertEquals(table.getRowCount(), 8);
145+
for (int i = 0; i < table.getColumnCount(); i++) {
146+
assertEquals(table.getValue(i, 7), DATA[8][i]);
147+
}
148+
149+
checkTableModifiedRow(table, null, 7);
150+
}
151+
92152
// TODO - Add more tests.
93153

94154
// -- Helper methods --
@@ -109,4 +169,46 @@ private CharTable createTable() {
109169
return table;
110170
}
111171

172+
private void checkTableModifiedColumn(final CharTable table,
173+
final Character[] values, final int mod)
174+
{
175+
for (int r = 0; r < table.getRowCount(); r++) {
176+
for (int c = 0; c < table.getColumnCount(); c++) {
177+
if ( c == mod && values != null ) {
178+
assertEquals(table.getValue(c, r), values[r].charValue());
179+
}
180+
else if ( c > mod && values != null ) {
181+
assertEquals(table.getValue(c, r), DATA[r][c - 1]);
182+
}
183+
else if ( c >= mod && values == null ) {
184+
assertEquals(table.getValue(c, r), DATA[r][c + 1]);
185+
}
186+
else {
187+
assertEquals(table.getValue(c, r), DATA[r][c]);
188+
}
189+
}
190+
}
191+
}
192+
193+
private void checkTableModifiedRow(final CharTable table, final char[] values,
194+
final int mod)
195+
{
196+
for (int r = 0; r < table.getRowCount(); r++) {
197+
for (int c = 0; c < table.getColumnCount(); c++) {
198+
if ( r == mod && values != null ) {
199+
assertEquals(table.getValue(c, r), values[c]);
200+
}
201+
else if ( r > mod && values != null) {
202+
assertEquals(table.getValue(c, r), DATA[r-1][c]);
203+
}
204+
else if ( r >= mod && values == null ) {
205+
assertEquals(table.getValue(c, r), DATA[r+1][c]);
206+
}
207+
else {
208+
assertEquals(table.getValue(c, r), DATA[r][c]);
209+
}
210+
}
211+
}
212+
}
213+
112214
}

0 commit comments

Comments
 (0)