diff --git a/core/src/main/java/tech/tablesaw/api/ColumnType.java b/core/src/main/java/tech/tablesaw/api/ColumnType.java index 0ca61683f..12d22b7a0 100644 --- a/core/src/main/java/tech/tablesaw/api/ColumnType.java +++ b/core/src/main/java/tech/tablesaw/api/ColumnType.java @@ -82,16 +82,11 @@ static ColumnType valueOf(String name) { /** TODO: Research this method to provide a good comment */ AbstractColumnParser customParser(ReadOptions options); - /** TODO: Research this method to provide a good comment */ - default boolean compare(int rowNumber, Column temp, Column original) { - Object o1 = original.get(rowNumber); - Object o2 = temp.get(temp.size() - 1); - return o1 == null ? o2 == null : o1.equals(o2); - } - /** * Returns true if the value at the specified index in column1 is equal to the value at the * specified index in column 2 + * + * @throws {@code IndexOutOfBoundsException} if either index exceeds the corresponding column size */ default boolean compare(int col1Row, Column col1, int col2Row, Column col2) { Object o1 = col1.get(col1Row); diff --git a/core/src/main/java/tech/tablesaw/api/Table.java b/core/src/main/java/tech/tablesaw/api/Table.java index f507749c4..5cd842503 100644 --- a/core/src/main/java/tech/tablesaw/api/Table.java +++ b/core/src/main/java/tech/tablesaw/api/Table.java @@ -529,17 +529,22 @@ public void copyRowsToTable(int[] rows, Table newTable) { } /** - * Returns {@code true} if the row @rowNumber in table1 holds the same data as the row at - * rowNumber in table2 + * 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 {@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) { - int columnCount = table1.columnCount(); - boolean result; + final int columnCount = table1.columnCount(); + if (columnCount != table2.columnCount()) { + return false; + } for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { ColumnType columnType = table1.column(columnIndex).type(); - result = - columnType.compare(rowNumber, table2.column(columnIndex), table1.column(columnIndex)); - if (!result) { + if (!columnType.compare( + rowNumber, table2.column(columnIndex), rowNumber, table1.column(columnIndex))) { return false; } } diff --git a/core/src/test/java/tech/tablesaw/api/TableTest.java b/core/src/test/java/tech/tablesaw/api/TableTest.java index d659a0ead..51c8b90e8 100644 --- a/core/src/test/java/tech/tablesaw/api/TableTest.java +++ b/core/src/test/java/tech/tablesaw/api/TableTest.java @@ -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"); @@ -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 @@ -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"); + } }