1

我对 Fitnesse 还很陌生。我正在使用子集查询表。它工作正常,除非我必须比较一个浮点值。有什么方法可以将这些值与一定的准确度进行比较?

例如,当我比较 4.12 和 4.1234 时,测试应该通过。但是现在,它看起来像是在进行字符串比较并且失败了。无论如何我可以覆盖验证部分吗?

4

2 回答 2

1

您可以使用波浪字符来表示“大约等于”,例如 ~=4.12

有关详细信息,请参阅http://fitnesse.org/FitNesse.UserGuide.SliM.ValueComparisons 。

于 2012-05-28T06:26:30.687 回答
0

为了实现 Double 和 Float 值的近似比较,我重写了夹具类,如下所示:

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);
        }
    }
}

两个 TypeAdapter 类同时使用相对值epsilon和绝对值精度进行比较。从预期输入中检测精度,因此 23.099 的精度为 0.001。他们还期望特殊值,例如 NaN 的“nan”和 +/-Infinitive 的“inf”。这是一个例子:

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;
        }
    }
}
于 2017-12-21T15:50:20.270 回答