gensym.lsp 3.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Jun 14 05:43:47 2003
;;;; Contains: Tests of GENSYM

(in-package :cl-test)

;;; Gensym returns unique symbols
(deftest gensym.1
  (equal (gensym) (gensym))
  nil)

;;; Gensym returns symbols with distinct print names
(deftest gensym.2
  (string= (symbol-name (gensym))
sds's avatar
sds committed
16
           (symbol-name (gensym)))
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
  nil)

;;; Gensym uses the *gensym-counter* special variable,
;;; but does not increment it until after the symbol
;;; has been created.
(deftest gensym.3
  (let ((*gensym-counter* 1))
     (symbol-name (gensym)))
  #.(string '#:g1))

;;; Gensym uses the string argument instead of the default
(deftest gensym.4
  (let ((*gensym-counter* 1327))
    (symbol-name (gensym "FOO")))
  "FOO1327")

;;; The symbol returned by gensym should be unbound
(deftest gensym.5
  (boundp (gensym))
  nil)

;;; The symbol returned by gensym should have no function binding
(deftest gensym.6
  (fboundp (gensym))
  nil)

;;; The symbol returned by gensym should have no property list
(deftest gensym.7
  (symbol-plist (gensym))
  nil)

;;; The symbol returned by gensym should be uninterned
(deftest gensym.8
  (symbol-package (gensym))
  nil)

;;; *gensym-counter* is incremented by gensym
(deftest gensym.9
  (let ((*gensym-counter* 12345))
    (gensym)
    *gensym-counter*)
  12346)

;;; Gensym works when *gensym-counter* is Really Big
;;; (and does not increment the counter until after creating
;;; the symbol.)
(deftest gensym.10
  (let ((*gensym-counter* 1234567890123456789012345678901234567890))
    (symbol-name (gensym)))
  #.(string '#:g1234567890123456789012345678901234567890))

;;; gensym increments Really Big values of *gensym-counter*
(deftest gensym.11
  (let ((*gensym-counter* 12345678901234567890123456789012345678901234567890))
    (gensym)
    *gensym-counter*)
  12345678901234567890123456789012345678901234567891)

;;; Gensym uses an integer argument instead of the counter
(deftest gensym.12
  (let ((*gensym-counter* 10))
    (symbol-name (gensym 123)))
  #.(string '#:g123))

;;; When given an integer argument, gensym does not increment the
;;; *gensym-counter*
(deftest gensym.13
  (let ((*gensym-counter* 10))
    (gensym 123)
    *gensym-counter*)
  10)

89
90
91
92
93
94
95
;;; GENSYM counter is a non-negative integer
(deftest gensym-counter.1
  (and (integerp *gensym-counter*)
       (>= *gensym-counter* 0)
       t)
  t)

96
97
98
99
100
101
;;; Check response to erroneous arguments
;;; Note! NIL is not the same as no argument
;;; gensym should be implemented so that its only
;;; argument defaults to "G", with NIL causing an error.

(deftest gensym.error.1
102
103
  (check-type-error #'gensym #'(lambda (x) (typep x '(or string unsigned-byte))))
  nil)
104
105

(deftest gensym.error.7
106
107
  (signals-error (gensym 10 'foo) program-error)
  t)
108
109

(deftest gensym.error.8
110
111
112
113
114
115
116
  (signals-error (locally (gensym t) t) type-error)
  t)

(deftest gensym.error.9
  (signals-error (gensym "FOO" nil) program-error)
  t)

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;;;

(deftest gensym.error.10
    (signals-error
     (let ((*gensym-counter* -1))
       (gensym))
     type-error)
  t)

(deftest gensym.error.11
    (signals-error
     (let ((*gensym-counter* (1- most-negative-fixnum)))
       (gensym))
     type-error)
  t)

(deftest gensym.error.12
    (signals-error
     (let ((*gensym-counter* 'defun))
       (gensym))
     type-error)
  t)