Advertisement
timothy235

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

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