Examples

Write down one or more expressions that use this function (in correct Scheme syntax, so that if only the function were already defined, you could type them in and get answers), along with the "right answer" you expect each example to produce. This step is useful in at least three ways.
  1. It allows you to try out the syntax people will use to invoke the function, and perhaps learn that your first guess at the syntax is inconvenient to use, so you should change the syntax before wasting a lot of time implementing it.
  2. It allows you to discover, early on, that your contract wasn't quite right, and that you actually want different kinds of information going in or out. If that happens, change the contract now and re-write your examples accordingly; that will waste less time than going on and having to change the contract later.
  3. It gives you a ready source of test cases, already in legal Scheme syntax, thus removing one possible excuse for skipping the Testing step, below.

Start with the simplest possible examples, then work up to more and more complicated examples.

One way to write this is in a Scheme comment, e.g.

; Cube function: find the third power of a number
; cube: number => number
; (cube 0) => 0
; (cube -3/5) => -27/125
; (cube (cube 3)) => 19683

Another is to write it directly in the Definitions window, specifying the correct answers with "should be..." , but then insert the function definition in front of it, e.g.

; Cube function: find the third power of a number
; cube: number => number
function definition will go here
(cube 0) "should be 0"
(cube -3/5) "should be -27/125"
(cube (cube 3)) "should be 19683"

Both of these approaches are sort of a pain in practice: you have to go through the actual answers, one by one, and compare them with what you said the right answers should be. A more convenient approach uses a built-in function named check-expect: you give it the expression you want to test, and the right answer, and it'll run all your tests and give you a report on how many of them worked, and which didn't. For example,

; Cube function: find the third power of a number
; cube: number => number
function definition will go here
(check-expect (cube 0) 0)
(check-expect (cube -3/5) -27/125)

If you're using a version of DrScheme that starts with a "3", you'll need to add the "testing.ss" teachpack (go to the Language menu, choose "Add Teachpack", and find it on the left hand side), and you'll need to add the single line

(generate-report)
at the very end of your Definitions pane, after all of the check-expects for all of the functions. If you're using a DrScheme version starting with a "4", you don't need to do either of these things.


Last modified: Mon Aug 25 11:54:40 EDT 2008
Stephen Bloch / sbloch@adelphi.edu