diff --git a/README.md b/README.md index 9c91b4e..686ccf4 100644 --- a/README.md +++ b/README.md @@ -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 val truthy: bool -> expectation end structure Test: @@ -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), diff --git a/bin/generate b/bin/generate index 3156055..c344f40 100755 --- a/bin/generate +++ b/bin/generate @@ -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, diff --git a/exercises/practice/accumulate/testlib.sml b/exercises/practice/accumulate/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/accumulate/testlib.sml +++ b/exercises/practice/accumulate/testlib.sml @@ -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 = ( diff --git a/exercises/practice/all-your-base/testlib.sml b/exercises/practice/all-your-base/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/all-your-base/testlib.sml +++ b/exercises/practice/all-your-base/testlib.sml @@ -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 = ( diff --git a/exercises/practice/allergies/testlib.sml b/exercises/practice/allergies/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/allergies/testlib.sml +++ b/exercises/practice/allergies/testlib.sml @@ -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 = ( diff --git a/exercises/practice/anagram/testlib.sml b/exercises/practice/anagram/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/anagram/testlib.sml +++ b/exercises/practice/anagram/testlib.sml @@ -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 = ( diff --git a/exercises/practice/atbash-cipher/testlib.sml b/exercises/practice/atbash-cipher/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/atbash-cipher/testlib.sml +++ b/exercises/practice/atbash-cipher/testlib.sml @@ -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 = ( diff --git a/exercises/practice/binary/testlib.sml b/exercises/practice/binary/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/binary/testlib.sml +++ b/exercises/practice/binary/testlib.sml @@ -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 = ( diff --git a/exercises/practice/bob/testlib.sml b/exercises/practice/bob/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/bob/testlib.sml +++ b/exercises/practice/bob/testlib.sml @@ -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 = ( diff --git a/exercises/practice/collatz-conjecture/testlib.sml b/exercises/practice/collatz-conjecture/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/collatz-conjecture/testlib.sml +++ b/exercises/practice/collatz-conjecture/testlib.sml @@ -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 = ( diff --git a/exercises/practice/diamond/testlib.sml b/exercises/practice/diamond/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/diamond/testlib.sml +++ b/exercises/practice/diamond/testlib.sml @@ -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 = ( diff --git a/exercises/practice/difference-of-squares/testlib.sml b/exercises/practice/difference-of-squares/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/difference-of-squares/testlib.sml +++ b/exercises/practice/difference-of-squares/testlib.sml @@ -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 = ( diff --git a/exercises/practice/flatten-array/testlib.sml b/exercises/practice/flatten-array/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/flatten-array/testlib.sml +++ b/exercises/practice/flatten-array/testlib.sml @@ -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 = ( diff --git a/exercises/practice/hamming/testlib.sml b/exercises/practice/hamming/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/hamming/testlib.sml +++ b/exercises/practice/hamming/testlib.sml @@ -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 = ( diff --git a/exercises/practice/hello-world/testlib.sml b/exercises/practice/hello-world/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/hello-world/testlib.sml +++ b/exercises/practice/hello-world/testlib.sml @@ -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 = ( diff --git a/exercises/practice/leap/testlib.sml b/exercises/practice/leap/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/leap/testlib.sml +++ b/exercises/practice/leap/testlib.sml @@ -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 = ( diff --git a/exercises/practice/list-ops/testlib.sml b/exercises/practice/list-ops/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/list-ops/testlib.sml +++ b/exercises/practice/list-ops/testlib.sml @@ -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 = ( diff --git a/exercises/practice/matching-brackets/testlib.sml b/exercises/practice/matching-brackets/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/matching-brackets/testlib.sml +++ b/exercises/practice/matching-brackets/testlib.sml @@ -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 = ( diff --git a/exercises/practice/nth-prime/testlib.sml b/exercises/practice/nth-prime/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/nth-prime/testlib.sml +++ b/exercises/practice/nth-prime/testlib.sml @@ -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 = ( diff --git a/exercises/practice/pangram/testlib.sml b/exercises/practice/pangram/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/pangram/testlib.sml +++ b/exercises/practice/pangram/testlib.sml @@ -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 = ( diff --git a/exercises/practice/perfect-numbers/testlib.sml b/exercises/practice/perfect-numbers/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/perfect-numbers/testlib.sml +++ b/exercises/practice/perfect-numbers/testlib.sml @@ -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 = ( diff --git a/exercises/practice/phone-number/testlib.sml b/exercises/practice/phone-number/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/phone-number/testlib.sml +++ b/exercises/practice/phone-number/testlib.sml @@ -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 = ( diff --git a/exercises/practice/pig-latin/testlib.sml b/exercises/practice/pig-latin/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/pig-latin/testlib.sml +++ b/exercises/practice/pig-latin/testlib.sml @@ -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 = ( diff --git a/exercises/practice/prime-factors/testlib.sml b/exercises/practice/prime-factors/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/prime-factors/testlib.sml +++ b/exercises/practice/prime-factors/testlib.sml @@ -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 = ( diff --git a/exercises/practice/raindrops/testlib.sml b/exercises/practice/raindrops/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/raindrops/testlib.sml +++ b/exercises/practice/raindrops/testlib.sml @@ -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 = ( diff --git a/exercises/practice/rna-transcription/testlib.sml b/exercises/practice/rna-transcription/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/rna-transcription/testlib.sml +++ b/exercises/practice/rna-transcription/testlib.sml @@ -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 = ( diff --git a/exercises/practice/roman-numerals/testlib.sml b/exercises/practice/roman-numerals/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/roman-numerals/testlib.sml +++ b/exercises/practice/roman-numerals/testlib.sml @@ -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 = ( diff --git a/exercises/practice/sum-of-multiples/testlib.sml b/exercises/practice/sum-of-multiples/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/sum-of-multiples/testlib.sml +++ b/exercises/practice/sum-of-multiples/testlib.sml @@ -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 = ( diff --git a/exercises/practice/two-fer/testlib.sml b/exercises/practice/two-fer/testlib.sml index b9291bc..0c8370c 100644 --- a/exercises/practice/two-fer/testlib.sml +++ b/exercises/practice/two-fer/testlib.sml @@ -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 = ( diff --git a/lib/testlib.sml b/lib/testlib.sml index b9291bc..0c8370c 100644 --- a/lib/testlib.sml +++ b/lib/testlib.sml @@ -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 = (