Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart test with roughly equal

I am now trying to use dart:test features.

I can write something like:

expect(areaUnderCurveWithRectangleRule(f1, 0,1,1000), equals(2));

But as we know, in float/double calculation, there is no such thing as precise equal. So I am wondering if there is a roughly equal testing method? It will return true for two double values, if their difference is within a certain epsilon (say, 1E-6) or certain percentage?

If not, will this make a good feature request to Dart team?

like image 232
TaylorR Avatar asked Apr 05 '20 02:04

TaylorR


People also ask

What is a Dart test?

Dart Daily Air Removal Test is a pre-assembled test to evaluate the effectiveness of air removal from the sterilizer chamber during a prevacuum steam sterilizer cycle. The Dart test is designed for use in a 132-134°C (270-274°F) prevacuum cycle with an exposure time of 3-1/2 to 4 minutes.

How do you run a Dart test?

A single test file can be run just using dart test path/to/test. dart (as of Dart 2.10 - prior sdk versions must use pub run test instead of dart test ). Many tests can be run at a time using dart test path/to/dir .

What is a Dart unit?

The Disaster Assistance and Rescue Team (DART) is the elite unit of the Singapore Civil Defence Force (SCDF) that specialises in complex incidents such as technical rescue, urban search and rescue, water rescue operations and prolonged firefighting. Disaster Assistance and Rescue Team.


1 Answers

dart:test provides a closeTo matcher for this purpose:

expect(areaUnderCurveWithRectangleRule(f1, 0,1,1000), closeTo(2, epsilon));

Note that closeTo uses an absolute delta, so a single threshold might not be appropriate for floating-point values that have very different magnitudes.

If you instead want a version that compares based on a percentage, it should be easy to wrap closeTo with your own function, e.g.:

Matcher closeToPercentage(num value, double fraction) {
  final delta = value * fraction;
  return closeTo(value, delta);
}
like image 166
jamesdlin Avatar answered Nov 15 '22 09:11

jamesdlin