[plt-scheme] Test the Program

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Thu Sep 3 15:33:00 EDT 2009

>> Asher, Gregg wrote:
>>> When I define a program in the definitions window and then enter  
>>> the program name in the interactive window substituting a value  
>>> for the parameter and then "Run" (my version of Dr. Scheme  
>>> doesn't have an "Execute" button) the program I don't the results  
>>> of the program but a message "This program should be tested.

You've gotten several different correct answers to this, each  
addressing a different aspect of what's going on.  Let me try to put  
them all together in context.

1) You're not seeing the results of the expression you typed in the  
Interactions pane.  This is because you hit "Run" .  The "Run" button  
starts by throwing away everything in the Interactions pane, then  
evaluating everything in the Definitions pane.  To solve this, type  
an expression in Interactions and then just hit the "Enter" or  
"Return" key on your keyboard rather than clicking the "Run" button.   
(However, whenever you change languages, change language preferences,  
add or remove a teachpack, or edit anything in the Definitions pane,  
you'll need to click the "Run" button in order for it to take effect.)

2) You're getting a message that says "This program should be  
tested."  This happens when you "Run" a Definitions pane that doesn't  
contain any test cases written with "check-expect" (or its friends).   
To solve this, write test cases in the Definitions pane, preferably  
for each function you define.  For example, the Definitions pane  
might look like this:

; area-of-disk : number(radius) -> number

(check-expect (area-of-disk 0) 0)
(check-within (area-of-disk 1) 3.14 .01)
	; the third parameter is a "close enough" criterion, since this  
function does inexact computations
(check-within (area-of-disk 2) 12.56 .01)

(define (area-of-disk radius)
	(* pi radius radius))

; cube : number -> number

(define (cube num)
	(* num num num))

(check-expect (cube 0) 0)
(check-expect (cube 4) 64)
(check-expect (cube -5) -125)

Note that "check-expect", "check-within", etc. can appear either  
before or after the definition of the function they're supposed to  
test; you can decide for yourself which fits your pedagogical style  
better.  The advantage of "before the definition" is that students  
can follow the design recipe without "backing up" in the Definitions  
pane: contract, test cases, skeleton becoming a definition.



Posted on the users mailing list.