format-s.lsp 8.6 KB
Newer Older
1 2 3 4 5 6
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Aug  3 11:55:07 2004
;;;; Contains: Test of the ~S format directive

(in-package :cl-test)
7
(compile-and-load "printer-aux.lsp")
8 9 10

(deftest format.s.1
  (let ((*print-readably* nil)
sds's avatar
sds committed
11
        (*print-case* :upcase))
12 13 14
    (format nil "~s" nil))
  "NIL")

pfdietz's avatar
pfdietz committed
15 16
(deftest formatter.s.1
  (let ((*print-readably* nil)
sds's avatar
sds committed
17
        (*print-case* :upcase))
pfdietz's avatar
pfdietz committed
18 19 20
    (formatter-call-to-string (formatter "~s") nil))
  "NIL")

21 22
(def-format-test format.s.2
  "~:s" (nil) "()")
23 24 25

(deftest format.s.3
  (let ((*print-readably* nil)
sds's avatar
sds committed
26
        (*print-case* :upcase))
27 28 29
    (format nil "~:s" '(nil)))
  "(NIL)")

pfdietz's avatar
pfdietz committed
30 31
(deftest formatter.s.3
  (let ((*print-readably* nil)
sds's avatar
sds committed
32
        (*print-case* :upcase))
pfdietz's avatar
pfdietz committed
33 34 35
    (formatter-call-to-string (formatter "~:s") '(nil)))
  "(NIL)")

36 37
(deftest format.s.4
  (let ((*print-readably* nil)
sds's avatar
sds committed
38
        (*print-case* :downcase))
39 40 41
    (format nil "~s" 'nil))
  "nil")

pfdietz's avatar
pfdietz committed
42 43
(deftest formatter.s.4
  (let ((*print-readably* nil)
sds's avatar
sds committed
44
        (*print-case* :downcase))
pfdietz's avatar
pfdietz committed
45 46 47
    (formatter-call-to-string (formatter "~s") 'nil))
  "nil")

48 49
(deftest format.s.5
  (let ((*print-readably* nil)
sds's avatar
sds committed
50
        (*print-case* :capitalize))
51 52 53
    (format nil "~s" 'nil))
  "Nil")

pfdietz's avatar
pfdietz committed
54 55
(deftest formatter.s.5
  (let ((*print-readably* nil)
sds's avatar
sds committed
56
        (*print-case* :capitalize))
pfdietz's avatar
pfdietz committed
57 58 59
    (formatter-call-to-string (formatter "~s") 'nil))
  "Nil")

60 61
(def-format-test format.s.6
  "~:s" (#(nil)) "#(NIL)")
62 63

(deftest format.s.7
pfdietz's avatar
pfdietz committed
64 65 66 67
  (let ((fn (formatter "~S")))
    (with-standard-io-syntax
     (let ((*print-readably* nil))
       (loop for c across +standard-chars+
sds's avatar
sds committed
68 69 70 71 72
             for s = (format nil "~S" c)
             for s2 = (formatter-call-to-string fn c)
             for c2 = (read-from-string s)
             unless (and (eql c c2) (string= s s2))
             collect (list c s c2 s2)))))
73 74 75
  nil)

(deftest format.s.8
pfdietz's avatar
pfdietz committed
76 77 78 79
  (let ((fn (formatter "~s")))
    (with-standard-io-syntax
     (let ((*print-readably* nil))
       (loop with count = 0
sds's avatar
sds committed
80 81 82 83 84 85 86 87 88 89 90 91
             for i from 0 below (min #x10000 char-code-limit)
             for c = (code-char i)
             for s1 = (and c (format nil "#\\~:c" c))
             for s2 = (and c (format nil "~S" c))
             for s3 = (formatter-call-to-string fn c)
             unless (or (null c)
                        (graphic-char-p c)
                        (and (string= s1 s2) (string= s2 s3)))
              do (incf count) and collect (list c s1 s2)
             when (> count 100)
              collect "count limit exceeded"
              and do (loop-finish)))))
92 93 94 95 96 97 98 99
  nil)

(deftest format.s.9
  (with-standard-io-syntax
   (let ((*print-readably* nil))
     (apply
      #'values
      (loop for i from 1 to 10
sds's avatar
sds committed
100 101 102 103 104 105
            for fmt = (format nil "~~~d@s" i)
            for s = (format nil fmt nil)
            for fn = (eval `(formatter ,fmt))
            for s2 = (formatter-call-to-string fn nil)
            do (assert (string= s s2))
            collect s))))
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  "NIL"
  "NIL"
  "NIL"
  " NIL"
  "  NIL"
  "   NIL"
  "    NIL"
  "     NIL"
  "      NIL"
  "       NIL")

(deftest format.s.10
  (with-standard-io-syntax
   (let ((*print-readably* nil))
     (apply
      #'values
      (loop for i from 1 to 10
sds's avatar
sds committed
123 124 125 126 127 128
            for fmt = (format nil "~~~dS" i)
            for s = (format nil fmt nil)
            for fn = (eval `(formatter ,fmt))
            for s2 = (formatter-call-to-string fn nil)
            do (assert (string= s s2))
            collect s))))
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  "NIL"
  "NIL"
  "NIL"
  "NIL "
  "NIL  "
  "NIL   "
  "NIL    "
  "NIL     "
  "NIL      "
  "NIL       ")

(deftest format.s.11
  (with-standard-io-syntax
   (let ((*print-readably* nil))
     (apply
      #'values
      (loop for i from 1 to 10
sds's avatar
sds committed
146 147 148 149 150 151
            for fmt = (format nil "~~~d@:S" i)
            for s = (format nil fmt nil)
            for fn = (eval `(formatter ,fmt))
            for s2 = (formatter-call-to-string fn nil)
            do (assert (string= s s2))
            collect s))))
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  "()"
  "()"
  " ()"
  "  ()"
  "   ()"
  "    ()"
  "     ()"
  "      ()"
  "       ()"
  "        ()")

(deftest format.s.12
  (with-standard-io-syntax
   (let ((*print-readably* nil))
     (apply
      #'values
      (loop for i from 1 to 10
sds's avatar
sds committed
169 170 171 172 173 174
            for fmt = (format nil "~~~d:s" i)
            for s = (format nil fmt nil)
            for fn = (eval `(formatter ,fmt))
            for s2 = (formatter-call-to-string fn nil)
            do (assert (string= s s2))
            collect s))))
175 176 177 178 179 180 181 182 183 184 185 186 187
  "()"
  "()"
  "() "
  "()  "
  "()   "
  "()    "
  "()     "
  "()      "
  "()       "
  "()        ")

(deftest format.s.13
  (with-standard-io-syntax
pfdietz's avatar
pfdietz committed
188
   (let ((*print-readably* nil)
sds's avatar
sds committed
189
         (fn (formatter "~V:s")))
190 191 192
     (apply
      #'values
      (loop for i from 1 to 10
sds's avatar
sds committed
193 194 195 196
            for s = (format nil "~v:S" i nil)
            for s2 = (formatter-call-to-string fn i nil)
            do (assert (string= s s2))
            collect s))))
197 198 199 200 201 202 203 204 205 206 207 208 209
  "()"
  "()"
  "() "
  "()  "
  "()   "
  "()    "
  "()     "
  "()      "
  "()       "
  "()        ")

(deftest format.s.14
  (with-standard-io-syntax
pfdietz's avatar
pfdietz committed
210
   (let ((*print-readably* nil)
sds's avatar
sds committed
211
         (fn (formatter "~V@:s")))
212 213 214
     (apply
      #'values
      (loop for i from 1 to 10
sds's avatar
sds committed
215 216 217 218
            for s = (format nil "~v:@s" i nil)
            for s2 = (formatter-call-to-string fn i nil)
            do (assert (string= s s2))
            collect s))))
219 220 221 222 223 224 225 226 227 228 229
  "()"
  "()"
  " ()"
  "  ()"
  "   ()"
  "    ()"
  "     ()"
  "      ()"
  "       ()"
  "        ()")

230 231
(def-format-test format.s.15
  "~vS" (nil nil) "NIL")
232

233 234
(def-format-test format.s.16
  "~v:S" (nil nil) "()")
235

236 237
(def-format-test format.s.17
  "~@S" (nil) "NIL")
238

239 240
(def-format-test format.s.18
  "~v@S" (nil nil) "NIL")
241

242 243
(def-format-test format.s.19
  "~v:@s" (nil nil) "()")
244

245 246
(def-format-test format.s.20
  "~v@:s" (nil nil) "()")
247 248 249

;;; With colinc specified

250 251
(def-format-test format.s.21
  "~3,1s" (nil) "NIL")
252

253 254
(def-format-test format.s.22
  "~4,3s" (nil) "NIL   ")
255

256 257
(def-format-test format.s.23
  "~3,3@s" (nil) "NIL")
258

259 260
(def-format-test format.s.24
  "~4,4@s" (nil) "    NIL")
261

262 263
(def-format-test format.s.25
  "~5,3@s" (nil) "   NIL")
264

265 266
(def-format-test format.s.26
  "~5,3S" (nil) "NIL   ")
267

268 269
(def-format-test format.s.27
  "~7,3@s" (nil) "      NIL")
270

271 272
(def-format-test format.s.28
  "~7,3S" (nil) "NIL      ")
273 274 275 276 277 278

;;; With minpad

(deftest format.s.29
  (with-standard-io-syntax
   (let ((*print-readably* nil)
sds's avatar
sds committed
279 280
         (*package* (find-package :cl-test))
         (fn (formatter "~V,,2s")))
281
     (loop for i from -4 to 10
sds's avatar
sds committed
282 283 284 285
           for s = (format nil "~v,,2S" i 'ABC)
           for s2 = (formatter-call-to-string fn i 'ABC)
           do (assert (string= s s2))
           collect s)))
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  ("ABC  "
   "ABC  "
   "ABC  "
   "ABC  "
   "ABC  "
   "ABC  "
   "ABC  "
   "ABC  "
   "ABC  "
   "ABC  "
   "ABC   "
   "ABC    "
   "ABC     "
   "ABC      "
   "ABC       "))

302 303
(def-format-test format.s.30
  "~3,,+2S" ('ABC) "ABC  ")
304

305 306
(def-format-test format.s.31
  "~3,,0S" ('ABC) "ABC")
307

308 309
(def-format-test format.s.32
  "~3,,-1S" ('ABC) "ABC")
310

311 312
(def-format-test format.s.33
  "~3,,0S" ('ABCD) "ABCD")
313

314 315
(def-format-test format.s.34
  "~3,,-1S" ('ABCD) "ABCD")
316 317 318

;;; With padchar

319 320
(def-format-test format.s.35
  "~4,,,'XS" ('AB) "ABXX")
321

322 323
(def-format-test format.s.36
  "~4,,,s" ('AB) "AB  ")
324

325 326
(def-format-test format.s.37
  "~4,,,'X@s" ('AB) "XXAB")
327

328 329
(def-format-test format.s.38
  "~4,,,@S" ('AB) "  AB")
330

331 332
(def-format-test format.s.39
  "~10,,,vS" (nil 'ABCDE) "ABCDE     ")
333

334 335
(def-format-test format.s.40
  "~10,,,v@S" (nil 'ABCDE) "     ABCDE")
336

337 338
(def-format-test format.s.41
  "~10,,,vs" (#\* 'ABCDE) "ABCDE*****")
339

340 341
(def-format-test format.s.42
  "~10,,,v@s" (#\* 'ABCDE) "*****ABCDE")
342 343 344

;;; Other tests

345 346
(def-format-test format.s.43
  "~3,,vS" (nil 246) "246")
347 348 349 350

(deftest format.s.44
  (with-standard-io-syntax
   (let ((*print-readably* nil)
sds's avatar
sds committed
351 352
         (*package* (find-package :cl-test))
         (fn (formatter "~3,,vs")))
353
     (loop for i from 0 to 6
sds's avatar
sds committed
354 355 356 357
           for s = (format nil "~3,,vS" i 'ABC)
           for s2 = (formatter-call-to-string fn i 'ABC)
           do (assert (string= s s2))
           collect s)))
358 359 360 361 362 363 364 365 366 367 368
  ("ABC"
   "ABC "
   "ABC  "
   "ABC   "
   "ABC    "
   "ABC     "
   "ABC      "))

(deftest format.s.44a
  (with-standard-io-syntax
   (let ((*print-readably* nil)
sds's avatar
sds committed
369 370
         (*package* (find-package :cl-test))
         (fn (formatter "~3,,V@S")))
371
     (loop for i from 0 to 6
sds's avatar
sds committed
372 373 374 375
           for s = (format nil "~3,,v@S" i 'ABC)
           for s2 = (formatter-call-to-string fn i 'ABC)
           do (assert (string= s s2))
           collect s)))
376 377 378 379 380 381 382 383
  ("ABC"
   " ABC"
   "  ABC"
   "   ABC"
   "    ABC"
   "     ABC"
   "      ABC"))

384 385
(def-format-test format.s.45
  "~4,,vs" (-1 1234) "1234")
386

387 388
(def-format-test format.s.46
  "~5,vS" (nil 123) "123  ")
389

390 391
(def-format-test format.s.47
  "~5,vS" (3 456) "456   ")
392

393 394
(def-format-test format.s.48
  "~5,v@S" (3 789) "   789")