Skip to content

Commit

Permalink
nearTo fix (#205)
Browse files Browse the repository at this point in the history
* Change nearTo to use a delta value.

The exercise space-age is using a variation of nearTo that seems more
coherent to me.  I discovered this while testing the redeploy-testlib
script.  This adds the better nearTo to the "blessed" testlib.sml in
lib.  We need a few changes.  `nearTo` is referenced in the readme and
it is used in the generator.  It is only used by the space-age
exercise though.  Here is a dump of the grep command I used to
determine this (with the space-age occurrences truncated for brevity).

```
$ grep --exclude "*testlib.sml" -R nearTo .
./README.md:    val nearTo: real -> real -> expectation
./README.md:      (fn _ => baz (123) |> Expect.nearTo 123.10),
./exercises/practice/space-age/test.sml:      (fn _ => age_on Earth 1000000000 |> Expect.nearTo 0.005 31.69),
More occurrences in ./exercises/practice/space-age/test.sml...
./bin/generate:            'nearTo %s' % output
```

As we see here, we need to update the readme in two places and
bin/generate in one place.  I will isolate those changes to another
commit and then yet another commit will provide the updated test to
all exercises.  We will probably want these three commits squashed
into one in the end, but I think finer granularity is fine during the
PR process.

* Update peripheral files concerning the improved nearTo.

* Deploy the new testlib to all exercises.
  • Loading branch information
guygastineau authored Jun 24, 2022
1 parent 1fc935e commit b977127
Show file tree
Hide file tree
Showing 30 changed files with 115 additions and 87 deletions.
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
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

0 comments on commit b977127

Please sign in to comment.