low-test.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber, Martin Gasbichler
  3. ; Tests for many of the primitives, both inlined and close-compiled.
  4. ; This prints `Hello' three times and then the first command argument, if any,
  5. ; and finally reads a line from standard input and prints it to standard output.
  6. (define (start arg in in-encoding out out-encoding error-out error-out-encoding resumer-records)
  7. (call-with-values
  8. (lambda ()
  9. (values "H" "e" "ll" "o" " "))
  10. (lambda (h e ll o sp)
  11. (write-string h out)
  12. (write-string e out)
  13. (write-string ll out)
  14. (write-string o out)
  15. (write-string sp out)))
  16. (call-with-values
  17. (lambda ()
  18. (apply values "H" "e" '("ll" "o" " ")))
  19. (lambda (h e ll o sp)
  20. (write-string h out)
  21. (write-string e out)
  22. (write-string ll out)
  23. (write-string o out)
  24. (write-string sp out)))
  25. (call-with-values
  26. (lambda ()
  27. (values "H" "e" "ll" "o" " "))
  28. (lambda (h e . more)
  29. (write-string h out)
  30. (write-string e out)
  31. (letrec ((loop (lambda (more)
  32. (if (eq? '() more)
  33. 0
  34. (begin
  35. (write-string (car more) out)
  36. (loop (cdr more)))))))
  37. (loop more))))
  38. (if (vector? arg)
  39. (if (< 0 (vector-length arg))
  40. (write-byte-vector (vector-ref arg 0) out)))
  41. (newline out)
  42. (write-string "=" out) (newline out)
  43. (bool-test (= 1 1) "(= 1 1)" #t out)
  44. (bool-test (= 1 2) "(= 1 2)" #f out)
  45. (bool-test (= 1 1 1) "(= 1 1 1)" #t out)
  46. (bool-test (= 1 2 3) "(= 1 2 3)" #f out)
  47. (bool-test (= 1 1 1 3) "(= 1 1 1 3)" #f out)
  48. (bool-test (= 1 1 1 1) "(= 1 1 1 1)" #t out)
  49. (bool-test (= 1 1 2 3) "(= 1 1 2 3)" #f out)
  50. (bool-test (= 1 1 1 3) "(= 1 1 1 3)" #f out)
  51. (write-string "==" out) (newline out)
  52. ((lambda (=)
  53. (bool-test (= 1 1) "(= 1 1)" #t out)
  54. (bool-test (= 1 2) "(= 1 2)" #f out)
  55. (bool-test (= 1 1 1) "(= 1 1 1)" #t out)
  56. (bool-test (= 1 2 3) "(= 1 2 3)" #f out)
  57. (bool-test (= 1 1 3) "(= 1 1 3)" #f out)
  58. (bool-test (= 1 1 1 3) "(= 1 1 1 3)" #f out)
  59. (bool-test (= 1 1 1 1) "(= 1 1 1 1)" #t out)
  60. (bool-test (= 1 1 2 3) "(= 1 1 2 3)" #f out)
  61. (bool-test (= 1 1 1 3) "(= 1 1 1 3)" #f out))
  62. =)
  63. (write-string "<" out) (newline out)
  64. (bool-test (< 1 1) "(< 1 1)" #f out)
  65. (bool-test (< 1 2) "(< 1 2)" #t out)
  66. (bool-test (< 1 1 1) "(< 1 1 1)" #f out)
  67. (bool-test (< 1 2 3) "(< 1 2 3)" #t out)
  68. (bool-test (< 1 2 2) "(< 1 2 2)" #f out)
  69. (write-string "<<" out) (newline out)
  70. ((lambda (<)
  71. (bool-test (< 1 1) "(< 1 1)" #f out)
  72. (bool-test (< 1 2) "(< 1 2)" #t out)
  73. (bool-test (< 1 1 1) "(< 1 1 1)" #f out)
  74. (bool-test (< 1 2 3) "(< 1 2 3)" #t out)
  75. (bool-test (< 1 2 2) "(< 1 2 2)" #f out))
  76. <)
  77. (write-string "<=" out) (newline out)
  78. (bool-test (<= 1 1) "(<= 1 1)" #t out)
  79. (bool-test (<= 1 2) "(<= 1 2)" #t out)
  80. (bool-test (<= 2 1) "(<= 2 1)" #f out)
  81. (bool-test (<= 1 1 1) "(<= 1 1 1)" #t out)
  82. (bool-test (<= 1 2 3) "(<= 1 2 3)" #t out)
  83. (bool-test (<= 1 2 2) "(<= 1 2 2)" #t out)
  84. (bool-test (<= 1 2 1) "(<= 1 2 1)" #f out)
  85. (write-string "<=<=" out) (newline out)
  86. ((lambda (<=)
  87. (bool-test (<= 1 1) "(<= 1 1)" #t out)
  88. (bool-test (<= 1 2) "(<= 1 2)" #t out)
  89. (bool-test (<= 2 1) "(<= 2 1)" #f out)
  90. (bool-test (<= 1 1 1) "(<= 1 1 1)" #t out)
  91. (bool-test (<= 1 2 3) "(<= 1 2 3)" #t out)
  92. (bool-test (<= 1 2 2) "(<= 1 2 2)" #t out)
  93. (bool-test (<= 1 2 1) "(<= 1 2 1)" #f out))
  94. <=)
  95. (write-string ">" out) (newline out)
  96. (bool-test (> 1 1) "(> 1 1)" #f out)
  97. (bool-test (> 2 1) "(> 2 1)" #t out)
  98. (bool-test (> 1 1 1) "(> 1 1 1)" #f out)
  99. (bool-test (> 3 2 1) "(> 3 2 1)" #t out)
  100. (bool-test (> 2 1 1) "(> 2 1 1)" #f out)
  101. (write-string ">>" out) (newline out)
  102. ((lambda (>)
  103. (bool-test (> 1 1) "(> 1 1)" #f out)
  104. (bool-test (> 2 1) "(> 2 1)" #t out)
  105. (bool-test (> 1 1 1) "(> 1 1 1)" #f out)
  106. (bool-test (> 3 2 1) "(> 3 2 1)" #t out)
  107. (bool-test (> 2 1 1) "(> 2 1 1)" #f out))
  108. >)
  109. (write-string ">=" out) (newline out)
  110. (bool-test (>= 1 1) "(>= 1 1)" #t out)
  111. (bool-test (>= 2 1) "(>= 2 1)" #t out)
  112. (bool-test (>= 1 2) "(>= 1 2)" #f out)
  113. (bool-test (>= 1 1 1) "(>= 1 1 1)" #t out)
  114. (bool-test (>= 3 2 1) "(>= 3 2 1)" #t out)
  115. (bool-test (>= 2 1 1) "(>= 2 1 1)" #t out)
  116. (bool-test (>= 2 1 2) "(>= 2 1 2)" #f out)
  117. (write-string ">=>=" out) (newline out)
  118. ((lambda (>=)
  119. (bool-test (>= 1 1) "(>= 1 1)" #t out)
  120. (bool-test (>= 2 1) "(>= 2 1)" #t out)
  121. (bool-test (>= 1 2) "(>= 1 2)" #f out)
  122. (bool-test (>= 1 1 1) "(>= 1 1 1)" #t out)
  123. (bool-test (>= 3 2 1) "(>= 3 2 1)" #t out)
  124. (bool-test (>= 2 1 1) "(>= 2 1 1)" #t out)
  125. (bool-test (>= 2 1 2) "(>= 2 1 2)" #f out))
  126. >=)
  127. (arith-test (vector-length (make-vector 3)) "make-vector0" 3 out)
  128. (arith-test (vector-length (make-vector 3 4)) "make-vector1" 3 out)
  129. (arith-test (vector-ref (make-vector 3 4) 2) "make-vector2" 4 out)
  130. ((lambda (make-vector)
  131. (arith-test (vector-length (make-vector 3)) "make-vector3" 3 out)
  132. (arith-test (vector-length (make-vector 3 4)) "make-vector4" 3 out)
  133. (arith-test (vector-ref (make-vector 3 4) 2) "make-vector5" 4 out))
  134. make-vector)
  135. (arith-test (string-length (make-string 3)) "make-string0" 3 out)
  136. (arith-test (string-length (make-string 3 #\a)) "make-string1" 3 out)
  137. (arith-test (- (char->scalar-value (string-ref (make-string 3) 2))
  138. (char->scalar-value #\?))
  139. "make-string2" 0 out)
  140. (arith-test (- (char->scalar-value (string-ref (make-string 3 #\a) 2))
  141. (char->scalar-value #\a))
  142. "make-string3" 0 out)
  143. ((lambda (make-string)
  144. (arith-test (string-length (make-string 3)) "make-string4" 3 out)
  145. (arith-test (string-length (make-string 3 #\a)) "make-string5" 3 out)
  146. (arith-test (- (char->scalar-value (string-ref (make-string 3) 2))
  147. (char->scalar-value #\?))
  148. "make-string6" 0 out)
  149. (arith-test (- (char->scalar-value (string-ref (make-string 3 #\a) 2))
  150. (char->scalar-value #\a))
  151. "make-string7" 0 out))
  152. make-string)
  153. (arith-test (apply + '()) "(apply + '())" 0 out)
  154. (arith-test (apply + '(1)) "(apply + '(1))" 1 out)
  155. (arith-test (apply + 1 '()) "(apply + 1 '())" 1 out)
  156. (arith-test (apply + '(1 2)) "(apply + '(1 2))" 3 out)
  157. (arith-test (apply + 1 '(2)) "(apply + 1 '(2))" 3 out)
  158. (arith-test (apply + 1 2 '()) "(apply + 1 2 '())" 3 out)
  159. (arith-test (apply + '(1 2 3)) "(apply + '(1 2 3))" 6 out)
  160. (arith-test (apply + 1 2 '(3)) "(apply + 1 2 '(3))" 6 out)
  161. (bool-test (apply < '(1 2)) "(apply < '(1 2))" #t out)
  162. (bool-test (apply < 1 '(2)) "(apply < 1 '(2))" #t out)
  163. (bool-test (apply < 1 2 '()) "(apply < 1 2 '())" #t out)
  164. (bool-test (apply < '(1 2 3)) "(apply < '(1 2 3))" #t out)
  165. (bool-test (apply < 1 2 '(3)) "(apply < 1 2 '(3))" #t out)
  166. (arith-test (apply apply (list + '(1 2 3)))
  167. "(apply apply (list + '(1 2 3)))" 6 out)
  168. (arith-test (apply apply (list + 1 2 '(3)))
  169. "(apply apply (list + 1 2 '(3)))" 6 out)
  170. (arith-test (apply + (apply apply list 1 '(2 (3))))
  171. "(apply + (apply apply list 1 '(2 (3))))" 6 out)
  172. (arith-test (apply apply (list + 1 2 3 '()))
  173. "(apply apply (list + 1 2 3 '()))" 6 out)
  174. (arith-test (apply apply + 1 '(2 3 ())) "(apply apply + 1 '(2 3 ())" 6 out)
  175. (arith-test (apply apply + 1 '(2 (3))) "(apply apply + 1 '(2 3 ())" 6 out)
  176. (arith-test (apply apply + 1 2 3 '(())) "(apply + 1 2 3 '(()))" 6 out)
  177. ((lambda (apply)
  178. (arith-test (apply + '(1 2 3)) "(apply + '(1 2 3))" 6 out)
  179. (arith-test (apply + 1 2 '(3)) "(apply + 1 2 '(3))" 6 out)
  180. (arith-test (apply apply (list + '(1 2 3)))
  181. "(apply apply (list + '(1 2 3)))" 6 out)
  182. (arith-test (apply apply (list + 1 2 '(3)))
  183. "(apply apply (list + 1 2 '(3)))" 6 out)
  184. (arith-test (apply apply (list + 1 2 3 '()))
  185. "(apply apply (list + 1 2 3 '()))" 6 out)
  186. (arith-test (apply apply + 1 '(2 3 ())) "(apply apply + 1 '(2 3 ())" 6 out)
  187. (arith-test (apply apply + 1 '(2 (3))) "(apply apply + 1 '(2 3 ())" 6 out)
  188. (arith-test (apply apply + 1 2 3 '(())) "(apply + 1 2 3 '(()))" 6 out))
  189. apply)
  190. (arith-test (+) "(+)" 0 out)
  191. (arith-test (+ 3) "(+ 3)" 3 out)
  192. (arith-test (+ 3 4) "(+ 3 4)" 7 out)
  193. (arith-test (+ 3 4 5) "(+ 3 4 5)" 12 out)
  194. (arith-test (+ 1 2 3 4 5 6 7 8 9 10 -50) "(+ 1 2 3 ... 10 -50) = 5" 5 out)
  195. ((lambda (+)
  196. (arith-test (+) "(+) closed" 0 out)
  197. (arith-test (+ 3) "(+ 3) closed" 3 out)
  198. (arith-test (+ 3 4) "(+ 3 4) closed" 7 out)
  199. (arith-test (+ 3 4 5) "(+ 3 4 5) closed" 12 out)
  200. (arith-test (+ 3 4 5 6) "(+ 3 4 5 6) closed" 18 out)
  201. (arith-test (+ 3 4 5 6 7) "(+ 3 4 5 6 7) closed" 25 out)
  202. (arith-test (+ 1 2 3 4 5 6 7 8 9 10 -50) "(+ 1 2 3 ... 10 -50) = 5 closed" 5 out))
  203. +)
  204. (arith-test (*) "(*)" 1 out)
  205. (arith-test (* 3) "(* 3)" 3 out)
  206. (arith-test (* 3 4) "(* 3 4)" 12 out)
  207. (arith-test (* 3 4 2) "(* 3 4 2)" 24 out)
  208. ((lambda (*)
  209. (arith-test (*) "(*)" 1 out)
  210. (arith-test (* 3) "(* 3)" 3 out)
  211. (arith-test (* 3 4) "(* 3 4)" 12 out)
  212. (arith-test (* 3 4 2) "(* 3 4 2)" 24 out))
  213. *)
  214. (arith-test (- -3) "(- -3)" 3 out)
  215. (arith-test (- 3 -4) "(- 3 -4)" 7 out)
  216. ((lambda (-)
  217. (arith-test (- -3) "(- -3)" 3 out)
  218. (arith-test (- 3 -4) "(- 3 -4)" 7 out))
  219. -)
  220. (arith-test (/ 1) "(/ 1)" 1 out)
  221. (arith-test (/ 12 4) "(/ 12 4)" 3 out)
  222. ((lambda (/)
  223. (arith-test (/ 1) "(/ 1)" 1 out)
  224. (arith-test (/ 12 4) "(/ 12 4)" 3 out))
  225. /)
  226. (arith-test (bitwise-ior) "(bitwise-ior)" 0 out)
  227. (arith-test (bitwise-ior 5) "(bitwise-ior 5)" 5 out)
  228. (arith-test (bitwise-ior 5 3) "(bitwise-ior 5 3)" 7 out)
  229. (arith-test (bitwise-ior 5 3 16) "(bitwise-ior 5 3 16)" 23 out)
  230. (arith-test (bitwise-xor) "(bitwise-xor)" 0 out)
  231. (arith-test (bitwise-xor 5) "(bitwise-xor 5)" 5 out)
  232. (arith-test (bitwise-xor 5 3) "(bitwise-xor 5 3)" 6 out)
  233. (arith-test (bitwise-xor 5 3 16) "(bitwise-xor 5 3 16)" 22 out)
  234. (arith-test (- (bitwise-and)) "(- (bitwise-and))" 1 out)
  235. (arith-test (bitwise-and 5) "(bitwise-and 5)" 5 out)
  236. (arith-test (bitwise-and 7 3) "(bitwise-and 7 3)" 3 out)
  237. (arith-test (bitwise-and 7 3 17) "(bitwise-and 7 3 17)" 1 out)
  238. (apply write-string (read-string in) (cons out '()))
  239. (newline out)
  240. 0)
  241. (define (length l n)
  242. (if (eq? l '()) n (length (cdr l) (+ n 1))))
  243. (define (list . x) x)
  244. (define numbers
  245. '#("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
  246. "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
  247. "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"))
  248. (define (number->string n)
  249. (vector-ref numbers n))
  250. (define (arith-test n s a out)
  251. (if (= n a)
  252. (begin
  253. (write-string "Success: " out)
  254. (write-string s out)
  255. (newline out))
  256. (begin
  257. (write-string "Failure: " out)
  258. (write-string s out)
  259. (write-string " = " out)
  260. (write-string (number->string n) out)
  261. (write-string " and not " out)
  262. (write-string (number->string a) out)
  263. (newline out))))
  264. (define (bool-test n s a out)
  265. (if (eq? n a)
  266. (begin
  267. (write-string "Success: " out)
  268. (write-string s out)
  269. (newline out))
  270. (begin
  271. (write-string "Failure: " out)
  272. (write-string s out)
  273. (write-string " => " out)
  274. (write-string (if n "#t" "#f") out)
  275. (newline out))))
  276. (define (write-string string . channel-option) ; test n-ary procedures
  277. (write-byte-vector (string->byte-vector string)
  278. (car channel-option)))
  279. (define (write-byte-vector bytes channel)
  280. (channel-maybe-write channel
  281. bytes
  282. 0
  283. (byte-vector-length bytes)))
  284. (define (string->byte-vector string)
  285. ((lambda (size)
  286. ((lambda (bytes)
  287. (letrec ((loop
  288. (lambda (i)
  289. (if (< i size)
  290. (begin
  291. (byte-vector-set! bytes i
  292. (char->scalar-value (string-ref string i)))
  293. (loop (+ 1 i)))))))
  294. (loop 0)
  295. bytes))
  296. (make-byte-vector size 0)))
  297. (string-length string)))
  298. (define (newline channel)
  299. (write-string "
  300. " channel))
  301. (define (read-string in)
  302. ((lambda (buffer)
  303. (letrec ((loop (lambda (have)
  304. ((lambda (got)
  305. (if (eq? got (eof-object))
  306. "eof"
  307. ((lambda (len)
  308. (if len
  309. ((lambda (string)
  310. (copy-bytes-to-string! buffer string len)
  311. string)
  312. (make-string len #\space))
  313. (loop (+ have got))))
  314. (has-newline buffer have got))))
  315. (channel-maybe-read in buffer have (- 80 have) #f)))))
  316. (loop 0)))
  317. (make-byte-vector 80 (char->scalar-value #\space))))
  318. (define (has-newline bytes start count)
  319. (letrec ((loop (lambda (i)
  320. (if (= i count)
  321. #f
  322. (if (char=? #\newline
  323. (scalar-value->char
  324. (byte-vector-ref bytes (+ start i))))
  325. (+ start i)
  326. (loop (+ i 1)))))))
  327. (loop 0)))
  328. (define (copy-bytes-to-string! from to count)
  329. (letrec ((loop (lambda (i)
  330. (if (< i count)
  331. (begin
  332. (string-set! to i
  333. (scalar-value->char (byte-vector-ref from i)))
  334. (loop (+ i 1)))))))
  335. (loop 0)))