summaryrefslogtreecommitdiff
path: root/test-suite/tests/alist.test
blob: 1e10864d0c8f3732615fb2d53489df1a5a645051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
;;;; alist.test --- tests guile's alists     -*- scheme -*-
;;;; Copyright (C) 1999, 2001, 2006, 2017 Free Software Foundation, Inc.
;;;; 
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite alist)
  #:use-module (test-suite lib))

(define-syntax-rule (pass-if-not str form)
  (pass-if str (not form)))

(define (safe-assq-ref alist elt)
  (let ((x (assq elt alist)))
    (if x (cdr x) x)))

(define (safe-assv-ref alist elt)
  (let ((x (assv elt alist)))
    (if x (cdr x) x)))

(define (safe-assoc-ref alist elt)
  (let ((x (assoc elt alist)))
    (if x (cdr x) x)))
  
;;; Creators, getters
(let ((a (acons 'a 'b (acons 'c 'd (acons 'e 'f '()))))
      (b (acons "this" "is" (acons "a" "test" '())))
      (deformed '(a b c d e f g)))
  (pass-if "acons"
	   (and (equal? a '((a . b) (c . d) (e . f)))
		(equal? b '(("this" . "is") ("a" . "test")))))
  (pass-if "sloppy-assq"
	   (let ((x (sloppy-assq 'c a)))
	     (and (pair? x)
		  (eq? (car x) 'c)
		  (eq? (cdr x) 'd))))
  (pass-if "sloppy-assq not"
	   (let ((x (sloppy-assq "this" b)))
	     (not x)))
  (pass-if "sloppy-assv"
	   (let ((x (sloppy-assv 'c a)))
	     (and (pair? x)
		  (eq? (car x) 'c)
		  (eq? (cdr x) 'd))))
  (pass-if "sloppy-assv not"
	   (let ((x (sloppy-assv "this" b)))
	     (not x)))
  (pass-if "sloppy-assoc"
	   (let ((x (sloppy-assoc "this" b)))
	     (and (pair? x)
		  (string=? (cdr x) "is"))))
  (pass-if "sloppy-assoc not"
	   (let ((x (sloppy-assoc "heehee" b)))
	     (not x)))
  (pass-if "assq"
	   (let ((x (assq 'c a)))
	     (and (pair? x)
		  (eq? (car x) 'c)
		  (eq? (cdr x) 'd))))
  (pass-if-exception "assq deformed"
    exception:wrong-type-arg
    (assq 'x deformed))
  (pass-if-not "assq not" (assq 'r a))
  (pass-if "assv"
	   (let ((x (assv 'a a)))
	     (and (pair? x)
		  (eq? (car x) 'a)
		  (eq? (cdr x) 'b))))
  (pass-if-exception "assv deformed"
    exception:wrong-type-arg
    (assv 'x deformed))
  (pass-if-not "assv not" (assq "this" b))

  (pass-if "assoc"
	   (let ((x (assoc "this" b)))
	     (and (pair? x)
		  (string=? (car x) "this")
		  (string=? (cdr x) "is"))))
  (pass-if-exception "assoc deformed"
    exception:wrong-type-arg
    (assoc 'x deformed))
  (pass-if-not "assoc not" (assoc "this isn't" b)))


;;; Refers
(let ((a '((foo bar) (baz quux)))
      (b '(("one" 2 3) ("four" 5 6) ("seven" 8 9)))
      (deformed '(thats a real sloppy assq you got there)))
  (pass-if "assq-ref"
	   (let ((x (assq-ref a 'foo)))
	     (and (list? x)
		  (eq? (car x) 'bar))))

  (pass-if-not "assq-ref not" (assq-ref b "one"))
  (pass-if "assv-ref"
	   (let ((x (assv-ref a 'baz)))
	     (and (list? x)
		  (eq? (car x) 'quux))))

  (pass-if-not "assv-ref not" (assv-ref b "one"))

  (pass-if "assoc-ref"
	   (let ((x (assoc-ref b "one")))
	     (and (list? x)
		  (eqv? (car x) 2)
		  (eqv? (cadr x) 3))))


  (pass-if-not "assoc-ref not" (assoc-ref a 'testing))

  (pass-if-not "assv-ref deformed"
               (assv-ref deformed 'sloppy))

  (pass-if-not "assoc-ref deformed"
               (assoc-ref deformed 'sloppy))

  (pass-if-not "assq-ref deformed"
               (assq-ref deformed 'sloppy)))


;;; Setters
(let ((a '((another . silly) (alist . test-case)))
      (b '(("this" "one" "has") ("strings" "!")))
      (deformed '(canada is a cold nation)))
  (pass-if "assq-set!"
	   (begin
	     (set! a (assq-set! a 'another 'stupid))
	     (let ((x (safe-assq-ref a 'another)))
	       (and x
		    (symbol? x) (eq? x 'stupid)))))

  (pass-if "assq-set! add"
	   (begin
	     (set! a (assq-set! a 'fickle 'pickle))
	     (let ((x (safe-assq-ref a 'fickle)))
	       (and x (symbol? x)
		    (eq? x 'pickle)))))

  (pass-if "assv-set!"
	   (begin
	     (set! a (assv-set! a 'another 'boring))
	     (let ((x (safe-assv-ref a 'another)))
		   (and x
			(eq? x 'boring)))))
  (pass-if "assv-set! add"
	   (begin
	     (set! a (assv-set! a 'whistle '(while you work)))
	     (let ((x (safe-assv-ref a 'whistle)))
	       (and x (equal? x '(while you work))))))

  (pass-if "assoc-set!"
	   (begin
	     (set! b (assoc-set! b "this" "has"))
	     (let ((x (safe-assoc-ref b "this")))
	       (and x (string? x)
		    (string=? x "has")))))
  (pass-if "assoc-set! add"
	   (begin
	     (set! b (assoc-set! b "flugle" "horn"))
	     (let ((x (safe-assoc-ref b "flugle")))
	       (and x (string? x)
		    (string=? x "horn")))))

  (pass-if-equal "assq-set! deformed"
      (assq-set! deformed 'cold '(very cold))
    '((cold very cold) canada is a cold nation))   

  (pass-if-equal "assv-set! deformed"
      (assv-set! deformed 'canada 'Canada)
    '((canada . Canada) canada is a cold nation))

  (pass-if-equal "assoc-set! deformed"
      (assoc-set! deformed 'canada '(Iceland hence the name))
    '((canada Iceland hence the name) canada is a cold nation)))

;;; Removers

(let ((a '((a b) (c d) (e boring)))
      (b '(("what" .  "else") ("could" . "I") ("say" . "here")))
      (deformed 1))
  (pass-if "assq-remove!"
	   (begin 
	     (set! a (assq-remove! a 'a))
	     (equal? a '((c d) (e boring)))))
  (pass-if "assv-remove!"
	   (begin
	     (set! a (assv-remove! a 'c))
	     (equal? a '((e boring)))))
  (pass-if "assoc-remove!"
	   (begin
	     (set! b (assoc-remove! b "what"))
	     (equal? b '(("could" . "I") ("say" . "here")))))

  (pass-if-equal "assq-remove! deformed"
      (assq-remove! deformed 'puddle)
    1)

  (pass-if-equal "assv-remove! deformed"
      (assv-remove! deformed 'splashing)
    1)

  (pass-if-equal "assoc-remove! deformed"
      (assoc-remove! deformed 'fun)
    1))