reader-check.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. (define-test-suite r6rs-reader-tests)
  3. (define-test-case tokens r6rs-reader-tests
  4. (check
  5. (get-datum (make-string-input-port "-123")) => -123)
  6. (check
  7. (get-datum (make-string-input-port "+123")) => 123)
  8. (check
  9. (get-datum (make-string-input-port "...")) => '...)
  10. (check
  11. (get-datum (make-string-input-port "(...)")) => '(...))
  12. (check
  13. (get-datum (make-string-input-port "(... foo ... baz)")) => '(... foo ... baz))
  14. (check-exception
  15. (get-datum (make-string-input-port "..")))
  16. (check
  17. (get-datum (make-string-input-port ".5")) => 0.5)
  18. (check
  19. (get-datum (make-string-input-port "(1 2 3)")) => '(1 2 3))
  20. (check-exception
  21. (get-datum (make-string-input-port "(1 2 3]")))
  22. (check
  23. (get-datum (make-string-input-port "foo")) => 'foo)
  24. (check
  25. (get-datum (make-string-input-port "fOo")) => (string->symbol "fOo"))
  26. (check
  27. (get-datum (make-string-input-port "[1 2 3]")) => '(1 2 3))
  28. (check
  29. (get-datum (make-string-input-port "#\\linefeed")) => (integer->char 10))
  30. (check
  31. (get-datum (make-string-input-port "#\\x578")) => (integer->char #x578))
  32. (check
  33. (get-datum (make-string-input-port "\"\\a\\b\\t\\n\\v\\f\\r\\\"\\\\\""))
  34. => (list->string (map integer->char '(7 8 9 #xA #xB #xC #xD #x22 #x5c))))
  35. (check
  36. (get-datum (make-string-input-port "\"\\x578;\\x123;\""))
  37. => (list->string (map integer->char '(#x578 #x123))))
  38. (check-exception
  39. (get-datum (make-string-input-port "\"\\x578;\\x123\"")))
  40. (check-exception
  41. (get-datum (make-string-input-port "\"\\x578\\x123\"")))
  42. (check-exception
  43. (get-datum (make-string-input-port "#\\Alarm")))
  44. (check
  45. (get-datum (make-string-input-port "h\\x65;llo")) => 'hello)
  46. (check-exception
  47. (get-datum (make-string-input-port "h\\x65llo")))
  48. (check
  49. (get-datum (make-string-input-port "\\x2e;reader.")) => (string->symbol ".reader."))
  50. (check
  51. (get-datum (make-string-input-port "'foo")) => '(quote foo))
  52. (check
  53. (get-datum (make-string-input-port "`foo")) => '(quasiquote foo))
  54. (check
  55. (get-datum (make-string-input-port ",foo")) => '(unquote foo))
  56. (check
  57. (get-datum (make-string-input-port ",@foo")) => '(unquote-splicing foo))
  58. (check
  59. (get-datum (make-string-input-port "#'foo")) => '(syntax foo))
  60. (check
  61. (get-datum (make-string-input-port "#`foo")) => '(quasisyntax foo))
  62. (check
  63. (get-datum (make-string-input-port "#,foo")) => '(unsyntax foo))
  64. (check
  65. (get-datum (make-string-input-port "#,@foo")) => '(unsyntax-splicing foo))
  66. (check
  67. (get-datum (make-string-input-port "(1 #| foo bar |# 2 3)")) => '(1 2 3))
  68. (check
  69. (get-datum (make-string-input-port "(1 #| foo #| bar |# |# 2 3)")) => '(1 2 3))
  70. (check
  71. (get-datum (make-string-input-port "(1 #;(foo bar baz) 2 3)")) => '(1 2 3))
  72. (check
  73. (get-datum (make-string-input-port "->foo")) => (string->symbol "->foo"))
  74. (check
  75. (get-datum (make-string-input-port "#vu8(1 2 3 4 5)")) (=> blob=?) (u8-list->blob '(1 2 3 4 5)))
  76. (check
  77. (get-datum (make-string-input-port "(#t #f #b1001 #T #F #B1001)")) => '(#t #f 9 #t #f 9))
  78. (check-exception
  79. (get-datum (make-string-input-port "@")))
  80. (check
  81. (get-datum (make-string-input-port "a@")) => 'a@)
  82. (check
  83. (get-datum (make-string-input-port (string (integer->char #xa0)))) => (eof-object))
  84. (check
  85. (get-datum (make-string-input-port (string (integer->char #xa1)))) =>
  86. (string->symbol (string (integer->char #xa1))))
  87. (check-exception
  88. (get-datum (make-string-input-port "(#\\Z#\\F)")))
  89. (check
  90. (get-datum (make-string-input-port "(#\\Z #\\F)")) => '(#\Z #\F))
  91. (check-exception
  92. (get-datum (make-string-input-port "->#")))
  93. (check-exception
  94. (get-datum (make-string-input-port "(a#b)")))
  95. (check-exception
  96. (get-datum (make-string-input-port "(a,b)"))) ; missing delimiter
  97. (check
  98. (get-datum (make-string-input-port "(a ,b)")) => '(a (unquote b)))
  99. (check-exception
  100. (get-datum (make-string-input-port "(#\\A,b)"))) ; missing delimiter
  101. (check
  102. (get-datum (make-string-input-port "(#\\A ,b)")) => '(#\A (unquote b)))
  103. (check-exception
  104. (get-datum (make-string-input-port "(#\\t,b)")))
  105. (check
  106. (get-datum (make-string-input-port "(#\\t ,b)")) => '(#\t (unquote b)))
  107. (check-exception
  108. (get-datum (make-string-input-port "(#t#f)")))
  109. (check
  110. (get-datum (make-string-input-port "(#t #f)")) => '(#t #f))
  111. (check
  112. (get-datum (make-string-input-port "#!r6rs")) => (eof-object))
  113. (check
  114. (get-datum (make-string-input-port "#!r6rs ")) => (eof-object))
  115. (check
  116. (get-datum (make-string-input-port "#!r6rs a")) => 'a))
  117. (define (single-character-passes lo hi)
  118. (do ((i lo (+ 1 i))
  119. (r '()
  120. (guard
  121. (_
  122. (else r))
  123. (get-datum (make-string-input-port (string (integer->char i))))
  124. (cons i r))))
  125. ((> i hi)
  126. (reverse r))))
  127. (define-test-case single-character-passes r6rs-reader-tests
  128. (check (single-character-passes 0 255)
  129. => '(9 10 11 12 13 32 33 36 37 38 42 43 45 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 94 95 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 126 133 160 161 162 163 164 165 166 167 168 169 170 172 174 175 176 177 178 179 180 181 182 183 184 185 186 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255)))
  130. (define (count-single-character-passes lo hi)
  131. (do ((i lo (+ 1 i))
  132. (r 0
  133. (guard
  134. (_
  135. (else r))
  136. (get-datum (make-string-input-port (string (integer->char i))))
  137. (+ 1 r))))
  138. ((> i hi)
  139. r)))
  140. (define-test-case count-single-character-passes r6rs-reader-tests
  141. ;; this takes about a minute on Mike's machine
  142. (check (count-single-character-passes 0 #x10FFFF) => 235735))