Advertisement
timothy235

sicp-4-2-2-lazy-repl-test

Mar 18th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 6.92 KB | None | 0 0
  1. #lang racket
  2. (require rackunit
  3.          "4-2-2-lazy-repl-program.rkt")
  4.  
  5. ;; Same as 4-1-4-repl-test.rkt but includes tests for lazy evaluation and
  6. ;; memoization.
  7.  
  8. (test-case "self-evaluating expression"
  9.            (check-equal? (my-eval "x" the-global-environment)
  10.                          "x")
  11.            (check-equal? (my-eval 23 the-global-environment)
  12.                          23)
  13.            )
  14.  
  15. (test-case "quotation"
  16.            (define quote-expr '(quote ("x" 23 a)))
  17.            (check-equal? (my-eval quote-expr the-global-environment)
  18.                          '("x" 23 a))
  19.            )
  20.  
  21. (test-case "variable definition"
  22.            (define def-expr '(define a 42))
  23.            (check-equal? (my-eval def-expr the-global-environment)
  24.                          'ok)
  25.            (check-equal? (my-eval 'a the-global-environment)
  26.                          42)
  27.            )
  28.  
  29. (test-case "procedure definition"
  30.            (define proc-expr '(define (multiply x y) (* x y)))
  31.            (check-equal? (my-eval proc-expr the-global-environment)
  32.                          'ok)
  33.            )
  34.  
  35. (test-case "procedure application"
  36.            (define application-expr '(multiply 3 4))
  37.            (check-equal? (my-eval application-expr the-global-environment)
  38.                          12)
  39.            (check-equal? (my-eval '(car (cons 1 (cons 2 '())))
  40.                                   the-global-environment)
  41.                          1)
  42.            )
  43.  
  44. (test-case "assignment"
  45.            (define assign-expr '(set! a 23))
  46.            (check-equal? (my-eval assign-expr the-global-environment)
  47.                          'ok)
  48.            (check-equal? (my-eval 'a the-global-environment)
  49.                          23)
  50.            )
  51.  
  52. (test-case "predicate"
  53.            (check-true (true? (my-eval 'a the-global-environment)))
  54.            (check-true (true? (my-eval "false" the-global-environment)))
  55.            (check-false (true? (my-eval 'false the-global-environment)))
  56.            (check-true (false? (my-eval 'false the-global-environment)))
  57.            )
  58.  
  59. (test-case "if statement"
  60.            (define ie '(if true 1 2))
  61.            (check-equal? (my-eval ie the-global-environment)
  62.                          1)
  63.            (define ie2 '(if false 1 2))
  64.            (check-equal? (my-eval ie2 the-global-environment)
  65.                          2)
  66.            )
  67.  
  68. (test-case "cond statement"
  69.            (define ce '(cond ("a" 1 2) ("b" 3)))
  70.            (check-equal? (my-eval ce the-global-environment)
  71.                          2)
  72.            (define alt-ce '(cond ((cons 1 (cons 2 '())) => car) (else 87)))
  73.            (check-equal? (my-eval alt-ce the-global-environment)
  74.                          1)
  75.            )
  76.  
  77. (test-case "begin statement"
  78.            (define be '(begin "a" "b" 1 2 (* 3 4)))
  79.            (check-equal? (my-eval be the-global-environment)
  80.                          12)
  81.            )
  82.  
  83. (test-case "boolean operator"
  84.            (my-eval '(define c 38) the-global-environment)
  85.            (check-equal? (my-eval '(and 1 "a" c) the-global-environment)
  86.                          38)
  87.            (check-equal? (my-eval '(and 1 1 false) the-global-environment)
  88.                         false)
  89.            (check-equal? (my-eval '(or false false 1) the-global-environment)
  90.                          1)
  91.            (check-equal? (my-eval '(or false false false) the-global-environment)
  92.                         false)
  93.            )
  94.  
  95. (test-case "let"
  96.            (define le '(let ((x 1) (y 2) (z 3)) 'side-effect (+ x y z)))
  97.            (check-equal? (my-eval le the-global-environment)
  98.                          6)
  99.            (define le2 '(let ((x 1) (y 2)) (+ x y)))
  100.            (check-equal? (my-eval le2 the-global-environment)
  101.                          3)
  102.            (define le3 '(let ((x 1)) 'side-effect (+ x 2)))
  103.            (check-equal? (my-eval le3 the-global-environment)
  104.                          3)
  105.            (define le4 '(let () 'side-effect (+ 3 2)))
  106.            (check-equal? (my-eval le4 the-global-environment)
  107.                          5)
  108.            (define le5 '(let () (+ 3 2)))
  109.            (check-equal? (my-eval le5 the-global-environment)
  110.                          5)
  111.            (define le6 '(let ([x 1]) (define y 2) (+ x y)))
  112.            (check-equal? (my-eval le6 the-global-environment)
  113.                          3)
  114.            )
  115.  
  116. (test-case "named-let"
  117.            (my-eval '(define (fib n)
  118.                        (let fib-iter ((a 1)
  119.                                       (b 0)
  120.                                       (my-count n))
  121.                          (if (= my-count 0)
  122.                            b
  123.                            (fib-iter (+ a b) a (+ my-count -1)))))
  124.                     the-global-environment)
  125.            ; use actual-value to force the values
  126.            (check-equal? (actual-value '(fib 0) the-global-environment)
  127.                          0)
  128.            (check-equal? (actual-value '(fib 1) the-global-environment)
  129.                          1)
  130.            (check-equal? (actual-value '(fib 10) the-global-environment)
  131.                          55)
  132.            )
  133.  
  134. (test-case "let*"
  135.            (define lse '(let* ((x 1) (y (+ x 1)) (z (+ y 1))) (+ x y z)))
  136.            (check-equal? (my-eval lse the-global-environment)
  137.                          6)
  138.            (define lse2 '(let* ((a 1) (b 2)) 'side-effect (+ a b)))
  139.            (check-equal? (my-eval lse2 the-global-environment)
  140.                          3)
  141.            (define lse3 '(let* ((a 1)) 'side-effect (+ a 2)))
  142.            (check-equal? (my-eval lse3 the-global-environment)
  143.                          3)
  144.            (define lse4 '(let* ((a 1)) (+ a 2)))
  145.            (check-equal? (my-eval lse4 the-global-environment)
  146.                          3)
  147.            )
  148.  
  149. (test-case "lazy evaluation and memoization"
  150.            (my-eval '(define (try a b) (if (= a 0) 1 b))
  151.                     the-global-environment)
  152.            (check-equal? (my-eval '(try 0 (/ 1 0)) the-global-environment)
  153.                          1)
  154.            (my-eval '(define test-count 0) the-global-environment)
  155.            (check-equal? (my-eval 'test-count the-global-environment)
  156.                          0)
  157.            (my-eval '(define (id x) (set! test-count (+ test-count 1)) x)
  158.                     the-global-environment)
  159.            (my-eval '(define w (id (id 10))) the-global-environment)
  160.            (check-equal? (my-eval 'test-count the-global-environment)
  161.                          1)
  162.            (check-equal? (actual-value 'w the-global-environment)
  163.                          10)
  164.            (check-equal? (my-eval 'test-count the-global-environment)
  165.                          2)
  166.            ; check if test-count is incremented again
  167.            (check-equal? (actual-value 'w the-global-environment)
  168.                          10)
  169.            ; test-count should not increment again
  170.            (check-equal? (my-eval 'test-count the-global-environment)
  171.                          2)
  172.            )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement