Advertisement
timothy235

sicp-4-2-3-lazy-lists-repl-test

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