forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringWriterTests.cs
More file actions
474 lines (401 loc) · 15.3 KB
/
StringWriterTests.cs
File metadata and controls
474 lines (401 loc) · 15.3 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.Tests;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
public class StringWriterTests
{
static int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue, short.MinValue };
static int[] iArrLargeValues = new int[] { int.MaxValue, int.MaxValue - 1, int.MaxValue / 2, int.MaxValue / 10, int.MaxValue / 100 };
static int[] iArrValidValues = new int[] { 10000, 100000, int.MaxValue / 2000, int.MaxValue / 5000, short.MaxValue };
private static StringBuilder getSb()
{
var chArr = TestDataProvider.CharData;
var sb = new StringBuilder(40);
for (int i = 0; i < chArr.Length; i++)
sb.Append(chArr[i]);
return sb;
}
[Fact]
public static void Ctor()
{
StringWriter sw = new StringWriter();
Assert.NotNull(sw);
}
[Fact]
public static void CtorWithStringBuilder()
{
var sb = getSb();
StringWriter sw = new StringWriter(getSb());
Assert.NotNull(sw);
Assert.Equal(sb.Length, sw.GetStringBuilder().Length);
}
[Fact]
public static void CtorWithCultureInfo()
{
StringWriter sw = new StringWriter(new CultureInfo("en-gb"));
Assert.NotNull(sw);
Assert.Equal(new CultureInfo("en-gb"), sw.FormatProvider);
}
[Fact]
public static void SimpleWriter()
{
var sw = new StringWriter();
sw.Write(4);
var sb = sw.GetStringBuilder();
Assert.Equal("4", sb.ToString());
}
[Fact]
public static void WriteArray()
{
var chArr = TestDataProvider.CharData;
StringBuilder sb = getSb();
StringWriter sw = new StringWriter(sb);
var sr = new StringReader(sw.GetStringBuilder().ToString());
for (int i = 0; i < chArr.Length; i++)
{
int tmp = sr.Read();
Assert.Equal((int)chArr[i], tmp);
}
}
[Fact]
public static void CantWriteNullArray()
{
var sw = new StringWriter();
Assert.Throws<ArgumentNullException>(() => sw.Write(null, 0, 0));
}
[Fact]
public static void CantWriteNegativeOffset()
{
var sw = new StringWriter();
Assert.Throws<ArgumentOutOfRangeException>(() => sw.Write(new char[0], -1, 0));
}
[Fact]
public static void CantWriteNegativeCount()
{
var sw = new StringWriter();
Assert.Throws<ArgumentOutOfRangeException>(() => sw.Write(new char[0], 0, -1));
}
[Fact]
public static void CantWriteIndexLargeValues()
{
var chArr = TestDataProvider.CharData;
for (int i = 0; i < iArrLargeValues.Length; i++)
{
StringWriter sw = new StringWriter();
AssertExtensions.Throws<ArgumentException>(null, () => sw.Write(chArr, iArrLargeValues[i], chArr.Length));
}
}
[Fact]
public static void CantWriteCountLargeValues()
{
var chArr = TestDataProvider.CharData;
for (int i = 0; i < iArrLargeValues.Length; i++)
{
StringWriter sw = new StringWriter();
AssertExtensions.Throws<ArgumentException>(null, () => sw.Write(chArr, 0, iArrLargeValues[i]));
}
}
[Fact]
public static void WriteWithOffset()
{
StringWriter sw = new StringWriter();
StringReader sr;
var chArr = TestDataProvider.CharData;
sw.Write(chArr, 2, 5);
sr = new StringReader(sw.ToString());
for (int i = 2; i < 7; i++)
{
int tmp = sr.Read();
Assert.Equal((int)chArr[i], tmp);
}
}
[Fact]
public static void WriteWithLargeIndex()
{
for (int i = 0; i < iArrValidValues.Length; i++)
{
StringBuilder sb = new StringBuilder(int.MaxValue / 2000);
StringWriter sw = new StringWriter(sb);
var chArr = new char[int.MaxValue / 2000];
for (int j = 0; j < chArr.Length; j++)
chArr[j] = (char)(j % 256);
sw.Write(chArr, iArrValidValues[i] - 1, 1);
string strTemp = sw.GetStringBuilder().ToString();
Assert.Equal(1, strTemp.Length);
}
}
[Fact]
public static void WriteWithLargeCount()
{
for (int i = 0; i < iArrValidValues.Length; i++)
{
StringBuilder sb = new StringBuilder(int.MaxValue / 2000);
StringWriter sw = new StringWriter(sb);
var chArr = new char[int.MaxValue / 2000];
for (int j = 0; j < chArr.Length; j++)
chArr[j] = (char)(j % 256);
sw.Write(chArr, 0, iArrValidValues[i]);
string strTemp = sw.GetStringBuilder().ToString();
Assert.Equal(iArrValidValues[i], strTemp.Length);
}
}
[Fact]
public static void NewStringWriterIsEmpty()
{
var sw = new StringWriter();
Assert.Equal(string.Empty, sw.ToString());
}
[Fact]
public static void NewStringWriterHasEmptyStringBuilder()
{
var sw = new StringWriter();
Assert.Equal(string.Empty, sw.GetStringBuilder().ToString());
}
[Fact]
public static void ToStringReturnsWrittenData()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter(sb);
sw.Write(sb.ToString());
Assert.Equal(sb.ToString(), sw.ToString());
}
[Fact]
public static void StringBuilderHasCorrectData()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter(sb);
sw.Write(sb.ToString());
Assert.Equal(sb.ToString(), sw.GetStringBuilder().ToString());
}
[Fact]
public static void Closed_DisposedExceptions()
{
StringWriter sw = new StringWriter();
sw.Close();
ValidateDisposedExceptions(sw);
}
[Fact]
public static void Disposed_DisposedExceptions()
{
StringWriter sw = new StringWriter();
sw.Dispose();
ValidateDisposedExceptions(sw);
}
private static void ValidateDisposedExceptions(StringWriter sw)
{
Assert.Throws<ObjectDisposedException>(() => { sw.Write('a'); });
Assert.Throws<ObjectDisposedException>(() => { sw.Write(new char[10], 0, 1); });
Assert.Throws<ObjectDisposedException>(() => { sw.Write("abc"); });
}
[Fact]
public static async Task FlushAsyncWorks()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter(sb);
sw.Write(sb.ToString());
await sw.FlushAsync(); // I think this is a noop in this case
Assert.Equal(sb.ToString(), sw.GetStringBuilder().ToString());
}
[Fact]
public static void MiscWrites()
{
var sw = new StringWriter();
sw.Write('H');
sw.Write("ello World!");
Assert.Equal("Hello World!", sw.ToString());
}
[Fact]
public static async Task MiscWritesAsync()
{
var sw = new StringWriter();
await sw.WriteAsync('H');
await sw.WriteAsync(new char[] { 'e', 'l', 'l', 'o', ' ' });
await sw.WriteAsync("World!");
Assert.Equal("Hello World!", sw.ToString());
}
[Fact]
public static async Task MiscWriteLineAsync()
{
var sw = new StringWriter();
await sw.WriteLineAsync('H');
await sw.WriteLineAsync(new char[] { 'e', 'l', 'l', 'o' });
await sw.WriteLineAsync("World!");
Assert.Equal(
string.Format("H{0}ello{0}World!{0}", Environment.NewLine),
sw.ToString());
}
[Fact]
public static void GetEncoding()
{
var sw = new StringWriter();
Assert.Equal(new UnicodeEncoding(false, false), sw.Encoding);
Assert.Equal(Encoding.Unicode.WebName, sw.Encoding.WebName);
}
[Fact]
public static void TestWriteMisc()
{
using (new ThreadCultureChange("en-US")) // floating-point formatting comparison depends on culture
{
var sw = new StringWriter();
sw.Write(true);
sw.Write((char)'a');
sw.Write(new decimal(1234.01));
sw.Write((double)3452342.01);
sw.Write((int)23456);
sw.Write((long)long.MinValue);
sw.Write((float)1234.50f);
sw.Write((uint)uint.MaxValue);
sw.Write((ulong)ulong.MaxValue);
Assert.Equal("Truea1234.013452342.0123456-92233720368547758081234.5429496729518446744073709551615", sw.ToString());
}
}
[Fact]
public static void TestWriteObject()
{
var sw = new StringWriter();
sw.Write(new object());
Assert.Equal("System.Object", sw.ToString());
}
[Fact]
public static void TestWriteLineMisc()
{
using (new ThreadCultureChange("en-US")) // floating-point formatting comparison depends on culture
{
var sw = new StringWriter();
sw.WriteLine((bool)false);
sw.WriteLine((char)'B');
sw.WriteLine((int)987);
sw.WriteLine((long)875634);
sw.WriteLine((float)1.23457f);
sw.WriteLine((uint)45634563);
sw.WriteLine((ulong.MaxValue));
Assert.Equal(
string.Format("False{0}B{0}987{0}875634{0}1.23457{0}45634563{0}18446744073709551615{0}", Environment.NewLine),
sw.ToString());
}
}
[Fact]
public static void TestWriteLineObject()
{
var sw = new StringWriter();
sw.WriteLine(new object());
Assert.Equal("System.Object" + Environment.NewLine, sw.ToString());
}
[Fact]
public static void TestWriteLineAsyncCharArray()
{
StringWriter sw = new StringWriter();
sw.WriteLineAsync(new char[] { 'H', 'e', 'l', 'l', 'o' });
Assert.Equal("Hello" + Environment.NewLine, sw.ToString());
}
[Fact]
public async Task NullNewLineAsync()
{
using (MemoryStream ms = new MemoryStream())
{
string newLine;
using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8, 16, true))
{
newLine = sw.NewLine;
await sw.WriteLineAsync(default(string));
await sw.WriteLineAsync(default(string));
}
ms.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(ms))
{
Assert.Equal(newLine + newLine, await sr.ReadToEndAsync());
}
}
}
[Fact]
public async Task WriteSpanMemory_Success()
{
var sw = new StringWriter();
sw.Write((Span<char>)new char[0]);
sw.Write((Span<char>)new char[] { 'a' });
sw.Write((Span<char>)new char[] { 'b', 'c', 'd' });
sw.WriteLine((Span<char>)new char[] { 'e' });
await sw.WriteAsync((ReadOnlyMemory<char>)new char[0]);
await sw.WriteAsync((ReadOnlyMemory<char>)new char[] { 'f' });
await sw.WriteAsync((ReadOnlyMemory<char>)new char[] { 'g', 'h', 'i' });
await sw.WriteLineAsync((ReadOnlyMemory<char>)new char[] { 'j' });
Assert.Equal("abcde" + Environment.NewLine + "fghij" + Environment.NewLine, sw.ToString());
}
[Fact]
public async Task Precanceled_ThrowsException()
{
var writer = new StringWriter();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteAsync(Memory<char>.Empty, new CancellationToken(true)));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => writer.WriteLineAsync(Memory<char>.Empty, new CancellationToken(true)));
}
[Fact]
public void TestWriteStringBuilder()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter();
sw.Write(sb);
Assert.Equal(sb.ToString(), sw.ToString());
}
[Fact]
public async Task TestWriteAsyncStringBuilder()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter();
await sw.WriteAsync(sb);
Assert.Equal(sb.ToString(), sw.ToString());
}
[Fact]
public void TestWriteAsyncStringBuilderCancelled()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
Assert.Equal(TaskStatus.Canceled, sw.WriteAsync(sb, cts.Token).Status);
}
[Fact]
public void TestWriteLineStringBuilder()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter();
sw.WriteLine(sb);
Assert.Equal(sb.ToString() + Environment.NewLine, sw.ToString());
}
[Fact]
public async Task TestWriteLineAsyncStringBuilder()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter();
await sw.WriteLineAsync(sb);
Assert.Equal(sb.ToString() + Environment.NewLine, sw.ToString());
}
[Fact]
public void TestWriteLineAsyncStringBuilderCancelled()
{
StringBuilder sb = getSb();
StringWriter sw = new StringWriter();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
Assert.Equal(TaskStatus.Canceled, sw.WriteLineAsync(sb, cts.Token).Status);
}
[Fact]
public void DisposeAsync_ClosesWriterAndLeavesBuilderAvailable()
{
var sb = new StringBuilder();
var sw = new StringWriter(sb);
sw.Write("hello");
Assert.True(sw.DisposeAsync().IsCompletedSuccessfully);
Assert.True(sw.DisposeAsync().IsCompletedSuccessfully);
Assert.Throws<ObjectDisposedException>(() => sw.Write(42));
Assert.Equal("hello", sb.ToString());
}
}
}