;;; The beginning of a test module for mp2. (module mp2-test (lib "eopl.ss" "eopl") (require "drscheme-init.scm") (require (only "mp2.scm" product merge sort sort/predicate leaf interior-node leaf? lson rson contents-of double-tree )) (stop-after-first-error #f) ; (use #t stop testing after failure) ;; test : Symbol * (A B ... -> Z) * (list A B ...) * Z -> void or error ;; usage: (test nm fcn args ans) applies fcn to args; if the ;; result is not equal to ans, then signals a test failure. (define test (lambda (name proc args result) (test-aftermath name args result (run-experiment proc args result equal?)))) ;; test/comparison : ;; Symbol * (A B ... -> Z) * (list A B ...) * Z * (Z -> boolean) ;; -> void ;; usage: (test/comparison nm fcn args res okay?) ;; applies fcn to args and uses okay? to compare the result against res; ;; if the okay? predicate returns false, then a test failure is signalled. (define test/comparison (lambda (name proc args result correct?) (test-aftermath name args result (run-experiment proc args result correct?)))) ;; test-aftermath : Symbol * (list A B ...) * (cons Boolean Z) -> void ;; -> void ;; usage: (test-aftermath name args t) reports the test outcome t. (define test-aftermath (lambda (name args result t) (let ((was-a-success (car t)) (answer-given (cdr t))) (cond (was-a-success (eopl:printf "Success on test ~a~%" (cons name args))) (else (cond ((stop-after-first-error) (eopl:error name "~a evaluated to ~a, but we want: ~a" (cons name args) answer-given result)) (else (eopl:printf "Failure on test ~a; evaluated to ~a, but we want: ~a~%" (cons name args) answer-given result) ))))))) ;; is-permutation-of? : List * List -> Boolean ;; ;; usage: (is-permutation-of? list1 list2) returns true if and only if ;; list1 is a permutation of list2 (define is-permutation-of? (lambda (list1 list2) (cond ((null? list1) (null? list2)) ((member (car list1) list2) (is-permutation-of? (cdr list1) (remove-first (car list1) list2))) (else #f)))) ;; remove-first : Z * List -> List ;; ;; usage: (remove-first z list1) returns a list that is like list1 ;; but omits the first occurrence of z (if any). (define remove-first (lambda (z list1) (cond ((null? list1) list1) ((equal? z (car list1)) (cdr list1)) (else (cons (car list1) (remove-first z (cdr list1))))))) ;; EOPL 1.21 (test/comparison 'product product '((a b c) (x y)) '((a x) (a y) (b x) (b y) (c x) (c y)) is-permutation-of?) ;; EOPL 1.28 (test 'merge merge (list '(1 4) '(1 2 8)) '(1 1 2 4 8)) (test 'merge merge (list '(35 62 81 90 91) '(3 83 85 90)) '(3 35 62 81 83 85 90 90 91)) ;; EOPL 1.29 (test 'sort sort (list '(8 2 5 2 3)) '(2 2 3 5 8)) ;; EOPL 1.31 (test 'sort/predicate sort/predicate (list < '(8 2 5 2 3)) '(2 2 3 5 8)) (test 'sort/predicate sort/predicate (list > '(8 2 5 2 3)) '(8 5 3 2 2)) ;; EOPL 1.32 ;; EOPL 1.33 )