1 ;;;; list.test --- tests guile's lists -*- scheme -*-
2 ;;;; Copyright (C) 2000 Free Software Foundation, Inc.
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.
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.
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
19 ;;;; As a special exception, the Free Software Foundation gives permission
20 ;;;; for additional uses of the text contained in its release of GUILE.
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.
28 ;;;; This exception does not however invalidate any other reasons why
29 ;;;; the executable file might be covered by the GNU General Public License.
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.
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.
43 (use-modules (ice-9 documentation))
51 ;; This unique tag is reserved for the unroll and diff-unrolled functions.
54 (define circle-indicator
55 (cons 'circle 'indicator))
58 ;; Extract every single scheme object that is contained within OBJ into a new
59 ;; data structure. That means, if OBJ somewhere contains a pair, the newly
60 ;; created structure holds a reference to the pair as well as references to
61 ;; the car and cdr of that pair. For vectors, the newly created structure
62 ;; holds a reference to that vector as well as references to every element of
63 ;; that vector. Since this is done recursively, the original data structure
64 ;; is deeply unrolled. If there are circles within the original data
65 ;; structures, every reference that points backwards into the data structure
66 ;; is denoted by storing the circle-indicator tag as well as the object the
67 ;; circular reference points to.
71 (let unroll* ((objct obj)
74 (let loop ((object objct)
77 (if (memq object histry)
78 (cons (cons circle-indicator object) result)
79 (let ((history (cons object histry)))
81 (loop (cdr object) history
82 (cons (cons object (unroll* (car object) history))
88 (vector->list object)))
90 (else (cons object result)))))))))
93 ;; Compare two data-structures that were generated with unroll. If any of the
94 ;; elements found not to be eq?, return a pair that holds the position of the
95 ;; first found differences of the two data structures. If all elements are
96 ;; found to be eq?, #f is returned.
99 (define (diff-unrolled a b)
100 (cond ;; has everything been compared already?
101 ((and (null? a) (null? b))
103 ;; do both structures still contain elements?
104 ((and (pair? a) (pair? b))
105 (cond ;; are the next elements both plain objects?
106 ((and (not (pair? (car a))) (not (pair? (car b))))
107 (if (eq? (car a) (car b))
108 (diff-unrolled (cdr a) (cdr b))
110 ;; are the next elements both container objects?
111 ((and (pair? (car a)) (pair? (car b)))
112 (if (eq? (caar a) (caar b))
113 (cond ;; do both objects close a circular structure?
114 ((eq? circle-indicator (caar a))
115 (if (eq? (cdar a) (cdar b))
116 (diff-unrolled (cdr a) (cdr b))
118 ;; do both objects hold a vector?
120 (or (let loop ((a1 (cdar a)) (b1 (cdar b)))
122 ((and (null? a1) (null? b1))
124 ((and (pair? a1) (pair? b1))
125 (or (diff-unrolled (car a1) (car b1))
126 (loop (cdr a1) (cdr b1))))
129 (diff-unrolled (cdr a) (cdr b))))
130 ;; do both objects hold a pair?
132 (or (diff-unrolled (cdar a) (cdar b))
133 (diff-unrolled (cdr a) (cdr b)))))
162 (with-test-prefix "append!"
164 ;; Is documentation available?
166 (pass-if "documented?" (object-documentation append!))
168 ;; Is the handling of empty lists as arguments correct?
170 (pass-if "no arguments"
174 (pass-if "empty list argument"
178 (pass-if "some empty list arguments"
179 (eq? (append! '() '() '())
182 ;; Does the last non-empty-list argument remain unchanged?
184 (pass-if "some empty lists with non-empty list"
185 (let* ((foo (list 1 2))
186 (foo-unrolled (unroll foo))
187 (tst (append! '() '() '() foo))
188 (tst-unrolled (unroll tst)))
190 (not (diff-unrolled foo-unrolled tst-unrolled)))))
192 (pass-if "some empty lists with improper list"
193 (let* ((foo (cons 1 2))
194 (foo-unrolled (unroll foo))
195 (tst (append! '() '() '() foo))
196 (tst-unrolled (unroll tst)))
198 (not (diff-unrolled foo-unrolled tst-unrolled)))))
200 (pass-if "some empty lists with circular list"
201 (let ((foo (list 1 2)))
202 (set-cdr! (cdr foo) (cdr foo))
203 (let* ((foo-unrolled (unroll foo))
204 (tst (append! '() '() '() foo))
205 (tst-unrolled (unroll tst)))
207 (not (diff-unrolled foo-unrolled tst-unrolled))))))
209 (pass-if "some empty lists with non list object"
210 (let* ((foo (vector 1 2 3))
211 (foo-unrolled (unroll foo))
212 (tst (append! '() '() '() foo))
213 (tst-unrolled (unroll tst)))
215 (not (diff-unrolled foo-unrolled tst-unrolled)))))
217 (pass-if "non-empty list between empty lists"
218 (let* ((foo (list 1 2))
219 (foo-unrolled (unroll foo))
220 (tst (append! '() '() '() foo '() '() '()))
221 (tst-unrolled (unroll tst)))
223 (not (diff-unrolled foo-unrolled tst-unrolled)))))
225 ;; Are arbitrary lists append!ed correctly?
227 (pass-if "two one-element lists"
228 (let* ((foo (list 1))
229 (foo-unrolled (unroll foo))
231 (bar-unrolled (unroll bar))
232 (tst (append! foo bar))
233 (tst-unrolled (unroll tst))
234 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
235 (and (equal? tst '(1 2))
236 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
237 (not (diff-unrolled bar-unrolled (cdr diff-foo-tst))))))
239 (pass-if "three one-element lists"
240 (let* ((foo (list 1))
241 (foo-unrolled (unroll foo))
243 (bar-unrolled (unroll bar))
245 (baz-unrolled (unroll baz))
246 (tst (append! foo bar baz))
247 (tst-unrolled (unroll tst))
248 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
249 (and (equal? tst '(1 2 3))
250 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
251 (let* ((tst-unrolled-2 (cdr diff-foo-tst))
252 (diff-foo-bar (diff-unrolled bar-unrolled tst-unrolled-2)))
253 (and (not (diff-unrolled (car diff-foo-bar) (unroll '())))
254 (not (diff-unrolled baz-unrolled (cdr diff-foo-bar))))))))
256 (pass-if "two two-element lists"
257 (let* ((foo (list 1 2))
258 (foo-unrolled (unroll foo))
260 (bar-unrolled (unroll bar))
261 (tst (append! foo bar))
262 (tst-unrolled (unroll tst))
263 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
264 (and (equal? tst '(1 2 3 4))
265 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
266 (not (diff-unrolled bar-unrolled (cdr diff-foo-tst))))))
268 (pass-if "three two-element lists"
269 (let* ((foo (list 1 2))
270 (foo-unrolled (unroll foo))
272 (bar-unrolled (unroll bar))
274 (baz-unrolled (unroll baz))
275 (tst (append! foo bar baz))
276 (tst-unrolled (unroll tst))
277 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
278 (and (equal? tst '(1 2 3 4 5 6))
279 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
280 (let* ((tst-unrolled-2 (cdr diff-foo-tst))
281 (diff-foo-bar (diff-unrolled bar-unrolled tst-unrolled-2)))
282 (and (not (diff-unrolled (car diff-foo-bar) (unroll '())))
283 (not (diff-unrolled baz-unrolled (cdr diff-foo-bar))))))))
285 (pass-if "empty list between non-empty lists"
286 (let* ((foo (list 1 2))
287 (foo-unrolled (unroll foo))
289 (bar-unrolled (unroll bar))
291 (baz-unrolled (unroll baz))
292 (tst (append! foo '() bar '() '() baz '() '() '()))
293 (tst-unrolled (unroll tst))
294 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
295 (and (equal? tst '(1 2 3 4 5 6))
296 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
297 (let* ((tst-unrolled-2 (cdr diff-foo-tst))
298 (diff-foo-bar (diff-unrolled bar-unrolled tst-unrolled-2)))
299 (and (not (diff-unrolled (car diff-foo-bar) (unroll '())))
300 (not (diff-unrolled baz-unrolled (cdr diff-foo-bar))))))))
302 (pass-if "list and improper list"
303 (let* ((foo (list 1 2))
304 (foo-unrolled (unroll foo))
306 (bar-unrolled (unroll bar))
307 (tst (append! foo bar))
308 (tst-unrolled (unroll tst))
309 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
310 (and (equal? tst '(1 2 3 . 4))
311 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
312 (not (diff-unrolled bar-unrolled (cdr diff-foo-tst))))))
314 (pass-if "list and circular list"
315 (let* ((foo (list 1 2))
316 (foo-unrolled (unroll foo))
318 (set-cdr! (cddr bar) (cdr bar))
319 (let* ((bar-unrolled (unroll bar))
320 (tst (append! foo bar))
321 (tst-unrolled (unroll tst))
322 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
323 (and (equal? (map (lambda (n x) (eqv? (list-ref tst n) x))
325 '(1 2 3 4 5 4 5 4 5))
326 '(#t #t #t #t #t #t #t #t #t))
327 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
328 (not (diff-unrolled bar-unrolled (cdr diff-foo-tst)))))))
330 (pass-if "list and non list object"
331 (let* ((foo (list 1 2))
332 (foo-unrolled (unroll foo))
334 (bar-unrolled (unroll bar))
335 (tst (append! foo bar))
336 (tst-unrolled (unroll tst))
337 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
338 (and (equal? tst '(1 2 . #(3 4)))
339 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
340 (not (diff-unrolled bar-unrolled (cdr diff-foo-tst))))))
342 (pass-if "several arbitrary lists"
343 (equal? (append! (list 1 2)
345 (list (list 5) (list 6))
348 (list (cons 12 13) 14)
358 (pass-if "list to itself"
359 (let* ((foo (list 1 2))
360 (foo-unrolled (unroll foo))
361 (tst (append! foo foo))
362 (tst-unrolled (unroll tst))
363 (diff-foo-tst (diff-unrolled foo-unrolled tst-unrolled)))
364 (and (equal? (map (lambda (n x) (eqv? (list-ref tst n) x))
367 '(#t #t #t #t #t #t))
368 (not (diff-unrolled (car diff-foo-tst) (unroll '())))
369 (eq? (caar (cdr diff-foo-tst)) circle-indicator)
370 (eq? (cdar (cdr diff-foo-tst)) foo))))
372 ;; Are wrong type arguments detected correctly?
374 (with-test-prefix "wrong argument"
376 (expect-fail "improper list and empty list"
377 (catch 'wrong-type-arg
379 (append! (cons 1 2) '())
384 (expect-fail "improper list and list"
385 (catch 'wrong-type-arg
387 (append! (cons 1 2) (list 3 4))
392 (expect-fail "list, improper list and list"
393 (catch 'wrong-type-arg
395 (append! (list 1 2) (cons 3 4) (list 5 6))
400 (expect-fail "circular list and empty list"
401 (let ((foo (list 1 2 3)))
402 (set-cdr! (cddr foo) (cdr foo))
405 (catch 'wrong-type-arg
414 (expect-fail "circular list and list"
415 (let ((foo (list 1 2 3)))
416 (set-cdr! (cddr foo) (cdr foo))
419 (catch 'wrong-type-arg
421 (append! foo (list 4 5))
428 (expect-fail "list, circular list and list"
429 (let ((foo (list 3 4 5)))
430 (set-cdr! (cddr foo) (cdr foo))
433 (catch 'wrong-type-arg
435 (append! (list 1 2) foo (list 6 7))
454 (with-test-prefix "list-ref"
456 ;; Is documentation available?
458 (pass-if "documented?" (object-documentation list-ref))
460 (with-test-prefix "argument error"
462 (with-test-prefix "non list argument"
465 (with-test-prefix "improper list argument"
468 (with-test-prefix "non integer index"
471 (with-test-prefix "index out of range"
473 (with-test-prefix "empty list"
499 (with-test-prefix "non-empty list"
501 (pass-if "index > length"
520 (with-test-prefix "list-set!"
522 ;; Is documentation available?
524 (pass-if "documented?" (object-documentation list-set!))
526 (with-test-prefix "argument error"
528 (with-test-prefix "non list argument"
531 (with-test-prefix "improper list argument"
534 (with-test-prefix "read-only list argument"
537 (with-test-prefix "non integer index"
540 (with-test-prefix "index out of range"
542 (with-test-prefix "empty list"
547 (list-set! (list) 0 #t)
555 (list-set! (list) 1 #t)
563 (list-set! (list) -1 #t)
568 (with-test-prefix "non-empty list"
570 (pass-if "index > length"
573 (list-set! (list 1) 1 #t)
581 (list-set! (list 1) -1 #t)
595 (with-test-prefix "list-cdr-set!"
597 ;; Is documentation available?
599 (pass-if "documented?" (object-documentation list-cdr-set!))
601 (with-test-prefix "argument error"
603 (with-test-prefix "non list argument"
606 (with-test-prefix "improper list argument"
609 (with-test-prefix "read-only list argument"
612 (with-test-prefix "non integer index"
615 (with-test-prefix "index out of range"
617 (with-test-prefix "empty list"
622 (list-cdr-set! (list) 0 #t)
630 (list-cdr-set! (list) 1 #t)
638 (list-cdr-set! (list) -1 #t)
643 (with-test-prefix "non-empty list"
645 (pass-if "index > length"
648 (list-cdr-set! (list 1) 1 #t)
656 (list-cdr-set! (list 1) -1 #t)