|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
3 | 3 |
|
4 | 4 | using System;
|
5 |
| -using System.Collections.Generic; |
6 | 5 | using System.IO;
|
7 | 6 | using System.Text;
|
| 7 | +using System.Threading.Tasks; |
8 | 8 | using Xunit;
|
9 | 9 |
|
10 | 10 | public class ReadAndWrite
|
@@ -97,6 +97,11 @@ private static void WriteCore()
|
97 | 97 |
|
98 | 98 | private static void WriteLineCore()
|
99 | 99 | {
|
| 100 | + Assert.Equal(Environment.NewLine, Console.Out.NewLine); |
| 101 | + Console.Out.NewLine = "abcd"; |
| 102 | + Assert.Equal("abcd", Console.Out.NewLine); |
| 103 | + Console.Out.NewLine = Environment.NewLine; |
| 104 | + |
100 | 105 | // We just want to ensure none of these throw exceptions, we don't actually validate
|
101 | 106 | // what was written.
|
102 | 107 |
|
@@ -126,6 +131,133 @@ private static void WriteLineCore()
|
126 | 131 | Console.WriteLine("Hello World");
|
127 | 132 | }
|
128 | 133 |
|
| 134 | + [Fact] |
| 135 | + public static async Task OutWriteAndWriteLineOverloads() |
| 136 | + { |
| 137 | + TextWriter savedStandardOutput = Console.Out; |
| 138 | + try |
| 139 | + { |
| 140 | + using (var sw = new StreamWriter(new MemoryStream())) |
| 141 | + { |
| 142 | + Console.SetOut(sw); |
| 143 | + TextWriter writer = Console.Out; |
| 144 | + Assert.NotNull(writer); |
| 145 | + Assert.NotEqual(writer, sw); // the writer we provide gets wrapped |
| 146 | + |
| 147 | + // We just want to ensure none of these throw exceptions, we don't actually validate |
| 148 | + // what was written. |
| 149 | + |
| 150 | + writer.Write("{0}", 32); |
| 151 | + writer.Write("{0} {1}", 32, "Hello"); |
| 152 | + writer.Write("{0} {1} {2}", 32, "Hello", (uint)50); |
| 153 | + writer.Write("{0} {1} {2} {3}", 32, "Hello", (uint)50, (ulong)5); |
| 154 | + writer.Write("{0} {1} {2} {3} {4}", 32, "Hello", (uint)50, (ulong)5, 'a'); |
| 155 | + writer.Write(true); |
| 156 | + writer.Write('a'); |
| 157 | + writer.Write(new char[] { 'a', 'b', 'c', 'd', }); |
| 158 | + writer.Write(new char[] { 'a', 'b', 'c', 'd', }, 1, 2); |
| 159 | + writer.Write(1.23d); |
| 160 | + writer.Write(123.456M); |
| 161 | + writer.Write(1.234f); |
| 162 | + writer.Write(39); |
| 163 | + writer.Write(50u); |
| 164 | + writer.Write(50L); |
| 165 | + writer.Write(50UL); |
| 166 | + writer.Write(new object()); |
| 167 | + writer.Write("Hello World"); |
| 168 | + |
| 169 | + writer.Flush(); |
| 170 | + |
| 171 | + await writer.WriteAsync('c'); |
| 172 | + await writer.WriteAsync(new char[] { 'a', 'b', 'c', 'd' }); |
| 173 | + await writer.WriteAsync(new char[] { 'a', 'b', 'c', 'd' }, 1, 2); |
| 174 | + await writer.WriteAsync("Hello World"); |
| 175 | + |
| 176 | + await writer.WriteLineAsync('c'); |
| 177 | + await writer.WriteLineAsync(new char[] { 'a', 'b', 'c', 'd' }); |
| 178 | + await writer.WriteLineAsync(new char[] { 'a', 'b', 'c', 'd' }, 1, 2); |
| 179 | + await writer.WriteLineAsync("Hello World"); |
| 180 | + |
| 181 | + await writer.FlushAsync(); |
| 182 | + } |
| 183 | + } |
| 184 | + finally |
| 185 | + { |
| 186 | + Console.SetOut(savedStandardOutput); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + [Fact] |
| 191 | + public static unsafe void ValidateConsoleEncoding() |
| 192 | + { |
| 193 | + Assert.Same(Console.Out, Console.Out); |
| 194 | + |
| 195 | + Encoding encoding = Console.Out.Encoding; |
| 196 | + Assert.NotNull(encoding); |
| 197 | + Assert.Same(encoding, Console.Out.Encoding); |
| 198 | + |
| 199 | + // The primary purpose of ConsoleEncoding is to return an empty preamble. |
| 200 | + Assert.Equal(Array.Empty<byte>(), encoding.GetPreamble()); |
| 201 | + |
| 202 | + // There's not much validation we can do, but we can at least invoke members |
| 203 | + // to ensure they don't throw exceptions as they delegate to the underlying |
| 204 | + // encoding wrapped by ConsoleEncoding. |
| 205 | + |
| 206 | + Assert.False(string.IsNullOrWhiteSpace(encoding.EncodingName)); |
| 207 | + Assert.False(string.IsNullOrWhiteSpace(encoding.WebName)); |
| 208 | + Assert.True(encoding.CodePage >= 0); |
| 209 | + bool ignored = encoding.IsSingleByte; |
| 210 | + |
| 211 | + // And we can validate that the encoding is self-consistent by roundtripping |
| 212 | + // data between chars and bytes. |
| 213 | + |
| 214 | + string str = "This is the input string."; |
| 215 | + char[] strAsChars = str.ToCharArray(); |
| 216 | + byte[] strAsBytes = encoding.GetBytes(str); |
| 217 | + Assert.Equal(strAsBytes.Length, encoding.GetByteCount(str)); |
| 218 | + Assert.True(encoding.GetMaxByteCount(str.Length) >= strAsBytes.Length); |
| 219 | + |
| 220 | + Assert.Equal(str, encoding.GetString(strAsBytes)); |
| 221 | + Assert.Equal(str, encoding.GetString(strAsBytes, 0, strAsBytes.Length)); |
| 222 | + Assert.Equal(str, new string(encoding.GetChars(strAsBytes))); |
| 223 | + Assert.Equal(str, new string(encoding.GetChars(strAsBytes, 0, strAsBytes.Length))); |
| 224 | + fixed (byte* bytesPtr = strAsBytes) |
| 225 | + { |
| 226 | + char[] outputArr = new char[encoding.GetMaxCharCount(strAsBytes.Length)]; |
| 227 | + |
| 228 | + int len = encoding.GetChars(strAsBytes, 0, strAsBytes.Length, outputArr, 0); |
| 229 | + Assert.Equal(str, new string(outputArr, 0, len)); |
| 230 | + Assert.Equal(len, encoding.GetCharCount(strAsBytes)); |
| 231 | + Assert.Equal(len, encoding.GetCharCount(strAsBytes, 0, strAsBytes.Length)); |
| 232 | + |
| 233 | + fixed (char* charsPtr = outputArr) |
| 234 | + { |
| 235 | + len = encoding.GetChars(bytesPtr, strAsBytes.Length, charsPtr, outputArr.Length); |
| 236 | + Assert.Equal(str, new string(charsPtr, 0, len)); |
| 237 | + Assert.Equal(len, encoding.GetCharCount(bytesPtr, strAsBytes.Length)); |
| 238 | + } |
| 239 | + |
| 240 | + Assert.Equal(str, encoding.GetString(bytesPtr, strAsBytes.Length)); |
| 241 | + } |
| 242 | + |
| 243 | + Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars)); |
| 244 | + Assert.Equal(strAsBytes, encoding.GetBytes(strAsChars, 0, strAsChars.Length)); |
| 245 | + Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars)); |
| 246 | + Assert.Equal(strAsBytes.Length, encoding.GetByteCount(strAsChars, 0, strAsChars.Length)); |
| 247 | + fixed (char* charsPtr = strAsChars) |
| 248 | + { |
| 249 | + Assert.Equal(strAsBytes.Length, encoding.GetByteCount(charsPtr, strAsChars.Length)); |
| 250 | + |
| 251 | + byte[] outputArr = new byte[encoding.GetMaxByteCount(strAsChars.Length)]; |
| 252 | + Assert.Equal(strAsBytes.Length, encoding.GetBytes(strAsChars, 0, strAsChars.Length, outputArr, 0)); |
| 253 | + fixed (byte* bytesPtr = outputArr) |
| 254 | + { |
| 255 | + Assert.Equal(strAsBytes.Length, encoding.GetBytes(charsPtr, strAsChars.Length, bytesPtr, outputArr.Length)); |
| 256 | + } |
| 257 | + Assert.Equal(strAsBytes.Length, encoding.GetBytes(str, 0, str.Length, outputArr, 0)); |
| 258 | + } |
| 259 | + } |
| 260 | + |
129 | 261 | static readonly string[] s_testLines = new string[] {
|
130 | 262 | "3232 Hello32 Hello 5032 Hello 50 532 Hello 50 5 aTrueaabcdbc1.23123.4561.23439505050System.ObjectHello World",
|
131 | 263 | "32",
|
|
0 commit comments