/tests/moby-programs/exercise-control.rkt

http://github.com/dyoo/mzscheme-vm · Racket · 133 lines · 89 code · 40 blank · 4 comment · 23 complexity · a646070bfa7f4abe9451059efa8b606c MD5 · raw file

  1. #lang s-exp "../../lang/wescheme.ss"
  2. (printf "exercise-control.rkt\n")
  3. (check-expect (if true
  4. 'ok
  5. 'not-ok)
  6. 'ok)
  7. (check-expect (if false
  8. 'not-ok
  9. 'ok)
  10. 'ok)
  11. (check-expect (cond [true 'ok]
  12. [else 'not-ok])
  13. 'ok)
  14. (check-expect (cond [false 'not-ok]
  15. [else 'ok])
  16. 'ok)
  17. (check-expect (case 42
  18. [(1) 'not-ok]
  19. [(2) 'not-ok]
  20. [(42) 'ok])
  21. 'ok)
  22. (check-expect (case 42
  23. [(1) 'not-ok]
  24. [(2) 'not-ok]
  25. [(42) 'ok])
  26. 'ok)
  27. ;; Runtime error: we should see if the test isn't boolean
  28. (with-handlers ([exn:fail?
  29. (lambda (exn)
  30. (unless (string=? "cond: question result is not true or false: 42"
  31. (exn-message exn))
  32. (error 'cond-test)))])
  33. (cond
  34. [42
  35. (error 'uh-oh)]
  36. [else
  37. (error 'cond-test)]))
  38. ;; Test fall-through
  39. (with-handlers ([exn:fail?
  40. (lambda (exn)
  41. (unless (string=? "cond: all question results were false"
  42. (exn-message exn))
  43. (error 'cond-test)))])
  44. (cond
  45. [false (error 'uh-oh)]))
  46. ;; Runtime error: we should see if the test isn't boolean
  47. (with-handlers ([exn:fail?
  48. (lambda (exn)
  49. (unless (string=? "if: question result is not true or false: \"not a boolean\""
  50. (exn-message exn))
  51. (error 'cond-test)))])
  52. (if "not a boolean"
  53. (error 'uh-oh)
  54. (error 'uh-oh)))
  55. ;; Check fall-though with case being an error
  56. (with-handlers ([exn:fail?
  57. (lambda (exn)
  58. (unless (string=? "case: the expression matched none of the choices"
  59. (exn-message exn))
  60. (error 'case-test)))])
  61. (case 42
  62. [(1) (error 'case)]
  63. [(2) (error 'case)]
  64. [(3) (error 'case)])
  65. (error 'case))
  66. (with-handlers ([exn:fail?
  67. (lambda (exn)
  68. (unless (string=? "when: question result is not true or false: \"not a boolean\""
  69. (exn-message exn))
  70. (error 'when-boolean-test)))])
  71. (when "not a boolean"
  72. (error 'uh-oh)))
  73. (with-handlers ([exn:fail?
  74. (lambda (exn)
  75. (unless (string=? "unless: question result is not true or false: \"not a boolean\""
  76. (exn-message exn))
  77. (error 'unless-boolean-test)))])
  78. (unless "not a boolean"
  79. (error 'uh-oh)))
  80. (unless (= 0 0)
  81. (error 'huh?))
  82. (when (= 0 1)
  83. (error 'huh?))
  84. (check-expect (let/cc return
  85. (begin
  86. (return 42)
  87. (error)))
  88. 42)
  89. (check-expect (let/cc return
  90. (begin
  91. 'fall-through))
  92. 'fall-through)
  93. (printf "exercise-control.rkt end\n")