lib-test.scm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. ;; -*- coding: utf-8 -*-
  2. (test-begin "libs" 278)
  3. (test-begin "vectors")
  4. (test-equal '(dah dah didah)
  5. (vector->list '#(dah dah didah)))
  6. (test-equal '(dah)
  7. (vector->list '#(dah dah didah) 1 2))
  8. (test-equal #(dididit dah)
  9. (list->vector '(dididit dah)))
  10. (test-equal #(#\A #\B #\C)
  11. (string->vector "ABC"))
  12. (test-equal #(#\B #\C)
  13. (string->vector "ABCDE" 1 3))
  14. (test-equal "123"
  15. (vector->string #(#\1 #\2 #\3)))
  16. (test-equal "345"
  17. (vector->string #(#\1 #\2 #\3 #\4 #\5) 2))
  18. (test-equal "34"
  19. (vector->string #(#\1 #\2 #\3 #\4 #\5) 2 4))
  20. (let* ((a #(1 8 2 8))
  21. (b (vector-copy a)))
  22. (vector-set! b 0 3)
  23. (test-equal #(3 8 2 8) b)
  24. (test-equal #(8 2) (vector-copy b 1 3)))
  25. (test-equal #(a b c d e f)
  26. (vector-append #(a b c) #(d e f)))
  27. (let* ((a (vector 1 2 3 4 5))
  28. (b (vector 10 20 30 40 50)))
  29. (vector-copy! b 1 a 0 2)
  30. (test-equal #(10 1 2 40 50) b))
  31. (test-equal #(1 2 smash smash 5)
  32. (let ()
  33. (define a (vector 1 2 3 4 5))
  34. (vector-fill! a 'smash 2 4)
  35. a))
  36. (test-end)
  37. (test-begin "bytevectors") ;; Some bytevector tests
  38. (define bytes1 (bytevector #xCE #xBB))
  39. (define lambda-char #\x3bb)
  40. (define lambda-string (string lambda-char))
  41. (test-equal #t (bytevector? bytes1))
  42. (test-equal 2 (bytevector-length bytes1))
  43. (test-equal 187 (bytevector-u8-ref bytes1 1))
  44. (test-equal #f (bytevector? lambda-string))
  45. (let ((bv (bytevector 1 2 3 4)))
  46. (bytevector-u8-set! bv 1 3)
  47. (test-equal #u8(1 3 3 4) bv))
  48. (let ((a #u8(1 2 3 4 5)))
  49. (test-equal #u8(3 4) (bytevector-copy a 2 4)))
  50. (let ((a (bytevector 1 2 3 4 5))
  51. (b (bytevector 10 20 30 40 50)))
  52. (bytevector-copy! b 1 a 0 2)
  53. (test-equal #u8(10 1 2 40 50) b))
  54. (test-equal #u8(0 1 2 3 4 5)
  55. (bytevector-append #u8(0 1 2) #u8(3 4 5)))
  56. (test-equal "A" (utf8->string #u8(#x41)))
  57. (test-equal #u8(#xCE #xBB) (string->utf8 "λ"))
  58. (test-equal bytes1 (string->utf8 lambda-string))
  59. (test-equal lambda-string (utf8->string bytes1))
  60. (! hellox-str "Hæll◉ 😂!")
  61. (! hellox-utf8 #u8(#x48 #xc3 #xa6 #x6c #x6c #xe2 #x97 #x89
  62. #x20 #xf0 #x9f #x98 #x82 #x21))
  63. (test-equal hellox-utf8 (string->utf8 hellox-str))
  64. (test-equal hellox-str (utf8->string hellox-utf8))
  65. (test-end)
  66. (import (srfi :2 and-let*))
  67. (test-equal 1 (and-let* () 1))
  68. (test-equal 2 (and-let* () 1 2))
  69. (test-equal #t (and-let* ()))
  70. (test-equal #f (let ((x #f)) (and-let* (x))))
  71. (test-equal 1 (let ((x 1)) (and-let* (x))))
  72. (test-equal #f (and-let* ((x #f)) ))
  73. (test-equal 1 (and-let* ((x 1)) ))
  74. (test-error (eval '(and-let* ( #f (x 1)))))
  75. (test-equal #f (and-let* ( (#f) (x 1)) ))
  76. (test-error (eval '(and-let* (2 (x 1)))))
  77. (test-equal 1 (and-let* ( (2) (x 1)) ))
  78. (test-equal 2 (and-let* ( (x 1) (2)) ))
  79. (test-equal #f (let ((x #f)) (and-let* (x) x)))
  80. (test-equal "" (let ((x "")) (and-let* (x) x)))
  81. (test-equal "" (let ((x "")) (and-let* (x) )))
  82. (test-equal 2 (let ((x 1)) (and-let* (x) (+ x 1))))
  83. (define xf #f)
  84. (test-equal #f (and-let* (xf) (+ (dynamic xf) 1)))
  85. (test-equal 2 (let ((x 1)) (and-let* (((positive? x))) (+ x 1))))
  86. (test-equal #t (let ((x 1)) (and-let* (((positive? x))) )))
  87. (test-equal #f (let ((x 0)) (and-let* (((positive? x))) (+ x 1))))
  88. (test-equal 3 (let ((x 1)) (and-let* (((positive? x)) (x (+ x 1))) (+ x 1))) )
  89. ;(must-be-a-syntax-error
  90. ; (let ((x 1)) (and-let* (((positive? x)) (x (+ x 1)) (x (+ x 1))) (+ x 1)))
  91. ;)
  92. (test-equal 2 (let ((x 1)) (and-let* (x ((positive? x))) (+ x 1))))
  93. (test-equal 2 (let ((x 1)) (and-let* ( ((begin x)) ((positive? x))) (+ x 1))))
  94. (test-equal #f (let ((x 0)) (and-let* (x ((positive? x))) (+ x 1))))
  95. (test-equal #f (and-let* (xf ((positive? xf))) (+ (dynamic xf) 1)))
  96. (test-equal #f (and-let* (((begin xf)) ((positive? xf))) (+ (dynamic xf) 1)))
  97. (test-equal #f (let ((x 1)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))))
  98. (test-equal #f (let ((x 0)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))))
  99. (test-equal #f (and-let* (xf (y (- (dynamic xf) 1)) ((positive? y))) (/ xf y)))
  100. (test-equal 3/2 (let ((x 3)) (and-let* (x (y (- x 1)) ((positive? y))) (/ x y))))
  101. (define (symbol-parts s::symbol)
  102. (list (symbol-local-name s) (symbol-namespace-uri s) (symbol-prefix s)))
  103. (test-equal '("loc1" "uri1" "pre")
  104. (symbol-parts (symbol "loc1" "uri1" "pre")))
  105. (test-equal '("loc2" "uri2" "pre")
  106. (symbol-parts (symbol "loc2" "uri2" "pre")))
  107. (test-equal '("loc3" "uri3" "pre")
  108. (symbol-parts (apply symbol (list "loc3" "uri3" "pre"))))
  109. (test-equal '("loc4" "uri4" "")
  110. (symbol-parts (symbol "loc4" "uri4")))
  111. (test-equal '("loc5" "uri5" "pre")
  112. (symbol-parts (symbol "loc5" (namespace "uri5" "pre"))))
  113. (test-equal '("abc:def" "" "")
  114. (symbol-parts '|abc:def|))
  115. (test-equal '("def" "" "abc")
  116. (symbol-parts 'abc:def))
  117. (require 'xml)
  118. (test-equal '("abc" "URI" "")
  119. (symbol-parts (element-name #<abc xmlns="URI"/>)))
  120. ;; Contributed by Helmut Eller.
  121. (define (test-ev-req)
  122. (let* ((file (java.io.File:createTempFile "foo" ".scm"))
  123. (filename (file:getAbsolutePath))
  124. (now (lambda () (java.lang.System:currentTimeMillis)))
  125. (cache-time (max gnu.expr.ModuleManager:LAST_MODIFIED_CACHE_TIME
  126. 1000))
  127. (wait (lambda () (let* ((date (file:lastModified)))
  128. (let loop ()
  129. (when (< (- (now) date) (* 2 cache-time))
  130. (sleep 0.5))))))
  131. (make-form (lambda (bar-body)
  132. &{
  133. &|(module-export foo)
  134. &|(module-static #t)
  135. &|(module-compile-options
  136. &| warn-invoke-unknown-method: #t
  137. &| warn-undefined-variable: #t)
  138. &|(define (foo) (bar))
  139. &|(define (bar) &[bar-body])
  140. }))
  141. (write-forms (lambda (bar-body)
  142. (wait)
  143. (call-with-output-file filename
  144. (lambda (stream)
  145. (write-string (make-form bar-body) stream)))
  146. (wait))))
  147. (try-finally
  148. (begin
  149. (write-forms &{"version 1"})
  150. (eval `(begin (require ,filename)
  151. (define foo-1 foo)
  152. (define result-1 (foo-1)))
  153. (interaction-environment))
  154. (write-forms &{"version 2"})
  155. (eval `(begin (require ,filename)
  156. (define result-2 (foo-1))
  157. (list result-1 result-2))
  158. (interaction-environment)))
  159. (delete-file filename))))
  160. (test-equal
  161. '("version 1" "version 2")
  162. (test-ev-req))
  163. (require 'syntax-utils)
  164. (test-equal 'x (expand 'x))
  165. (test-equal 1 (expand 1))
  166. (test-equal '(let ((x 10)) x) (expand '(let ((x 10)) x)))
  167. (test-equal '(lambda (x) x) (expand '(lambda (x) x)))
  168. (test-equal '(if x 'a 'b) (expand '(if x 'a 'b)))
  169. (test-equal '(set x 10) (expand '(set! x 10)))
  170. (test-equal '(begin (x) (y)) (expand '(begin (x) (y))))
  171. (test-equal "foo" (expand "foo"))
  172. (test-equal '(quote (a b c)) (expand ''(a b c)))
  173. (test-equal #f (expand '#f))
  174. (test-equal #t (expand '#t))
  175. (test-equal '(if (= x 1) (quote a) (if (= x 2) (quote b)))
  176. (expand '(cond ((= x 1) 'a)
  177. ((= x 2) 'b))))
  178. (test-equal '((let ((loop #!undefined))
  179. (begin (set loop (lambda () (loop))) loop)))
  180. (expand '(let loop () (loop))))
  181. (test-equal '(let ((x #!undefined)) (set x 10))
  182. (expand '(define x 10)))
  183. (test-equal '(as <String> (quote a))
  184. (expand '(as String 'a)))
  185. (test-equal '(lambda (a b c) #f)
  186. (expand '(lambda (a b c) #f)))
  187. (test-equal '(lambda (#!rest r) #f)
  188. (expand '(lambda r #f)))
  189. (test-equal '(lambda (#!rest r) #f)
  190. (expand '(lambda (#!rest r) #f)))
  191. (test-equal '(lambda (a b c #!rest r) #f)
  192. (expand '(lambda (a b c #!rest r) #f)))
  193. (test-equal '(lambda (#!optional d e f) #f)
  194. (expand '(lambda (#!optional d e f) #f)))
  195. (test-equal '(lambda (a b c #!optional d e f) #f)
  196. (expand '(lambda (a b c #!optional d e f) #f)))
  197. (test-equal '(lambda (a b c #!optional d e f #!rest r) #f)
  198. (expand '(lambda (a b c #!optional d e f #!rest r) #f)))
  199. (test-equal '(lambda (a b c #!rest r #!key d e f ) #f)
  200. (expand '(lambda (a b c #!rest r #!key d e f ) #f)))
  201. (test-equal '(lambda (a b c #!optional d e f #!rest r #!key g i j ) #f)
  202. (expand '(lambda (a b c #!optional d e f #!rest r #!key g i j)
  203. #f)))
  204. (import (srfi :41 streams))
  205. (define strm123
  206. (stream-cons 1
  207. (stream-cons 2
  208. (stream-cons 3
  209. stream-null))))
  210. (test-equal 1 (stream-car strm123))
  211. (test-equal 2 (stream-car (stream-cdr strm123)))
  212. (test-equal #f
  213. (stream-pair?
  214. (stream-cdr
  215. (stream-cons (/ 1 0) stream-null))))
  216. (test-equal #f (stream? (list 1 2 3)))
  217. (test-equal 3 (stream-length strm123))
  218. (define iter
  219. (stream-lambda (f x)
  220. (stream-cons x (iter f (f x)))))
  221. (define nats (iter (lambda (x) (+ x 1)) 0))
  222. (test-equal 1 (stream-car (stream-cdr nats)))
  223. (define stream-add
  224. (stream-lambda (s1 s2)
  225. (stream-cons
  226. (+ (stream-car s1) (stream-car s2))
  227. (stream-add (stream-cdr s1)
  228. (stream-cdr s2)))))
  229. (define evens (stream-add nats nats))
  230. (test-equal 0 (stream-car evens))
  231. (test-equal 2 (stream-car (stream-cdr evens)))
  232. (test-equal 4 (stream-car (stream-cdr (stream-cdr evens))))
  233. (define (square x) (* x x))
  234. (test-equal '(81 9) (stream->list (stream-map square (stream 9 3))))
  235. (define (sigma f m n)
  236. (stream-fold + 0
  237. (stream-map f (stream-range m (+ n 1)))))
  238. (test-equal 338350 (sigma square 1 100))
  239. (test-equal '(1 2 3 2 1)
  240. (stream->list
  241. (stream-concat
  242. (stream
  243. (stream 1 2) (stream) (stream 3 2 1)))))
  244. (test-equal
  245. '(0 1 4 9 16 25 36 49 64 81)
  246. (stream->list 10
  247. (stream-map (lambda (x) (* x x))
  248. (stream-from 0))))
  249. (test-equal '(3 4 3 4 3 4 3)
  250. (stream->list 7
  251. (stream-constant 3 4)))
  252. (test-equal '(1 3 5 7 9)
  253. (stream->list 5
  254. (stream-filter odd? (stream-from 0))))
  255. (test-equal '(0 4 16 36 64)
  256. (stream->list
  257. (stream-of (* x x)
  258. (x in (stream-range 0 10))
  259. (even? x))))
  260. (test-equal '((1 1) (1 2) (2 1) (2 2) (3 1) (3 2))
  261. (stream->list
  262. (stream-of (list a b)
  263. (a in (stream-range 1 4))
  264. (b in (stream-range 1 3)))))
  265. (test-equal '((1 2) (1 3) (1 4) (2 3) (2 4) (3 4))
  266. (stream->list
  267. (stream-of (list i j)
  268. (i in (stream-range 1 5))
  269. (j in (stream-range (+ i 1) 5)))))
  270. (define (stream-partition pred? strm)
  271. (stream-unfolds
  272. (lambda (s)
  273. (if (stream-null? s)
  274. (values s '() '())
  275. (let ((a (stream-car s))
  276. (d (stream-cdr s)))
  277. (if (pred? a)
  278. (values d (list a) #f)
  279. (values d #f (list a))))))
  280. strm))
  281. (test-equal '((1 3 5) (2 4))
  282. (call-with-values
  283. (lambda ()
  284. (stream-partition odd?
  285. (stream-range 1 6)))
  286. (lambda (odds evens)
  287. (list (stream->list odds)
  288. (stream->list evens)))))
  289. (define primes (let ()
  290. (define-stream (next base mult strm)
  291. (let ((first (stream-car strm))
  292. (rest (stream-cdr strm)))
  293. (cond ((< first mult)
  294. (stream-cons first
  295. (next base mult rest)))
  296. ((< mult first)
  297. (next base (+ base mult) strm))
  298. (else (next base
  299. (+ base mult) rest)))))
  300. (define-stream (sift base strm)
  301. (next base (+ base base) strm))
  302. (define-stream (sieve strm)
  303. (let ((first (stream-car strm))
  304. (rest (stream-cdr strm)))
  305. (stream-cons first
  306. (sieve (sift first rest)))))
  307. (sieve (stream-from 2))))
  308. (test-equal 997
  309. (stream-car
  310. (stream-reverse
  311. (stream-take-while
  312. (lambda (x) (< x 1000))
  313. primes))))
  314. (define-stream (stream-finds eql? obj strm)
  315. (stream-of (car x)
  316. (x in (stream-zip (stream-from 0) strm))
  317. (eql? obj (cadr x))))
  318. (define (stream-find eql? obj strm)
  319. (stream-car
  320. (stream-append
  321. (stream-finds eql? obj strm)
  322. (stream #f))))
  323. (test-equal 2
  324. (stream-find char=? #\l
  325. (list->stream
  326. (string->list "hello"))))
  327. (test-equal #f
  328. (stream-find char=? #\l
  329. (list->stream
  330. (string->list "goodbye"))))
  331. (define power-table
  332. (stream-of
  333. (stream-of (expt m n)
  334. (m in (stream-from 1)))
  335. (n in (stream-from 2))))
  336. (test-equal '(1 8 27 64 125 216 343 512 729 1000)
  337. (stream->list 10 (stream-ref power-table 1)))
  338. (test-equal '(0 1 2 3 4)
  339. (stream-take 5 (stream-iterate (lambda (x) (+ x 1)) 0)))
  340. (test-equal '(1 2 4 8 16)
  341. (stream-take 5 (stream-iterate (lambda (x) (* x 2)) 1)))
  342. (test-equal
  343. '(1 2 3/2 5/3 8/5 13/8 21/13 34/21)
  344. (stream-take 8 (stream-iterate (lambda (x) (+ 1 (/ x))) 1)))
  345. (test-equal '(0 1 3 6 10 15 )
  346. (stream-take 6 (stream-scan + 0 (stream-from 1))))
  347. (define-stream (stream-merge lt? . strms)
  348. (define-stream (merge xx yy)
  349. (stream-match xx (() yy) ((x . xs)
  350. (stream-match yy (() xx) ((y . ys)
  351. (if (lt? y x)
  352. (stream-cons y (merge xx ys))
  353. (stream-cons x (merge xs yy))))))))
  354. (stream-let loop ((strms strms))
  355. (cond ((null? strms) stream-null)
  356. ((null? (cdr strms)) (car strms))
  357. (else (merge (car strms)
  358. (apply stream-merge lt?
  359. (cdr strms)))))))
  360. (define-stream (stream-unique eql? strm)
  361. (if (stream-null? strm)
  362. stream-null
  363. (stream-cons (stream-car strm)
  364. (stream-unique eql?
  365. (stream-drop-while
  366. (lambda (x)
  367. (eql? (stream-car strm) x))
  368. strm)))))
  369. (define (lsec proc . args)
  370. (lambda x (apply proc (append args x))))
  371. (define hamming
  372. (stream-cons 1
  373. (stream-unique =
  374. (stream-merge <
  375. (stream-map (lsec * 2) hamming)
  376. (stream-map (lsec * 3) hamming)
  377. (stream-map (lsec * 5) hamming)))))
  378. (test-equal '(1 2 3 4 5 6 8 9 10 12) (stream-take 10 hamming))
  379. (test-begin "rnrs-lists" 50)
  380. (import (rnrs lists))
  381. (test-equal 4 (find even? '(3 1 4 1 5 9)))
  382. (test-equal #f (find even? '(3 1 5 1 5 9)))
  383. (test-equal #f (for-all even? '(3 1 4 1 5 9)))
  384. (test-equal #f (for-all even? '(3 1 4 1 5 9 . 2)))
  385. (test-equal #t (for-all even? '(2 4 14)))
  386. (test-error (for-all even? '(2 4 14 . 9)))
  387. (test-equal 14 (for-all (lambda (n) (and (even? n) n)) '(2 4 14)))
  388. (test-equal #t (for-all < '(1 2 3) '(2 3 4)))
  389. (test-equal #f (for-all < '(1 2 4) '(2 3 4)))
  390. (test-equal #t (exists even? '(3 1 4 1 5 9)))
  391. (test-equal #f (exists even? '(3 1 1 5 9)))
  392. (test-error (exists even? '(3 1 1 5 9 . 2)))
  393. (test-equal 2 (exists (lambda (n) (and (even? n) n)) '(2 1 4 14)))
  394. (test-equal #t (exists < '(1 2 4) '(2 3 4)))
  395. (test-equal #f (exists > '(1 2 3) '(2 3 4)))
  396. (test-equal '(4 2 6) (filter even? '(3 1 4 1 5 9 2 6)))
  397. (test-equal '((4 2 6) (3 1 1 5 9))
  398. (call-with-values
  399. (lambda ()
  400. (partition even? '(3 1 4 1 5 9 2 6)))
  401. (lambda (evens odds)
  402. (list evens odds))))
  403. (test-equal 15 (fold-left + 0 '(1 2 3 4 5)))
  404. (test-equal '(5 4 3 2 1) (fold-left (lambda (a e) (cons e a)) '()
  405. '(1 2 3 4 5)))
  406. (test-equal 7 (fold-left (lambda (count x)
  407. (if (odd? x) (+ count 1) count))
  408. 0 '(3 1 4 1 5 9 2 6 5 3)))
  409. (test-equal 7 (fold-left (lambda (max-len s)
  410. (max max-len (string-length s)))
  411. 0 '("longest" "long" "longer")))
  412. (test-equal '((((q) . a) . b) . c) (fold-left cons '(q) '(a b c)))
  413. (test-equal 21 (fold-left + 0 '(1 2 3) '(4 5 6)))
  414. (test-equal 15 (fold-right + 0 '(1 2 3 4 5)))
  415. (test-equal '(1 2 3 4 5) (fold-right cons '() '(1 2 3 4 5)))
  416. (test-equal '(3 1 1 5 9 5) (fold-right (lambda (x l)
  417. (if (odd? x) (cons x l) l))
  418. '() '(3 1 4 1 5 9 2 6 5)))
  419. (test-equal '(a b c q) (fold-right cons '(q) '(a b c)))
  420. (test-equal 21 (fold-right + 0 '(1 2 3) '(4 5 6)))
  421. (test-equal '(3 1 1 5 9 5) (remp even? '(3 1 4 1 5 9 2 6 5)))
  422. (test-equal '(3 4 5 9 2 6 5) (remove 1 '(3 1 4 1 5 9 2 6 5)))
  423. (test-equal '(3 4 5 9 2 6 5) (remv 1 '(3 1 4 1 5 9 2 6 5)))
  424. (test-equal '(bar baz) (remq 'foo '(bar foo baz)))
  425. (test-equal '(4 1 5 9 2 6 5) (memp even? '(3 1 4 1 5 9 2 6 5)))
  426. (test-equal '(a b c) (memq 'a '(a b c)))
  427. (test-equal '(b c) (memq 'b '(a b c)))
  428. (test-equal #f (memq 'a '(b c d)))
  429. (test-equal #f (memq (list 'a) '(b (a) c)))
  430. (test-equal '((a) c) (member (list 'a) '(b (a) c)))
  431. ;; (test-equal '(101 102) (memq 101 '(100 101 102))) ; result unspecified
  432. (test-equal '(101 102) (memv 101 '(100 101 102)))
  433. (define d '((3 a) (1 b) (4 c)))
  434. (test-equal '(4 c) (assp even? d))
  435. (test-equal '(3 a) (assp odd? d))
  436. (define e '((a 1) (b 2) (c 3)))
  437. (test-equal '(a 1) (assq 'a e))
  438. (test-equal '(b 2) (assq 'b e))
  439. (test-equal #f (assq 'd e))
  440. (test-equal #f (assq (list 'a) '(((a)) ((b)) ((c)))))
  441. (test-equal '((a)) (assoc (list 'a) '(((a)) ((b)) ((c)))))
  442. ;; (test-equal '(5 7) (assq 5 '((2 3) (5 7) (11 13)))) ; result unspecified
  443. (test-equal '(5 7) (assv 5 '((2 3) (5 7) (11 13))))
  444. (test-equal '(1 2 3 4 5) (cons* 1 2 '(3 4 5)))
  445. (test-equal '(1 2 . 3) (cons* 1 2 3))
  446. (test-equal 1 (cons* 1))
  447. (test-end "rnrs-lists")
  448. (test-begin "arglists")
  449. (import (kawa arglist))
  450. (let ()
  451. (! a1 [10 11 12])
  452. (test-equal 3 (arglist-arg-count a1))
  453. (! a2 (arglist 2 k1: "K1" k2: "K2" @a1))
  454. (test-equal 6 (arglist-arg-count a2))
  455. (test-equal "k2" (arglist-key-ref a2 2))
  456. (test-equal "K2" (arglist-arg-ref a2 2))
  457. (test-equal 11 (arglist-arg-ref a2 4))
  458. (test-equal 2 (arglist-key-index a2 "k2"))
  459. (test-equal "K2" (arglist-key-value a2 "k2" "none"))
  460. (test-equal -1 (arglist-key-index a2 "k3"))
  461. (test-equal "none" (arglist-key-value a2 "k3" "none"))
  462. (let ((out (open-output-string)))
  463. (arglist-walk a2
  464. (lambda (key arg)
  465. (if key (format out "{~a: ~w}" key arg)
  466. (format out "{~w}" arg))))
  467. (test-equal "{2}{k1: \"K1\"}{k2: \"K2\"}{10}{11}{12}"
  468. (get-output-string out)))
  469. )
  470. (test-end "arglists")
  471. (test-begin "strings")
  472. (import (kawa string-cursors))
  473. (define str1 "a😂b😼c")
  474. (define sc1e::string-cursor (string-cursor-end str1))
  475. (define str1lst '())
  476. (do ((sc::string-cursor (string-cursor-start str1)
  477. (string-cursor-next str1 sc)))
  478. ((string-cursor>=? sc sc1e))
  479. (set! str1lst (cons (as int (string-cursor-ref str1 sc)) str1lst)))
  480. (test-equal '(97 128514 98 128572 99) (reverse str1lst))
  481. (set! str1lst '())
  482. (do ((sc::string-cursor (string-cursor-start str1)
  483. (string-cursor-next-quick sc)))
  484. ((string-cursor>=? sc sc1e))
  485. (let* ((ch (string-cursor-ref str1 sc))
  486. (r (if (char=? ch #\ignorable-char) 'ignorable (string ch))))
  487. (set! str1lst (cons r str1lst))))
  488. (test-equal '("a" "😂" ignorable "b" "😼" ignorable "c")
  489. (reverse str1lst))
  490. (define str2lst '())
  491. (string-cursor-for-each
  492. (lambda (x) (set! str2lst (cons (char->integer x) str2lst)))
  493. str1 (as string-cursor 3))
  494. (test-equal '(98 128572 99) (reverse str2lst))
  495. ;; FIXME more
  496. (test-end)
  497. (test-begin "char-sets" 91)
  498. (import (srfi :14 char-sets))
  499. (import (rnrs sorting))
  500. ; char-set=
  501. (test-equal #t (char-set=))
  502. (test-equal #t (char-set= char-set:full))
  503. (test-equal #t (char-set= char-set:full char-set:full))
  504. (test-equal #f (char-set= char-set:empty char-set:full))
  505. (test-equal #t (char-set= char-set:empty (char-set)))
  506. ; char-set<=
  507. (test-equal #t (char-set<=))
  508. (test-equal #t (char-set<= char-set:empty))
  509. (test-equal #t (char-set<= char-set:empty char-set:full))
  510. (test-equal #t (char-set<= char-set:empty char-set:lower-case
  511. char-set:full))
  512. (test-equal #t (char-set<= (char-set #\u) (char-set #\u)))
  513. (test-equal #t (char-set<= (char-set #\u) (char-set #\u #\a)))
  514. ; char-set-hash
  515. (test-equal #t (= (char-set-hash char-set:empty)
  516. (char-set-hash (char-set))))
  517. (test-equal #t (<= 0 (char-set-hash char-set:lower-case 50) 49))
  518. ; char-set-cursor, char-set-ref, char-set-cursor-next,
  519. ; end-of-char-set?
  520. (define cs (char-set #\H #\e #\l #\l #\o #\, #\W #\o #\r #\l #\d))
  521. (test-equal '(#\, #\H #\W #\d #\e #\l #\o #\r)
  522. (list-sort char<?
  523. (let lp ((cur (char-set-cursor cs)) (ans '()))
  524. (if (end-of-char-set? cur) ans
  525. (lp (char-set-cursor-next cs cur)
  526. (cons (char-set-ref cs cur) ans))))))
  527. (test-equal #t (end-of-char-set? (char-set-cursor char-set:empty)))
  528. ; char-set-fold
  529. (test-equal '(#\, #\H #\W #\d #\e #\l #\o #\r)
  530. (list-sort char<? (char-set-fold cons '() cs)))
  531. (test-equal 0 (char-set-fold (lambda (c i) (+ i 1)) 0 char-set:empty))
  532. (test-equal 128 (char-set-fold (lambda (c i) (+ i 1)) 0 char-set:ascii))
  533. ; char-set-unfold, char-set-unfold!
  534. (define abc (char-set #\a #\b #\c))
  535. (test-equal #t (char-set= abc (char-set-unfold
  536. null? car cdr '(#\a #\b #\c))))
  537. (test-equal #t (char-set= abc (char-set-unfold
  538. null? car cdr '(#\a #\c)
  539. (char-set #\b))))
  540. (test-equal #t (char-set= abc (char-set-unfold!
  541. null? car cdr '(#\a #\c)
  542. (char-set #\b))))
  543. ; also testing the definition of char-set:full
  544. (test-equal #t (char-set= char-set:full
  545. (char-set-unfold
  546. (lambda (i) (> i #x10FFFF))
  547. integer->char
  548. (lambda (i) (+ i 1))
  549. 0)))
  550. ; char-set-for-each is only useful for side-effects, so no test
  551. ; provided
  552. ; char-set-map
  553. (test-equal #t (char-set= abc (char-set-map char-downcase
  554. (char-set #\A #\B #\C))))
  555. ; char-set-copy
  556. (test-equal #t (equal? char-set:empty (char-set-copy char-set:empty)))
  557. (test-equal '(#\, #\H #\W #\d #\e #\l #\o #\r)
  558. (list-sort char<? (char-set-fold cons '()
  559. (char-set-copy cs))))
  560. ; list->char-set, list->char-set!
  561. (test-equal #t (char-set= abc (list->char-set '(#\a #\b #\c))))
  562. (test-equal #t (char-set= abc (list->char-set '(#\a)
  563. (char-set #\b #\c))))
  564. (test-equal #t (char-set= abc (list->char-set! '(#\a)
  565. (char-set #\b #\c))))
  566. ; string->char-set, string->char-set!
  567. (test-equal #t (char-set= abc (string->char-set "abc")))
  568. (test-equal #t (char-set= abc (string->char-set "ab"
  569. (char-set #\c))))
  570. (test-equal #t (char-set= abc (string->char-set! "ab"
  571. (char-set #\c))))
  572. ; char-set-filter and the meanings of some standard character sets
  573. (test-equal #t (char-set=
  574. char-set:empty
  575. (char-set-filter (lambda (c) #f) char-set:full)))
  576. (test-equal #t (char-set=
  577. char-set:ascii
  578. (char-set-filter
  579. (lambda (c) (> 128 (char->integer c)))
  580. char-set:full)))
  581. (test-equal #t (char-set=
  582. char-set:iso-control
  583. (char-set-filter
  584. (lambda (c) (java.lang.Character:isISOControl
  585. (char->integer c))) char-set:full)))
  586. (test-equal #t (char-set=
  587. char-set:title-case
  588. (char-set-filter
  589. (lambda (c) (java.lang.Character:title-case?
  590. (char->integer c))) char-set:full)))
  591. ;; Some of these tests only succeed on Java 9 (or later), which
  592. ;; supports Unicode 8.0. On earlier Javas, the java.lang.Character
  593. ;; predicates will disagree with the char-set definitions.
  594. (define-syntax expect-fail-unless-unicode-8
  595. (syntax-rules ()
  596. ((_ count)
  597. (cond-expand (java-9)
  598. (else (test-expect-fail count))))))
  599. (define-syntax expect-fail-unless
  600. (syntax-rules ()
  601. ((_ version count)
  602. (cond-expand (version)
  603. (else (test-expect-fail count))))))
  604. (expect-fail-unless java-9 6)
  605. (test-equal #t (char-set= ; only on Java 9
  606. char-set:lower-case
  607. (char-set-filter
  608. (lambda (c) (java.lang.Character:lower-case?
  609. (char->integer c))) char-set:full)))
  610. (test-equal #t (char-set= ; only on Java 9
  611. char-set:upper-case
  612. (char-set-filter
  613. (lambda (c) (java.lang.Character:upper-case?
  614. (char->integer c))) char-set:full)))
  615. (test-equal #t (char-set= ; only on Java 9
  616. char-set:letter
  617. (char-set-filter
  618. (lambda (c) (java.lang.Character:letter?
  619. (char->integer c))) char-set:full)))
  620. (test-equal #t (char-set= ; only on Java 9
  621. char-set:digit
  622. (char-set-filter
  623. (lambda (c) (java.lang.Character:digit?
  624. (char->integer c))) char-set:full)))
  625. (test-equal ; only on Java 8
  626. #t (char-set=
  627. char-set:punctuation
  628. (char-set-filter
  629. (lambda (c)
  630. (let ((type ::byte (java.lang.Character:get-type
  631. (char->integer c))))
  632. (or (= type java.lang.Character:CONNECTOR_PUNCTUATION)
  633. (= type java.lang.Character:DASH_PUNCTUATION)
  634. (= type java.lang.Character:START_PUNCTUATION)
  635. (= type java.lang.Character:END_PUNCTUATION)
  636. (= type java.lang.Character:INITIAL_QUOTE_PUNCTUATION)
  637. (= type java.lang.Character:FINAL_QUOTE_PUNCTUATION)
  638. (= type java.lang.Character:OTHER_PUNCTUATION))))
  639. char-set:full)))
  640. (test-equal ; only on Java 9
  641. #t (char-set=
  642. char-set:symbol
  643. (char-set-filter
  644. (lambda (c)
  645. (let ((type ::byte (java.lang.Character:get-type
  646. (char->integer c))))
  647. (or (= type java.lang.Character:MATH_SYMBOL)
  648. (= type java.lang.Character:CURRENCY_SYMBOL)
  649. (= type java.lang.Character:MODIFIER_SYMBOL)
  650. (= type java.lang.Character:OTHER_SYMBOL))))
  651. char-set:full)))
  652. (expect-fail-unless java-9 2)
  653. (test-equal
  654. #t (char-set=
  655. char-set:whitespace
  656. (char-set-filter
  657. (lambda (c)
  658. (or (char=? c #\u0009)
  659. (char=? c #\u000a)
  660. (char=? c #\u000b)
  661. (char=? c #\u000c)
  662. (char=? c #\u000d)
  663. (let ((type ::byte (java.lang.Character:get-type
  664. (char->integer c))))
  665. (or (= type java.lang.Character:SPACE_SEPARATOR)
  666. (= type java.lang.Character:LINE_SEPARATOR)
  667. (= type java.lang.Character:PARAGRAPH_SEPARATOR)))))
  668. char-set:full)))
  669. (test-equal
  670. #t (char-set=
  671. char-set:blank
  672. (char-set-filter
  673. (lambda (c)
  674. (or (char=? c #\u0009)
  675. (let ((type ::byte (java.lang.Character:get-type
  676. (char->integer c))))
  677. (= type java.lang.Character:SPACE_SEPARATOR))))
  678. char-set:full)))
  679. ; char-set-filter!
  680. (test-equal #t (char-set= (char-set #\a #\b #\c)
  681. (char-set-filter!
  682. char-lower-case?
  683. (char-set #\b #\c #\D #\E #\F)
  684. (char-set #\a))))
  685. ; ucs-range->char-set, ucs-range->char-set!
  686. (test-equal #t (char-set= char-set:ascii (ucs-range->char-set 0 128)))
  687. (test-equal
  688. #t (char-set= char-set:full
  689. (ucs-range->char-set! 100 (+ 1 #x10FFFF) #f
  690. (ucs-range->char-set 0 100))))
  691. ; ->char-set
  692. (define csa ::char-set (char-set #\a))
  693. (test-equal #t (char-set= csa (->char-set (char-set #\a))))
  694. (test-equal #t (char-set= csa (->char-set #\a)))
  695. (test-equal #t (char-set= csa (->char-set "a")))
  696. ; char-set->list, char-set->string -- order is not guaranteed
  697. (test-equal '(#\a #\b #\c) (list-sort char<? (char-set->list abc)))
  698. (test-equal "a" (char-set->string csa))
  699. (test-equal #t (let ((s ::String (char-set->string abc)))
  700. (or (string=? s "abc") (string=? s "acb")
  701. (string=? s "bac") (string=? s "bca")
  702. (string=? s "cab") (string=? s "cba"))))
  703. ; char-set-size, char-set-count
  704. (test-equal 1 (char-set-size csa))
  705. (test-equal 0 (char-set-size char-set:empty))
  706. (test-equal 3 (char-set-size abc))
  707. (test-equal 2 (char-set-size (char-set #\A #\z)))
  708. (test-equal (char-set-size char-set:letter)
  709. (char-set-count (lambda (c) #t) char-set:letter))
  710. (test-equal 1 (char-set-count char-lower-case? (char-set #\A #\z)))
  711. ; char-set-contains?, char-set-every, char-set-any
  712. (test-equal #f (char-set-contains? char-set:upper-case #\a))
  713. (test-equal #t (char-set-contains?
  714. char-set:full
  715. (integer->char (remainder
  716. (abs ((java.util.Random):nextInt))
  717. (+ 1 #x10FFFF)))))
  718. (test-equal #f (char-set-any char-upper-case? char-set:lower-case))
  719. (expect-fail-unless java-9 1)
  720. (test-equal #t (char-set-every char-upper-case? char-set:upper-case))
  721. ; char-set-adjoin, char-set-adjoin!
  722. (test-equal #t (char-set= abc
  723. (char-set-adjoin (char-set #\a) #\b #\c)))
  724. (test-equal #t (char-set= abc
  725. (char-set-adjoin! (char-set #\a) #\b #\c)))
  726. ; char-set-delete, char-set-delete!
  727. (test-equal #t (char-set=
  728. abc
  729. (char-set-delete (string->char-set "fdbaec")
  730. #\d #\e #\f)))
  731. ; char-set-complement, char-set-complement!
  732. (test-equal #t (char-set= char-set:full
  733. (char-set-complement char-set:empty)))
  734. (test-equal #t (char-set= char-set:empty
  735. (char-set-complement! (char-set-complement
  736. char-set:empty))))
  737. ; char-set-union, char-set-union!, meanings of standard derived sets
  738. (test-equal #t (char-set= char-set:empty (char-set-union)))
  739. (test-equal #t (char-set= abc (char-set-union abc)))
  740. (test-equal #t (char-set= abc (char-set-union abc abc)))
  741. (test-equal #t (char-set= char-set:letter+digit
  742. (char-set-union char-set:letter
  743. char-set:digit)))
  744. (test-equal #t (char-set= char-set:graphic
  745. (char-set-union char-set:letter+digit
  746. char-set:punctuation
  747. char-set:symbol)))
  748. (test-equal #t (char-set= char-set:printing
  749. (char-set-union char-set:graphic
  750. char-set:whitespace)))
  751. (test-equal #t (char-set= abc (char-set-union! (char-set #\a)
  752. (char-set #\b)
  753. (char-set #\c))))
  754. (test-equal
  755. #t
  756. (char-set= char-set:full
  757. (char-set-union char-set:letter
  758. (char-set-complement char-set:letter))))
  759. ; char-set-intersection, char-set-intersection!
  760. (test-equal #t (char-set= char-set:full (char-set-intersection)))
  761. (test-equal #t (char-set= char-set:graphic
  762. (char-set-intersection char-set:graphic)))
  763. (test-equal
  764. #t
  765. (char-set= char-set:empty
  766. (char-set-intersection abc (char-set-complement abc))))
  767. (test-equal
  768. #t
  769. (char-set= (string->char-set "aeiou")
  770. (char-set-intersection
  771. char-set:lower-case
  772. (string->char-set "abcdefghijklmnopqrstuvwxyz")
  773. (string->char-set
  774. "oNLY VoWeLS aRe LoWeR CaSe iN THiS, You See?"))))
  775. (test-equal
  776. #t
  777. (char-set= (string->char-set "aeiou")
  778. (char-set-intersection!
  779. (string->char-set "abcdefghijklmnopqrstuvwxyz")
  780. (string->char-set
  781. "oNLY VoWeLS aRe LoWeR CaSe iN THiS, You See?"))))
  782. ; char-set-difference, char-set-difference!
  783. (test-equal
  784. #t
  785. (char-set= abc
  786. (char-set-difference (string->char-set "abcde")
  787. (string->char-set "de"))))
  788. (test-equal
  789. #t
  790. (char-set= abc
  791. (char-set-difference! (string->char-set "abcde")
  792. (string->char-set "de"))))
  793. (test-equal
  794. #t
  795. (char-set= char-set:letter
  796. (char-set-difference char-set:letter+digit
  797. char-set:digit)))
  798. ; char-set-xor, char-set-xor!
  799. (test-equal #t (char-set= char-set:empty (char-set-xor)))
  800. (test-equal
  801. #t (char-set= abc (char-set-xor (char-set #\a)
  802. (char-set #\b)
  803. (char-set #\c))))
  804. (test-equal
  805. #t (char-set= abc (char-set-xor! (char-set #\a)
  806. (char-set #\b)
  807. (char-set #\c))))
  808. (test-equal #t (char-set= abc (char-set-xor abc abc abc)))
  809. (test-equal #t (char-set= (string->char-set "adz")
  810. (char-set-xor! (string->char-set "abcd")
  811. (string->char-set "bc")
  812. (char-set #\z))))
  813. (test-equal #t (char-set= (string->char-set "abde")
  814. (char-set-xor abc
  815. (string->char-set "cde"))))
  816. ; char-set-diff+intersection, char-set-diff+intersection!
  817. (test-equal '((#\a) (#\b #\c))
  818. (call-with-values
  819. (lambda ()
  820. (char-set-diff+intersection
  821. (string->char-set "abc") (string->char-set "bc")))
  822. (lambda (diff intersection)
  823. (list (list-sort char<? (char-set->list diff))
  824. (list-sort char<? (char-set->list
  825. intersection))))))
  826. (test-equal '((#\a) (#\b #\c))
  827. (call-with-values
  828. (lambda ()
  829. (char-set-diff+intersection!
  830. (string->char-set "abc") (string->char-set "bc")))
  831. (lambda (diff intersection)
  832. (list (list-sort char<? (char-set->list diff))
  833. (list-sort char<? (char-set->list
  834. intersection))))))
  835. (test-end)
  836. (import (srfi :13 strings))
  837. (test-equal 15 (string-contains "eek -- what a geek." "ee" 12 18))
  838. ;;; Test SRFI-13 string-append/shared
  839. (let ((str "abc"))
  840. (test-equal "" (string-append/shared))
  841. (test-equal "" (string-append/shared ""))
  842. (test-equal "abc" (string-append/shared str))
  843. (set! str (string-append/shared str "123" "xy"))
  844. (test-equal "abc123xy" (string-append/shared str))
  845. (test-equal "abc123xy" str))
  846. (test-end)