Skip to content

Commit

Permalink
Test migrated to TableTest. Correct Javadoc and code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccleva committed Jan 8, 2025
1 parent fc73dd5 commit 450e18d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 69 deletions.
8 changes: 2 additions & 6 deletions core/src/main/java/tech/tablesaw/api/ColumnType.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,8 @@ static ColumnType valueOf(String name) {
AbstractColumnParser<?> customParser(ReadOptions options);

/**
* Compare the row at {@code rownumber} in {@code column1} and {@code column2} and returns whether they are equals.
* @param rowNumber the row to compare
* @param column1 the first column to compare
* @param column2 the second column to compare
* @return true if row {@code rownumber} is equals in both columns
* @throws {@code IndexOutOfBoundsException} if {@code rownumber} exceeds either column size
* Compares the row at {@code rowNumber} in {@code column1} and {@code column2} and returns whether they are equal.
* @throws {@code IndexOutOfBoundsException} if {@code rowNumber} exceeds either column size
*/
default boolean compare(int rowNumber, Column<?> column1, Column<?> column2) {
Object o1 = column2.get(rowNumber);
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/tech/tablesaw/api/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,16 +530,14 @@ public void copyRowsToTable(int[] rows, Table newTable) {

/**
* Returns {@code true} if the row {@code rowNumber} in {@code table1} holds the same values than the row at
* {@code rowNumber} in {@code table2}. Returns false if the number of columns is different in the two tables.
* @param rowNumber the row to compare
* @param table1 the first table to compare
* @param table2 the second table to compare
* @return false if row {@code rowNumber} is different in {@code table1} and {@code table2}
* @throws {@code IndexOutOfBoundsException} if {@code rownumber} exceeds either table number of rows
* {@code rowNumber} in {@code table2}. Returns {@code false} if the number of columns is different in the two tables.
* @throws {@code IndexOutOfBoundsException} if {@code rowNumber} exceeds either table number of rows
*/
public static boolean compareRows(int rowNumber, Table table1, Table table2) {
final int columnCount = table1.columnCount();
if (columnCount != table2.columnCount()) return false;
if (columnCount != table2.columnCount()) {
return false;
}
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
ColumnType columnType = table1.column(columnIndex).type();
if (!columnType.compare(rowNumber, table2.column(columnIndex), table1.column(columnIndex))) {
Expand Down
56 changes: 0 additions & 56 deletions core/src/test/java/tech/tablesaw/api/TableCompareRowsTest.java

This file was deleted.

35 changes: 35 additions & 0 deletions core/src/test/java/tech/tablesaw/api/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class TableTest {
private static final ColumnType[] BUSH_COLUMN_TYPES = {LOCAL_DATE, INTEGER, STRING};
private static Table bush;
private static Table bushMinimized;
private static Table missingValues;

private Table table;
private final DoubleColumn f1 = DoubleColumn.create("f1");
Expand All @@ -64,6 +65,8 @@ static void readTables() {
.columnTypes(BUSH_COLUMN_TYPES));
ColumnType[] types = {LOCAL_DATE, SHORT, STRING};
bushMinimized = Table.read().csv(CsvReadOptions.builder("../data/bush.csv").columnTypes(types));
missingValues = Table.read().csv(CsvReadOptions.builder("../data/missing_values.csv")
.missingValueIndicator("-"));
}

@BeforeEach
Expand Down Expand Up @@ -916,4 +919,36 @@ public void testToStringColumnsWithVaryingSizes() {
fail("toString shouldn't throw " + e);
}
}

@Test
void testCompareRowsIdentical() {
for(int i = 0; i < missingValues.rowCount(); i++) {
assertTrue(Table.compareRows(i, missingValues, missingValues), "Row " + i + " is not equal to itself");
}
}

@Test
void testCompareRowsDifferent() {
Table differentTable = missingValues.copy().sortDescendingOn("Sales");
for(int i = 0; i < missingValues.rowCount(); i++) {
assertFalse(Table.compareRows(i, missingValues, differentTable), "Row " + i + " is equal to a different row");
}
}

@Test
void testCompareRowsDifferentColumns() {
Table differentTable = missingValues.copy().removeColumns("Sales");
for(int i = 0; i < missingValues.rowCount(); i++) {
assertFalse(Table.compareRows(i, missingValues, differentTable), "Row " + i + " is equal to a row with less columns");
}
}

@Test
void testCompareRowsOutOfBound() {
Table differentTable = missingValues.copy().dropRows(0);
int lastRowNumber = missingValues.rowCount() - 1;
assertThrows(IndexOutOfBoundsException.class,
() -> Table.compareRows(lastRowNumber, missingValues, differentTable),
"Row outside range does not throw exception");
}
}

0 comments on commit 450e18d

Please sign in to comment.