Next: , Previous: , Up: Lisp Unit   [Contents][Index]


9.2.2 How to Test

> (in-package :cs325-user)
    #<PACKAGE CS325-USER>
> (define-test pick-greater
       (assert-equal 5 (pick-greater 2 5))
       (assert-equal 5 (pick-greater 5 2))
       (assert-equal 10 (pick-greater 10 10))
       (assert-equal 0 (pick-greater -5 0))
   )
   ==> PICK-GREATER

Following good test-first programming practice, we run these tests before writing any code.

> (run-tests pick-greater)
  ==> PICK-GREATER: Undefined function PICK-GREATER called with arguments (2 5).

This shows that we need to do some work. So we define our broken version of pick-greater.

> (defun pick-greater (x y) x)  ;; deliberately wrong
    PICK-GREATER

Now we run the tests again:

> (run-tests pick-greater)
  PICK-GREATER: (PICK-GREATER 2 5) failed: Expected 5 but saw 2
  PICK-GREATER: (PICK-GREATER -5 0) failed: Expected 0 but saw -5
  PICK-GREATER: 2 assertions passed, 2 failed.

This shows two failures. In both cases, the equality test returned ‘NIL’. In the first case it was because (pick-greater 2 5) returned 2 when 5 was expected, and in the second case, it was because (pick-greater -5 0) returned -5 when 0 was expected.