do-all-symbols.lsp 3.13 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Feb 21 18:27:22 2004
;;;; Contains: Tests of DO-ALL-SYMBOLS

6

7 8 9 10 11

(def-macro-test do-all-symbols.error.1
  (do-all-symbols (x)))

;;; FIXME  Add tests for non-error cases
pfdietz's avatar
pfdietz committed
12 13 14 15 16 17 18 19

(deftest do-all-symbols.1
  (let ((symbols nil))
    (do-all-symbols (sym) (push sym symbols))
    (let ((hash (make-hash-table :test 'eq)))
      (with-package-iterator
       (f (list-all-packages) :internal :external :inherited)
       (loop
sds's avatar
sds committed
20 21 22
        (multiple-value-bind (found sym) (f)
          (unless found (return))
          (setf (gethash sym hash) t))))
pfdietz's avatar
pfdietz committed
23 24 25 26 27 28 29 30 31 32 33 34 35
      ;; hash now contains all symbols accessible in any package
      ;; Check that all symbols from DO-ALL-SYMBOLS are in this
      ;; package
      (loop for s in symbols unless (gethash s hash) collect s)))
  nil)

;; This is the converse of do-all-symbols.1
(deftest do-all-symbols.2
  (let ((symbols nil))
    (with-package-iterator
     (f (list-all-packages) :internal :external :inherited)
     (loop
      (multiple-value-bind (found sym) (f)
sds's avatar
sds committed
36 37
        (unless found (return))`
        (push sym symbols))))
pfdietz's avatar
pfdietz committed
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
    (let ((hash (make-hash-table :test 'eq)))
      (do-all-symbols (s) (setf (gethash s hash) t))
      (loop for s in symbols unless (gethash s hash) collect s)))
  nil)

(deftest do-all-symbols.3
  (let ((sym (gensym)))
    (do-all-symbols (s t) (assert (not (eq s sym)))))
  t)

(deftest do-all-symbols.4
  (let ((x :bad))
    (do-all-symbols (x x)))
  nil)

(deftest do-all-symbols.5
  (block nil
    (do-all-symbols (x (return :bad)))
    :good)
  :good)

(deftest do-all-symbols.6
  (do-all-symbols (x :bad) (return :good))
  :good)

(deftest do-all-symbols.7
  (block done
    (tagbody
     (do-all-symbols (x (return-from done :good))
       (go 1)
       (return-from done :bad1)
       1)
     1
     (return-from done :bad2)))
  :good)

(deftest do-all-symbols.8
  (block done
    (tagbody
     (do-all-symbols (x (return-from done :good))
       (go tag)
       (return-from done :bad1)
       tag)
     tag
     (return-from done :bad2)))
  :good)

85
;;; Test that do-all-symbols accepts declarations
pfdietz's avatar
pfdietz committed
86

87 88
(deftest do-all-symbols.9
  (let ((x 0)
sds's avatar
sds committed
89
        (y 1))
90 91 92 93 94 95 96
    (do-all-symbols (z nil)
      (declare (type (integer * 0) x))
      (declare (type (integer 1 *) y))
      (declare (ignore z))
      (when (< x y) (return :good))))
  :good)

97

98 99 100 101 102
;;; Default return is NIL

(deftest do-all-symbols.10
  (do-all-symbols (s) (declare (ignore s)))
  nil)
103 104 105 106 107 108 109 110

;;; Free declaration scope tests

(deftest do-all-symbols.11
  (let ((x :good))
    (declare (special x))
    (let ((x :bad))
      (do-all-symbols (s x)
sds's avatar
sds committed
111
        (declare (special x)))))
112
  :good)
113 114 115 116 117 118 119

;;; Executing a return actually terminates the loop

(deftest do-all-symbols.12
  (let ((should-have-returned nil))
    (block done
      (do-all-symbols (s :bad1)
sds's avatar
sds committed
120 121 122 123
        (when should-have-returned
          (return-from done :bad2))
        (setq should-have-returned t)
        (return :good))))
124
  :good)
125 126 127 128 129 130 131 132 133

;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest do-all-symbols.13
  (macrolet
   ((%m (z) z))
   (do-all-symbols (s (expand-in-current-env (%m :good)))))
  :good)