fix build with #!-SB-UNICODE
[sbcl.git] / tests / unicode-normalization.impure.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;;
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
11
12 ;;; KLUDGE: eventually we will export NORMALIZE-STRING from somewhere.
13 ;;; Until we do, import it here so we can test it without putting ::
14 ;;; everywhere.
15 (import 'sb-impl::normalize-string)
16
17 (defun parse-one-line (line)
18   (do* ((i 0 (1+ i))
19         (start 0 (1+ end))
20         (end (position #\; line :start start) (position #\; line :start start))
21         result)
22        ((= i 5) (nreverse result))
23     (with-input-from-string (s (subseq line start (1+ end)))
24       (let ((*read-base* 16.))
25         (push (map 'string 'code-char (read-delimited-list #\; s)) result)))))
26
27 (defmacro assert-all-string= (base &body others)
28   `(progn
29      ,@(loop for test in others
30           collect `(assert (string= ,base ,test)))))
31
32 (defun test-line (c1 c2 c3 c4 c5)
33   ;; NFC
34   (assert-all-string= c2
35     (normalize-string c1 :nfc)
36     (normalize-string c2 :nfc)
37     (normalize-string c3 :nfc))
38   (assert-all-string= c4
39     (normalize-string c4 :nfc)
40     (normalize-string c5 :nfc))
41
42   ;; NFD
43   (assert-all-string= c3
44     (normalize-string c1 :nfd)
45     (normalize-string c2 :nfd)
46     (normalize-string c3 :nfd))
47   (assert-all-string= c5
48     (normalize-string c4 :nfd)
49     (normalize-string c5 :nfd))
50
51   ;; NFKC
52   (assert-all-string= c4
53     (normalize-string c1 :nfkc)
54     (normalize-string c2 :nfkc)
55     (normalize-string c3 :nfkc)
56     (normalize-string c4 :nfkc)
57     (normalize-string c5 :nfkc))
58
59   ;; NFKD
60   (assert-all-string= c5
61     (normalize-string c1 :nfkd)
62     (normalize-string c2 :nfkd)
63     (normalize-string c3 :nfkd)
64     (normalize-string c4 :nfkd)
65     (normalize-string c5 :nfkd)))
66
67 (defun test-no-normalization (string)
68   (assert-all-string= string
69     (normalize-string string :nfc)
70     (normalize-string string :nfd)
71     (normalize-string string :nfkc)
72     (normalize-string string :nfkd)))
73
74 (defun test-normalization ()
75   (declare (optimize (debug 2)))
76   (with-open-file (s "data/NormalizationTest.txt" :external-format :latin1)
77     (do ((line (read-line s) (read-line s)))
78         ((char/= #\# (char line 0))
79          (assert (string= "@Part0" line :end2 6))
80          (assert (char= #\# (char (read-line s) 0)))))
81     ;; Part0: specific cases
82     (with-test (:name (:unicode-normalization :part0))
83       (do ((line (read-line s) (read-line s)))
84           ((char= #\# (char line 0))
85            (assert (string= "@Part1" (read-line s) :end2 6))
86            (assert (char= #\# (char (read-line s) 0)))
87            (assert (char= #\# (char (read-line s) 0))))
88         (destructuring-bind (c1 c2 c3 c4 c5)
89             (parse-one-line line)
90           (test-line c1 c2 c3 c4 c5))))
91     ;; Part1: single characters.  (Extra work to check for conformance
92     ;; on unlisted entries)
93     (with-test (:name (:unicode-normalization :part1))
94       (do ((line (read-line s) (read-line s))
95            (code 0))
96           ((char= #\# (char line 0))
97            (do ((code code (1+ code)))
98                ((= code #x110000))
99              (test-no-normalization (string (code-char code))))
100            (assert (string= "@Part2" (read-line s) :end2 6))
101            (assert (char= #\# (char (read-line s) 0))))
102         (destructuring-bind (c1 c2 c3 c4 c5)
103             (parse-one-line line)
104           (do ((c code (1+ c)))
105               ((= c (char-code (char c1 0)))
106                (test-line c1 c2 c3 c4 c5)
107                (setf code (1+ c)))
108             (test-no-normalization (string (code-char code)))))))
109     ;; Part2: Canonical Order Test
110     (with-test (:name (:unicode-normalization :part2))
111       (do ((line (read-line s) (read-line s)))
112           ((char= #\# (char line 0))
113            (assert (string= "@Part3" (read-line s) :end2 6))
114            (assert (char= #\# (char (read-line s) 0))))
115         (destructuring-bind (c1 c2 c3 c4 c5)
116             (parse-one-line line)
117           (test-line c1 c2 c3 c4 c5))))
118     ;; Part3: PRI #29 Test
119     (with-test (:name (:unicode-normalization :part3))
120       (do ((line (read-line s) (read-line s)))
121           ((char= #\# (char line 0))
122            (assert (char= #\# (char (read-line s) 0)))
123            (assert (null (read-line s nil nil))))
124         (destructuring-bind (c1 c2 c3 c4 c5)
125             (parse-one-line line)
126           (test-line c1 c2 c3 c4 c5))))))
127
128 (test-normalization)