Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_ascii.d

raw
1 @safe unittest
2 {
3 import std.ascii;
4
5 import std.conv : to;
6
7 assert(42.to!string(16, LetterCase.upper) == "2A");
8 assert(42.to!string(16, LetterCase.lower) == "2a");
9 }
10
11 @safe unittest
12 {
13 import std.ascii;
14
15 import std.digest.hmac : hmac;
16 import std.digest : toHexString;
17 import std.digest.sha : SHA1;
18 import std.string : representation;
19
20 const sha1HMAC = "A very long phrase".representation
21 .hmac!SHA1("secret".representation)
22 .toHexString!(LetterCase.lower);
23 assert(sha1HMAC == "49f2073c7bf58577e8c9ae59fe8cfd37c9ab94e5");
24 }
25
26 @safe pure nothrow @nogc unittest
27 {
28 import std.ascii;
29
30 import std.algorithm.comparison, std.algorithm.searching, std.range, std.traits;
31
32 // Because all ASCII characters fit in char, so do these
33 static assert(ControlChar.ack.sizeof == 1);
34
35 // All control characters except del are in row starting from 0
36 static assert(EnumMembers!ControlChar.only.until(ControlChar.del).equal(iota(32)));
37
38 static assert(ControlChar.nul == '\0');
39 static assert(ControlChar.bel == '\a');
40 static assert(ControlChar.bs == '\b');
41 static assert(ControlChar.ff == '\f');
42 static assert(ControlChar.lf == '\n');
43 static assert(ControlChar.cr == '\r');
44 static assert(ControlChar.tab == '\t');
45 static assert(ControlChar.vt == '\v');
46 }
47
48 @safe pure nothrow unittest
49 {
50 import std.ascii;
51
52 import std.conv;
53 //Control character table can be used in place of hexcodes.
54 with (ControlChar) assert(text("Phobos", us, "Deimos", us, "Tango", rs) == "Phobos\x1FDeimos\x1FTango\x1E");
55 }
56
57 @safe pure nothrow @nogc unittest
58 {
59 import std.ascii;
60
61 assert( isAlphaNum('A'));
62 assert( isAlphaNum('1'));
63 assert(!isAlphaNum('#'));
64
65 // N.B.: does not return true for non-ASCII Unicode alphanumerics:
66 assert(!isAlphaNum('á'));
67 }
68
69 @safe pure nothrow @nogc unittest
70 {
71 import std.ascii;
72
73 assert( isAlpha('A'));
74 assert(!isAlpha('1'));
75 assert(!isAlpha('#'));
76
77 // N.B.: does not return true for non-ASCII Unicode alphabetic characters:
78 assert(!isAlpha('á'));
79 }
80
81 @safe pure nothrow @nogc unittest
82 {
83 import std.ascii;
84
85 assert( isLower('a'));
86 assert(!isLower('A'));
87 assert(!isLower('#'));
88
89 // N.B.: does not return true for non-ASCII Unicode lowercase letters
90 assert(!isLower('á'));
91 assert(!isLower('Á'));
92 }
93
94 @safe pure nothrow @nogc unittest
95 {
96 import std.ascii;
97
98 assert( isUpper('A'));
99 assert(!isUpper('a'));
100 assert(!isUpper('#'));
101
102 // N.B.: does not return true for non-ASCII Unicode uppercase letters
103 assert(!isUpper('á'));
104 assert(!isUpper('Á'));
105 }
106
107 @safe pure nothrow @nogc unittest
108 {
109 import std.ascii;
110
111 assert( isDigit('3'));
112 assert( isDigit('8'));
113 assert(!isDigit('B'));
114 assert(!isDigit('#'));
115
116 // N.B.: does not return true for non-ASCII Unicode numbers
117 assert(!isDigit('0')); // full-width digit zero (U+FF10)
118 assert(!isDigit('4')); // full-width digit four (U+FF14)
119 }
120
121 @safe pure nothrow @nogc unittest
122 {
123 import std.ascii;
124
125 assert( isOctalDigit('0'));
126 assert( isOctalDigit('7'));
127 assert(!isOctalDigit('8'));
128 assert(!isOctalDigit('A'));
129 assert(!isOctalDigit('#'));
130 }
131
132 @safe pure nothrow @nogc unittest
133 {
134 import std.ascii;
135
136 assert( isHexDigit('0'));
137 assert( isHexDigit('A'));
138 assert( isHexDigit('f')); // lowercase hex digits are accepted
139 assert(!isHexDigit('g'));
140 assert(!isHexDigit('G'));
141 assert(!isHexDigit('#'));
142 }
143
144 @safe pure nothrow @nogc unittest
145 {
146 import std.ascii;
147
148 assert( isWhite(' '));
149 assert( isWhite('\t'));
150 assert( isWhite('\n'));
151 assert(!isWhite('1'));
152 assert(!isWhite('a'));
153 assert(!isWhite('#'));
154
155 // N.B.: Does not return true for non-ASCII Unicode whitespace characters.
156 static import std.uni;
157 assert(std.uni.isWhite('\u00A0'));
158 assert(!isWhite('\u00A0')); // std.ascii.isWhite
159 }
160
161 @safe pure nothrow @nogc unittest
162 {
163 import std.ascii;
164
165 assert( isControl('\0'));
166 assert( isControl('\022'));
167 assert( isControl('\n')); // newline is both whitespace and control
168 assert(!isControl(' '));
169 assert(!isControl('1'));
170 assert(!isControl('a'));
171 assert(!isControl('#'));
172
173 // N.B.: non-ASCII Unicode control characters are not recognized:
174 assert(!isControl('\u0080'));
175 assert(!isControl('\u2028'));
176 assert(!isControl('\u2029'));
177 }
178
179 @safe pure nothrow @nogc unittest
180 {
181 import std.ascii;
182
183 assert( isPunctuation('.'));
184 assert( isPunctuation(','));
185 assert( isPunctuation(':'));
186 assert( isPunctuation('!'));
187 assert( isPunctuation('#'));
188 assert( isPunctuation('~'));
189 assert( isPunctuation('+'));
190 assert( isPunctuation('_'));
191
192 assert(!isPunctuation('1'));
193 assert(!isPunctuation('a'));
194 assert(!isPunctuation(' '));
195 assert(!isPunctuation('\n'));
196 assert(!isPunctuation('\0'));
197
198 // N.B.: Non-ASCII Unicode punctuation characters are not recognized.
199 assert(!isPunctuation('\u2012')); // (U+2012 = en-dash)
200 }
201
202 @safe pure nothrow @nogc unittest
203 {
204 import std.ascii;
205
206 assert( isGraphical('1'));
207 assert( isGraphical('a'));
208 assert( isGraphical('#'));
209 assert(!isGraphical(' ')); // whitespace is not graphical
210 assert(!isGraphical('\n'));
211 assert(!isGraphical('\0'));
212
213 // N.B.: Unicode graphical characters are not regarded as such.
214 assert(!isGraphical('á'));
215 }
216
217 @safe pure nothrow @nogc unittest
218 {
219 import std.ascii;
220
221 assert( isPrintable(' ')); // whitespace is printable
222 assert( isPrintable('1'));
223 assert( isPrintable('a'));
224 assert( isPrintable('#'));
225 assert(!isPrintable('\0')); // control characters are not printable
226
227 // N.B.: Printable non-ASCII Unicode characters are not recognized.
228 assert(!isPrintable('á'));
229 }
230
231 @safe pure nothrow @nogc unittest
232 {
233 import std.ascii;
234
235 assert( isASCII('a'));
236 assert(!isASCII('á'));
237 }
238
239 @safe pure nothrow @nogc unittest
240 {
241 import std.ascii;
242
243 assert(toLower('a') == 'a');
244 assert(toLower('A') == 'a');
245 assert(toLower('#') == '#');
246
247 // N.B.: Non-ASCII Unicode uppercase letters are not converted.
248 assert(toLower('Á') == 'Á');
249 }
250
251 @safe pure nothrow @nogc unittest
252 {
253 import std.ascii;
254
255 assert(toUpper('a') == 'A');
256 assert(toUpper('A') == 'A');
257 assert(toUpper('#') == '#');
258
259 // N.B.: Non-ASCII Unicode lowercase letters are not converted.
260 assert(toUpper('á') == 'á');
261 }
262