Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_encoding.d

raw
1 @safe unittest
2 {
3 import std.encoding;
4
5 assert(encodingName!(char) == "UTF-8");
6 assert(encodingName!(wchar) == "UTF-16");
7 assert(encodingName!(dchar) == "UTF-32");
8 assert(encodingName!(AsciiChar) == "ASCII");
9 assert(encodingName!(Latin1Char) == "ISO-8859-1");
10 assert(encodingName!(Latin2Char) == "ISO-8859-2");
11 assert(encodingName!(Windows1250Char) == "windows-1250");
12 assert(encodingName!(Windows1251Char) == "windows-1251");
13 assert(encodingName!(Windows1252Char) == "windows-1252");
14 }
15
16 @safe pure unittest
17 {
18 import std.encoding;
19
20 assert( canEncode!(Latin1Char)('A'));
21 assert( canEncode!(Latin2Char)('A'));
22 assert(!canEncode!(AsciiChar)('\u00A0'));
23 assert( canEncode!(Latin1Char)('\u00A0'));
24 assert( canEncode!(Latin2Char)('\u00A0'));
25 assert( canEncode!(Windows1250Char)('\u20AC'));
26 assert(!canEncode!(Windows1250Char)('\u20AD'));
27 assert(!canEncode!(Windows1250Char)('\uFFFD'));
28 assert( canEncode!(Windows1251Char)('\u0402'));
29 assert(!canEncode!(Windows1251Char)('\u20AD'));
30 assert(!canEncode!(Windows1251Char)('\uFFFD'));
31 assert( canEncode!(Windows1252Char)('\u20AC'));
32 assert(!canEncode!(Windows1252Char)('\u20AD'));
33 assert(!canEncode!(Windows1252Char)('\uFFFD'));
34 assert(!canEncode!(char)(cast(dchar) 0x110000));
35 }
36
37 @safe pure unittest
38 {
39 import std.encoding;
40
41 import std.algorithm.searching : find;
42 import std.utf : byDchar;
43
44 assert("The quick brown fox"
45 .byDchar
46 .find!(x => !canEncode!AsciiChar(x))
47 .empty);
48 }
49
50 @system pure unittest
51 {
52 import std.encoding;
53
54 assert(!isValidCodeUnit(cast(char) 0xC0));
55 assert(!isValidCodeUnit(cast(char) 0xFF));
56 assert( isValidCodeUnit(cast(wchar) 0xD800));
57 assert(!isValidCodeUnit(cast(dchar) 0xD800));
58 assert(!isValidCodeUnit(cast(AsciiChar) 0xA0));
59 assert( isValidCodeUnit(cast(Windows1250Char) 0x80));
60 assert(!isValidCodeUnit(cast(Windows1250Char) 0x81));
61 assert( isValidCodeUnit(cast(Windows1251Char) 0x80));
62 assert(!isValidCodeUnit(cast(Windows1251Char) 0x98));
63 assert( isValidCodeUnit(cast(Windows1252Char) 0x80));
64 assert(!isValidCodeUnit(cast(Windows1252Char) 0x81));
65 }
66
67 @system pure unittest
68 {
69 import std.encoding;
70
71 assert( isValid("\u20AC100"));
72 assert(!isValid(cast(char[3])[167, 133, 175]));
73 }
74
75 @system pure unittest
76 {
77 import std.encoding;
78
79 assert(sanitize("hello \xF0\x80world") == "hello \xEF\xBF\xBDworld");
80 }
81
82 @system pure unittest
83 {
84 import std.encoding;
85
86 assert(firstSequence("\u20AC1000") == "\u20AC".length);
87 assert(firstSequence("hel") == "h".length);
88 }
89
90 @system pure unittest
91 {
92 import std.encoding;
93
94 assert(lastSequence("1000\u20AC") == "\u20AC".length);
95 assert(lastSequence("hellö") == "ö".length);
96 }
97
98 @system pure unittest
99 {
100 import std.encoding;
101
102 assert(index("\u20AC100",1) == 3);
103 assert(index("hällo",2) == 3);
104 }
105
106 @system unittest
107 {
108 import std.encoding;
109
110 string s = "hello";
111 string t;
112 foreach (c;codePoints(s))
113 {
114 t ~= cast(char) c;
115 }
116 assert(s == t);
117 }
118
119 @system unittest
120 {
121 import std.encoding;
122
123 char[] a;
124 foreach (c;codeUnits!(char)(cast(dchar)'\u20AC'))
125 {
126 a ~= c;
127 }
128 assert(a.length == 3);
129 assert(a[0] == 0xE2);
130 assert(a[1] == 0x82);
131 assert(a[2] == 0xAC);
132 }
133
134 @system pure unittest
135 {
136 import std.encoding;
137
138 wstring ws;
139 // transcode from UTF-8 to UTF-16
140 transcode("hello world",ws);
141 assert(ws == "hello world"w);
142
143 Latin1String ls;
144 // transcode from UTF-16 to ISO-8859-1
145 transcode(ws, ls);
146 assert(ls == "hello world");
147 }
148
149 @system unittest
150 {
151 import std.encoding;
152
153 import std.format : format;
154
155 auto ts = dchar(0x0000FEFF) ~ "Hello World"d;
156
157 auto entry = getBOM(cast(ubyte[]) ts);
158 version (BigEndian)
159 {
160 assert(entry.schema == BOM.utf32be, format("%s", entry.schema));
161 }
162 else
163 {
164 assert(entry.schema == BOM.utf32le, format("%s", entry.schema));
165 }
166 }
167