string-trim.lsp 5.91 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Thu Oct  3 21:53:38 2002
;;;; Contains: Tests for STRING-TRIM

(in-package :cl-test)

(deftest string-trim.1
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
10
         (s2 (string-trim "ab" s)))
pfdietz's avatar
pfdietz committed
11 12 13 14 15 16
    (values s s2))
  "abcdaba"
  "cd")

(deftest string-trim.2
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
17
         (s2 (string-trim '(#\a #\b) s)))
pfdietz's avatar
pfdietz committed
18 19 20 21 22 23
    (values s s2))
  "abcdaba"
  "cd")

(deftest string-trim.3
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
24
         (s2 (string-trim #(#\a #\b) s)))
pfdietz's avatar
pfdietz committed
25 26 27 28 29 30
    (values s s2))
  "abcdaba"
  "cd")

(deftest string-trim.4
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
31 32
         (s2 (string-trim (make-array 2 :initial-contents '(#\a #\b))
                          s)))
pfdietz's avatar
pfdietz committed
33 34 35 36 37 38
    (values s s2))
  "abcdaba"
  "cd")

(deftest string-trim.5
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
39 40 41
         (s2 (string-trim (make-array 2 :initial-contents '(#\a #\b)
                                      :element-type 'character)
                          s)))
pfdietz's avatar
pfdietz committed
42 43 44 45 46 47
    (values s s2))
  "abcdaba"
  "cd")

(deftest string-trim.6
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
48 49 50
         (s2 (string-trim (make-array 2 :initial-contents '(#\a #\b)
                                      :element-type 'standard-char)
                          s)))
pfdietz's avatar
pfdietz committed
51 52 53 54 55 56
    (values s s2))
  "abcdaba"
  "cd")

(deftest string-trim.7
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
57 58 59
         (s2 (string-trim (make-array 2 :initial-contents '(#\a #\b)
                                      :element-type 'base-char)
                          s)))
pfdietz's avatar
pfdietz committed
60 61 62 63
    (values s s2))
  "abcdaba"
  "cd")

pfdietz's avatar
pfdietz committed
64 65
(deftest string-trim.8
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
66 67 68 69
         (s2 (string-trim (make-array 4 :initial-contents '(#\a #\b #\c #\d)
                                      :element-type 'character
                                      :fill-pointer 2)
                          s)))
pfdietz's avatar
pfdietz committed
70 71 72
    (values s s2))
  "abcdaba"
  "cd")
pfdietz's avatar
pfdietz committed
73

74 75
(deftest string-trim.8a
  (let* ((s (copy-seq "abcdaba"))
sds's avatar
sds committed
76 77 78 79
         (s2 (string-trim (make-array 4 :initial-contents '(#\a #\b #\c #\d)
                                      :element-type 'base-char
                                      :fill-pointer 2)
                          s)))
80 81 82 83
    (values s s2))
  "abcdaba"
  "cd")

pfdietz's avatar
pfdietz committed
84
(deftest string-trim.9
85
  (let* ((s (make-array 7 :initial-contents "abcdaba"
sds's avatar
sds committed
86 87 88
                        :element-type 'character
                        ))
         (s2 (string-trim "ab" s)))
pfdietz's avatar
pfdietz committed
89 90 91
    (values s s2))
  "abcdaba"
  "cd")
pfdietz's avatar
pfdietz committed
92

pfdietz's avatar
pfdietz committed
93
(deftest string-trim.10
94
  (let* ((s (make-array 9 :initial-contents "abcdabadd"
sds's avatar
sds committed
95 96 97
                        :element-type 'character
                        :fill-pointer 7))
         (s2 (string-trim "ab" s)))
pfdietz's avatar
pfdietz committed
98 99 100 101
    (values s s2))
  "abcdaba"
  "cd")

102 103
(deftest string-trim.10a
  (let* ((s (make-array 9 :initial-contents "abcdabadd"
sds's avatar
sds committed
104 105 106 107
                        :element-type 'base-char
                        :adjustable t
                        :fill-pointer 7))
         (s2 (string-trim "ab" s)))
108 109 110 111
    (values s s2))
  "abcdaba"
  "cd")

