#lang scheme ;;; Change this line, depending on which ;;; file you want to run test-cases on: (require "hw06-L0.ss") ; Like check-expect, except that we can use this inside map. ; (define (my-check-expect actual expected test-info) (cond [(and (string? actual) (string? expected)) ; When comparing strings, ignore whitespace: (my-check-expect (string->sexpr actual) (string->sexpr expected) test-info)] [(not (equal? actual expected)) (begin (display (format "!!! Failed test ~v:\nGot ~v, expected ~v.\n" test-info actual expected)) false)] [else true])) ;;;; Some test-functions for manual inspecting the output: ;; (Call these, to see exactly what a failed test case is doing.) ; get-test : (-> int string) ; Return the i'th test case (the raw, unparsed string) ; (define (get-test i) (first (list-ref tests i))) ; test-parse : (-> int void) ; A manual helper function: ; Extract the i'th test string, and parse it. ; Print the result. ; (define (test-parse i) (printf "~v => ~v" (get-test i) (parse-string (get-test i)))) ; test-eval : (-> int void) ; A manual helper function: ; Extract the i'th test string, parse it, and eval it. ; Print the result. ; (define (test-eval i) (printf "~v => ~v" (get-test i) (eval (parse-string (get-test i))))) ; test-toString : (-> int string) ; A manual helper function: ; Extract the i'th test string, parse it, and then ; convert the internal representation into a string. ; (define (test-expr->string i) (expr->string (parse-string (get-test i)))) ; Check that the internal representation ; (if present in 'tests') is as expected: ; (define (test-parse-all) (map (lambda (t) (or (< (length t) 3) (my-check-expect (parse-string (first t)) (third t) "internal representation"))) tests)) ; Check that s = (expr->string (parse-string s)). ; More precisely, because of differences in parens (and pretty-printing) in expr->string, ; we'll check that (parse-string s) = (parse-string (expr->string (parse-string s))) ; (define (test-expr->string-all) (map (lambda (t) (my-check-expect (expr->string (parse-string (first t))) (first t) "expr->string should be inverse of parse-string:")) tests)) ; ; Actually, we won't get *exactly* the same string back: ; Check that the internal representation (if present in 'tests') is as expected: ; Check eval: ; (define (test-eval-all) (map (lambda (t) (my-check-expect (eval (parse-string (first t))) (second t) (format "eval ~a" (first t)))) tests)) (define (test-all) (list (test-parse-all) (test-expr->string-all) (test-eval-all))) (define (string->sexpr str) (read (open-input-string str))) (test-all)