From 455cdac6e50c980ea849ff10b020ef0b27ac229a Mon Sep 17 00:00:00 2001
From: Peter Powers <pmpowers@usgs.gov>
Date: Thu, 23 Sep 2021 20:27:25 -0600
Subject: [PATCH] intervalTable tests; other cleaning and comments

---
 .../earthquake/nshmp/data/IntervalArray.java  | 25 ++++---
 .../earthquake/nshmp/data/IntervalTable.java  | 27 ++++---
 .../nshmp/data/IntervalTableTests.java        | 73 +++++++++++++++++++
 3 files changed, 102 insertions(+), 23 deletions(-)

diff --git a/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalArray.java b/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalArray.java
index 8a6858d4..90b92b14 100644
--- a/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalArray.java
+++ b/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalArray.java
@@ -271,7 +271,8 @@ public final class IntervalArray {
     }
 
     /**
-     * Add supplied values to this builder.
+     * Add supplied values to this builder. Assumes that supplied value array is
+     * the same size or smaller than the internal builder data array.
      *
      * @param values to add
      * @throws IndexOutOfBoundsException if values overrun array
@@ -286,7 +287,9 @@ public final class IntervalArray {
     }
 
     /**
-     * Add the y-values of the supplied sequence to this builder.
+     * Add the y-values of the supplied sequence to this builder. Assumes that
+     * supplied sequence is the same size or smaller than the internal builder
+     * data array.
      *
      * @param sequence to add
      * @throws IndexOutOfBoundsException if values overrun array
@@ -312,6 +315,15 @@ public final class IntervalArray {
       return this;
     }
 
+    /*
+     * Check hash codes of row arrays in case fromModel or copyOf has been used,
+     * otherwise check array equality.
+     */
+    IntervalArray validateArray(IntervalArray that) {
+      checkArgument(Arrays.equals(this.rows, that.rows));
+      return that;
+    }
+
     /**
      * Add each value-pair of the supplied sequence to the appropriate interval.
      *
@@ -338,15 +350,6 @@ public final class IntervalArray {
       return this;
     }
 
-    /*
-     * Check hash codes of row arrays in case fromModel or copyOf has been used,
-     * otherwise check array equality.
-     */
-    IntervalArray validateArray(IntervalArray that) {
-      checkArgument(Arrays.equals(this.rows, that.rows));
-      return that;
-    }
-
     /*
      * Data is not copied on build() so we dereference data arrays to prevent
      * lingering builders from further modifying data.
diff --git a/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalTable.java b/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalTable.java
index 3d3530c3..91b490bc 100644
--- a/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalTable.java
+++ b/src/main/java/gov/usgs/earthquake/nshmp/data/IntervalTable.java
@@ -378,7 +378,9 @@ public final class IntervalTable {
     }
 
     /**
-     * Add to the values in the specified row.
+     * Add to the values in the specified row. Assumes that supplied value array
+     * is the same size or smaller than the columns of the internal builder data
+     * array.
      *
      * @param row key
      * @param values to add
@@ -396,7 +398,8 @@ public final class IntervalTable {
 
     /**
      * Add the y-values of the supplied sequence to the values in the specified
-     * row.
+     * row. Assumes that supplied sequence is the same size or smaller than the
+     * columns of the internal builder data array.
      *
      * @param row key
      * @param sequence to add
@@ -423,6 +426,16 @@ public final class IntervalTable {
       return this;
     }
 
+    /*
+     * Checks hash codes of row and column arrays in case fromModel or copyOf
+     * has been used, otherwise check array equality.
+     */
+    IntervalTable validateTable(IntervalTable that) {
+      checkArgument(Arrays.equals(this.rows, that.rows));
+      checkArgument(Arrays.equals(this.columns, that.columns));
+      return that;
+    }
+
     /**
      * Multiply ({@code scale}) all values in this builder.
      * @param scale factor
@@ -438,16 +451,6 @@ public final class IntervalTable {
       return this;
     }
 
-    /*
-     * Checks hash codes of row and column arrays in case fromModel or copyOf
-     * has been used, otherwise check array equality.
-     */
-    IntervalTable validateTable(IntervalTable that) {
-      checkArgument(Arrays.equals(this.rows, that.rows) &&
-          Arrays.equals(this.columns, that.columns));
-      return that;
-    }
-
     /*
      * Data is not copied on build() so we dereference data arrays to prevent
      * lingering builders from further modifying data.
diff --git a/src/test/java/gov/usgs/earthquake/nshmp/data/IntervalTableTests.java b/src/test/java/gov/usgs/earthquake/nshmp/data/IntervalTableTests.java
index 13cf82c1..fc69acff 100644
--- a/src/test/java/gov/usgs/earthquake/nshmp/data/IntervalTableTests.java
+++ b/src/test/java/gov/usgs/earthquake/nshmp/data/IntervalTableTests.java
@@ -127,6 +127,18 @@ class IntervalTableTests {
     assertTrue(expected.equals(actual));
   }
 
+  @Test
+  final void builderInitTests() {
+    // test branch where columns have been initialized but not rows.
+    Builder builder = new Builder()
+        .columns(COL_MIN, COL_MAX, Δ)
+        .rows(ROW_MIN, ROW_MAX, Δ);
+    Arrays.stream(ROWS).forEach(row -> builder.add(row, COLS));
+    IntervalTable table = builder.build();
+    assertEquals(table.rows(), TABLE.rows());
+    assertEquals(table.columns(), TABLE.columns());
+  }
+
   @Test
   final void builderCopyOfTests() {
     IntervalTable actual = Builder.copyOf(TABLE).build();
@@ -174,9 +186,33 @@ class IntervalTableTests {
     assertEquals(expected, actualCol);
   }
 
+  @Test
+  final void builderSetTests() {
+    int index = 0;
+    double expected = 1.25;
+    IntervalTable actual = Builder.copyOf(TABLE)
+        .set(ROWS[index], COLS[index], expected)
+        .build();
+
+    assertEquals(expected, actual.get(ROWS[index], COLS[index]), 0);
+    assertEquals(expected, actual.get(index, index), 0);
+  }
+
   @Test
   final void builderAddTests() {
+
+    // add(double row, double column, double value) and
+    // add(int row, int column, double value)
     int index = 0;
+    double addValue = 1.25;
+    double expect = 5.75;
+    IntervalTable actual = Builder.copyOf(TABLE)
+        .add(ROWS[index], COLS[index], addValue)
+        .build();
+
+    assertEquals(expect, actual.get(ROWS[index], COLS[index]), 0);
+    assertEquals(expect, actual.get(index, index), 0);
+
     double[] expected = Arrays.stream(COLS).map(x -> x + x).toArray();
 
     // add(double row, double[] values)
@@ -192,6 +228,43 @@ class IntervalTableTests {
         .build();
 
     assertArrayEquals(expected, actualB.row(index).yValues().toArray(), 0);
+
+    // add(IntervalTable)
+    IntervalTable tableToAdd = Builder.copyOf(TABLE).build();
+    IntervalTable actualD = Builder.copyOf(TABLE)
+        .add(tableToAdd)
+        .build();
+    assertEquals(9.0, actualD.get(0, 0), 0.0);
+  }
+
+  @Test
+  final void builderSetData() {
+    double[][] data = { { 5, 4, 3, 2 }, { 6, 7, 8, 9 }, { 1, 0, -1, -2 } };
+    IntervalTable ia = Builder.copyOf(TABLE)
+        .data(data)
+        .build();
+    // really should be assertSame, but data is not visible
+    assertArrayEquals(data[0], ia.row(0).yValues().toArray());
+    assertArrayEquals(data[1], ia.row(1).yValues().toArray());
+    assertArrayEquals(data[2], ia.row(2).yValues().toArray());
+  }
+
+  @Test
+  final void builderMultiplyTests() {
+    IntervalTable actual = Builder.copyOf(TABLE)
+        .multiply(3.0)
+        .build();
+    assertEquals(13.5, actual.get(0, 0));
+    assertEquals(22.5, actual.get(3, 3));
+  }
+
+  @Test
+  final void builderBuiltTests() {
+    assertThrows(IllegalStateException.class, () -> {
+      Builder b = Builder.copyOf(TABLE);
+      b.build();
+      b.build();
+    });
   }
 
 }
-- 
GitLab