Question

I have two arrays of double. Is there a way using FluentAssertions to compare the arrays element-by-element, using the .BeApproximately() technique?

One range value would suffice for the entire array.

Example:

double[] source = { 10.01, 8.01, 6.01 };
double[] target = { 10.0, 8.0, 6.0  };

// THE FOLLOWING IS NOT IMPLEMENTED
target.Should().BeApproximately(source, 0.01);   

Is there an alternative approach?

Was it helpful?

Solution

There's an overload on the generic collection assertions that takes a Func that you can use to apply any predicate during comparison. With that, you could do something like:

source.Should().Equal(target, (left, right) => AreEqualApproximately(left, right, 0.01));

The only thing you need to do is to create that method yourself.

OTHER TIPS

I know it's preferable to compare the list but you could iterate it and compare them individually. I can't test the code right now but the following should work...

double[] source = { 10.01, 8.01, 6.01 };
double[] target = { 10.0, 8.0, 6.0  };

for(var i=0; i<source.Length; i++)
    target[i].Should().BeApproximately(source[i], 0.01)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top