;; ;; FILE: homework06-tests.rkt ;; AUTHOR: Eugene Wallingford ;; DATE: 2020/02/26 ;; COMMENT: This file loads "homework06.rkt" and runs tests on its ;; publicly-defined functions. ;; ;; MODIFIED: 2022/03/18 ;; CHANGE: Reorganized the tests and added doc strings to the tests. ;; #lang racket (require rackunit) (require "homework06.rkt") ;; -------------------------------------------------------------------------- ;; Problem 1 (structural recursion) ;; -------------------------------------------------------------------------- (check-equal? (tails '()) '(()) "tails of empty list") (check-equal? (tails '(1 2 3)) '((1 2 3) (2 3) (3) ()) "tails of 3-list") (check-equal? (tails '((a b) (c d))) '(((a b) (c d)) ((c d)) ()) "tails with list of lists") ;(check-equal? (tails (range 1 10)) ; '((1 2 3 4 5 6 7 8 9) (2 3 4 5 6 7 8 9) ; (3 4 5 6 7 8 9) (4 5 6 7 8 9) ; (5 6 7 8 9) (6 7 8 9) ; (7 8 9) (8 9) (9) ())) ;; -------------------------------------------------------------------------- ;; Problem 2 (mutual recursion) ;; -------------------------------------------------------------------------- (check-true (n-list? '()) "n-list? on empty list") (check-true (n-list? '(1 2 3 4 5 6 7 8 9 0)) "n-list? on flat list") (check-true (n-list? '(1 (2 (3 4) 5) 6)) "n-list? on nested list") (check-false (n-list? 'a) "n-list? not a list") (check-false (n-list? '(a b c)) "n-list? not numbers (flat)") (check-false (n-list? '(1 2 3 4 5 6 7 e 9 0)) "n-list? oops, there's an 'e") (check-false (n-list? '(1 (2 (3 a) 5) 6)) "n-list? oops, a nested 'a") ;; -------------------------------------------------------------------------- ;; Problem 3 (structural recursion) ;; -------------------------------------------------------------------------- (check-equal? (tree-min 8) 8 "tree-min single node") (check-equal? (tree-min '(2 8 6)) 2 "tree-min simple tree") (check-equal? (tree-min '(1 (2 8 6) (4 3 9))) 1 "tree-min max at top level") (check-equal? (tree-min '(7 (12 8 6) (4 3 9))) 3 "tree-min max at nested level") ;(check-equal? (tree-min '(8 (13 11 (5 24 6)) (15 (12 10 14) 20))) ; 5) ;; -------------------------------------------------------------------------- ;; Problem 4 (little language) ;; -------------------------------------------------------------------------- (check-equal? (declared-vars 'x) '() "declared-vars: single var") (check-equal? (declared-vars '(square x)) '() "declared-vars: simple app") (check-equal? (declared-vars '(lambda (y) (x y))) '(y) "declared-vars: single lambda") (check-equal? (declared-vars '(lambda (x) (lambda (z) z))) '(x z) "declared-vars: nested lambda") (check-equal? (declared-vars '((lambda (x) (lambda (z) z)) (lambda (x) y) )) '(x z x) "declared-vars: app with nested lambdas") (check-equal? (declared-vars '(lambda (f) (lambda (x) (lambda (y) (lambda (z) (f (x (y z)))))))) '(f x y z) "declared-vars: deeply-nested lambdas") ;; -------------------------------------------------------------------------- ;; Problem 5 (little language) ;; -------------------------------------------------------------------------- (check-equal? (prefix->postfix 'x) 'x "pre->post: single var") (check-equal? (prefix->postfix '(square x)) '(x square) "pre->post: simple app") (check-equal? (prefix->postfix '(lambda (y) y)) '(lambda (y) y) "pre->post: simple lambda") (check-equal? (prefix->postfix '(lambda (y) (x y))) '(lambda (y) (y x)) "pre->post: lambda with app") (check-equal? (prefix->postfix '(lambda (f) (lambda (x) (lambda (y) (lambda (z) (f (x (y z)))))))) '(lambda (f) (lambda (x) (lambda (y) (lambda (z) (((z y) x) f))))) "pre->post: deeply-nested lambda") ;; --------------------------------------------------------------------------