Debian Codegraph @

test/include/srfi-175-tests.scm

raw
1 ;; Copyright 2019 Lassi Kortela
2 ;; SPDX-License-Identifier: MIT
3
4 '(import (scheme base) (scheme file) (scheme read) (scheme write) (srfi 175))
5
6 '(define-syntax want
7 (syntax-rules ()
8 ((_ right-answer (proc args ...))
9 (unless (equal? right-answer (proc args ...))
10 (display "Failed: wanted ")
11 (write right-answer)
12 (display " but got ")
13 (write (proc args ...))
14 (display " from ")
15 (display '(proc args ...))
16 (newline)))))
17
18 (want #f (ascii-codepoint? -1))
19 (want #t (ascii-codepoint? 0))
20 (want #t (ascii-codepoint? #x7f))
21 (want #f (ascii-codepoint? #x80))
22
23 (want #t (ascii-char? (integer->char 0)))
24 (want #t (ascii-char? (integer->char #x7f)))
25 (want #f (ascii-char? (integer->char #x80)))
26
27 (want #t (ascii-string? ""))
28 (want #t (ascii-string? "a"))
29 (want #t (ascii-string? "a b c"))
30 (want #f (ascii-string? "å b o"))
31 (want #t (ascii-string? (make-string 1 (integer->char #x7f))))
32 (want #f (ascii-string? (make-string 1 (integer->char #x80))))
33
34 (want #t (ascii-bytevector? (string->utf8 "")))
35 (want #t (ascii-bytevector? (string->utf8 "a")))
36 (want #t (ascii-bytevector? (string->utf8 "a b c")))
37 (want #f (ascii-bytevector? (string->utf8 "å b o")))
38 (want #t (ascii-bytevector?
39 (string->utf8 (make-string 1 (integer->char #x7f)))))
40 (want #f (ascii-bytevector?
41 (string->utf8 (make-string 1 (integer->char #x80)))))
42
43 (want #t (ascii-non-control? #\space))
44 (want #f (ascii-non-control? #\tab))
45 (want #f (ascii-non-control? #\newline))
46 (want #f (ascii-non-control? (integer->char #x0d)))
47
48 (want #t (ascii-space-or-tab? #\space))
49 (want #t (ascii-space-or-tab? #\tab))
50 (want #f (ascii-space-or-tab? #\newline))
51 (want #f (ascii-non-control? (integer->char #x0d)))
52
53 (let ((lowers "abcdefghijklmnopqrstuvwxyz")
54 (uppers "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
55 (let loop ((i 0))
56 (when (< i 26)
57 (let ((lower (string-ref lowers i))
58 (upper (string-ref uppers i)))
59 (want upper (ascii-upcase upper))
60 (want upper (ascii-upcase lower))
61 (want lower (ascii-downcase upper))
62 (want lower (ascii-downcase lower))
63 (loop (+ i 1))))))
64
65 (let loop ((cc 0))
66 (when (< cc #x80)
67 (unless (ascii-alphabetic? cc)
68 (want cc (ascii-upcase cc))
69 (want cc (ascii-downcase cc)))
70 (loop (+ cc 1))))
71
72 (let loop ((cc 0))
73 (when (< cc #x80)
74 (want #f (ascii-char? cc))
75 (want #t (ascii-char? (integer->char cc)))
76 (cond ((ascii-alphabetic? cc)
77 (want #t (ascii-upper-case? (ascii-upcase cc)))
78 (want #t (ascii-lower-case? (ascii-downcase cc)))
79 (want #f (ascii-lower-case? (ascii-upcase cc)))
80 (want #f (ascii-upper-case? (ascii-downcase cc)))
81 (want #t (ascii-alphanumeric? cc))
82 (want #t (ascii-non-control? cc))
83 (want #f (ascii-other-graphic? cc))
84 (want #f (ascii-control? cc))
85 (want #f (ascii-numeric? cc))
86 (want #f (ascii-whitespace? cc))
87 (want #f (ascii-space-or-tab? cc)))
88 ((ascii-control? cc)
89 (want #f (ascii-non-control? cc))
90 (want #f (ascii-other-graphic? cc))
91 (want cc
92 (ascii-graphic->control
93 (ascii-control->graphic cc)))
94 (want (integer->char cc)
95 (ascii-graphic->control
96 (ascii-control->graphic (integer->char cc)))))
97 ((member cc '(#\( #\) #\[ #\] #\{ #\} #\< #\>))
98 (want cc (ascii-mirror-bracket (ascii-mirror-bracket cc)))))
99 (loop (+ cc 1))))
100
101 (let outer ((a 0))
102 (when (< a 26)
103 (let inner ((b 0))
104 (if (= b 26)
105 (outer (+ a 1))
106 (begin (want (= a b) (ascii-ci=?
107 (ascii-nth-lower-case a)
108 (ascii-nth-upper-case b)))
109 (want (< a b) (ascii-ci<?
110 (ascii-nth-lower-case a)
111 (ascii-nth-upper-case b)))
112 (want (<= a b) (ascii-ci<=?
113 (ascii-nth-lower-case a)
114 (ascii-nth-upper-case b)))
115 (want (> a b) (ascii-ci>?
116 (ascii-nth-lower-case a)
117 (ascii-nth-upper-case b)))
118 (want (>= a b) (ascii-ci>=?
119 (ascii-nth-lower-case a)
120 (ascii-nth-upper-case b)))
121 (inner (+ b 1)))))))
122
123 (ascii-ci>? #\A #\_)
124 (ascii-ci>? #\Z #\_)
125
126 (want #f (ascii-char? -1))
127 (want #f (ascii-char? #x80))
128 (want #f (ascii-char? (integer->char #x80)))
129
130 (want #f (ascii-control? -1))
131 (want #t (ascii-control? #x00))
132 (want #t (ascii-control? #x1f))
133 (want #f (ascii-control? #x20))
134 (want #f (ascii-control? #x7e))
135 (want #t (ascii-control? #x7f))
136 (want #f (ascii-control? #x80))
137
138 (want 0 (ascii-digit-value #\0 10))
139 (want 0 (ascii-digit-value #\0 1))
140 (want #f (ascii-digit-value #\0 0))
141 (want #f (ascii-digit-value #\0 -1))
142 (want 7 (ascii-digit-value #\7 8))
143 (want #f (ascii-digit-value #\7 7))
144 (want #f (ascii-digit-value #\: 10))
145
146 (want 0 (ascii-upper-case-value #\A 0 26))
147 (want 25 (ascii-upper-case-value #\Z 0 26))
148 (want #f (ascii-upper-case-value #\Z 0 25))
149
150 (want 0 (ascii-lower-case-value #\a 0 26))
151 (want 25 (ascii-lower-case-value #\z 0 26))
152 (want #f (ascii-lower-case-value #\z 0 25))
153
154 (want 0 (ascii-lower-case-value #\a 0 1))
155 (want #f (ascii-lower-case-value #\a 0 0))
156 (want #f (ascii-lower-case-value #\a 0 -1))
157 (want 9001 (ascii-lower-case-value #\b 9000 2))
158
159 (want #f (ascii-nth-digit -1))
160 (want #\0 (ascii-nth-digit 0))
161 (want #\9 (ascii-nth-digit 9))
162 (want #f (ascii-nth-digit 10))
163
164 (want #\Z (ascii-nth-upper-case -1))
165 (want #\A (ascii-nth-upper-case 0))
166 (want #\Z (ascii-nth-upper-case 25))
167 (want #\A (ascii-nth-upper-case 26))
168
169 (want #\z (ascii-nth-lower-case -1))
170 (want #\a (ascii-nth-lower-case 0))
171 (want #\z (ascii-nth-lower-case 25))
172 (want #\a (ascii-nth-lower-case 26))
173
174 (define (count-matching predicates value)
175 (let loop ((ps predicates) (n 0))
176 (if (null? ps) n (loop (cdr ps) (if ((car ps) value) (+ n 1) n)))))
177
178 (define (union? whole . parts)
179 (let check ((cc 0))
180 (or (= cc #x80)
181 (if (and (whole cc) (not (= 1 (count-matching parts cc))))
182 #f (check (+ cc 1))))))
183
184 (define (subset? small-set . bigger-sets)
185 (let check ((cc 0))
186 (or (= cc #x80)
187 (if (and (small-set cc) (= 0 (count-matching bigger-sets cc)))
188 #f (check (+ cc 1))))))
189
190 (define (disjoint? . predicates)
191 (let check ((cc 0))
192 (or (= cc #x80) (and (<= (count-matching predicates cc) 1)
193 (check (+ cc 1))))))
194
195 (define (decimal-numeric? x) (ascii-numeric? x))
196
197 (want #t (union? ascii-alphanumeric? ascii-alphabetic? decimal-numeric?))
198 (want #t (union? ascii-alphabetic? ascii-upper-case? ascii-lower-case?))
199
200 (want #t (subset? ascii-space-or-tab? ascii-whitespace?))
201 (want #t (subset? ascii-other-graphic? ascii-non-control?))
202 (want #t (subset? ascii-upper-case? ascii-alphabetic? ascii-non-control?))
203 (want #t (subset? ascii-lower-case? ascii-alphabetic? ascii-non-control?))
204 (want #t (subset? ascii-alphabetic? ascii-alphanumeric? ascii-non-control?))
205 (want #t (subset? decimal-numeric? ascii-alphanumeric? ascii-non-control?))
206 (want #t (subset? ascii-alphanumeric? ascii-non-control?))
207
208 (want #t (disjoint? ascii-control? ascii-non-control?))
209 (want #t (disjoint? ascii-whitespace?
210 ascii-other-graphic?
211 ascii-upper-case?
212 ascii-lower-case?
213 decimal-numeric?))
214 (want #t (disjoint? ascii-control?
215 ascii-other-graphic?
216 ascii-upper-case?
217 ascii-lower-case?
218 decimal-numeric?))
219
220 (define (check-string-ci a b cmp)
221 (want (= cmp 0) (ascii-string-ci=? a b))
222 (want (< cmp 0) (ascii-string-ci<? a b))
223 (want (> cmp 0) (ascii-string-ci>? a b))
224 (want (<= cmp 0) (ascii-string-ci<=? a b))
225 (want (>= cmp 0) (ascii-string-ci>=? a b)))
226
227 (check-string-ci "" "" 0)
228 (check-string-ci "a" "a" 0)
229 (check-string-ci "A" "a" 0)
230 (check-string-ci "a" "A" 0)
231
232 (check-string-ci "a" "b" -1)
233 (check-string-ci "b" "a" 1)
234
235 (check-string-ci "a" "B" -1)
236 (check-string-ci "B" "a" 1)
237
238 (check-string-ci "aa" "aa" 0)
239 (check-string-ci "aa" "ab" -1)
240 (check-string-ci "ab" "aa" 1)
241 (check-string-ci "aa" "aaa" -1)
242 (check-string-ci "aaa" "aa" 1)