pfdietz's avatar
pfdietz committed
112
(deftest string-trim.11
113
  (let* ((s (make-array 7 :initial-contents "abcdaba"
sds's avatar
sds committed
114 115 116
                        :element-type 'standard-char
                        ))
         (s2 (string-trim "ab" s)))
pfdietz's avatar
pfdietz committed
117 118 119 120 121
    (values s s2))
  "abcdaba"
  "cd")

(deftest string-trim.12
122
  (let* ((s (make-array 7 :initial-contents "abcdaba"
sds's avatar
sds committed
123 124 125
                        :element-type 'base-char
                        ))
         (s2 (string-trim "ab" s)))
pfdietz's avatar
pfdietz committed
126 127 128
    (values s s2))
  "abcdaba"
  "cd")
129 130 131 132

;;; Test that trimming is case sensitive
(deftest string-trim.13
  (let* ((s (copy-seq "Aa"))
sds's avatar
sds committed
133
         (s2 (string-trim "a" s)))
134 135 136 137 138
    (values s s2))
  "Aa" "A")

(deftest string-trim.14
  (let* ((s '|abcdaba|)
sds's avatar
sds committed
139
         (s2 (string-trim "ab" s)))
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    (values (symbol-name s) s2))
  "abcdaba"
  "cd")

(deftest string-trim.15
  (string-trim "abc" "")
  "")

(deftest string-trim.16
  (string-trim "a" #\a)
  "")

(deftest string-trim.17
  (string-trim "b" #\a)
  "a")

(deftest string-trim.18
  (string-trim "" (copy-seq "abcde"))
  "abcde")

(deftest string-trim.19
  (string-trim "abc" (copy-seq "abcabcabc"))
  "")

164
(deftest string-trim.20
165
  :notes (:nil-vectors-are-strings)
166 167 168 169
  (string-trim "abcd" (make-array '(0) :element-type nil))
  "")

(deftest string-trim.21
170
  :notes (:nil-vectors-are-strings)
171 172 173
  (string-trim (make-array '(0) :element-type nil) "abcd")
  "abcd")

174 175
(deftest string-trim.22
  (let ((s (make-array '(6) :initial-contents "abcaeb"
sds's avatar
sds committed
176 177
                       :element-type 'base-char
                       :adjustable t)))
178 179 180 181 182
    (values (string-trim "ab" s) s))
  "cae" "abcaeb")

(deftest string-trim.23
  (let ((s (make-array '(6) :initial-contents "abcaeb"
sds's avatar
sds committed
183 184
                       :element-type 'character
                       :adjustable t)))
185 186 187 188 189
    (values (string-trim "ab" s) s))
  "cae" "abcaeb")

(deftest string-trim.24
  (let* ((etype 'base-char)
sds's avatar
sds committed
190 191 192 193 194
         (s0 (make-array '(6) :initial-contents "abcaeb"
                         :element-type etype))
         (s (make-array '(3) :element-type etype
                        :displaced-to s0
                        :displaced-index-offset 1)))
195 196 197 198 199
    (values (string-trim "ab" s) s s0))
  "c" "bca" "abcaeb")

(deftest string-trim.25
  (let* ((etype 'character)
sds's avatar
sds committed
200 201 202 203 204
         (s0 (make-array '(6) :initial-contents "abcaeb"
                         :element-type etype))
         (s (make-array '(3) :element-type etype
                        :displaced-to s0
                        :displaced-index-offset 1)))
205 206 207
    (values (string-trim "ab" s) s s0))
  "c" "bca" "abcaeb")

208 209 210 211
(deftest string-trim.order.1
  (let ((i 0) x y)
    (values
     (string-trim (progn (setf x (incf i)) " ")
sds's avatar
sds committed
212 213
                  (progn (setf y (incf i))
                         (copy-seq "   abc d e f  ")))
214 215 216
     i x y))
  "abc d e f" 2 1 2)

pfdietz's avatar
pfdietz committed
217 218
(def-fold-test string-trim.fold.1 (string-trim " " " abcd "))

219
;;; Error cases
220

221
(deftest string-trim.error.1
222 223
  (signals-error (string-trim) program-error)
  t)
224

225
(deftest string-trim.error.2
226 227
  (signals-error (string-trim "abc") program-error)
  t)
228

229
(deftest string-trim.error.3
230 231
  (signals-error (string-trim "abc" "abcdddabc" nil) program-error)
  t)