问题
I am fairly new to Fitnesse. I am using the Subset Query Table. It works fine except when I have to compare a float value. Is there a any way I can compare the values to certain level of accuracy ?
For instance the test should pass when I compare 4.12 to 4.1234. But right now, it looks like it is making a string comparison and it fails. Is there anyway I can override the validation part ?
回答1:
You can use the tilde character to mean 'approximately equal to', e.g. ~=4.12
See http://fitnesse.org/FitNesse.UserGuide.SliM.ValueComparisons for more details.
回答2:
To achieve approx comparison of Double and Float values, I have override the fixture class as the following:
public class ApproxColumnFixture extends ColumnFixture {
@Override
public void check(Parse cell, TypeAdapter a) {
if (a.type == Double.class) {
super.check(cell, new ApproxDoubleAdapter(a));
} else if (a.type == Float.class) {
super.check(cell, new ApproxFloatAdapter(a));
} else {
super.check(cell, a);
}
}
}
The two TypeAdapter class use both relative value epsilon and absolute value precision for comparison. The precision is detected from the expected input, so that 23.099 has precision 0.001. They also expect special values, such as "nan" for NaN, and "inf" for +/-Infinitive. Here is one example:
public class ApproxDoubleAdapter extends TypeAdapter {
public final Double ZERO = new Double(0.0);
private final double epsilon;
private int precisions = -1;
public ApproxDoubleAdapter(final TypeAdapter classDoubleAdapter, double epsilon) {
this.target = classDoubleAdapter.target;
this.fixture = classDoubleAdapter.fixture;
this.field = classDoubleAdapter.field;
this.method = classDoubleAdapter.method;
this.type = classDoubleAdapter.type;
this.isRegex = classDoubleAdapter.isRegex;
this.epsilon = epsilon;
}
public ApproxDoubleAdapter(final TypeAdapter adapt) {
this(adapt, 0.0001);
}
public Object parse(String s) throws Exception {
if ((s == null) || s.equals("null")) {
return null;
}
if (s.equals("0")) {
return ZERO;
}
if (s.equals("nan")) {
return Double.NaN;
}
if (s.equals("inf")) {
return Double.POSITIVE_INFINITY;
}
precisions = s.indexOf(".");
if (precisions >= 0) {
precisions = s.length() - 1 - precisions;
}
return new Double( Double.parseDouble(s) );
}
public boolean equals(Object a, Object b) {
if (a == null) {
return (b == null);
}
if (b == null) {
return (a == null);
}
if ((a.getClass() != Double.class) || (b.getClass() != Double.class)) {
return false;
}
double aV = (Double) a;
double bV = (Double) b;
if (Double.isNaN(aV)) {
return Double.isNaN(bV);
}
if (Double.isNaN(bV)) {
return Double.isNaN(aV);
}
if (Double.isNaN(aV)) {
return Double.isNaN(bV);
}
if (Double.isInfinite(aV)) {
return Double.isInfinite(bV);
}
if (Double.isInfinite(bV)) {
return Double.isInfinite(aV);
}
final double diff = Math.abs(aV - bV);
if (diff <= Math.abs(aV + bV) * epsilon) {
return true;
}
if (precisions > 0) {
return diff <= Math.pow(10, precisions);
} else if (aV == 0.0) {
return diff < epsilon;
} else {
return false;
}
}
}
来源:https://stackoverflow.com/questions/10775173/float-value-in-slim-query-table-comparison