question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

equalWithin on DU types of float

See original GitHub issue

Description

I am building test on discriminated union types that can take on a case =of float=. However the =equalWithin= operator is not able to compare two numbers within a range for such types, making it hard to test things as I need to drill down to perform explicit float conversion. See example:

Repro steps

type myType = | Number of float | Name of string

// inside test:
1.1 * (1. + 2.) |> should (equalWithin 1E-10)  3.3 //this test passes 
(Number (1.1 * (1. + 2.))) |> should (equalWithin 1E-10)  (Number 3.3) //this test fails

Expected behavior

I would like both tests to pass or for there to be a simple way to write the test. Consider example:

let a = //code that produces a record containing myType instances.
a |> should (equalWithin 1E-10) {g="string"; h=Number 4;....}

Currently I have to test field by field so I can perform explicit conversion to floats for the number fields. Maybe the equalWithin operator could be expanded to cover types with declared double fields or similar?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sergey-tihoncommented, Oct 16, 2021

@CaptnCodr yes, I agree.

That how we write tests in .NET (C#, F#) - multiple assertions for primitive types. If you need more, you can implement helper functions for your project or override equality for record type.

0reactions
CaptnCodrcommented, Oct 16, 2021

Hey @johanvts

What simply FsUnit does is:

  1. it consumes methods (or are adapted) of specific frameworks (xunit, nunit, mstest) and provides it via operators e.g.: Assert.AreEqual(expected, actual) => actual |> should equal expected
  2. it follows that operators are in FsUnit implemented that are real assertions in nunit, xunit and/or mstest
  3. there is no “extra logic” implemented only the most necessary to satisfy the operation

equal and equalWithin are very different:

  • equal is a simple check: x = y
  • equalWithin checks if the actual, expected and tolerance are convertible to float and if the difference between actual and expected is less than the tolerance then this test passes

equal handles all types. equalWithin handles numeral types with a given range (tolerance).

This feature does not fit in FsUnit, the reasons are described above.

I would like to close this issue. Do you agree with it, @sergey-tihon?

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Comparing Floating Point Numbers - Mod the Machine
Below is a more elaborate version of the IsEqual function that supports various types of comparisons. ' Compares two numbers to see if...
Read more >
Triggering googletest error on EXPECT_FLOAT_EQ even ...
I'm creating unit tests for a program that performs floating point computations in C++. Throughout the unit test file, I've been using ...
Read more >
Floats
Floats. A Go library with utilities for floats. For now, there are only functions to compare floats according to floating-point-gui.de.
Read more >
Verifying the Floating-Point Computation Equivalence of ...
All sub-expressions must be of the same floating-point type, float, double, or long double. Table 1: Rewrite Sequence Statistics. Rules. (1) (2) ...
Read more >
ISEQUALFP: Check equality within floating point precision
This function accepts two float values (single or double) or arrays of floats, and returns a logical value indicating whether they are equal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found