alist.test 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. ;;;; alist.test --- tests guile's alists -*- scheme -*-
  2. ;;;; Copyright (C) 1999, 2001, 2006 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library 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 GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (use-modules (test-suite lib))
  18. ;;; (gbh) some of these are duplicated in r4rs. This is probably a bit
  19. ;;; more thorough, though (maybe overkill? I need it, anyway).
  20. ;;;
  21. ;;;
  22. ;;; Also: it will fail on the ass*-ref & remove functions.
  23. ;;; Sloppy versions should be added with the current behaviour
  24. ;;; (it's the only set of 'ref functions that won't cause an
  25. ;;; error on an incorrect arg); they aren't actually used anywhere
  26. ;;; so changing's not a big deal.
  27. ;;; Misc
  28. (define-macro (pass-if-not str form)
  29. `(pass-if ,str (not ,form)))
  30. (define (safe-assq-ref alist elt)
  31. (let ((x (assq elt alist)))
  32. (if x (cdr x) x)))
  33. (define (safe-assv-ref alist elt)
  34. (let ((x (assv elt alist)))
  35. (if x (cdr x) x)))
  36. (define (safe-assoc-ref alist elt)
  37. (let ((x (assoc elt alist)))
  38. (if x (cdr x) x)))
  39. ;;; Creators, getters
  40. (let ((a (acons 'a 'b (acons 'c 'd (acons 'e 'f '()))))
  41. (b (acons "this" "is" (acons "a" "test" '())))
  42. (deformed '(a b c d e f g)))
  43. (pass-if "acons"
  44. (and (equal? a '((a . b) (c . d) (e . f)))
  45. (equal? b '(("this" . "is") ("a" . "test")))))
  46. (pass-if "sloppy-assq"
  47. (let ((x (sloppy-assq 'c a)))
  48. (and (pair? x)
  49. (eq? (car x) 'c)
  50. (eq? (cdr x) 'd))))
  51. (pass-if "sloppy-assq not"
  52. (let ((x (sloppy-assq "this" b)))
  53. (not x)))
  54. (pass-if "sloppy-assv"
  55. (let ((x (sloppy-assv 'c a)))
  56. (and (pair? x)
  57. (eq? (car x) 'c)
  58. (eq? (cdr x) 'd))))
  59. (pass-if "sloppy-assv not"
  60. (let ((x (sloppy-assv "this" b)))
  61. (not x)))
  62. (pass-if "sloppy-assoc"
  63. (let ((x (sloppy-assoc "this" b)))
  64. (and (pair? x)
  65. (string=? (cdr x) "is"))))
  66. (pass-if "sloppy-assoc not"
  67. (let ((x (sloppy-assoc "heehee" b)))
  68. (not x)))
  69. (pass-if "assq"
  70. (let ((x (assq 'c a)))
  71. (and (pair? x)
  72. (eq? (car x) 'c)
  73. (eq? (cdr x) 'd))))
  74. (pass-if-exception "assq deformed"
  75. exception:wrong-type-arg
  76. (assq 'x deformed))
  77. (pass-if-not "assq not" (assq 'r a))
  78. (pass-if "assv"
  79. (let ((x (assv 'a a)))
  80. (and (pair? x)
  81. (eq? (car x) 'a)
  82. (eq? (cdr x) 'b))))
  83. (pass-if-exception "assv deformed"
  84. exception:wrong-type-arg
  85. (assv 'x deformed))
  86. (pass-if-not "assv not" (assq "this" b))
  87. (pass-if "assoc"
  88. (let ((x (assoc "this" b)))
  89. (and (pair? x)
  90. (string=? (car x) "this")
  91. (string=? (cdr x) "is"))))
  92. (pass-if-exception "assoc deformed"
  93. exception:wrong-type-arg
  94. (assoc 'x deformed))
  95. (pass-if-not "assoc not" (assoc "this isn't" b)))
  96. ;;; Refers
  97. (let ((a '((foo bar) (baz quux)))
  98. (b '(("one" 2 3) ("four" 5 6) ("seven" 8 9)))
  99. (deformed '(thats a real sloppy assq you got there)))
  100. (pass-if "assq-ref"
  101. (let ((x (assq-ref a 'foo)))
  102. (and (list? x)
  103. (eq? (car x) 'bar))))
  104. (pass-if-not "assq-ref not" (assq-ref b "one"))
  105. (pass-if "assv-ref"
  106. (let ((x (assv-ref a 'baz)))
  107. (and (list? x)
  108. (eq? (car x) 'quux))))
  109. (pass-if-not "assv-ref not" (assv-ref b "one"))
  110. (pass-if "assoc-ref"
  111. (let ((x (assoc-ref b "one")))
  112. (and (list? x)
  113. (eqv? (car x) 2)
  114. (eqv? (cadr x) 3))))
  115. (pass-if-not "assoc-ref not" (assoc-ref a 'testing))
  116. (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
  117. (pass-if-exception "assv-ref deformed"
  118. exception:wrong-type-arg
  119. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  120. (assv-ref deformed 'sloppy))
  121. (pass-if-exception "assoc-ref deformed"
  122. exception:wrong-type-arg
  123. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  124. (assoc-ref deformed 'sloppy))
  125. (pass-if-exception "assq-ref deformed"
  126. exception:wrong-type-arg
  127. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  128. (assq-ref deformed 'sloppy))))
  129. ;;; Setters
  130. (let ((a '((another . silly) (alist . test-case)))
  131. (b '(("this" "one" "has") ("strings" "!")))
  132. (deformed '(canada is a cold nation)))
  133. (pass-if "assq-set!"
  134. (begin
  135. (set! a (assq-set! a 'another 'stupid))
  136. (let ((x (safe-assq-ref a 'another)))
  137. (and x
  138. (symbol? x) (eq? x 'stupid)))))
  139. (pass-if "assq-set! add"
  140. (begin
  141. (set! a (assq-set! a 'fickle 'pickle))
  142. (let ((x (safe-assq-ref a 'fickle)))
  143. (and x (symbol? x)
  144. (eq? x 'pickle)))))
  145. (pass-if "assv-set!"
  146. (begin
  147. (set! a (assv-set! a 'another 'boring))
  148. (let ((x (safe-assv-ref a 'another)))
  149. (and x
  150. (eq? x 'boring)))))
  151. (pass-if "assv-set! add"
  152. (begin
  153. (set! a (assv-set! a 'whistle '(while you work)))
  154. (let ((x (safe-assv-ref a 'whistle)))
  155. (and x (equal? x '(while you work))))))
  156. (pass-if "assoc-set!"
  157. (begin
  158. (set! b (assoc-set! b "this" "has"))
  159. (let ((x (safe-assoc-ref b "this")))
  160. (and x (string? x)
  161. (string=? x "has")))))
  162. (pass-if "assoc-set! add"
  163. (begin
  164. (set! b (assoc-set! b "flugle" "horn"))
  165. (let ((x (safe-assoc-ref b "flugle")))
  166. (and x (string? x)
  167. (string=? x "horn")))))
  168. (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
  169. (pass-if-exception "assq-set! deformed"
  170. exception:wrong-type-arg
  171. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  172. (assq-set! deformed 'cold '(very cold)))
  173. (pass-if-exception "assv-set! deformed"
  174. exception:wrong-type-arg
  175. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  176. (assv-set! deformed 'canada 'Canada))
  177. (pass-if-exception "assoc-set! deformed"
  178. exception:wrong-type-arg
  179. (if (not have-sloppy-assv-ref?) (throw 'unsupported))
  180. (assoc-set! deformed 'canada '(Iceland hence the name)))))
  181. ;;; Removers
  182. (let ((a '((a b) (c d) (e boring)))
  183. (b '(("what" . "else") ("could" . "I") ("say" . "here")))
  184. (deformed 1))
  185. (pass-if "assq-remove!"
  186. (begin
  187. (set! a (assq-remove! a 'a))
  188. (equal? a '((c d) (e boring)))))
  189. (pass-if "assv-remove!"
  190. (begin
  191. (set! a (assv-remove! a 'c))
  192. (equal? a '((e boring)))))
  193. (pass-if "assoc-remove!"
  194. (begin
  195. (set! b (assoc-remove! b "what"))
  196. (equal? b '(("could" . "I") ("say" . "here")))))
  197. (let* ((have-sloppy-assq-remove? (defined? 'sloppy-assq-remove)))
  198. (pass-if-exception "assq-remove! deformed"
  199. exception:wrong-type-arg
  200. (if (not have-sloppy-assq-remove?) (throw 'unsupported))
  201. (assq-remove! deformed 'puddle))
  202. (pass-if-exception "assv-remove! deformed"
  203. exception:wrong-type-arg
  204. (if (not have-sloppy-assq-remove?) (throw 'unsupported))
  205. (assv-remove! deformed 'splashing))
  206. (pass-if-exception "assoc-remove! deformed"
  207. exception:wrong-type-arg
  208. (if (not have-sloppy-assq-remove?) (throw 'unsupported))
  209. (assoc-remove! deformed 'fun))))