-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathEncodingExtensionsTests.cs
653 lines (514 loc) · 30.6 KB
/
EncodingExtensionsTests.cs
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Memory.Tests.SequenceReader;
using Xunit;
namespace System.Text.Tests
{
public class EncodingExtensionsTests
{
private static readonly char[] AllScalarsAsUtf16 = CreateAllScalarsAsUtf16(); // 2,160,640 chars
private static readonly byte[] AllScalarsAsUtf8 = Encoding.UTF8.GetBytes(AllScalarsAsUtf16); // 4,382,592 bytes
private static char[] CreateAllScalarsAsUtf16()
{
List<char> list = new List<char>(2_160_640);
// Add U+0000 .. U+D7FF
for (int i = 0; i < 0xD800; i++)
{
list.Add((char)i);
}
// Add U+E000 .. U+10FFFF
Span<char> scratch = stackalloc char[2]; // max UTF-16 sequence length
for (int i = 0xE000; i <= 0x10FFFF; i++)
{
foreach (char ch in scratch.Slice(0, new Rune(i).EncodeToUtf16(scratch)))
{
list.Add(ch);
}
}
char[] allScalarsAsChars = list.ToArray();
// U+0000 .. U+D7FF = 55,296 1-char sequences
// U+E000 .. U+FFFF = 8,192 1-char sequences
// U+10000 .. U+10FFFF = 1,048,576 2-char sequences
// total = 2,160,640 chars to encode all scalars as UTF-16
//
// U+0000 .. U+007F = 128 1-byte sequences
// U+0080 .. U+07FF = 1,920 2-byte sequences
// U+0800 .. U+D7FF = 53,247 3-byte sequences
// U+E000 .. U+FFFF = 8,192 3-byte sequences
// U+10000 .. U+10FFFF = 1,048,576 4-byte sequences
// total = 4,382,592 bytes to encode all scalars as UTF-8
Assert.Equal(2_160_640, allScalarsAsChars.Length);
Assert.Equal(4_382_592, Encoding.UTF8.GetByteCount(allScalarsAsChars));
return allScalarsAsChars;
}
[Fact]
public static void Convert_Decoder_ReadOnlySpan_IBufferWriter_ParamChecks()
{
Decoder decoder = Encoding.UTF8.GetDecoder();
IBufferWriter<char> writer = new ArrayBufferWriter<char>();
Assert.Throws<ArgumentNullException>("decoder", () => EncodingExtensions.Convert((Decoder)null, ReadOnlySpan<byte>.Empty, writer, true, out _, out _));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.Convert(decoder, ReadOnlySpan<byte>.Empty, (IBufferWriter<char>)null, true, out _, out _));
}
[Fact]
public static void Convert_Decoder_ReadOnlySpan_IBufferWriter()
{
Decoder decoder = Encoding.UTF8.GetDecoder();
ArrayBufferWriter<char> writer = new ArrayBufferWriter<char>();
// First, a small input with no flushing and no leftover data.
ReadOnlySpan<byte> inputData = Encoding.UTF8.GetBytes("Hello");
EncodingExtensions.Convert(decoder, inputData, writer, flush: false, out long charsUsed, out bool completed);
Assert.Equal(5, charsUsed);
Assert.True(completed);
// Then, a large input with no flushing and leftover data.
inputData = Encoding.UTF8.GetBytes(new string('x', 20_000_000)).Concat(new byte[] { 0xE0, 0xA0 }).ToArray();
EncodingExtensions.Convert(decoder, inputData, writer, flush: false, out charsUsed, out completed);
Assert.Equal(20_000_000, charsUsed);
Assert.True(completed);
// Then, a large input with flushing and leftover data (should be replaced).
inputData = new byte[] { 0x80 }.Concat(Encoding.UTF8.GetBytes(new string('x', 20_000_000))).Concat(new byte[] { 0xE0 }).ToArray();
EncodingExtensions.Convert(decoder, inputData, writer, flush: true, out charsUsed, out completed);
Assert.Equal(20_000_002, charsUsed); // 1 for leftover at beginning, 1 for replacement at end
Assert.True(completed);
// Now make sure all of the data was decoded properly.
Assert.Equal(
expected: "Hello" + new string('x', 20_000_000) + '\u0800' + new string('x', 20_000_000) + '\ufffd',
actual: writer.WrittenSpan.ToString());
}
[Fact]
public static void Convert_Decoder_ReadOnlySequence_IBufferWriter_ParamChecks()
{
Decoder decoder = Encoding.UTF8.GetDecoder();
IBufferWriter<char> writer = new ArrayBufferWriter<char>();
Assert.Throws<ArgumentNullException>("decoder", () => EncodingExtensions.Convert((Decoder)null, ReadOnlySequence<byte>.Empty, writer, true, out _, out _));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.Convert(decoder, ReadOnlySequence<byte>.Empty, (IBufferWriter<char>)null, true, out _, out _));
}
[Fact]
public static void Convert_Decoder_ReadOnlySequence_IBufferWriter()
{
Decoder decoder = Encoding.UTF8.GetDecoder();
ArrayBufferWriter<char> writer = new ArrayBufferWriter<char>();
// First, input with no flushing and no leftover data.
ReadOnlySequence<byte> inputData = SequenceFactory.Create(
new byte[] { 0x20 }, // U+0020
new byte[] { 0x61, 0xC2 }, // U+0061 and U+0080 (continues on next line)
new byte[] { 0x80, 0xED, 0x9F, 0xBF }); // (cont.) + U+D7FF
EncodingExtensions.Convert(decoder, inputData, writer, flush: false, out long charsUsed, out bool completed);
Assert.Equal(4, charsUsed);
Assert.True(completed);
// Then, input with no flushing and leftover data.
inputData = SequenceFactory.Create(
new byte[] { 0xF4, 0x80 }); // U+100000 (continues on next line)
EncodingExtensions.Convert(decoder, inputData, writer, flush: false, out charsUsed, out completed);
Assert.Equal(0, charsUsed);
Assert.True(completed);
// Then, input with flushing and leftover data (should be replaced).
inputData = SequenceFactory.Create(
new byte[] { 0x80, 0x80 }, // (cont.)
new byte[] { 0xC2 }); // leftover data (should be replaced)
EncodingExtensions.Convert(decoder, inputData, writer, flush: true, out charsUsed, out completed);
Assert.Equal(3, charsUsed);
Assert.True(completed);
// Now make sure all of the data was decoded properly.
Assert.Equal("\u0020\u0061\u0080\ud7ff\U00100000\ufffd", writer.WrittenSpan.ToString());
}
[Fact]
public static void Convert_Encoder_ReadOnlySpan_IBufferWriter_ParamChecks()
{
Encoder encoder = Encoding.UTF8.GetEncoder();
IBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
Assert.Throws<ArgumentNullException>("encoder", () => EncodingExtensions.Convert((Encoder)null, ReadOnlySpan<char>.Empty, writer, true, out _, out _));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.Convert(encoder, ReadOnlySpan<char>.Empty, (IBufferWriter<byte>)null, true, out _, out _));
}
[Fact]
public static void Convert_Encoder_ReadOnlySpan_IBufferWriter()
{
Encoder encoder = Encoding.UTF8.GetEncoder();
ArrayBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
// First, a small input with no flushing and no leftover data.
ReadOnlySpan<char> inputData = "Hello";
EncodingExtensions.Convert(encoder, inputData, writer, flush: false, out long bytesUsed, out bool completed);
Assert.Equal(5, bytesUsed);
Assert.True(completed);
// Then, a large input with no flushing and leftover data.
inputData = new string('x', 20_000_000) + '\ud800';
EncodingExtensions.Convert(encoder, inputData, writer, flush: false, out bytesUsed, out completed);
Assert.Equal(20_000_000, bytesUsed);
Assert.True(completed);
// Then, a large input with flushing and leftover data (should be replaced).
inputData = '\udc00' + new string('x', 20_000_000) + '\ud800';
EncodingExtensions.Convert(encoder, inputData, writer, flush: true, out bytesUsed, out completed);
Assert.Equal(20_000_007, bytesUsed); // 4 for supplementary at beginning, 3 for replacement at end
Assert.True(completed);
// Now make sure all of the data was encoded properly.
// Use SequenceEqual instead of Assert.Equal for perf.
Assert.True(
Encoding.UTF8.GetBytes("Hello" + new string('x', 20_000_000) + "\U00010000" + new string('x', 20_000_000) + '\ufffd').AsSpan().SequenceEqual(writer.WrittenSpan));
}
[Fact]
public static void Convert_Encoder_ReadOnlySequence_IBufferWriter_ParamChecks()
{
Encoder encoder = Encoding.UTF8.GetEncoder();
IBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
Assert.Throws<ArgumentNullException>("encoder", () => EncodingExtensions.Convert((Encoder)null, ReadOnlySequence<char>.Empty, writer, true, out _, out _));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.Convert(encoder, ReadOnlySequence<char>.Empty, (IBufferWriter<byte>)null, true, out _, out _));
}
[Fact]
public static void Convert_Encoder_ReadOnlySequence_IBufferWriter()
{
Encoder encoder = Encoding.UTF8.GetEncoder();
ArrayBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
// First, input with no flushing and no leftover data.
ReadOnlySequence<char> inputData = SequenceFactory.Create(
new char[] { '\u0020' }, // U+0020
new char[] { '\ud7ff' }); // U+D7FF
EncodingExtensions.Convert(encoder, inputData, writer, flush: false, out long bytesUsed, out bool completed);
Assert.Equal(4, bytesUsed);
Assert.True(completed);
// Then, input with no flushing and leftover data.
inputData = SequenceFactory.Create(
new char[] { '\udbc0' }); // U+100000 (continues on next line)
EncodingExtensions.Convert(encoder, inputData, writer, flush: false, out bytesUsed, out completed);
Assert.Equal(0, bytesUsed);
Assert.True(completed);
// Then, input with flushing and leftover data (should be replaced).
inputData = SequenceFactory.Create(
new char[] { '\udc00' }, // (cont.)
new char[] { '\ud800' }); // leftover data (should be replaced)
EncodingExtensions.Convert(encoder, inputData, writer, flush: true, out bytesUsed, out completed);
Assert.Equal(7, bytesUsed);
Assert.True(completed);
// Now make sure all of the data was decoded properly.
Assert.Equal(Encoding.UTF8.GetBytes("\u0020\ud7ff\U00100000\ufffd"), writer.WrittenSpan.ToArray());
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySequence_ParamChecks()
{
ReadOnlySequence<char> sequence = new ReadOnlySequence<char>(new char[0]);
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetBytes(null, sequence));
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySequence()
{
// First try the single-segment code path.
ReadOnlySequence<char> sequence = new ReadOnlySequence<char>("Hello!".ToCharArray());
Assert.Equal(Encoding.UTF8.GetBytes("Hello!"), EncodingExtensions.GetBytes(Encoding.UTF8, sequence));
// Next try the multi-segment code path.
// We've intentionally split multi-char subsequences here to test flushing mechanisms.
sequence = SequenceFactory.Create(
new char[] { '\u0020' }, // U+0020
new char[] { '\u0061', '\u0080' }, // U+0061 and U+0080 (continues on next line)
new char[] { '\ud800' }, // U+10000 (continues on next line)
new char[] { }, // empty segment, just to make sure we handle it correctly
new char[] { '\udc00', '\udbff' }, // (cont.) + U+10FFFF (continues on next line)
new char[] { '\udfff' }, // (cont.)
new char[] { '\ud800' }); // leftover data (should be replaced)
Assert.Equal(Encoding.UTF8.GetBytes("\u0020\u0061\u0080\U00010000\U0010FFFF\ufffd"), EncodingExtensions.GetBytes(Encoding.UTF8, sequence));
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySequence_IBufferWriter_SingleSegment()
{
ReadOnlySequence<char> sequence = new ReadOnlySequence<char>("Hello".ToCharArray());
ArrayBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
long bytesWritten = EncodingExtensions.GetBytes(Encoding.UTF8, sequence, writer);
Assert.Equal(5, bytesWritten);
Assert.Equal(Encoding.UTF8.GetBytes("Hello"), writer.WrittenSpan.ToArray());
}
[Fact]
[OuterLoop] // this test takes ~10 seconds on modern hardware since it operates over GBs of data
public static void GetBytes_Encoding_ReadOnlySequence_IBufferWriter_LargeMultiSegment()
{
ReadOnlySequence<char> sequence = GetLargeRepeatingReadOnlySequence<char>(AllScalarsAsUtf16, 1500); // ~ 3.2bn chars of UTF-16 input
RepeatingValidatingBufferWriter<byte> writer = new RepeatingValidatingBufferWriter<byte>(AllScalarsAsUtf8);
long expectedBytesWritten = 1500 * (long)AllScalarsAsUtf8.Length;
long actualBytesWritten = EncodingExtensions.GetBytes(Encoding.UTF8, sequence, writer);
Assert.Equal(expectedBytesWritten, actualBytesWritten);
Assert.Equal(expectedBytesWritten, writer.TotalElementsWritten); // our writer will validate as data is written to it
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySequence_IBufferWriter_ParamChecks()
{
ReadOnlySequence<char> sequence = new ReadOnlySequence<char>(new char[0]);
IBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetBytes((Encoding)null, sequence, writer));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.GetBytes(Encoding.UTF8, sequence, (IBufferWriter<byte>)null));
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySequence_Span_ParamChecks()
{
ReadOnlySequence<char> sequence = new ReadOnlySequence<char>(new char[0]);
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetBytes((Encoding)null, sequence, Span<byte>.Empty));
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySequence_Span()
{
Span<byte> destination = stackalloc byte[32];
// First try the single-segment code path.
ReadOnlySequence<char> sequence = new ReadOnlySequence<char>("Hello!".ToCharArray());
Assert.Equal(
expected: Encoding.UTF8.GetBytes("Hello!"),
actual: destination.Slice(0, EncodingExtensions.GetBytes(Encoding.UTF8, sequence, destination)).ToArray());
// Next try the multi-segment code path.
// We've intentionally split multi-char subsequences here to test flushing mechanisms.
sequence = SequenceFactory.Create(
new char[] { '\u0020' }, // U+0020
new char[] { '\u0061', '\u0080' }, // U+0061 and U+0080 (continues on next line)
new char[] { '\ud800' }, // U+10000 (continues on next line)
new char[] { }, // empty segment, just to make sure we handle it correctly
new char[] { '\udc00', '\udbff' }, // (cont.) + U+10FFFF (continues on next line)
new char[] { '\udfff' }, // (cont.)
new char[] { '\ud800' }); // leftover data (should be replaced)
Assert.Equal(
expected: Encoding.UTF8.GetBytes("\u0020\u0061\u0080\U00010000\U0010FFFF\ufffd"),
actual: destination.Slice(0, EncodingExtensions.GetBytes(Encoding.UTF8, sequence, destination)).ToArray());
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySpan_IBufferWriter_ParamChecks()
{
IBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetBytes((Encoding)null, ReadOnlySpan<char>.Empty, writer));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.GetBytes(Encoding.UTF8, ReadOnlySpan<char>.Empty, (IBufferWriter<byte>)null));
}
[Fact]
public static void GetBytes_Encoding_ReadOnlySpan_IBufferWriter()
{
ArrayBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
// First, a small input that goes through the one-shot code path.
ReadOnlySpan<char> inputData = "Hello";
long bytesWritten = EncodingExtensions.GetBytes(Encoding.UTF8, inputData, writer);
Assert.Equal(5, bytesWritten);
Assert.Equal(Encoding.UTF8.GetBytes("Hello"), writer.WrittenSpan.ToArray());
// Then, a large input that goes through the chunked path.
// We alternate between 1-char and 2-char sequences so that the input will be split in
// several locations by the internal GetChars chunking logic. This helps us test
// that we're flowing the 'flush' parameter through the system correctly.
string largeString = string.Create(5_000_000, (object)null, (span, _) =>
{
while (span.Length >= 3)
{
span[0] = '\u00EA'; // U+00EA LATIN SMALL LETTER E WITH CIRCUMFLEX
span[1] = '\uD83D'; // U+1F405 TIGER
span[2] = '\uDC05';
span = span.Slice(3);
}
// There are 2 bytes left over.
Assert.Equal(2, span.Length);
span[0] = 'x';
span[1] = 'y';
});
writer = new ArrayBufferWriter<byte>();
inputData = largeString + '\uD800'; // standalone lead surrogate at end of input, testing replacement
bytesWritten = EncodingExtensions.GetBytes(Encoding.UTF8, inputData, writer);
Assert.Equal(10_000_001, bytesWritten); // 9,999,998 for data + 3 for repalcement char at end
// Now make sure all of the data was encoded properly.
Assert.True(Encoding.UTF8.GetBytes(largeString + "\ufffd").AsSpan().SequenceEqual(writer.WrittenSpan));
}
[Fact]
public static void GetString_Encoding_ReadOnlySequence()
{
// First try the single-segment code path.
ReadOnlySequence<byte> sequence = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes("Hello!"));
Assert.Equal("Hello!", EncodingExtensions.GetString(Encoding.UTF8, sequence));
// Next try the multi-segment code path.
// We've intentionally split multi-byte subsequences here to test flushing mechanisms.
sequence = SequenceFactory.Create(
new byte[] { 0x20 }, // U+0020
new byte[] { 0x61, 0xC2 }, // U+0061 and U+0080 (continues on next line)
new byte[] { 0x80, 0xED }, // (cont.) + U+D7FF (continues on next line)
new byte[] { }, // empty segment, just to make sure we handle it correctly
new byte[] { 0x9F, 0xBF, 0xF4, 0x80 }, // (cont.) + U+100000 (continues on next line)
new byte[] { 0x80, 0x80 }, // (cont.)
new byte[] { 0xC2 }); // leftover data (should be replaced)
Assert.Equal("\u0020\u0061\u0080\ud7ff\U00100000\ufffd", EncodingExtensions.GetString(Encoding.UTF8, sequence));
}
[Fact]
public static void GetString_Encoding_ReadOnlySequence_ParamChecks()
{
ReadOnlySequence<byte> sequence = new ReadOnlySequence<byte>(new byte[0]);
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetString(null, sequence));
}
[Fact]
public static void GetChars_Encoding_ReadOnlySequence_IBufferWriter_SingleSegment()
{
ReadOnlySequence<byte> sequence = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes("Hello"));
ArrayBufferWriter<char> writer = new ArrayBufferWriter<char>();
long charsWritten = EncodingExtensions.GetChars(Encoding.UTF8, sequence, writer);
Assert.Equal(5, charsWritten);
Assert.Equal("Hello", writer.WrittenSpan.ToString());
}
[Fact]
[OuterLoop] // this test takes ~10 seconds on modern hardware since it operates over GBs of data
public static void GetChars_Encoding_ReadOnlySequence_IBufferWriter_LargeMultiSegment()
{
ReadOnlySequence<byte> sequence = GetLargeRepeatingReadOnlySequence<byte>(AllScalarsAsUtf8, 1500); // ~ 6.5bn bytes of UTF-8 input
RepeatingValidatingBufferWriter<char> writer = new RepeatingValidatingBufferWriter<char>(AllScalarsAsUtf16);
long expectedCharsWritten = 1500 * (long)AllScalarsAsUtf16.Length;
long actualCharsWritten = EncodingExtensions.GetChars(Encoding.UTF8, sequence, writer);
Assert.Equal(expectedCharsWritten, actualCharsWritten);
Assert.Equal(expectedCharsWritten, writer.TotalElementsWritten); // our writer will validate as data is written to it
}
[Fact]
public static void GetChars_Encoding_ReadOnlySequence_IBufferWriter_ParamChecks()
{
ReadOnlySequence<byte> sequence = new ReadOnlySequence<byte>(new byte[0]);
IBufferWriter<char> writer = new ArrayBufferWriter<char>();
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetChars((Encoding)null, sequence, writer));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.GetChars(Encoding.UTF8, sequence, (IBufferWriter<char>)null));
}
[Fact]
public static void GetChars_Encoding_ReadOnlySequence_Span()
{
Span<char> destination = stackalloc char[32];
// First try the single-segment code path.
ReadOnlySequence<byte> sequence = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes("Hello!"));
Assert.Equal("Hello!", destination.Slice(0, EncodingExtensions.GetChars(Encoding.UTF8, sequence, destination)).ToString());
// Next try the multi-segment code path.
// We've intentionally split multi-byte subsequences here to test flushing mechanisms.
sequence = SequenceFactory.Create(
new byte[] { 0x20 }, // U+0020
new byte[] { 0x61, 0xC2 }, // U+0061 and U+0080 (continues on next line)
new byte[] { 0x80, 0xED }, // (cont.) + U+D7FF (continues on next line)
new byte[] { }, // empty segment, just to make sure we handle it correctly
new byte[] { 0x9F, 0xBF, 0xF4, 0x80 }, // (cont.) + U+100000 (continues on next line)
new byte[] { 0x80, 0x80 }, // (cont.)
new byte[] { 0xC2 }); // leftover data (should be replaced)
Assert.Equal("\u0020\u0061\u0080\ud7ff\U00100000\ufffd", destination.Slice(0, EncodingExtensions.GetChars(Encoding.UTF8, sequence, destination)).ToString());
}
[Fact]
public static void GetChars_Encoding_ReadOnlySequence_Span_ParamChecks()
{
ReadOnlySequence<byte> sequence = new ReadOnlySequence<byte>(new byte[0]);
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetChars((Encoding)null, sequence, Span<char>.Empty));
}
[Fact]
public static void GetChars_Encoding_ReadOnlySpan_IBufferWriter_ParamChecks()
{
IBufferWriter<char> writer = new ArrayBufferWriter<char>();
Assert.Throws<ArgumentNullException>("encoding", () => EncodingExtensions.GetChars((Encoding)null, ReadOnlySpan<byte>.Empty, writer));
Assert.Throws<ArgumentNullException>("writer", () => EncodingExtensions.GetChars(Encoding.UTF8, ReadOnlySpan<byte>.Empty, (IBufferWriter<char>)null));
}
[Fact]
public static void GetChars_Encoding_ReadOnlySpan_IBufferWriter()
{
ArrayBufferWriter<char> writer = new ArrayBufferWriter<char>();
// First, a small input that goes through the one-shot code path.
ReadOnlySpan<byte> inputData = Encoding.UTF8.GetBytes("Hello");
long charsWritten = EncodingExtensions.GetChars(Encoding.UTF8, inputData, writer);
Assert.Equal(5, charsWritten);
Assert.Equal("Hello", writer.WrittenSpan.ToString());
// Then, a large input that goes through the chunked path.
// We use U+1234 because it's a 3-byte UTF-8 sequence, which means it'll be split in
// several locations by the internal GetBytes chunking logic. This helps us test
// that we're flowing the 'flush' parameter through the system correctly.
writer = new ArrayBufferWriter<char>();
inputData = Encoding.UTF8.GetBytes(new string('\u1234', 5_000_000)).Concat(new byte[] { 0xE0 }).ToArray();
charsWritten = EncodingExtensions.GetChars(Encoding.UTF8, inputData, writer);
Assert.Equal(5_000_001, charsWritten); // 5 MM for data, 1 for replacement char at end
// Now make sure all of the data was decoded properly.
Assert.Equal(
expected: new string('\u1234', 5_000_000) + '\ufffd',
actual: writer.WrittenSpan.ToString());
}
/// <summary>
/// Returns a <see cref="ReadOnlySequence{T}"/> consisting of <paramref name="dataToRepeat"/> repeated <paramref name="repetitionCount"/> times.
/// This can be used to produce a sequence consisting of billions of elements while consuming a fraction of that memory.
/// </summary>
/// <returns></returns>
private static ReadOnlySequence<T> GetLargeRepeatingReadOnlySequence<T>(ReadOnlyMemory<T> dataToRepeat, int repetitionCount)
{
const int MAX_SEGMENT_LENGTH = 300_007; // a prime number, which ensures we'll have some multi-byte / multi-char splits if the data is long
MockSequenceSegment<T> firstSegment = null;
MockSequenceSegment<T> previousSegment = null;
MockSequenceSegment<T> lastSegment = null;
long runningTotalLength = 0;
for (int i = 0; i < repetitionCount; i++)
{
ReadOnlyMemory<T> remainingData = dataToRepeat;
while (!remainingData.IsEmpty)
{
int thisSegmentLength = Math.Min(remainingData.Length, MAX_SEGMENT_LENGTH);
lastSegment = new MockSequenceSegment<T>
{
Memory = remainingData.Slice(0, thisSegmentLength),
RunningIndex = runningTotalLength
};
if (previousSegment != null)
{
previousSegment.Next = lastSegment;
}
previousSegment = lastSegment;
if (firstSegment == null)
{
firstSegment = lastSegment;
}
remainingData = remainingData.Slice(thisSegmentLength);
runningTotalLength += thisSegmentLength;
}
}
return new ReadOnlySequence<T>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);
}
/// <summary>
/// An <see cref="IBufferWriter{T}"/> that validates that the data written to it consists of 'knownGoodData' repeated indefinitely.
/// </summary>
private class RepeatingValidatingBufferWriter<T> : IBufferWriter<T> where T : unmanaged, IEquatable<T>
{
private T[] _buffer;
private readonly ReadOnlyMemory<T> _knownGoodData;
public long TotalElementsWritten { get; private set; }
public RepeatingValidatingBufferWriter(ReadOnlyMemory<T> knownGoodData)
{
Assert.False(knownGoodData.IsEmpty);
_knownGoodData = knownGoodData;
}
public void Advance(int count)
{
ReadOnlySpan<T> bufferSpan = _buffer.AsSpan(0, count);
ReadOnlySpan<T> remainingGoodDataSpan = _knownGoodData.Span.Slice((int)(TotalElementsWritten % _knownGoodData.Length));
while (!bufferSpan.IsEmpty)
{
int compareLength = Math.Min(bufferSpan.Length, remainingGoodDataSpan.Length);
Assert.True(remainingGoodDataSpan.Slice(0, compareLength).SequenceEqual(bufferSpan.Slice(0, compareLength)));
remainingGoodDataSpan = remainingGoodDataSpan.Slice(compareLength);
if (remainingGoodDataSpan.IsEmpty)
{
remainingGoodDataSpan = _knownGoodData.Span;
}
bufferSpan = bufferSpan.Slice(compareLength);
}
TotalElementsWritten += count;
}
public Memory<T> GetMemory(int sizeHint) => throw new NotImplementedException();
public Span<T> GetSpan(int sizeHint)
{
if (_buffer is null || sizeHint > _buffer.Length)
{
_buffer = new T[Math.Max(sizeHint, 128)];
}
return _buffer;
}
}
/// <summary>
/// A <see cref="ReadOnlySequenceSegment{T}"/> where all members are public.
/// </summary>
private sealed class MockSequenceSegment<T> : ReadOnlySequenceSegment<T>
{
public new ReadOnlyMemory<T> Memory
{
get => base.Memory;
set => base.Memory = value;
}
public new ReadOnlySequenceSegment<T> Next
{
get => base.Next;
set => base.Next = value;
}
public new long RunningIndex
{
get => base.RunningIndex;
set => base.RunningIndex = value;
}
}
}
}