Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nearTo fix #205

Merged
merged 3 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ structure Expect:
val error: exn -> (unit -> 'a) -> expectation
datatype expectation = Fail of string * string | Pass
val falsy: bool -> expectation
val nearTo: real -> real -> expectation
val nearTo: real -> real -> real -> expectation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to update the README differently: I would actually delete the whole chapter "### testlib.sml" since it does not contain usefull information which cannot be obtained elsewhere. Moreover one never knows if the info is outdated. The signature is easy to get from lib/testlib.sml, and if an explicit signature EXPECT was usefull I would just add it to the file (with a corresponding structure Expect :> EXPECT =. For usage I would just refer to the tests. On the other hand I would add the info that the testlib.sml files are intended to be copies of lib/testlib.sml, directly at the place where "### testlib.sml" was (it is written somewhere else however).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are probably right, but I think that is outside the scope of this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I am fine if you think it should not be part of this PR. My reasoning was: If I have to touch in anyway and I think it is not needed anymore and it is nothing really important then I can just delete it now.

If you like you can open an issue on that or just do another PR.

val truthy: bool -> expectation
end
structure Test:
Expand Down Expand Up @@ -76,7 +76,7 @@ val testsuite =
(fn _ => bar () |> Expect.truthy),

test "something that baz does"
(fn _ => baz (123) |> Expect.nearTo 123.10),
(fn _ => baz (123) |> Expect.nearTo 0.001 123.10),

test "an exception from 'qux'"
(fn _ => (fn _ => qux (0, 0))) |> Expect.error QuxError),
Expand Down
2 changes: 1 addition & 1 deletion bin/generate
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def expectation(signature, fn, args, expected, force=False):
if signature['output'] == 'real':
return tmpl % (
invocation,
'nearTo %s' % output
'nearTo 0.001 %s' % output
)
return tmpl % (
invocation,
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/accumulate/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/all-your-base/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/allergies/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/anagram/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/atbash-cipher/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/binary/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/bob/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/collatz-conjecture/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/diamond/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/difference-of-squares/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/flatten-array/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/hamming/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/hello-world/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/leap/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/list-ops/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/matching-brackets/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/nth-prime/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/pangram/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/perfect-numbers/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/phone-number/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/pig-latin/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/prime-factors/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/raindrops/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
7 changes: 4 additions & 3 deletions exercises/practice/rna-transcription/testlib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ struct
then Pass
else failEq (PolyML.makestring b) (PolyML.makestring a)

fun nearTo b a =
if Real.== (a, b)
fun nearTo delta b a =
if Real.abs (a - b) <= delta * Real.abs a orelse
Real.abs (a - b) <= delta * Real.abs b
then Pass
else failEq (Real.toString b) (Real.toString a)
else failEq (Real.toString b ^ " +/- " ^ Real.toString delta) (Real.toString a)

fun anyError f =
(
Expand Down
Loading