Advertisement
timothy235

sicp-4-3-3-amb-repl-test

Mar 25th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 13.24 KB | None | 0 0
  1. #lang racket
  2. (require rackunit
  3.          "4-3-3-amb-repl-program.rkt")
  4.  
  5. ;; The same as 4-1-7-repl-test.rkt only new test cases for amb were added.
  6.  
  7. ;; Note that we can't check the 'try-again functionality because that only works with
  8. ;; the repl.
  9.  
  10. ;; default succeed and fail continuations used in calls to ambeval
  11. (define dsc (lambda (val fail) val))
  12. (define dfc (lambda () 'failed))
  13.  
  14. (test-case "self-evaluating expression"
  15.            (check-equal? (ambeval "x" the-global-environment dsc dfc)
  16.                          "x")
  17.            (check-equal? (ambeval 23 the-global-environment dsc dfc)
  18.                          23)
  19.            )
  20.  
  21. (test-case "quotation"
  22.            (define quote-expr '(quote ("x" 23 a)))
  23.            (check-equal? (ambeval quote-expr the-global-environment dsc dfc)
  24.                          '("x" 23 a))
  25.            )
  26.  
  27. (test-case "variable definition"
  28.            (define def-expr '(define a 42))
  29.            (check-equal? (ambeval def-expr the-global-environment dsc dfc)
  30.                          'ok)
  31.            (check-equal? (ambeval 'a the-global-environment dsc dfc)
  32.                          42)
  33.            )
  34.  
  35. (test-case "procedure definition"
  36.            (define proc-expr '(define (multiply x y) (* x y)))
  37.            (check-equal? (ambeval proc-expr the-global-environment dsc dfc)
  38.                          'ok)
  39.            )
  40.  
  41. (test-case "procedure application"
  42.            (define application-expr '(multiply 3 4))
  43.            (check-equal? (ambeval application-expr the-global-environment dsc dfc)
  44.                          12)
  45.            (check-equal? (ambeval '(car (cons 1 (cons 2 '())))
  46.                                   the-global-environment
  47.                                   dsc
  48.                                   dfc)
  49.                          1)
  50.            )
  51.  
  52. (test-case "assignment"
  53.            (define assign-expr '(set! a 23))
  54.            (check-equal? (ambeval assign-expr the-global-environment dsc dfc)
  55.                          'ok)
  56.            (check-equal? (ambeval 'a the-global-environment dsc dfc)
  57.                          23)
  58.            )
  59.  
  60. (test-case "predicate"
  61.            (check-true (true? (ambeval 'a the-global-environment dsc dfc)))
  62.            (check-true (true? (ambeval "false" the-global-environment dsc dfc)))
  63.            (check-false (true? (ambeval 'false the-global-environment dsc dfc)))
  64.            (check-true (false? (ambeval 'false the-global-environment dsc dfc)))
  65.            )
  66.  
  67. (test-case "if statement"
  68.            (define ie '(if true 1 2))
  69.            (check-equal? (ambeval ie the-global-environment dsc dfc)
  70.                          1)
  71.            (define ie2 '(if false 1 2))
  72.            (check-equal? (ambeval ie2 the-global-environment dsc dfc)
  73.                          2)
  74.            )
  75.  
  76. (test-case "cond statement"
  77.            (define ce '(cond ("a" 1 2) ("b" 3)))
  78.            (check-equal? (ambeval ce the-global-environment dsc dfc)
  79.                          2)
  80.            (define alt-ce '(cond ((cons 1 (cons 2 '())) => car) (else 87)))
  81.            (check-equal? (ambeval alt-ce the-global-environment dsc dfc)
  82.                          1)
  83.            )
  84.  
  85. (test-case "begin statement"
  86.            (define be '(begin "a" "b" 1 2 (* 3 4)))
  87.            (check-equal? (ambeval be the-global-environment dsc dfc)
  88.                          12)
  89.            )
  90.  
  91. (test-case "boolean operator"
  92.            (ambeval '(define c 38) the-global-environment dsc dfc)
  93.            (check-equal? (ambeval '(and 1 "a" c) the-global-environment dsc dfc)
  94.                          38)
  95.            (check-equal? (ambeval '(and 1 1 false) the-global-environment dsc dfc)
  96.                         false)
  97.            (check-equal? (ambeval '(or false false 1)
  98.                                   the-global-environment
  99.                                   dsc
  100.                                   dfc)
  101.                          1)
  102.            (check-equal? (ambeval '(or false false false)
  103.                                   the-global-environment
  104.                                   dsc
  105.                                   dfc)
  106.                         false)
  107.            )
  108.  
  109. (test-case "let"
  110.            (define le '(let ((x 1) (y 2) (z 3)) 'side-effect (+ x y z)))
  111.            (check-equal? (ambeval le the-global-environment dsc dfc)
  112.                          6)
  113.            (define le2 '(let ((x 1) (y 2)) (+ x y)))
  114.            (check-equal? (ambeval le2 the-global-environment dsc dfc)
  115.                          3)
  116.            (define le3 '(let ((x 1)) 'side-effect (+ x 2)))
  117.            (check-equal? (ambeval le3 the-global-environment dsc dfc)
  118.                          3)
  119.            (define le4 '(let () 'side-effect (+ 3 2)))
  120.            (check-equal? (ambeval le4 the-global-environment dsc dfc)
  121.                          5)
  122.            (define le5 '(let () (+ 3 2)))
  123.            (check-equal? (ambeval le5 the-global-environment dsc dfc)
  124.                          5)
  125.            ; let with internal defines
  126.            (define le6 '(let ([x 1])
  127.                           (define y 2)
  128.                           (+ x y)))
  129.            (check-equal? (ambeval le6 the-global-environment dsc dfc)
  130.                          3)
  131.            (define le7 '(let* ([x 1]
  132.                                [y (+ x 1)])
  133.                           (define z (+ x y))
  134.                           (define w (+ z 1))
  135.                           (+ x y z w)))
  136.            (check-equal? (ambeval le7 the-global-environment dsc dfc)
  137.                          10)
  138.            )
  139.  
  140. (test-case "named-let"
  141.            (ambeval '(define (fib n)
  142.                        (let fib-iter ((a 1)
  143.                                       (b 0)
  144.                                       (my-count n))
  145.                          (if (= my-count 0)
  146.                            b
  147.                            (fib-iter (+ a b) a (+ my-count -1)))))
  148.                     the-global-environment
  149.                     dsc
  150.                     dfc)
  151.            (check-equal? (ambeval '(fib 0) the-global-environment dsc dfc)
  152.                          0)
  153.            (check-equal? (ambeval '(fib 1) the-global-environment dsc dfc)
  154.                          1)
  155.            (check-equal? (ambeval '(fib 10) the-global-environment dsc dfc)
  156.                          55)
  157.            )
  158.  
  159. (test-case "let*"
  160.            (define lse '(let* ((x 1) (y (+ x 1)) (z (+ y 1))) (+ x y z)))
  161.            (check-equal? (ambeval lse the-global-environment dsc dfc)
  162.                          6)
  163.            (define lse2 '(let* ((a 1) (b 2)) 'side-effect (+ a b)))
  164.            (check-equal? (ambeval lse2 the-global-environment dsc dfc)
  165.                          3)
  166.            (define lse3 '(let* ((a 1)) 'side-effect (+ a 2)))
  167.            (check-equal? (ambeval lse3 the-global-environment dsc dfc)
  168.                          3)
  169.            (define lse4 '(let* ((a 1)) (+ a 2)))
  170.            (check-equal? (ambeval lse4 the-global-environment dsc dfc)
  171.                          3)
  172.            )
  173.  
  174. (test-case "letrec"
  175.            (ambeval '(define (my-even? x)
  176.                        (letrec ((ev? (lambda (n)
  177.                                        (if (= n 0)
  178.                                         true
  179.                                          (od? (- n 1)))))
  180.                                 (od? (lambda (n)
  181.                                        (if (= n 0)
  182.                                         false
  183.                                          (ev? (- n 1))))))
  184.                          (ev? x)))
  185.                     the-global-environment
  186.                     dsc
  187.                     dfc)
  188.            (check-equal? (ambeval '(my-even? 11) the-global-environment dsc dfc)
  189.                         false)
  190.            (check-equal? (ambeval '(my-even? 12) the-global-environment dsc dfc)
  191.                         true)
  192.            )
  193.  
  194. (test-case "multiple-dwelling"
  195.            (ambeval '(define (multiple-dwelling)
  196.                        (let ([baker (an-element-of (list 1 2 3 4 5))])
  197.                          (my-require (not (= baker 5)))
  198.                          (let ([cooper (amb 1 2 3 4 5)])
  199.                            (my-require (not (= cooper 1)))
  200.                            (let ([fletcher (amb 1 2 3 4 5)])
  201.                              (my-require (not (= fletcher 5)))
  202.                              (my-require (not (= fletcher 1)))
  203.                              (my-require (not (= (abs (- fletcher cooper)) 1)))
  204.                              (let ([miller (amb 1 2 3 4 5)])
  205.                                (my-require (> miller cooper))
  206.                                (let ([smith (amb 1 2 3 4 5)])
  207.                                  (my-require (distinct? (list baker
  208.                                                               cooper
  209.                                                               fletcher
  210.                                                               miller
  211.                                                               smith)))
  212.                                  (my-require (not (= (abs (- smith fletcher)) 1)))
  213.                                  (list (list 'baker baker)
  214.                                        (list 'cooper cooper)
  215.                                        (list 'fletcher fletcher)
  216.                                        (list 'miller miller)
  217.                                        (list 'smith smith))))))))
  218.                     the-global-environment
  219.                     dsc
  220.                     dfc)
  221.            (check-equal? (ambeval '(multiple-dwelling)
  222.                                   the-global-environment
  223.                                   dsc
  224.                                   dfc)
  225.                          '((baker 3)
  226.                            (cooper 2)
  227.                            (fletcher 4)
  228.                            (miller 5)
  229.                            (smith 1)))
  230.            )
  231.  
  232. (test-case "fathers-daughters-and-yachts"
  233.            (ambeval '(define (fathers-daughters-and-yachts)
  234.                        (define names '(mary-ann gabrielle lorna rosalind melissa))
  235.                        (let ([parker-daughter (an-element-of names)]
  236.                              [parker-yacht (an-element-of names)]
  237.                              [hood-daughter 'melissa]
  238.                              [hood-yacht 'gabrielle]
  239.                              [moore-daughter (an-element-of names)]
  240.                              [moore-yacht 'lorna]
  241.                              [downing-daughter (an-element-of names)]
  242.                              [downing-yacht 'melissa]
  243.                              [hall-daughter (an-element-of names)]
  244.                              [hall-yacht 'rosalind])
  245.                          (my-require (distinct?
  246.                                        (list moore-daughter
  247.                                              downing-daughter
  248.                                              hall-daughter
  249.                                              hood-daughter
  250.                                              parker-daughter)))
  251.                          (my-require (distinct?
  252.                                        (list moore-yacht
  253.                                              downing-yacht
  254.                                              hall-yacht
  255.                                              hood-yacht
  256.                                              parker-yacht)))
  257.                          (my-require (eq? moore-daughter 'mary-ann))
  258.                          (my-require (not (eq? moore-daughter moore-yacht)))
  259.                          (my-require (not (eq? downing-daughter downing-yacht)))
  260.                          (my-require (not (eq? hall-daughter hall-yacht)))
  261.                          (my-require (not (eq? hood-daughter hood-yacht)))
  262.                          (my-require (not (eq? parker-daughter parker-yacht)))
  263.                          (my-require (or (not (eq? moore-daughter 'gabrielle))
  264.                                          (eq? moore-yacht parker-daughter)))
  265.                          (my-require (or (not (eq? downing-daughter 'gabrielle))
  266.                                          (eq? downing-yacht parker-daughter)))
  267.                          (my-require (or (not (eq? hall-daughter 'gabrielle))
  268.                                          (eq? hall-yacht parker-daughter)))
  269.                          (my-require (or (not (eq? hood-daughter 'gabrielle))
  270.                                          (eq? hood-yacht parker-daughter)))
  271.                          (my-require (or (not (eq? parker-daughter 'gabrielle))
  272.                                          (eq? parker-yacht parker-daughter)))
  273.                          (list (list 'moore moore-daughter)
  274.                                (list 'downing downing-daughter)
  275.                                (list 'hall hall-daughter)
  276.                                (list 'hood hood-daughter)
  277.                                (list 'parker parker-daughter))))
  278.                     the-global-environment
  279.                     dsc
  280.                     dfc)
  281.            (check-equal? (ambeval '(fathers-daughters-and-yachts)
  282.                                   the-global-environment
  283.                                   dsc
  284.                                   dfc)
  285.                          '((moore mary-ann)
  286.                            (downing lorna)
  287.                            (hall gabrielle)
  288.                            (hood melissa)
  289.                            (parker rosalind)))
  290.            )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement