alist.test 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. ;;;; alist.test --- tests guile's alists -*- scheme -*-
  2. ;;;; Copyright (C) 1999, 2001, 2006, 2017 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. (define-module (test-suite alist)
  18. #:use-module (test-suite lib))
  19. (define-syntax-rule (pass-if-not str form)
  20. (pass-if str (not form)))
  21. (define (safe-assq-ref alist elt)
  22. (let ((x (assq elt alist)))
  23. (if x (cdr x) x)))
  24. (define (safe-assv-ref alist elt)
  25. (let ((x (assv elt alist)))
  26. (if x (cdr x) x)))
  27. (define (safe-assoc-ref alist elt)
  28. (let ((x (assoc elt alist)))
  29. (if x (cdr x) x)))
  30. ;;; Creators, getters
  31. (let ((a (acons 'a 'b (acons 'c 'd (acons 'e 'f '()))))
  32. (b (acons "this" "is" (acons "a" "test" '())))
  33. (deformed '(a b c d e f g)))
  34. (pass-if "acons"
  35. (and (equal? a '((a . b) (c . d) (e . f)))
  36. (equal? b '(("this" . "is") ("a" . "test")))))
  37. (pass-if "sloppy-assq"
  38. (let ((x (sloppy-assq 'c a)))
  39. (and (pair? x)
  40. (eq? (car x) 'c)
  41. (eq? (cdr x) 'd))))
  42. (pass-if "sloppy-assq not"
  43. (let ((x (sloppy-assq "this" b)))
  44. (not x)))
  45. (pass-if "sloppy-assv"
  46. (let ((x (sloppy-assv 'c a)))
  47. (and (pair? x)
  48. (eq? (car x) 'c)
  49. (eq? (cdr x) 'd))))
  50. (pass-if "sloppy-assv not"
  51. (let ((x (sloppy-assv "this" b)))
  52. (not x)))
  53. (pass-if "sloppy-assoc"
  54. (let ((x (sloppy-assoc "this" b)))
  55. (and (pair? x)
  56. (string=? (cdr x) "is"))))
  57. (pass-if "sloppy-assoc not"
  58. (let ((x (sloppy-assoc "heehee" b)))
  59. (not x)))
  60. (pass-if "assq"
  61. (let ((x (assq 'c a)))
  62. (and (pair? x)
  63. (eq? (car x) 'c)
  64. (eq? (cdr x) 'd))))
  65. (pass-if-exception "assq deformed"
  66. exception:wrong-type-arg
  67. (assq 'x deformed))
  68. (pass-if-not "assq not" (assq 'r a))
  69. (pass-if "assv"
  70. (let ((x (assv 'a a)))
  71. (and (pair? x)
  72. (eq? (car x) 'a)
  73. (eq? (cdr x) 'b))))
  74. (pass-if-exception "assv deformed"
  75. exception:wrong-type-arg
  76. (assv 'x deformed))
  77. (pass-if-not "assv not" (assq "this" b))
  78. (pass-if "assoc"
  79. (let ((x (assoc "this" b)))
  80. (and (pair? x)
  81. (string=? (car x) "this")
  82. (string=? (cdr x) "is"))))
  83. (pass-if-exception "assoc deformed"
  84. exception:wrong-type-arg
  85. (assoc 'x deformed))
  86. (pass-if-not "assoc not" (assoc "this isn't" b)))
  87. ;;; Refers
  88. (let ((a '((foo bar) (baz quux)))
  89. (b '(("one" 2 3) ("four" 5 6) ("seven" 8 9)))
  90. (deformed '(thats a real sloppy assq you got there)))
  91. (pass-if "assq-ref"
  92. (let ((x (assq-ref a 'foo)))
  93. (and (list? x)
  94. (eq? (car x) 'bar))))
  95. (pass-if-not "assq-ref not" (assq-ref b "one"))
  96. (pass-if "assv-ref"
  97. (let ((x (assv-ref a 'baz)))
  98. (and (list? x)
  99. (eq? (car x) 'quux))))
  100. (pass-if-not "assv-ref not" (assv-ref b "one"))
  101. (pass-if "assoc-ref"
  102. (let ((x (assoc-ref b "one")))
  103. (and (list? x)
  104. (eqv? (car x) 2)
  105. (eqv? (cadr x) 3))))
  106. (pass-if-not "assoc-ref not" (assoc-ref a 'testing))
  107. (pass-if-not "assv-ref deformed"
  108. (assv-ref deformed 'sloppy))
  109. (pass-if-not "assoc-ref deformed"
  110. (assoc-ref deformed 'sloppy))
  111. (pass-if-not "assq-ref deformed"
  112. (assq-ref deformed 'sloppy)))
  113. ;;; Setters
  114. (let ((a '((another . silly) (alist . test-case)))
  115. (b '(("this" "one" "has") ("strings" "!")))
  116. (deformed '(canada is a cold nation)))
  117. (pass-if "assq-set!"
  118. (begin
  119. (set! a (assq-set! a 'another 'stupid))
  120. (let ((x (safe-assq-ref a 'another)))
  121. (and x
  122. (symbol? x) (eq? x 'stupid)))))
  123. (pass-if "assq-set! add"
  124. (begin
  125. (set! a (assq-set! a 'fickle 'pickle))
  126. (let ((x (safe-assq-ref a 'fickle)))
  127. (and x (symbol? x)
  128. (eq? x 'pickle)))))
  129. (pass-if "assv-set!"
  130. (begin
  131. (set! a (assv-set! a 'another 'boring))
  132. (let ((x (safe-assv-ref a 'another)))
  133. (and x
  134. (eq? x 'boring)))))
  135. (pass-if "assv-set! add"
  136. (begin
  137. (set! a (assv-set! a 'whistle '(while you work)))
  138. (let ((x (safe-assv-ref a 'whistle)))
  139. (and x (equal? x '(while you work))))))
  140. (pass-if "assoc-set!"
  141. (begin
  142. (set! b (assoc-set! b "this" "has"))
  143. (let ((x (safe-assoc-ref b "this")))
  144. (and x (string? x)
  145. (string=? x "has")))))
  146. (pass-if "assoc-set! add"
  147. (begin
  148. (set! b (assoc-set! b "flugle" "horn"))
  149. (let ((x (safe-assoc-ref b "flugle")))
  150. (and x (string? x)
  151. (string=? x "horn")))))
  152. (pass-if-equal "assq-set! deformed"
  153. (assq-set! deformed 'cold '(very cold))
  154. '((cold very cold) canada is a cold nation))
  155. (pass-if-equal "assv-set! deformed"
  156. (assv-set! deformed 'canada 'Canada)
  157. '((canada . Canada) canada is a cold nation))
  158. (pass-if-equal "assoc-set! deformed"
  159. (assoc-set! deformed 'canada '(Iceland hence the name))
  160. '((canada Iceland hence the name) canada is a cold nation)))
  161. ;;; Removers
  162. (let ((a '((a b) (c d) (e boring)))
  163. (b '(("what" . "else") ("could" . "I") ("say" . "here")))
  164. (deformed 1))
  165. (pass-if "assq-remove!"
  166. (begin
  167. (set! a (assq-remove! a 'a))
  168. (equal? a '((c d) (e boring)))))
  169. (pass-if "assv-remove!"
  170. (begin
  171. (set! a (assv-remove! a 'c))
  172. (equal? a '((e boring)))))
  173. (pass-if "assoc-remove!"
  174. (begin
  175. (set! b (assoc-remove! b "what"))
  176. (equal? b '(("could" . "I") ("say" . "here")))))
  177. (pass-if-equal "assq-remove! deformed"
  178. (assq-remove! deformed 'puddle)
  179. 1)
  180. (pass-if-equal "assv-remove! deformed"
  181. (assv-remove! deformed 'splashing)
  182. 1)
  183. (pass-if-equal "assoc-remove! deformed"
  184. (assoc-remove! deformed 'fun)
  185. 1))