forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadAndWrite.cs
More file actions
335 lines (295 loc) · 11.7 KB
/
ReadAndWrite.cs
File metadata and controls
335 lines (295 loc) · 11.7 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Xunit;
public class ReadAndWrite
{
[Fact]
public static void WriteOverloads()
{
TextWriter savedStandardOutput = Console.Out;
try
{
using (MemoryStream memStream = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(memStream))
{
Console.SetOut(sw);
WriteCore();
}
}
}
finally
{
Console.SetOut(savedStandardOutput);
}
}
[Fact]
[OuterLoop]
public static void WriteOverloadsToRealConsole()
{
WriteCore();
}
[Fact]
public static void WriteLineOverloads()
{
TextWriter savedStandardOutput = Console.Out;
try
{
using (MemoryStream memStream = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(memStream))
{
Console.SetOut(sw);
WriteLineCore();
}
}
}
finally
{
Console.SetOut(savedStandardOutput);
}
}
[Fact]
[OuterLoop]
public static void WriteLineOverloadsToRealConsole()
{
WriteLineCore();
}
private static void WriteCore()
{
// We just want to ensure none of these throw exceptions, we don't actually validate
// what was written.
Console.Write("{0}", 32);
Console.Write("{0}", null);
Console.Write("{0} {1}", 32, "Hello");
Console.Write("{0}", null, null);
Console.Write("{0} {1} {2}", 32, "Hello", (uint)50);
Console.Write("{0}", null, null, null);
Console.Write("{0} {1} {2} {3}", 32, "Hello", (uint)50, (ulong)5);
Console.Write("{0}", null, null, null, null);
Console.Write("{0} {1} {2} {3} {4}", 32, "Hello", (uint)50, (ulong)5, 'a');
Console.Write("{0}", null, null, null, null, null);
Console.Write(true);
Console.Write('a');
Console.Write(new char[] { 'a', 'b', 'c', 'd', });
Console.Write(new char[] { 'a', 'b', 'c', 'd', }, 1, 2);
Console.Write(1.23d);
Console.Write(123.456M);
Console.Write(1.234f);
Console.Write(39);
Console.Write(50u);
Console.Write(50L);
Console.Write(50UL);
Console.Write(new object());
Console.Write("Hello World");
}
private static void WriteLineCore()
{
Assert.Equal(Environment.NewLine, Console.Out.NewLine);
Console.Out.NewLine = "abcd";
Assert.Equal("abcd", Console.Out.NewLine);
Console.Out.NewLine = Environment.NewLine;
// We just want to ensure none of these throw exceptions, we don't actually validate
// what was written.
Console.WriteLine();
Console.WriteLine("{0}", 32);
Console.WriteLine("{0}", null);
Console.WriteLine("{0} {1}", 32, "Hello");
Console.WriteLine("{0}", null, null);
Console.WriteLine("{0} {1} {2}", 32, "Hello", (uint)50);
Console.WriteLine("{0}", null, null, null);
Console.WriteLine("{0} {1} {2} {3}", 32, "Hello", (uint)50, (ulong)5);
Console.WriteLine("{0}", null, null, null, null);
Console.WriteLine("{0} {1} {2} {3} {4}", 32, "Hello", (uint)50, (ulong)5, 'a');
Console.WriteLine("{0}", null, null, null, null, null);
Console.WriteLine(true);
Console.WriteLine('a');
Console.WriteLine(new char[] { 'a', 'b', 'c', 'd', });
Console.WriteLine(new char[] { 'a', 'b', 'c', 'd', }, 1, 2);
Console.WriteLine(1.23);
Console.WriteLine(123.456M);
Console.WriteLine(1.234f);
Console.WriteLine(39);
Console.WriteLine(50u);
Console.WriteLine(50L);
Console.WriteLine(50UL);
Console.WriteLine(new object());
Console.WriteLine("Hello World");
}
[Fact]
public static async Task OutWriteAndWriteLineOverloads()
{
TextWriter savedStandardOutput = Console.Out;
try
{
using (var sw = new StreamWriter(new MemoryStream()))
{
Console.SetOut(sw);
TextWriter writer = Console.Out;
Assert.NotNull(writer);
Assert.NotEqual(writer, sw); // the writer we provide gets wrapped
// We just want to ensure none of these throw exceptions, we don't actually validate
// what was written.
writer.Write("{0}", 32);
writer.Write("{0} {1}", 32, "Hello");
writer.Write("{0} {1} {2}", 32, "Hello", (uint)50);
writer.Write("{0} {1} {2} {3}", 32, "Hello", (uint)50, (ulong)5);
writer.Write("{0} {1} {2} {3} {4}", 32, "Hello", (uint)50, (ulong)5, 'a');
writer.Write(true);
writer.Write('a');
writer.Write(new char[] { 'a', 'b', 'c', 'd', });
writer.Write(new char[] { 'a', 'b', 'c', 'd', }, 1, 2);
writer.Write(1.23d);
writer.Write(123.456M);
writer.Write(1.234f);
writer.Write(39);
writer.Write(50u);
writer.Write(50L);
writer.Write(50UL);
writer.Write(new object());
writer.Write("Hello World");
writer.Flush();
await writer.WriteAsync('c');
await writer.WriteAsync(new char[] { 'a', 'b', 'c', 'd' });
await writer.WriteAsync(new char[] { 'a', 'b', 'c', 'd' }, 1, 2);
await writer.WriteAsync("Hello World");
await writer.WriteLineAsync('c');
await writer.WriteLineAsync(new char[] { 'a', 'b', 'c', 'd' });
await writer.WriteLineAsync(new char[] { 'a', 'b', 'c', 'd' }, 1, 2);
await writer.WriteLineAsync("Hello World");
await writer.FlushAsync();
}
}
finally
{
Console.SetOut(savedStandardOutput);
}
}
[Fact]
public static unsafe void ValidateConsoleEncoding()
{
Assert.Same(Console.Out, Console.Out);
Encoding encoding = Console.Out.Encoding;
Assert.NotNull(encoding);
Assert.Same(encoding, Console.Out.Encoding);
// The primary purpose of ConsoleEncoding is to return an empty preamble.
Assert.Equal(Array.Empty<byte>(), encoding.GetPreamble());
// There's not much validation we can do, but we can at least invoke members
// to ensure they don't throw exceptions as they delegate to the underlying
// encoding wrapped by ConsoleEncoding.
Assert.False(string.IsNullOrWhiteSpace(encoding.EncodingName));
Assert.False(string.IsNullOrWhiteSpace(encoding.WebName));
Assert.True(encoding.CodePage >= 0);
bool ignored = encoding.IsSingleByte;
// And we can validate that the encoding is self-consistent by roundtripping
// data between chars and bytes.
string str = "This is the input string.";
char[] strAsChars = str.ToCharArray();
byte[] strAsBytes = encoding.GetBytes(str);
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(str));
Assert.True(encoding.GetMaxByteCount(str.Length) >= strAsBytes.Length);
Assert.Equal(str, encoding.GetString(strAsBytes));
Assert.Equal(str, encoding.GetString(strAsBytes, 0, strAsBytes.Length));
Assert.Equal(str, new string(encoding.GetChars(strAsBytes)));
Assert.Equal(str, new string(encoding.GetChars(strAsBytes, 0, strAsBytes.Length)));
fixed (byte* bytesPtr = strAsBytes)
{
char[] outputArr = new char[encoding.GetMaxCharCount(strAsBytes.Length)];
int len = encoding.GetChars(strAsBytes, 0, strAsBytes.Length, outputArr, 0);
Assert.Equal(str, new string(outputArr, 0, len));
Assert.Equal(len, encoding.GetCharCount(strAsBytes));
Assert.Equal(len, encoding.GetCharCount(strAsBytes, 0, strAsBytes.Length));
fixed (char* charsPtr = outputArr)
{
len = encoding.GetChars(bytesPtr, strAsBytes.Length, charsPtr, outputArr.Length);
Assert.Equal(str, new string(charsPtr, 0, len));
Assert.Equal(len, encoding.GetCharCount(bytesPtr, strAsBytes.Length));
}
Assert.Equal(str, encoding.GetString(bytesPtr, strAsBytes.Length));
}
Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars));
Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars, 0, strAsChars.Length));
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars));
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars, 0, strAsChars.Length));
fixed (char* charsPtr = strAsChars)
{
Assert.Equal(strAsBytes.Length, encoding.GetByteCount(charsPtr, strAsChars.Length));
byte[] outputArr = new byte[encoding.GetMaxByteCount(strAsChars.Length)];
Assert.Equal(strAsBytes.Length, encoding.GetBytes(strAsChars, 0, strAsChars.Length, outputArr, 0));
fixed (byte* bytesPtr = outputArr)
{
Assert.Equal(strAsBytes.Length, encoding.GetBytes(charsPtr, strAsChars.Length, bytesPtr, outputArr.Length));
}
Assert.Equal(strAsBytes.Length, encoding.GetBytes(str, 0, str.Length, outputArr, 0));
}
}
static readonly string[] s_testLines = new string[] {
"3232 Hello32 Hello 5032 Hello 50 532 Hello 50 5 aTrueaabcdbc1.23123.4561.23439505050System.ObjectHello World",
"32",
"",
"32 Hello",
"",
"32 Hello 50",
"",
"32 Hello 50 5",
"",
"32 Hello 50 5 a",
"",
"True",
"a",
"abcd",
"bc",
"1.23",
"123.456",
"1.234",
"39",
"50",
"50",
"50",
"System.Object",
"Hello World",
};
[Fact]
public static void ReadAndReadLine()
{
TextWriter savedStandardOutput = Console.Out;
TextReader savedStandardInput = Console.In;
try
{
using (MemoryStream memStream = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(memStream))
{
sw.WriteLine(string.Join(Environment.NewLine, s_testLines));
sw.Flush();
memStream.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(memStream))
{
Console.SetIn(sr);
for (int i = 0; i < s_testLines[0].Length; i++)
{
Assert.Equal(s_testLines[0][i], Console.Read());
}
// Read the newline at the end of the first line.
Assert.Equal("", Console.ReadLine());
for (int i = 1; i < s_testLines.Length; i++)
{
Assert.Equal(s_testLines[i], sr.ReadLine());
}
// We should be at EOF now.
Assert.Equal(-1, Console.Read());
}
}
}
}
finally
{
Console.SetOut(savedStandardOutput);
Console.SetIn(savedStandardInput);
}
}
}