Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Assertions: Compare two numeric collections approximately

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?

like image 962
David H Avatar asked Jun 11 '13 22:06

David H


2 Answers

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.

like image 162
Dennis Doomen Avatar answered Sep 19 '22 22:09

Dennis Doomen


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)
like image 38
Kevin Avatar answered Sep 19 '22 22:09

Kevin