string.lsp 3.28 KB
Newer Older
1 2 3
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Sep 30 19:16:59 2002
4
;;;; Contains: Tests for string related functions and classes
5 6 7 8

(in-package :cl-test)

(deftest string.1
9
  (subtypep* 'string 'array)
10 11 12
  t t)

(deftest string.2
13
  (subtypep* 'string 'vector)
14 15 16
  t t)

(deftest string.3
17
  (subtypep* 'string 'sequence)
18 19 20 21
  t t)

(deftest string.4
  (let ((s (string #\a)))
22
    (values (notnot (stringp s)) s))
23 24 25 26
  t "a")

(deftest string.5
  (let ((s (string "")))
27
    (values (notnot (stringp s)) s))
28 29 30 31
  t "")

(deftest string.6
  (let ((s (string '|FOO|)))
32
    (values (notnot (stringp s)) s))
33 34 35
  t "FOO")

(deftest string.7
36 37 38
  (check-predicate
   #'(lambda (x)
       (handler-case (stringp (string x))
sds's avatar
sds committed
39
                     (type-error () :caught))))
40
  nil)
41

sds's avatar
sds committed
42
(deftest string.8
43
  :notes (:allow-nil-arrays :nil-vectors-are-strings)
44 45 46 47
  (subtypep* '(array nil (*)) 'string)
  t t)

(deftest string.9
48
  :notes (:allow-nil-arrays :nil-vectors-are-strings)
49 50 51 52
  (subtypep* '(array nil 1) 'string)
  t t)

(deftest string.10
53
  :notes (:allow-nil-arrays :nil-vectors-are-strings)
54 55 56 57 58 59 60 61
  (string (make-array '(0) :element-type nil))
  "")

(deftest string.11
  (typep* "abcd" 'string)
  t)

(deftest string.12
62
  :notes (:allow-nil-arrays :nil-vectors-are-strings)
63 64 65 66
  (typep* (make-array '(17) :element-type nil) 'string)
  t)

(deftest string.13
67
  :notes (:allow-nil-arrays :nil-vectors-are-strings)
68 69 70
  (typep* (make-array '(0) :element-type nil) 'string)
  t)

pfdietz's avatar
pfdietz committed
71 72 73
(deftest string.14
  (let ((count 0))
    (loop for i below (min char-code-limit 65536)
sds's avatar
sds committed
74 75 76 77 78 79 80 81
          for c = (code-char i)
          for s = (and c (string c))
          when (and c
                    (or (not (stringp s))
                        (not (= (length s) 1))
                        (not (eql c (char s 0)))))
          collect (progn (incf count) (list i c s))
          until (>= count 100)))
82 83
  nil)

pfdietz's avatar
pfdietz committed
84 85 86
(deftest string.15
  (when (> char-code-limit 65536)
    (loop for i = (random char-code-limit)
sds's avatar
sds committed
87 88 89 90 91 92 93 94
          for c = (code-char i)
          for s = (and c (string c))
          repeat 2000
          when (and c
                    (or (not (stringp s))
                        (not (= (length s) 1))
                        (not (eql c (char s 0)))))
          collect (list i c s)))
95 96
  nil)

pfdietz's avatar
pfdietz committed
97
(deftest string.16
98
  (check-predicate #'(lambda (s) (or (not (stringp s)) (eq s (string s)))))
99 100
  nil)

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
(deftest string.17
  (typep* "abc" '(string))
  t)

(deftest string.18
  (typep* "abc" '(string *))
  t)

(deftest string.19
  (typep* "abc" '(string 3))
  t)

(deftest string.20
  (typep* "abc" '(string 2))
  nil)

(deftest string.21
  (typep* "abc" '(string 4))
  nil)

(deftest string.22
  (do-special-strings (s "X") (assert (typep s 'string)))
  nil)
sds's avatar
sds committed
124

125 126 127
(deftest string.23
  (do-special-strings (s "X") (assert (typep s '(string))))
  nil)
sds's avatar
sds committed
128

129 130 131
(deftest string.24
  (do-special-strings (s "X") (assert (typep s '(string *))))
  nil)
sds's avatar
sds committed
132

133 134
(deftest string.25
  (do-special-strings (s "X")
sds's avatar
sds committed
135 136
                      (or (array-has-fill-pointer-p s)
                          (assert (typep s '(string 1)))))
137 138 139 140 141 142
  nil)

(deftest string.26
  (let ((i 0))
    (values (string (progn (incf i) "")) i))
  "" 1)
143

pfdietz's avatar
pfdietz committed
144 145
(def-fold-test string.fold.1 (string #\A))

pfdietz's avatar
pfdietz committed
146
;;; Error tests
147

pfdietz's avatar
pfdietz committed
148 149
(deftest string.error.1
  (signals-error (string) program-error)
150 151
  t)

pfdietz's avatar
pfdietz committed
152 153
(deftest string.error.2
  (signals-error (string nil nil) program-error)
154
  t)