-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand unit test coverage of utility classes
- Loading branch information
Showing
4 changed files
with
283 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/common/UtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package ca.bc.gov.nrs.vdyp.io.common; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasEntry; | ||
import static org.hamcrest.Matchers.hasProperty; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.stringContainsInOrder; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import ca.bc.gov.nrs.vdyp.common.Utils; | ||
|
||
public class UtilsTest { | ||
|
||
@Test | ||
void testSingletonOrEmptyForNull() { | ||
var result = Utils.singletonOrEmpty(null); | ||
assertThat(result, Matchers.empty()); | ||
} | ||
|
||
@Test | ||
void testSingletonOrEmptyForNonNull() { | ||
var result = Utils.singletonOrEmpty("X"); | ||
assertThat(result, Matchers.contains("X")); | ||
} | ||
|
||
@Test | ||
void testExpectParsedControlMissing() { | ||
var ex = assertThrows( | ||
IllegalStateException.class, | ||
() -> Utils.expectParsedControl(Collections.emptyMap(), "NOT_PRESENT", Integer.class) | ||
); | ||
assertThat(ex, hasProperty("message", stringContainsInOrder("Expected control map to have", "NOT_PRESENT"))); | ||
} | ||
|
||
@Test | ||
void testExpectParsedControlWrongType() { | ||
var ex = assertThrows( | ||
IllegalStateException.class, | ||
() -> Utils.expectParsedControl(Collections.singletonMap("WRONG_TYPE", 2d), "WRONG_TYPE", Integer.class) | ||
); | ||
assertThat( | ||
ex, | ||
hasProperty( | ||
"message", | ||
stringContainsInOrder( | ||
"Expected control map entry", "WRONG_TYPE", "to be", "Integer", "was", "Double" | ||
) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void testExpectParsedControlStillString() { | ||
var ex = assertThrows( | ||
IllegalStateException.class, | ||
() -> Utils.expectParsedControl( | ||
Collections.singletonMap("WRONG_TYPE", "UNPARSED"), "WRONG_TYPE", Integer.class | ||
) | ||
); | ||
assertThat( | ||
ex, | ||
hasProperty( | ||
"message", | ||
stringContainsInOrder( | ||
"Expected control map entry", "WRONG_TYPE", "to be parsed but was still a String" | ||
) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void testExpectParsedControlPresent() { | ||
var result = Utils.expectParsedControl(Collections.singletonMap("PRESENT", 2), "PRESENT", Integer.class); | ||
assertThat(result, is(2)); | ||
} | ||
|
||
@Test | ||
void testExpectParsedControlPresentString() { | ||
var result = Utils.expectParsedControl(Collections.singletonMap("PRESENT", "X"), "PRESENT", String.class); | ||
assertThat(result, is("X")); | ||
} | ||
|
||
@Test | ||
void testCompareUsing() { | ||
var unit = Utils.compareUsing((String s) -> s.substring(1, 2)); // Compares Strings by their second character. | ||
|
||
var list = new ArrayList<>(List.of("12", "21", "33")); | ||
list.sort(unit); | ||
|
||
assertThat(list, Matchers.contains("21", "12", "33")); | ||
|
||
} | ||
|
||
@Test | ||
void testConstMap() { | ||
var unit = Utils.<String, String>constMap((x) -> { | ||
x.put("TEST", "VALUE"); | ||
}); | ||
|
||
assertThat(unit, hasEntry("TEST", "VALUE")); | ||
|
||
assertThrows(UnsupportedOperationException.class, () -> unit.put("TEST", "CHANGED")); | ||
assertThrows(UnsupportedOperationException.class, () -> unit.put("ANOTHER", "VALUE")); | ||
} | ||
|
||
} |
162 changes: 162 additions & 0 deletions
162
vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/model/CoefficientsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package ca.bc.gov.nrs.vdyp.model; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import ca.bc.gov.nrs.vdyp.test.VdypMatchers; | ||
|
||
public class CoefficientsTest { | ||
|
||
@Test | ||
void testGetCoe() { | ||
var unit = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
assertThat(unit.getCoe(-1), is(2f)); | ||
assertThat(unit.getCoe(0), is(3f)); | ||
assertThat(unit.getCoe(1), is(4f)); | ||
} | ||
|
||
@Test | ||
void testGetCoeOutOfBounds() { | ||
var unit = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> unit.getCoe(-2)); | ||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> unit.getCoe(2)); | ||
} | ||
|
||
@Test | ||
void testPairwiseInPlace() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, -1); | ||
unit1.pairwiseInPlace(unit2, (x, y) -> x + y); | ||
assertThat(unit1, VdypMatchers.coe(-1, 7f, 9f, 11f)); | ||
} | ||
|
||
@Test | ||
void testPairwiseInPlaceIndexMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, 0); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwiseInPlace(unit2, (x, y) -> x + y)); | ||
} | ||
|
||
@Test | ||
void testPairwiseInPlaceSizeMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f }, -1); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwiseInPlace(unit2, (x, y) -> x + y)); | ||
} | ||
|
||
@Test | ||
void testPairwiseInPlaceIndexed() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, -1); | ||
unit1.pairwiseInPlace(unit2, (x, y, i) -> x + y + i); | ||
assertThat(unit1, VdypMatchers.coe(-1, 6f, 9f, 12f)); | ||
} | ||
|
||
@Test | ||
void testPairwiseInPlaceIndexedIndexMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, 0); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwiseInPlace(unit2, (x, y, i) -> x + y + i)); | ||
} | ||
|
||
@Test | ||
void testPairwiseInPlaceIndexedSizeMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f }, -1); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwiseInPlace(unit2, (x, y, i) -> x + y + i)); | ||
} | ||
|
||
@Test | ||
void testPairwise() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, -1); | ||
var result = unit1.pairwise(unit2, (x, y) -> x + y); | ||
assertThat(result, VdypMatchers.coe(-1, 7f, 9f, 11f)); | ||
assertThat(unit1, VdypMatchers.coe(-1, 2f, 3f, 4f)); | ||
} | ||
|
||
@Test | ||
void testPairwiseIndexMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, 0); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwise(unit2, (x, y) -> x + y)); | ||
} | ||
|
||
@Test | ||
void testPairwiseSizeMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f }, -1); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwise(unit2, (x, y) -> x + y)); | ||
} | ||
|
||
@Test | ||
void testPairwiseIndexed() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, -1); | ||
var result = unit1.pairwise(unit2, (x, y, i) -> x + y + i); | ||
assertThat(result, VdypMatchers.coe(-1, 6f, 9f, 12f)); | ||
assertThat(unit1, VdypMatchers.coe(-1, 2f, 3f, 4f)); | ||
} | ||
|
||
@Test | ||
void testPairwiseIndexedIndexMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f, 7f }, 0); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwise(unit2, (x, y, i) -> x + y + i)); | ||
} | ||
|
||
@Test | ||
void testPairwiseIndexedSizeMissmatch() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var unit2 = new Coefficients(new float[] { 5f, 6f }, -1); | ||
assertThrows(IllegalArgumentException.class, () -> unit1.pairwise(unit2, (x, y, i) -> x + y + i)); | ||
} | ||
|
||
@Test | ||
void testScalarInPlace() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
unit1.scalarInPlace(x -> x * 2); | ||
assertThat(unit1, VdypMatchers.coe(-1, 4f, 6f, 8f)); | ||
} | ||
|
||
@Test | ||
void testScalarInPlaceIndexed() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
unit1.scalarInPlace((x, i) -> x * 2 + i); | ||
assertThat(unit1, VdypMatchers.coe(-1, 3f, 6f, 9f)); | ||
} | ||
|
||
@Test | ||
void testScalar() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var result = unit1.scalar(x -> x * 2); | ||
assertThat(result, VdypMatchers.coe(-1, 4f, 6f, 8f)); | ||
assertThat(unit1, VdypMatchers.coe(-1, 2f, 3f, 4f)); | ||
} | ||
|
||
@Test | ||
void testScalarIndexed() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
var result = unit1.scalar((x, i) -> x * 2 + i); | ||
assertThat(result, VdypMatchers.coe(-1, 3f, 6f, 9f)); | ||
assertThat(unit1, VdypMatchers.coe(-1, 2f, 3f, 4f)); | ||
} | ||
|
||
@Test | ||
void testScalarInPlaceSpecificIndex() { | ||
var unit1 = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
unit1.scalarInPlace(1, x -> x * 2); | ||
assertThat(unit1, VdypMatchers.coe(-1, 2f, 3f, 8f)); | ||
} | ||
|
||
@Test | ||
void testScalarInPlaceSpecificIndexOutOfBounds() { | ||
var unit = new Coefficients(new float[] { 2f, 3f, 4f }, -1); | ||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> unit.scalarInPlace(-2, x -> x * 2)); | ||
assertThrows(ArrayIndexOutOfBoundsException.class, () -> unit.scalarInPlace(2, x -> x * 2)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters