aboutsummaryrefslogtreecommitdiff
path: root/tests/utf8/driver.cxx
blob: f35e65e193da637e026f3089e0c6c12bfa866d2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// file      : tests/utf8/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <string>
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
import butl.utf8;
import butl.utility;
#else
#include <libbutl/utf8.mxx>
#include <libbutl/utility.mxx>
#endif

using namespace std;
using namespace butl;

int
main ()
{
  // utf8() tests.
  //
  auto utf8_error = [] (const string& s,
                        codepoint_types ts = codepoint_types::any,
                        const char32_t* wl = nullptr)
  {
    string error;
    assert (!utf8 (s, error, ts, wl));
    return error;
  };

  // Valid sequences.
  //
  // Empty.
  //
  assert (utf8 (""));

  // 1 code point.
  //
  assert (utf8 ("a"));                // 1 byte.
  assert (utf8 ("\xD0\xB0"));         // 2 bytes.
  assert (utf8 ("\xE4\xBA\x8C"));     // 3 bytes.
  assert (utf8 ("\xF0\x90\x8C\x82")); // 4 bytes.

  // Multiple code points.
  //
  assert (utf8 ("a\xD0\xB0\xE4\xBA\x8C\xF0\x90\x8C\x82"));

  // Ill-formed sequences.
  //
  // Long sequences.
  //
  assert (!utf8 ("\xF8")); // 5-byte sequence.
  assert (!utf8 ("\xFC")); // 6-byte sequence.

  assert (utf8_error ("\xF8") == "5-byte length UTF-8 sequence");
  assert (utf8_error ("\xFC") == "6-byte length UTF-8 sequence");
  assert (utf8_error ("\xFE") == "invalid UTF-8 sequence first byte (0xFE)");

  // 2-byte sequences.
  //
  assert (!utf8 ("\xC1\x80")); // Invalid first byte.
  assert (!utf8 ("\xD0y"));    // Invalid second byte.

  assert (utf8_error ("\xC1\x80") ==
          "invalid UTF-8 sequence first byte (0xC1)");

  assert (utf8_error ("\xD0y") ==
          "invalid UTF-8 sequence second byte (0x79 'y')");

  // 3-byte sequences.
  //
  assert (!utf8 ("\xE2\x70\x80")); // Invalid second byte.
  assert (!utf8 ("\xE2\x80\x70")); // Invalid third byte.

  assert (!utf8 ("\xED\xA0\x80")); // Min UTF-16 surrogate.
  assert (!utf8 ("\xED\xBF\xBF")); // Max UTF-16 surrogate.

  assert (utf8_error ("\xE2\x80\x70") ==
          "invalid UTF-8 sequence third byte (0x70 'p')");

  // 4-byte sequences.
  //
  assert (!utf8 ("\xF5\x80\x80\x80")); // Invalid first byte.
  assert (!utf8 ("\xF0\x80\x80\x80")); // Invalid second byte.
  assert (!utf8 ("\xF0\x90\x70\x80")); // Invalid third byte.
  assert (!utf8 ("\xF1\x80\x80\xC0")); // Invalid forth byte.

  assert (utf8_error ("\xF1\x80\x80\xC0") ==
          "invalid UTF-8 sequence forth byte (0xC0)");

  // Incomplete sequences.
  //
  assert (!utf8 ("\xD0"));         // 2-byte sequence.
  assert (!utf8 ("\xE4\xBA"));     // 3-byte sequence.
  assert (!utf8 ("\xF0\x90\x8C")); // 4-byte sequence.

  assert (utf8_error ("\xD0") == "incomplete UTF-8 sequence");

  // Missing sequence leading bytes.
  //
  assert (!utf8 ("\xB0xyz"));            // 2-byte sequence.
  assert (!utf8 ("\xBA\x8C\xD0\xB0yz")); // 3-byte sequence.
  assert (!utf8 ("\x8Cxyz"));            // 3-byte sequence.
  assert (!utf8 ("\x90\x8C\x82xyz"));    // 4-byte sequence.
  assert (!utf8 ("\x8C\x82xyz"));        // 4-byte sequence.
  assert (!utf8 ("\x82xyz"));            // 4-byte sequence.

  assert (utf8_error ("\xB0") == "invalid UTF-8 sequence first byte (0xB0)");

  // Above the valid codepoint range (0x10ffff + 1).
  //
  assert (!utf8 ("\xF4\x90\x80\x80"));

  assert (utf8_error ("\xF4\x90\x80\x80") ==
          "invalid UTF-8 sequence second byte (0x90)");

  // Whitelisting.
  //
  assert (utf8 ("\r\t\n"));

  // Matched codepoint types.
  //
  // Control.
  //
  assert (utf8 ("\r",   codepoint_types::control));
  assert (utf8 ("\x7F", codepoint_types::control));

  // Non-character.
  //
  assert (utf8 ("\xF4\x8F\xBF\xBF", codepoint_types::non_character));
  assert (utf8 ("\xEF\xB7\x90",     codepoint_types::non_character));

  // Private-use.
  //
  assert (utf8 ("\xEE\x80\x80",     codepoint_types::private_use));
  assert (utf8 ("\xF3\xB0\x80\x80", codepoint_types::private_use));

  // Reserved.
  //
  assert (utf8 ("\xF3\xA1\x80\x80", codepoint_types::reserved));
  assert (utf8 ("\xF0\xB0\x80\x80", codepoint_types::reserved));
  assert (utf8 ("\xF3\xA0\x82\x80", codepoint_types::reserved));

  // Format.
  //
  assert (utf8 ("\xC2\xAD",         codepoint_types::format));
  assert (utf8 ("\xD8\x80",         codepoint_types::format));
  assert (utf8 ("\xD8\x81",         codepoint_types::format));
  assert (utf8 ("\xD8\x85",         codepoint_types::format));
  assert (utf8 ("\xF3\xA0\x81\xBF", codepoint_types::format));

  // Graphic.
  //
  assert (utf8 ("\xC2\xAC",         codepoint_types::graphic));
  assert (utf8 ("\xC2\xAE",         codepoint_types::graphic));
  assert (utf8 ("\xD8\x86",         codepoint_types::graphic));
  assert (utf8 ("\xF3\xA0\x84\x80", codepoint_types::graphic));

  // Private-use & graphic.
  //
  assert (utf8 ("\xEE\x80\x80\xF3\xB0\x80\x80\xC2\xAC",
                codepoint_types::private_use | codepoint_types::graphic));

  // None.
  //
  assert (utf8 ("\t", codepoint_types::none, U"\t")); // Whitelisted.

  // Any.
  //
  assert (utf8 ("\t"));

  // Unmatched codepoint types.
  //
  assert (!utf8 ("\x7F", codepoint_types::graphic, U"\t"));      // Control.
  assert (!utf8 ("\xEF\xB7\x90", codepoint_types::graphic));     // Non-char.
  assert (!utf8 ("\xEE\x80\x80", codepoint_types::graphic));     // Private.
  assert (!utf8 ("\xF3\xA1\x80\x80", codepoint_types::graphic)); // Reserved.
  assert (!utf8 ("\xF3\xA0\x81\xBF", codepoint_types::graphic)); // Format.

  assert (utf8_error ("\xF3\xA0\x81\xBF", codepoint_types::graphic) ==
          "invalid Unicode codepoint (format)");

  assert (!utf8 ("\xC2\xAC", codepoint_types::format)); // Graphic.

  // Private-use & Graphic.
  //
  assert (!utf8 ("\xEE\x80\x80\xF3\xB0\x80\x80\xC2\xAC",
                 codepoint_types::format));

  assert (!utf8 ("a", codepoint_types::none)); // None.

  assert (utf8_error ("a", codepoint_types::none) ==
          "invalid Unicode codepoint (graphic)");

  // UTF-8 string length.
  //
  auto invalid_utf8 = [] (string s, codepoint_types ts = codepoint_types::any)
  {
    try
    {
      utf8_length (s, ts);
      return false;
    }
    catch (const invalid_argument&)
    {
      return true;
    }
  };

  assert (utf8_length ("") == 0);
  assert (utf8_length ("x\xD0\xB0\xE4\xBA\x8C\xF0\x90\x8C\x82y") == 5);

  assert (invalid_utf8 ("\xFE"));                         // Invalid byte.
  assert (invalid_utf8 ("\xD0"));                         // Incomplete.
  assert (invalid_utf8 ("\n", codepoint_types::graphic)); // Invalid codepoint.

  // to_utf8() tests.
  //
  auto roundtrip = [] (const char* s)
  {
    string r (s);
    to_utf8 (r, '?');
    return r == s;
  };

  auto sanitize = [] (string s, codepoint_types ts = codepoint_types::any)
  {
    to_utf8 (s, '?', ts);
    return s;
  };

  // Empty.
  //
  assert (roundtrip (""));

  // 1 code point.
  //
  assert (roundtrip ("a"));                // 1 byte.
  assert (roundtrip ("\xD0\xB0"));         // 2 bytes.
  assert (roundtrip ("\xE4\xBA\x8C"));     // 3 bytes.
  assert (roundtrip ("\xF0\x90\x8C\x82")); // 4 bytes.

  // Multiple code points.
  //
  assert (roundtrip ("a\xD0\xB0\xE4\xBA\x8C\xF0\x90\x8C\x82"));

  // Ill-formed sequences.
  //
  // Long sequence.
  //
  assert (sanitize ("\xF8") == "?"); // 5-byte sequence.

  // Invalid first byte followed by a second byte which ...
  //
  assert (sanitize ("\xC1\x80")     == "??");        // is a trailing byte.
  assert (sanitize ("\xC1y")        == "?y");        // starts 1-byte sequence.
  assert (sanitize ("\xC1\xD0\xB0") == "?\xD0\xB0"); // starts 2-byte sequence.
  assert (sanitize ("\xC1\xFE")     == "??");        // is not UTF-8.

  // Invalid second byte which ...
  //
  assert (sanitize ("\xD0y")        == "?y");        // starts 1-byte sequence.
  assert (sanitize ("\xD0\xD0\xB0") == "?\xD0\xB0"); // starts 2-byte sequence.
  assert (sanitize ("\xD0\xFE")     == "??");        // is not UTF-8.

  // Incomplete sequences.
  //
  assert (sanitize ("\xD0")     == "?");   // 2-byte sequence.
  assert (sanitize ("y\xD0")    == "y?");  // 2-byte sequence.
  assert (sanitize ("\xE4\xBA") == "??");  // 3-byte sequence.
  assert (sanitize ("\xD0\xD0") == "??");  // 2-byte sequence.

  // Incomplete recovery.
  //
  assert (sanitize ("\xD0\xFE")     == "??");  // 2-byte sequence.
  assert (sanitize ("\xD0\xFE\xFE") == "???"); // 2-byte sequence.

  assert (sanitize ("\xF4\x90\x80\x80") == "????"); // Above the range.
  assert (sanitize ("\xED\xA0\x80")     == "???");  // Min UTF-16 surrogate.
  assert (sanitize ("\xED\xBF\xBF")     == "???");  // Max UTF-16 surrogate.

  // Invalid codepoints.
  //
  auto sanitize_g = [&sanitize] (string s)
  {
    return sanitize (move (s), codepoint_types::graphic);
  };

  assert (sanitize_g ("\xEF\xB7\x90")  == "?");
  assert (sanitize_g ("y\xEF\xB7\x90") == "y?");
  assert (sanitize_g ("\xEF\xB7\x90y") == "?y");

  // Invalid during recovery.
  //
  assert (sanitize_g ("\xD0\n")     == "??");
  assert (sanitize_g ("\xD0\ny")    == "??y");
  assert (sanitize_g ("\xD0\xFE\n") == "???");

  assert (sanitize_g ("\xD0\xEF\xB7\x90") == "??");

  // utf8_validator::codepoint() tests.
  //
  {
    u32string r;
    size_t invalid_codepoints (0);

    string s ("a"
              "\xD0\xB0"
              "\n"                 // Control.
              "\xE4\xBA\x8C"
              "\xEE\x80\x80"       // Private-use.
              "\xF0\x90\x8C\x82");

    utf8_validator val (codepoint_types::graphic);

    for (char c: s)
    {
      pair<bool, bool> v (val.validate (c));

      if (v.first)
      {
        if (v.second)
          r.push_back (val.codepoint ());
      }
      else
        ++invalid_codepoints;
    }

    assert (r == U"a\x430\x4E8C\x10302");
    assert (invalid_codepoints == 2);
  }
}