string-capitalize.lsp 4.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Thu Oct  3 20:08:26 2002
;;;; Contains: Tests for STRING-CAPITALIZE

(in-package :cl-test)

(deftest string-capitalize.1
  (let ((s "abCd"))
    (values (string-capitalize s) s))
  "Abcd"
  "abCd")


(deftest string-capitalize.2
  (let ((s "0adA2Cdd3wXy"))
    (values (string-capitalize s) s))
18 19
  "0ada2cdd3wxy"
  "0adA2Cdd3wXy")
20 21 22 23 24 25 26 27 28 29 30 31 32

(deftest string-capitalize.3
  (let ((s "1a"))
    (values (string-capitalize s) s))
  "1a"
  "1a")

(deftest string-capitalize.4
  (let ((s "a1a"))
    (values (string-capitalize s) s))
  "A1a"
  "a1a")

pfdietz's avatar
pfdietz committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
(deftest string-capitalize.5
  (let ((s #\a))
    (values (string-capitalize s) s))
  "A"
  #\a)

(deftest string-capitalize.6
  (let ((s '|abcDe|))
    (values (string-capitalize s) (symbol-name s)))
  "Abcde"
  "abcDe")

(deftest string-capitalize.7
  (let ((s "ABCDEF"))
    (values
     (loop for i from 0 to 5
sds's avatar
sds committed
49
           collect (string-capitalize s :start i))
pfdietz's avatar
pfdietz committed
50 51 52 53 54 55 56 57
     s))
  ("Abcdef" "ABcdef" "ABCdef" "ABCDef" "ABCDEf" "ABCDEF")
  "ABCDEF")

(deftest string-capitalize.8
  (let ((s "ABCDEF"))
    (values
     (loop for i from 0 to 5
sds's avatar
sds committed
58
           collect (string-capitalize s :start i :end nil))
pfdietz's avatar
pfdietz committed
59 60 61 62 63 64 65 66
     s))
  ("Abcdef" "ABcdef" "ABCdef" "ABCDef" "ABCDEf" "ABCDEF")
  "ABCDEF")

(deftest string-capitalize.9
  (let ((s "ABCDEF"))
    (values
     (loop for i from 0 to 6
sds's avatar
sds committed
67
           collect (string-capitalize s :end i))
pfdietz's avatar
pfdietz committed
68 69 70 71 72 73 74 75
     s))
  ("ABCDEF" "ABCDEF" "AbCDEF" "AbcDEF" "AbcdEF" "AbcdeF" "Abcdef")
  "ABCDEF")

(deftest string-capitalize.10
  (let ((s "ABCDEF"))
    (values
     (loop for i from 0 to 5
sds's avatar
sds committed
76 77
           collect (loop for j from i to 6
                         collect (string-capitalize s :start i :end j)))
pfdietz's avatar
pfdietz committed
78 79 80 81 82 83 84 85
     s))
  (("ABCDEF" "ABCDEF" "AbCDEF" "AbcDEF" "AbcdEF" "AbcdeF" "Abcdef")
   ("ABCDEF" "ABCDEF" "ABcDEF" "ABcdEF" "ABcdeF" "ABcdef")
   ("ABCDEF" "ABCDEF" "ABCdEF" "ABCdeF" "ABCdef")
   ("ABCDEF" "ABCDEF" "ABCDeF" "ABCDef")
   ("ABCDEF" "ABCDEF" "ABCDEf")
   ("ABCDEF" "ABCDEF"))
  "ABCDEF")
86

87
(deftest string-capitalize.11
88
  :notes (:nil-vectors-are-strings)
89 90 91
  (string-capitalize (make-array '(0) :element-type nil))
  "")

92 93
(deftest string-capitalize.12
  (loop for type in '(standard-char base-char character)
sds's avatar
sds committed
94 95 96 97
        for s = (make-array '(10) :element-type type
                            :fill-pointer 5
                            :initial-contents "aB0cDefGHi")
        collect (list s (string-capitalize s)))
98 99 100 101 102
  (("aB0cD" "Ab0cd") ("aB0cD" "Ab0cd") ("aB0cD" "Ab0cd")))


(deftest string-capitalize.13
  (loop for type in '(standard-char base-char character)
sds's avatar
sds committed
103 104 105 106 107 108
        for s0 = (make-array '(10) :element-type type
                             :initial-contents "zZaB0cDefG")
        for s = (make-array '(5) :element-type type
                            :displaced-to s0
                            :displaced-index-offset 2)
        collect (list s (string-capitalize s)))
109 110 111 112
  (("aB0cD" "Ab0cd") ("aB0cD" "Ab0cd") ("aB0cD" "Ab0cd")))

(deftest string-capitalize.14
  (loop for type in '(standard-char base-char character)
sds's avatar
sds committed
113 114 115 116
        for s = (make-array '(5) :element-type type
                            :adjustable t
                            :initial-contents "aB0cD")
        collect (list s (string-capitalize s)))
117 118 119 120
  (("aB0cD" "Ab0cd") ("aB0cD" "Ab0cd") ("aB0cD" "Ab0cd")))

;;; Order of evaluation tests

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
(deftest string-capitalize.order.1
  (let ((i 0) a b c (s (copy-seq "abcdef")))
    (values
     (string-capitalize
      (progn (setf a (incf i)) s)
      :start (progn (setf b (incf i)) 1)
      :end   (progn (setf c (incf i)) 4))
     i a b c))
  "aBcdef" 3 1 2 3)

(deftest string-capitalize.order.2
  (let ((i 0) a b c (s (copy-seq "abcdef")))
    (values
     (string-capitalize
      (progn (setf a (incf i)) s)
      :end   (progn (setf b (incf i)) 4)
      :start (progn (setf c (incf i)) 1))
     i a b c))
  "aBcdef" 3 1 2 3)

pfdietz's avatar
pfdietz committed
141 142
(def-fold-test string-capitalize.fold.1 (string-capitalize "ABCDE"))

143 144 145
;;; Error cases

(deftest string-capitalize.error.1
146 147
  (signals-error (string-capitalize) program-error)
  t)
148 149

(deftest string-capitalize.error.2
150 151
  (signals-error (string-capitalize (copy-seq "abc") :bad t) program-error)
  t)
152 153

(deftest string-capitalize.error.3
154 155
  (signals-error (string-capitalize (copy-seq "abc") :start) program-error)
  t)
156 157

(deftest string-capitalize.error.4
158
  (signals-error (string-capitalize (copy-seq "abc") :bad t
sds's avatar
sds committed
159
                                      :allow-other-keys nil) program-error)
160
  t)
161 162

(deftest string-capitalize.error.5
163 164
  (signals-error (string-capitalize (copy-seq "abc") :end) program-error)
  t)
165 166

(deftest string-capitalize.error.6
167 168
  (signals-error (string-capitalize (copy-seq "abc") 1 2) program-error)
  t)