Skip to content

Commit

Permalink
Fix, document, and test ColumnType.compare and Table.compareRows (#21)
Browse files Browse the repository at this point in the history
* Fix document and test ColumnType.compare and Table.compareRows

* Fix sonar comment

* More explicit test name
  • Loading branch information
ccleva committed Jan 8, 2025
1 parent a1c9e2d commit fc73dd5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
15 changes: 11 additions & 4 deletions core/src/main/java/tech/tablesaw/api/ColumnType.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,17 @@ 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);
/**
* 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
*/
default boolean compare(int rowNumber, Column<?> column1, Column<?> column2) {
Object o1 = column2.get(rowNumber);
Object o2 = column1.get(rowNumber);
return o1 == null ? o2 == null : o1.equals(o2);
}

Expand Down
17 changes: 10 additions & 7 deletions core/src/main/java/tech/tablesaw/api/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,17 +529,20 @@ 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 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
*/
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), table1.column(columnIndex))) {
return false;
}
}
Expand Down
56 changes: 56 additions & 0 deletions core/src/test/java/tech/tablesaw/api/TableCompareRowsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tech.tablesaw.api;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import tech.tablesaw.io.csv.CsvReadOptions;

class TableCompareRowsTest {

private static final String SOURCE_FILE_NAME = "../data/missing_values.csv";
private static Table testTable;

@BeforeAll
static void readTables() {
testTable = Table.read().usingOptions(CsvReadOptions
.builder(new File(SOURCE_FILE_NAME))
.missingValueIndicator("-"));
}

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

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

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

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

}

0 comments on commit fc73dd5

Please sign in to comment.