Fix make-array transforms.
[sbcl.git] / tests / character.pure.lisp
1 ;;;; various CHARACTER tests without side effects
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
13
14 (cl:in-package :cl-user)
15
16 ;;; ANSI's specification of #'CHAR-NAME imposes these constraints.
17 ;;;
18 ;;; (Obviously, the numeric values in this test implicitly assume
19 ;;; we're using an ASCII-based character set.)
20 (dolist (i '(("Newline" 10)
21              ;; (ANSI also imposes a constraint on the "semi-standard
22              ;; character" "Linefeed", but in ASCII as interpreted by
23              ;; Unix it's shadowed by "Newline" and so doesn't exist
24              ;; as a separate character.)
25              ("Space" 32)
26              ("Tab" 9)
27              ("Page" 12)
28              ("Rubout" 127)
29              ("Return" 13)
30              ("Backspace" 8)))
31   (destructuring-bind (name code) i
32     (let ((named-char (name-char name))
33           (coded-char (code-char code)))
34       (assert (eql named-char coded-char))
35       (assert (characterp named-char))
36       (let ((coded-char-name (char-name coded-char)))
37         (assert (string= name coded-char-name))))))
38
39 ;;; Trivial tests for some unicode names
40 #+sb-unicode
41 (dolist (d '(("LATIN_CAPITAL_LETTER_A" 65)
42              ("LATIN_SMALL_LETTER_A" 97)
43              ("LATIN_SMALL_LETTER_CLOSED_OPEN_E" 666)
44              ("DIGRAM_FOR_GREATER_YIN" 9871)))
45   (destructuring-bind (name code) d
46     (assert (eql (code-char code) (name-char (string-downcase name))))
47     (assert (equal name (char-name (code-char code))))))
48
49 ;;; bug 230: CHAR= didn't check types of &REST arguments
50 (dolist (form '((code-char char-code-limit)
51                 (standard-char-p "a")
52                 (graphic-char-p "a")
53                 (alpha-char-p "a")
54                 (upper-case-p "a")
55                 (lower-case-p "a")
56                 (both-case-p "a")
57                 (digit-char-p "a")
58                 (alphanumericp "a")
59                 (char= #\a "a")
60                 (char/= #\a "a")
61                 (char< #\a #\b "c")
62                 (char-equal #\a #\a "b")
63                 (digit-char -1)
64                 (digit-char 4 1)
65                 (digit-char 4 37)))
66   (assert (raises-error? (apply (car form) (mapcar 'eval (cdr form))) type-error)))
67
68 (dotimes (i 256)
69   (let* ((char (code-char i))
70          (graphicp (graphic-char-p char))
71          (name (char-name char)))
72     (unless graphicp
73       (assert name))))
74
75 (assert (null (name-char 'foo)))
76
77 ;;; Between 1.0.4.53 and 1.0.4.69 character untagging was broken on
78 ;;; x86-64 if the result of the VOP was allocated on the stack, failing
79 ;;; an aver in the compiler.
80 (with-test (:name :character-untagging)
81   (compile nil
82            '(lambda (c0 c1 c2 c3 c4 c5 c6 c7
83                      c8 c9 ca cb cc cd ce cf)
84              (declare (type character c0 c1 c2 c3 c4 c5 c6 c7
85                        c8 c9 ca cb cc cd ce cf))
86              (char< c0 c1 c2 c3 c4 c5 c6 c7
87               c8 c9 ca cb cc cd ce cf))))
88
89 ;;; Characters could be coerced to subtypes of CHARACTER to which they
90 ;;; don't belong. Also, character designators that are not characters
91 ;;; could be coerced to proper subtypes of CHARACTER.
92 (with-test (:name :bug-841312)
93   ;; First let's make sure that the conditions hold that make the test
94   ;; valid: #\Nak is a BASE-CHAR, which at the same time ensures that
95   ;; STANDARD-CHAR is a proper subtype of BASE-CHAR, and under
96   ;; #+SB-UNICODE the character with code 955 exists and is not a
97   ;; BASE-CHAR.
98   (assert (typep #\Nak 'base-char))
99   #+sb-unicode
100   (assert (let ((c (code-char 955)))
101             (and c (not (typep c 'base-char)))))
102   ;; Test the formerly buggy coercions:
103   (macrolet ((assert-coerce-type-error (object type)
104                `(assert (raises-error? (coerce ,object ',type)
105                                        type-error))))
106     (assert-coerce-type-error #\Nak standard-char)
107     (assert-coerce-type-error #\a extended-char)
108     #+sb-unicode
109     (assert-coerce-type-error (code-char 955) base-char)
110     (assert-coerce-type-error 'a standard-char)
111     (assert-coerce-type-error "a" standard-char))
112   ;; The following coercions still need to be possible:
113   (macrolet ((assert-coercion (object type)
114                `(assert (typep (coerce ,object ',type) ',type))))
115     (assert-coercion #\a standard-char)
116     (assert-coercion #\Nak base-char)
117     #+sb-unicode
118     (assert-coercion (code-char 955) character)
119     (assert-coercion 'a character)
120     (assert-coercion "a" character)))
121
122 (with-test (:name :bug-994487)
123   (let ((f (compile nil `(lambda (char)
124                            (code-char (1+ (char-code char)))))))
125     (assert (equal `(function (t) (values (sb-kernel:character-set
126                                            ((1 . ,(1- char-code-limit))))
127                                           &optional))
128                    (sb-impl::%fun-type f)))))
129
130 (with-test (:name (:case-insensitive-char-comparisons :eacute))
131   (assert (char-equal (code-char 201) (code-char 233))))
132
133 (with-test (:name (:case-insensitive-char-comparisons :exhaustive))
134   (dotimes (i char-code-limit)
135     (let* ((char (code-char i))
136            (down (char-downcase char))
137            (up (char-upcase char)))
138       (assert (char-equal char char))
139       (when (char/= char down)
140         (assert (char-equal char down)))
141       (when (char/= char up)
142         (assert (char-equal char up))))))