Advertisement
timothy235

sicp-4-1-4-repl-test

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