Skip to content

Commit

Permalink
Cleaned up some cargo cult silliness in DoubleConstraint and added so…
Browse files Browse the repository at this point in the history
…me tests to exercise the min and max allowed values for SA_Param.
  • Loading branch information
larsbutler committed May 10, 2012
1 parent b73d943 commit 985fdbc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
59 changes: 12 additions & 47 deletions java/org/opensha/commons/param/DoubleConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public class DoubleConstraint extends ParameterConstraint<Double> {
protected final static boolean D = false;

/** The minimum value allowed in this constraint, inclusive */
protected Double min = null;
protected double min;
/** The maximum value allowed in this constraint, inclusive */
protected Double max = null;
protected double max;

/** No-Arg Constructor, constraints are null so all values allowed */
public DoubleConstraint() {
Expand All @@ -61,29 +61,14 @@ public DoubleConstraint() {
/**
* Constructor for the DoubleConstraint object. Sets the min and max values
* allowed in this constraint. No checks are performed that min and max are
* consistant with each other.
* consistent with each other.
*
* @param min
* The min value allowed
* @param max
* The max value allowed
*/
public DoubleConstraint(double min, double max) {
this.min = new Double(min);
this.max = new Double(max);
}

/**
* Constructor for the DoubleConstraint object. Sets the min and max values
* allowed in this constraint. No checks are performed that min and max are
* consistant with each other.
*
* @param min
* The min value allowed
* @param max
* The max value allowed
*/
public DoubleConstraint(Double min, Double max) {
this.min = min;
this.max = max;
}
Expand All @@ -103,25 +88,6 @@ public DoubleConstraint(Double min, Double max) {
public void setMinMax(double min, double max) throws EditableException {
String S = C + ": setMinMax(double, double): ";
checkEditable(S);
this.min = new Double(min);
this.max = new Double(max);
}

/**
* Sets the min and max values allowed in this constraint. No checks are
* performed that min and max are consistant with each other.
*
* @param min
* The new min value
* @param max
* The new max value
* @throws EditableException
* Thrown when the constraint or parameter containing this
* constraint has been made non-editable.
*/
public void setMinMax(Double min, Double max) throws EditableException {
String S = C + ": setMinMax(Double, Double): ";
checkEditable(S);
this.min = min;
this.max = max;
}
Expand Down Expand Up @@ -150,12 +116,13 @@ public Double getMax() {
*/
public boolean isAllowed(Double d) {
if (d == null)
{
return nullAllowed;
if ((min == null) || (max == null))
return true;
else if ((d.compareTo(min) >= 0) && (d.compareTo(max) <= 0))
return true;
return false;
}
else
{
return isAllowed((double)d);
}
}

/**
Expand All @@ -170,7 +137,7 @@ else if ((d.compareTo(min) >= 0) && (d.compareTo(max) <= 0))
* @return True if this is one of the allowed values.
*/
public boolean isAllowed(double d) {
return isAllowed(new Double(d));
return d >= min && d <= max;
}

/**
Expand All @@ -182,10 +149,8 @@ public String toString() {
StringBuffer b = new StringBuffer();
if (name != null)
b.append(TAB + "Name = " + name + '\n');
if (min != null)
b.append(TAB + "Min = " + min.toString() + '\n');
if (max != null)
b.append(TAB + "Max = " + max.toString() + '\n');
b.append(TAB + "Min = " + String.valueOf(min) + '\n');
b.append(TAB + "Max = " + String.valueOf(max) + '\n');
b.append(TAB + "Null Allowed = " + this.nullAllowed + '\n');
return b.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.opensha.sha.imr.param.IntensityMeasureParams;

import org.junit.Before;
import org.junit.Test;
import org.opensha.commons.param.DoubleDiscreteConstraint;
import org.opensha.commons.exceptions.WarningException;
import org.opensha.commons.exceptions.ConstraintException;

public class SA_ParamTest
{

private SA_Param saParam;

@Before
public void setUp()
{
double[] period = { 0.01, 0.02, 0.03, 0.04, 0.05,
0.075, 0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5, 0.75, 1, 1.5, 2, 3, 4,
5, 7.5, 10};
DoubleDiscreteConstraint ddc = new DoubleDiscreteConstraint();
for (double d : period)
{
ddc.addDouble(d);
}

PeriodParam pp = new PeriodParam(ddc);
DampingParam dp = new DampingParam();

saParam = new SA_Param(pp, dp);
}

@Test
public void testDefaultWarnMax()
{
// Should succeed with no error.
// This is maximum allowed value.
saParam.setValue(Math.log(Double.MAX_VALUE));
}

@Test(expected=WarningException.class)
public void testDefaultWarnMax2()
{
saParam.setValue(Double.MAX_VALUE);
}

@Test(expected=WarningException.class)
public void testDefaultWarnMax3()
{
// The test value just slightly exceeds the max allowed value.
saParam.setValue(Math.log(Double.MAX_VALUE) + 0.0000000001);
}

@Test
public void testMinValue()
{
saParam.setValue(Math.log(Double.MIN_VALUE));
}

@Test(expected=ConstraintException.class)
public void testMinValue2()
{
saParam.setValue(Math.log(Double.MIN_VALUE) - 0.0000000001);
}
}

0 comments on commit 985fdbc

Please sign in to comment.