|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Error checking helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; The following procedures may be used to help out writing ;; defensive code by allowing you to immediately signal an ;; error when a problem is detected. ;; ;; The following example shows several different ways of ;; using the assertion procedures. ;; (define (my-add x y) ;; (assert-t (number? x)) ;; (assert y number?) ;; (+ x y)) ;; Generates an error message if applying the pred procedure ;; to test-val does not generate a true value. (define (assert test-val pred) (if (not (pred test-val)) (error "ASSERT FAILED: " test-val pred))) ;; Generates an error if test-val and expected-val are not ;; equal? (define (assert-equal test-val expected-val) (if (not (equal? test-val expected-val)) (error "ASSERT-EQUAL FAILED: \n" " Expected value: " expected-val "\n" " Actual value: " test-val "\n"))) ;; Generates an error if test-val and expected-val are not ;; eqv? (define (assert-eqv test-val expected-val) (if (not (eqv? test-val expected-val)) (error "ASSERT-EQV FAILED: \n" " Expected value: " expected-val "\n" " Actual value: " test-val "\n"))) ;; Generates an error if test-val and expected-val are not ;; eq? (define (assert-eq test-val expected-val) (if (not (eq? test-val expected-val)) (error "ASSERT-EQ FAILED: \n" " Expected value: " expected-val "\n" " Actual value: " test-val "\n"))) ;; Generates an error if test-val and expected-val are not ;; = (define (assert= test-val expected-val) (if (not (= test-val expected-val)) (error "ASSERT= FAILED: \n" " Expected value: " expected-val "\n" " Actual value: " test-val "\n"))) ;; Generates an error if test-val is not #f (define (assert-f test-val) (if test-val (error "ASSERT-F FAILED"))) ;; Generates an error if test-value is #f (define (assert-t test-val) (if (not test-val) (error "ASSERT-T FAILED"))) |
Last updated: 2008-01-25 09:23:57 -0500 |