1 ;;;; $Id: elib-test.el,v 1.1.1.1 1998-10-07 11:10:57 jareth Exp $
2 ;;;; This file contains functions for testing all packages in ELIB.
4 ;; Copyright (C) 1991-1995 Free Software Foundation
6 ;; Author: Per Cederqvist <ceder@lysator.liu.se>
7 ;; Inge Wallin <inge@lysator.liu.se>
8 ;; Maintainer: elib-maintainers@lysator.liu.se
11 ;;;; This file is part of the GNU Emacs lisp library, Elib.
13 ;;;; GNU Elib is free software; you can redistribute it and/or modify
14 ;;;; it under the terms of the GNU General Public License as published by
15 ;;;; the Free Software Foundation; either version 2, or (at your option)
16 ;;;; any later version.
18 ;;;; GNU Elib is distributed in the hope that it will be useful,
19 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;;; GNU General Public License for more details.
23 ;;;; You should have received a copy of the GNU General Public License
24 ;;;; along with GNU Elib; see the file COPYING. If not, write to
25 ;;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;;;; Boston, MA 02111-1307, USA
33 ;;; ================================================================
34 ;;; Routines common to all the ELIB packages.
37 (defun test-package (package-test-suite)
38 "Run through a list of test cases.
39 Each test case is a list of one of the following types:
40 (test-case error expected-error)
41 (test-case result expected-result)
42 (test-case dont-care)"
44 (interactive "Xtest suite: ")
49 (if (not (test-a-case ele testno))
51 (setq testno (1+ testno))))
54 (princ "None of the errors have been found.\n"))))
57 (defun test-a-case (test-case testno)
58 ;; Return t if ok and nil if error.
60 (let* ((test-data (car test-case))
61 (result-type (car (cdr test-case)))
62 (expected-result (if (eq result-type 'dont-care)
64 (car (cdr (cdr test-case)))))
67 ((eq result-type 'error)
72 (setq result (eval test-data))
73 (princ (format "%3d: An error should have occured here: %s\n"
75 (princ (format " The result was: %s\n" result))
77 (error (if (equal error expected-result)
79 (princ (format "%3d: Wrong error.\n" testno))
80 (princ (format " %s\n" error))
81 (princ (format " The error should have been:\n %s\n"
85 ((or (eq result-type 'result)
86 (eq result-type 'dont-care))
90 (let ((result (eval test-data)))
91 (if (or (eq result-type 'dont-care)
92 (equal result expected-result))
96 "%3d: Wrong result while evaluating\n%s\nThe result: %s\n"
97 testno test-case result))
98 (princ (format " The result should have been: %s\n"
104 "%3d: An error occured while evaluating\n%s\nThe error:\n %s\n"
105 testno test-case error))
106 (if (eq result-type 'result)
107 (princ (format " The result should have been:\n %s\n"
111 (princ (format "%3d: Misformed test case:\n %s\n"
112 testno test-case))))))
115 ;;; ================================================================
116 ;;; Tests for the function variety of the stack.
120 '(((setq stk (stack-create)) result (STACK))
121 ((setq xyz 'xyz) result xyz)
123 ((stack-p stk) result t)
124 ((stack-empty stk) result t)
125 ((stack-pop stk) result nil)
126 ((stack-top stk) result nil)
127 ((stack-length stk) result 0)
128 ((stack-all stk) result nil)
130 ((stack-p xyz) result nil)
131 ((stack-empty xyz) error (wrong-type-argument listp xyz))
132 ((stack-pop xyz) error (wrong-type-argument listp xyz))
133 ((stack-top xyz) error (wrong-type-argument listp xyz))
134 ((stack-length xyz) error (wrong-type-argument listp xyz))
135 ((stack-all xyz) error (wrong-type-argument listp xyz))
137 ((setq stk2 (stack-copy stk)) result (STACK))
138 ((eq stk stk2) result nil)
139 ((equal stk stk2) result t)
141 ((stack-push stk 'a) result (a))
142 ((stack-top stk) result a)
143 ((stack-all stk) result (a))
144 ((stack-empty stk) result nil)
145 ((stack-length stk) result 1)
147 ((stack-push stk 'b) result (b a))
148 ((stack-all stk) result (b a))
149 ((stack-length stk) result 2)
150 ((stack-nth stk -1) result b)
151 ((stack-nth stk 0) result b)
152 ((stack-nth stk 1) result a)
153 ((stack-nth stk 2) result nil)
154 ((stack-nth stk 3) result nil)
156 ((setq stk2 (stack-copy stk)) result (STACK b a))
157 ((eq stk stk2) result nil)
158 ((equal stk stk2) result t)
160 ((stack-pop stk) result b)
161 ((stack-all stk) result (a))
162 ((stack-pop stk) result a)
163 ((stack-all stk) result nil)
164 ((stack-empty stk) result t)
166 ((stack-push stk 'a) result (a))
167 ((stack-push stk 'b) result (b a))
168 ((stack-clear stk) result nil)
169 ((stack-p stk) result t)
170 ((stack-empty stk) result t)
171 ((stack-pop stk) result nil)
172 ((stack-top stk) result nil)
173 ((stack-length stk) result 0)
174 ((stack-all stk) result nil)
176 (setq stack-m-tests stack-f-tests)
179 (defun test-stack-f ()
181 (let ((load-path '(".")))
183 (with-output-to-temp-buffer "*Elib-test*"
185 (test-package stack-f-tests))))
189 ;; Use the same tests for stack-m as for stack-f
191 (defun test-stack-m ()
193 (let ((load-path '(".")))
195 (with-output-to-temp-buffer "*Elib-test*"
197 (test-package stack-m-tests))))
202 ;;; ================================================================
203 ;;; Tests for the function variety of the queue.
207 '(((setq que (queue-create)) result (QUEUE nil))
208 ((setq xyz 'xyz) result xyz)
210 ((queue-p que) result t)
211 ((queue-empty que) result t)
212 ((queue-dequeue que) result nil)
213 ((queue-first que) result nil)
214 ((queue-length que) result 0)
215 ((queue-all que) result nil)
217 ((queue-p xyz) result nil)
218 ((queue-empty xyz) error (wrong-type-argument listp xyz))
219 ((queue-dequeue xyz) error (wrong-type-argument listp xyz))
220 ((queue-first xyz) error (wrong-type-argument listp xyz))
221 ((queue-length xyz) error (wrong-type-argument listp xyz))
222 ((queue-all xyz) error (wrong-type-argument listp xyz))
224 ((setq que2 (queue-copy que)) result (QUEUE nil))
225 ((eq que que2) result nil)
226 ((equal que que2) result t)
228 ((queue-enqueue que 'a) result (a))
229 ((queue-first que) result a)
230 ((queue-all que) result (a))
231 ((queue-empty que) result nil)
232 ((queue-length que) result 1)
234 ((queue-enqueue que 'b) result (b))
235 ((queue-all que) result (a b))
236 ((queue-length que) result 2)
237 ((queue-nth que -1) result a)
238 ((queue-nth que 0) result a)
239 ((queue-nth que 1) result b)
240 ((queue-nth que 2) result nil)
241 ((queue-nth que 3) result nil)
243 ((setq que2 (queue-copy que)) result (QUEUE (a b) b))
244 ((eq que que2) result nil)
245 ((equal que que2) result t)
247 ((queue-dequeue que) result a)
248 ((queue-all que) result (b))
249 ((queue-dequeue que) result b)
250 ((queue-all que) result nil)
251 ((queue-empty que) result t)
253 ((queue-enqueue que 'a) dont-care)
254 ((queue-enqueue que 'b) dont-care)
255 ((queue-clear que) dont-care)
256 ((queue-p que) result t)
257 ((queue-empty que) result t)
258 ((queue-dequeue que) result nil)
259 ((queue-first que) result nil)
260 ((queue-length que) result 0)
261 ((queue-all que) result nil)
263 (setq queue-m-tests queue-f-tests)
266 (defun test-queue-f ()
268 (let ((load-path '(".")))
270 (with-output-to-temp-buffer "*Elib-test*"
272 (test-package queue-f-tests))))
275 ;; Use the same tests for queue-m as for queue-f
277 (defun test-queue-m ()
279 (let ((load-path '(".")))
281 (with-output-to-temp-buffer "*Elib-test*"
283 (test-package queue-m-tests))))
286 ;;; ================================================================
287 ;;; Tests for the string package.
290 '(((string-replace-match "foo" "kallefoopelle" "bar")
291 result "kallebarpelle")
292 ((string-replace-match "[0-9]" "le127" "bar\\\\\\&\\\\")
293 result "lebar\\1\\27")
294 ((string-replace-match "f\\([^f]*\\)f\\([^f]*\\)f"
295 "abcdefghijfdddfojsan" "\\2\\1")
296 result "abcdedddghijojsan")
297 ((string-split "," "foo,bar,baz") result ("foo" "bar" "baz"))
298 ((string-split "," "foo,bar,baz" 2) result ("foo" "bar"))))
301 (defun test-string ()
303 (let ((load-path '(".")))
305 (with-output-to-temp-buffer "*Elib-test*"
307 (test-package string-tests))))
309 ;;; ================================================================
310 ;;; Tests for the doubly linked list.
314 ((setq a (dll-create)) dont-care)
316 ((dll-p nil) result nil)
317 ((setq b (dll-create-from-list (list 'a 'b 'c))) dont-care)
318 ((setq c (dll-copy b)) dont-care)
319 ((dll-enter-first c 'f) dont-care)
320 ((dll-enter-last c 'l) dont-care)
321 ((setq n (dll-nth c 2)) dont-care)
322 ((dll-element c n) result b)
323 ((dll-enter-after c n 'after) dont-care)
324 ((dll-element c n) result b)
325 ((dll-enter-before c n 'before) dont-care)
326 ((dll-element c n) result b)
327 ((dll-first a) result nil)
328 ((dll-first b) result a)
329 ((dll-first c) result f)
330 ((dll-empty b) result nil)
331 ((dll-last a) result nil)
332 ((dll-last b) result c)
333 ((dll-last c) result l)
334 ((dll-element c n) result b)
335 ((dll-all c) result (f a before b after c l))
336 ((dll-element c (dll-next c n)) result after)
337 ((dll-element c (dll-previous c n)) result before)
338 ((dll-next b (dll-nth b 2)) result nil)
339 ((dll-next b (dll-nth b -1)) result nil)
340 ((dll-previous c (dll-nth c 0)) result nil)
341 ((dll-all a) result nil)
342 ((dll-all b) result (a b c))
343 ((dll-all c) result (f a before b after c l))
344 ((dll-delete c n) result b)
345 ((dll-all c) result (f a before after c l))
346 ((setq d (dll-copy c)) dont-care)
347 ((dll-delete-first b) result a)
348 ((dll-all b) result (b c))
349 ((dll-delete-last b) result c)
350 ((dll-all b) result (b))
351 ((dll-delete-last b) result b)
352 ((dll-all b) result nil)
353 ((dll-clear c) dont-care)
354 ((dll-all c) result nil)
355 ((dll-clear c) dont-care)
356 ((dll-all c) result nil)
357 ((dll-p n) result nil)
360 ((dll-empty a) result t)
361 ((dll-empty b) result t)
362 ((dll-all d) result (f a before after c l))
363 ((dll-previous d (dll-nth d 0)) result nil)
365 (dll-map-reverse (function (lambda (x) (setq xxx (cons x xxx)))) d)
366 xxx) result (f a before after c l))
368 (dll-map (function (lambda (x) (setq xxx (cons x xxx)))) d)
369 xxx) result (l c after before a f))
370 ((dll-filter d (function (lambda (x)
372 (equal x 'before))))) dont-care)
373 ((dll-all d) result (before after))
374 ((dll-length a) result 0)
375 ((dll-length d) result 2)
376 ((setq a (dll-create-from-list (list 7 2 843 2 8 19 7 289))) dont-care)
377 ((dll-sort a (function <)) dont-care)
378 ((dll-all a) result (2 2 7 7 8 19 289 843))
381 (setq dll-debug-tests dll-tests)
385 (let ((load-path '(".")))
387 (with-output-to-temp-buffer "*Elib-test*"
389 (test-package dll-tests))))
391 (defun test-dll-debug ()
393 (let ((load-path '(".")))
394 (load-library "dll-debug")
395 (with-output-to-temp-buffer "*Elib-test*"
396 (princ "dll-debug:\n")
397 (test-package dll-debug-tests))))
399 ;;; ================================================================
400 ;;; Tests for the cookie mode.
404 ((kill-buffer (get-buffer-create "*Cookie-buffer*")) dont-care)
405 ((setq a (collection-create "*Cookie-buffer*"
409 ((collection-empty a) result t)
410 ((collection-length a) result 0)
411 ((collection-list-cookies a) result nil)
412 ((cookie-enter-first a "1") dont-care)
413 ((cookie-enter-last a "2") dont-care)
414 ((collection-append-cookies a '("3" "4")) dont-care)
415 ((collection-empty a) result nil)
416 ((collection-length a) result 4)
417 ((collection-list-cookies a) result ("1" "2" "3" "4"))
418 ((eq (collection-buffer a) (get-buffer "*Cookie-buffer*"))
420 ((save-window-excursion
421 (set-buffer (collection-buffer a))
423 result "Header\n1\n2\n3\n4\nFooter\n")
424 ((eq (tin-locate a 1) (tin-nth a 0)) result t)
425 ((eq (tin-locate a 2) (tin-nth a 0)) result t)
426 ((eq (tin-locate a 7) (tin-nth a 0)) result t)
427 ((eq (tin-locate a 8) (tin-nth a 0)) result t)
428 ((eq (tin-locate a 9) (tin-nth a 0)) result t)
429 ((eq (tin-locate a 9) (tin-nth a 1)) result nil)
430 ((eq (tin-locate a 10) (tin-nth a 0)) result nil)
431 ((eq (tin-locate a 10) (tin-nth a 1)) result t)
432 ((eq (tin-locate a 12) (tin-nth a 2)) result t)
433 ((eq (tin-locate a 14) (tin-nth a 3)) result t)
434 ((eq (tin-locate a 16) (tin-nth a 3)) result t)
435 ((eq (tin-locate a 18) (tin-nth a 3)) result t)
437 ;; Test that last-tin is reset when collection-filter-cookies is called.
438 ((tin-goto a (tin-locate a 12)) dont-care)
439 ((collection-filter-cookies
440 a (function (lambda (x) (not (string= x "3"))))) dont-care)
441 ((collection-list-cookies a) result ("1" "2" "4"))
442 ((tin-goto-previous a 13 1) dont-care)
446 (defun test-cookie ()
449 (let ((load-path '(".")))
450 (load-library "cookie")
451 (with-output-to-temp-buffer "*Elib-test*"
453 (test-package cookie-tests))))
456 ;;; ================================================================
457 ;;; Functions for testing a binary tree.
458 ;;; (not yet working)
464 (defun bintree-print (tree)
465 "Print the binary tree TREE on stdout."
467 (do-bintree-print (elib-bintree-root tree) 0)
471 (defun do-bintree-print (root level)
472 "Print the binary tre starting with the root node ROOT at level LEVEL."
475 (if (elib-node-left root)
476 (do-bintree-print (elib-node-left root) (1+ level)))
480 (setq lvl (1- lvl))))
481 (princ (elib-node-data root))
483 (if (elib-node-right root)
484 (do-bintree-print (elib-node-right root) (1+ level)))))
487 (defun bintree-enter-list (tree list)
488 "Enter into TREE all elements of LIST."
489 (mapcar (function (lambda (x) (elib-bintree-enter tree x)))
493 ;;; ================================================================
494 ;;; Functions for testing an AVL tree.
495 ;;; (not yet working)
501 (defun avltree-print (tree)
502 "Print the binary tree TREE on stdout."
504 (do-avltree-print (elib-avl-root tree) 0)
508 (defun do-avltree-print (root level)
509 "Print the binary tre starting with the root node ROOT at level LEVEL."
512 (if (elib-node-left root)
513 (do-avltree-print (elib-node-left root) (1+ level)))
517 (setq lvl (1- lvl))))
518 (princ (elib-node-data root))
520 (princ (elib-avl-node-balance root))
522 (if (elib-node-right root)
523 (do-avltree-print (elib-node-right root) (1+ level)))))
526 (defun avltree-enter-list (tree list)
527 "Enter into TREE all elements of LIST."
528 (mapcar (function (lambda (x) (elib-avltree-enter tree x)))
532 ;;; ================================================================
533 ;;; Test the entire Elib.
536 ;; Note that some tests are commented away. That is because they
537 ;; are not yet written.
546 ;; Check cookie immediately after dll-debug, so that we can
550 ;; Check cookie again, in case dll-debug behaves differently.
560 (defun elib-test-all ()
561 "Test all packages within ELIB for errors."
563 (let ((load-path '(".")))
564 (with-output-to-temp-buffer "*Elib-test*"
566 (lambda (package-name)
567 (load-library package-name)
568 (princ (format "%s:\n" package-name))
569 (test-package (eval (intern (concat package-name
573 ;;; elib-test.el ends here