alist.test 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ;;;; alist.test --- tests guile's alists -*- scheme -*-
  2. ;;;; Copyright (C) 1999 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. ;;;; Boston, MA 02111-1307 USA
  18. ;;;;
  19. ;;;; As a special exception, the Free Software Foundation gives permission
  20. ;;;; for additional uses of the text contained in its release of GUILE.
  21. ;;;;
  22. ;;;; The exception is that, if you link the GUILE library with other files
  23. ;;;; to produce an executable, this does not by itself cause the
  24. ;;;; resulting executable to be covered by the GNU General Public License.
  25. ;;;; Your use of that executable is in no way restricted on account of
  26. ;;;; linking the GUILE library code into it.
  27. ;;;;
  28. ;;;; This exception does not however invalidate any other reasons why
  29. ;;;; the executable file might be covered by the GNU General Public License.
  30. ;;;;
  31. ;;;; This exception applies only to the code released by the
  32. ;;;; Free Software Foundation under the name GUILE. If you copy
  33. ;;;; code from other Free Software Foundation releases into a copy of
  34. ;;;; GUILE, as the General Public License permits, the exception does
  35. ;;;; not apply to the code that you add in this way. To avoid misleading
  36. ;;;; anyone as to the status of such modified files, you must delete
  37. ;;;; this exception notice from them.
  38. ;;;;
  39. ;;;; If you write modifications of your own for GUILE, it is your choice
  40. ;;;; whether to permit this exception to apply to your modifications.
  41. ;;;; If you do not wish that, delete this exception notice.
  42. (use-modules (test-suite lib))
  43. ;;; (gbh) some of these are duplicated in r4rs. This is probably a bit
  44. ;;; more thorough, though (maybe overkill? I need it, anyway).
  45. ;;;
  46. ;;;
  47. ;;; Also: it will fail on the ass*-ref & remove functions.
  48. ;;; Sloppy versions should be added with the current behaviour
  49. ;;; (it's the only set of 'ref functions that won't cause an
  50. ;;; error on an incorrect arg); they aren't actually used anywhere
  51. ;;; so changing's not a big deal.
  52. ;;; Misc
  53. (define-macro (pass-if-not str form)
  54. `(pass-if ,str (not ,form)))
  55. (define (safe-assq-ref alist elt)
  56. (let ((x (assq elt alist)))
  57. (if x (cdr x) x)))
  58. (define (safe-assv-ref alist elt)
  59. (let ((x (assv elt alist)))
  60. (if x (cdr x) x)))
  61. (define (safe-assoc-ref alist elt)
  62. (let ((x (assoc elt alist)))
  63. (if x (cdr x) x)))
  64. ;;; Creators, getters
  65. (let ((a (acons 'a 'b (acons 'c 'd (acons 'e 'f ()))))
  66. (b (acons "this" "is" (acons "a" "test" ())))
  67. (deformed '(a b c d e f g)))
  68. (pass-if "alist: acons"
  69. (and (equal? a '((a . b) (c . d) (e . f)))
  70. (equal? b '(("this" . "is") ("a" . "test")))))
  71. (pass-if "alist: sloppy-assq"
  72. (let ((x (sloppy-assq 'c a)))
  73. (and (pair? x)
  74. (eq? (car x) 'c)
  75. (eq? (cdr x) 'd))))
  76. (pass-if "alist: sloppy-assq not"
  77. (let ((x (sloppy-assq "this" b)))
  78. (not x)))
  79. (pass-if "alist: sloppy-assv"
  80. (let ((x (sloppy-assv 'c a)))
  81. (and (pair? x)
  82. (eq? (car x) 'c)
  83. (eq? (cdr x) 'd))))
  84. (pass-if "alist: sloppy-assv not"
  85. (let ((x (sloppy-assv "this" b)))
  86. (not x)))
  87. (pass-if "alist: sloppy-assoc"
  88. (let ((x (sloppy-assoc "this" b)))
  89. (and (pair? x)
  90. (string=? (cdr x) "is"))))
  91. (pass-if "alist: sloppy-assoc not"
  92. (let ((x (sloppy-assoc "heehee" b)))
  93. (not x)))
  94. (pass-if "alist: assq"
  95. (let ((x (assq 'c a)))
  96. (and (pair? x)
  97. (eq? (car x) 'c)
  98. (eq? (cdr x) 'd))))
  99. (pass-if "alist: assq deformed"
  100. (catch 'wrong-type-arg
  101. (lambda ()
  102. (assq 'x deformed))
  103. (lambda (key . args)
  104. #t)))
  105. (pass-if-not "alist: assq not" (assq 'r a))
  106. (pass-if "alist: assv"
  107. (let ((x (assv 'a a)))
  108. (and (pair? x)
  109. (eq? (car x) 'a)
  110. (eq? (cdr x) 'b))))
  111. (pass-if "alist: assv deformed"
  112. (catch 'wrong-type-arg
  113. (lambda ()
  114. (assv 'x deformed)
  115. #f)
  116. (lambda (key . args)
  117. #t)))
  118. (pass-if-not "alist: assv not" (assq "this" b))
  119. (pass-if "alist: assoc"
  120. (let ((x (assoc "this" b)))
  121. (and (pair? x)
  122. (string=? (car x) "this")
  123. (string=? (cdr x) "is"))))
  124. (pass-if "alist: assoc deformed"
  125. (catch 'wrong-type-arg
  126. (lambda ()
  127. (assoc 'x deformed)
  128. #f)
  129. (lambda (key . args)
  130. #t)))
  131. (pass-if-not "alist: assoc not" (assoc "this isn't" b)))
  132. ;;; Refers
  133. (let ((a '((foo bar) (baz quux)))
  134. (b '(("one" 2 3) ("four" 5 6) ("seven" 8 9)))
  135. (deformed '(thats a real sloppy assq you got there)))
  136. (pass-if "alist: assq-ref"
  137. (let ((x (assq-ref a 'foo)))
  138. (and (list? x)
  139. (eq? (car x) 'bar))))
  140. (pass-if-not "alist: assq-ref not" (assq-ref b "one"))
  141. (pass-if "alist: assv-ref"
  142. (let ((x (assv-ref a 'baz)))
  143. (and (list? x)
  144. (eq? (car x) 'quux))))
  145. (pass-if-not "alist: assv-ref not" (assv-ref b "one"))
  146. (pass-if "alist: assoc-ref"
  147. (let ((x (assoc-ref b "one")))
  148. (and (list? x)
  149. (eq? (car x) 2)
  150. (eq? (cadr x) 3))))
  151. (pass-if-not "alist: assoc-ref not" (assoc-ref a 'testing))
  152. (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
  153. (pass-if "alist: assv-ref deformed"
  154. (catch 'wrong-type-arg
  155. (lambda ()
  156. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  157. (assv-ref deformed 'sloppy)
  158. #f)
  159. (lambda (key . args)
  160. #t)))
  161. (pass-if "alist: assoc-ref deformed"
  162. (catch 'wrong-type-arg
  163. (lambda ()
  164. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  165. (assoc-ref deformed 'sloppy)
  166. #f)
  167. (lambda (key . args)
  168. #t)))
  169. (pass-if "alist: assq-ref deformed"
  170. (catch 'wrong-type-arg
  171. (lambda ()
  172. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  173. (assq-ref deformed 'sloppy)
  174. #f)
  175. (lambda (key . args)
  176. #t)))))
  177. ;;; Setters
  178. (let ((a '((another . silly) (alist . test-case)))
  179. (b '(("this" "one" "has") ("strings" "!")))
  180. (deformed '(canada is a cold nation)))
  181. (pass-if "alist: assq-set!"
  182. (begin
  183. (set! a (assq-set! a 'another 'stupid))
  184. (let ((x (safe-assq-ref a 'another)))
  185. (and x
  186. (symbol? x) (eq? x 'stupid)))))
  187. (pass-if "alist: assq-set! add"
  188. (begin
  189. (set! a (assq-set! a 'fickle 'pickle))
  190. (let ((x (safe-assq-ref a 'fickle)))
  191. (and x (symbol? x)
  192. (eq? x 'pickle)))))
  193. (pass-if "alist: assv-set!"
  194. (begin
  195. (set! a (assv-set! a 'another 'boring))
  196. (let ((x (safe-assv-ref a 'another)))
  197. (and x
  198. (eq? x 'boring)))))
  199. (pass-if "alist: assv-set! add"
  200. (begin
  201. (set! a (assv-set! a 'whistle '(while you work)))
  202. (let ((x (safe-assv-ref a 'whistle)))
  203. (and x (equal? x '(while you work))))))
  204. (pass-if "alist: assoc-set!"
  205. (begin
  206. (set! b (assoc-set! b "this" "has"))
  207. (let ((x (safe-assoc-ref b "this")))
  208. (and x (string? x)
  209. (string=? x "has")))))
  210. (pass-if "alist: assoc-set! add"
  211. (begin
  212. (set! b (assoc-set! b "flugle" "horn"))
  213. (let ((x (safe-assoc-ref b "flugle")))
  214. (and x (string? x)
  215. (string=? x "horn")))))
  216. (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
  217. (pass-if "alist: assq-set! deformed"
  218. (catch 'wrong-type-arg
  219. (lambda ()
  220. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  221. (assq-set! deformed 'cold '(very cold))
  222. #f)
  223. (lambda (key . args)
  224. #t)))
  225. (pass-if "alist: assv-set! deformed"
  226. (catch 'wrong-type-arg
  227. (lambda ()
  228. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  229. (assv-set! deformed 'canada 'Canada)
  230. #f)
  231. (lambda (key . args)
  232. #t)))
  233. (pass-if "alist: assoc-set! deformed"
  234. (catch 'wrong-type-arg
  235. (lambda ()
  236. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  237. (assoc-set! deformed 'canada '(Iceland hence the name))
  238. #f)
  239. (lambda (key . args)
  240. #t)))))
  241. ;;; Removers
  242. (let ((a '((a b) (c d) (e boring)))
  243. (b '(("what" . "else") ("could" . "I") ("say" . "here")))
  244. (deformed 1))
  245. (pass-if "alist: assq-remove!"
  246. (begin
  247. (set! a (assq-remove! a 'a))
  248. (equal? a '((c d) (e boring)))))
  249. (pass-if "alist: assv-remove!"
  250. (begin
  251. (set! a (assv-remove! a 'c))
  252. (equal? a '((e boring)))))
  253. (pass-if "alist: assoc-remove!"
  254. (begin
  255. (set! b (assoc-remove! b "what"))
  256. (equal? b '(("could" . "I") ("say" . "here")))))
  257. (let* ((have-sloppy-assq-remove? (defined? 'sloppy-assq-remove)))
  258. (pass-if "alist: assq-remove! deformed"
  259. (catch 'wrong-type-arg
  260. (lambda ()
  261. (if (not have-sloppy-assq-remove?) (throw 'unsupported))
  262. (assq-remove! deformed 'puddle)
  263. #f)
  264. (lambda (key . args)
  265. #t)))
  266. (pass-if "alist: assv-remove! deformed"
  267. (catch 'wrong-type-arg
  268. (lambda ()
  269. (if (not have-sloppy-assq-remove?) (throw 'unsupported))
  270. (assv-remove! deformed 'splashing)
  271. #f)
  272. (lambda (key . args)
  273. #t)))
  274. (pass-if "alist: assoc-remove! deformed"
  275. (catch 'wrong-type-arg
  276. (lambda ()
  277. (if (not have-sloppy-assq-remove?) (throw 'unsupported))
  278. (assoc-remove! deformed 'fun)
  279. #f)
  280. (lambda (key . args)
  281. #t)))))