Skip to content

Commit

Permalink
Expand unit test coverage of utility classes
Browse files Browse the repository at this point in the history
  • Loading branch information
smithkm committed Nov 24, 2023
1 parent 45bbd12 commit 4d3827a
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public float getCoe(int i) {
return coe[getRealIndex(i)];
}

public void modifyCoe(int i, UnaryOperator<Float> op) {
setCoe(i, op.apply(getCoe(i)));
}

public void setCoe(int i, float value) {
coe[getRealIndex(i)] = value;
}
Expand Down
113 changes: 113 additions & 0 deletions vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/common/UtilsTest.java
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 vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/model/CoefficientsTest.java
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ca.bc.gov.nrs.vdyp.io.parse.LineParser;
import ca.bc.gov.nrs.vdyp.io.parse.OptionalResourceControlMapModifier;
import ca.bc.gov.nrs.vdyp.io.parse.ResourceParseException;
import ca.bc.gov.nrs.vdyp.common.FloatUnaryOperator;
import ca.bc.gov.nrs.vdyp.common.Utils;
import ca.bc.gov.nrs.vdyp.io.parse.GenusDefinitionParser;
import ca.bc.gov.nrs.vdyp.io.parse.ValueParseException;
Expand Down Expand Up @@ -159,11 +160,11 @@ public boolean isIgnoredLine(String line) {

if (coastalMod != 0.0) {
var coe = vetBqMap.get(sp0Alias, Region.COASTAL);
coe.modifyCoe(1, x -> x * coastalMod);
coe.scalarInPlace(1, (FloatUnaryOperator) x -> x * coastalMod);
}
if (interiorMod != 0.0) {
var coe = vetBqMap.get(sp0Alias, Region.INTERIOR);
coe.modifyCoe(1, x -> x * interiorMod);
coe.scalarInPlace(1, (FloatUnaryOperator) x -> x * interiorMod);
}
}
} else if (sequence >= 200 && sequence <= 299) {
Expand Down Expand Up @@ -199,16 +200,16 @@ public boolean isIgnoredLine(String line) {

modsByRegions(mods, 0, (m, r) -> {
var coe = hlP1Map.get(sp0Alias, r);
coe.modifyCoe(1, x -> x * m);
coe.modifyCoe(2, x -> x * m);
coe.scalarInPlace(1, (FloatUnaryOperator) x -> x * m);
coe.scalarInPlace(2, (FloatUnaryOperator) x -> x * m);
});
modsByRegions(mods, 0, (m, r) -> {
var coe = hlP2Map.get(sp0Alias, r);
coe.modifyCoe(1, x -> x * m);
coe.scalarInPlace(1, (FloatUnaryOperator) x -> x * m);
});
modsByRegions(mods, 0, (m, r) -> {
var coe = hlP3Map.get(sp0Alias, r);
coe.modifyCoe(1, x -> {
coe.scalarInPlace(1, (FloatUnaryOperator) x -> {
if (x > 0.0f && x < 1.0e06f) {
return x * m;
}
Expand All @@ -219,7 +220,7 @@ public boolean isIgnoredLine(String line) {
modsByRegions(mods, 2, (m, r) -> {
var coe = hlNPMap.get(sp0Alias, primarySp, r);
if (coe.getEquationIndex() == 1) {
coe.modifyCoe(1, x -> x * m);
coe.scalarInPlace(1, (FloatUnaryOperator) x -> x * m);
}
});
}
Expand Down

0 comments on commit 4d3827a

Please sign in to comment.