Merge pull request #725 from knocte/threadpool_init
[mono.git] / mcs / class / corlib / Test / System / ConsoleTest.cs
1 // ConsoleTest.cs - NUnit Test Cases for the System.Console class
2 //
3 // David Brandt (bucky@keystreams.com)
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // 
7
8 using NUnit.Framework;
9 using System;
10 using System.IO;
11 using System.Text;
12
13 namespace MonoTests.System
14 {
15 [TestFixture]
16 public class ConsoleTest
17 {
18         public ConsoleTest() {}
19
20         TextWriter _err;
21         TextReader _in;
22         TextWriter _out;
23
24         [SetUp]
25         public void SetUp() 
26         {
27                 _err = Console.Error;
28                 _in = Console.In;
29                 _out = Console.Out;
30         }
31
32         [TearDown]
33         public void TearDown() 
34         {
35                 Console.SetError(_err);
36                 Console.SetIn(_in);
37                 Console.SetOut(_out);
38         }
39
40         [Test]
41         public void TestError()
42         {
43                 Assert.IsNotNull (Console.Error, "No error");
44         }
45
46         [Test]
47         public void TestIn()
48         {
49                 Assert.IsNotNull (Console.In, "No in");
50         }
51
52         [Test]
53         public void TestOut()
54         {
55                 Assert.IsNotNull (Console.Out, "No out");
56         }
57
58         [Test]
59         public void TestOpenStandardError()
60         {
61                 {
62                         Stream err = Console.OpenStandardError ();
63                         Assert.IsNotNull (err, "Can't open error #1");
64                 }
65                 {
66                         Stream err = Console.OpenStandardError (512);
67                         Assert.IsNotNull (err, "Can't open error #2");
68                 }
69                 // Spec says these are here, MS implementation says no.
70                 //{
71                 //bool errorThrown = false;
72                 //try {
73                 //Stream err = Console.OpenStandardError(-1);
74                 //} catch (ArgumentOutOfRangeException) {
75                 //errorThrown = true;
76                 //}
77                 //Assert("negative buffer error not thrown", 
78                 //errorThrown);
79                 //}
80                 //{
81                 //bool errorThrown = false;
82                 //try {
83                 //Stream err = Console.OpenStandardError(0);
84                 //} catch (ArgumentOutOfRangeException) {
85                 //errorThrown = true;
86                 //}
87                 //Assert("zero buffer error not thrown", errorThrown);
88                 //}
89         }
90
91         [Test]
92         public void TestOpenStandardInput()
93         {
94                 {
95                         Stream in1 = Console.OpenStandardInput ();
96                         Assert.IsNotNull (in1, "Can't open input #1");
97                 }
98                 {
99                         Stream in1 = Console.OpenStandardInput (512);
100                         Assert.IsNotNull (in1, "Can't open input #2");
101                 }
102                 // see commented-out tests in TestOpenStandardError
103         }
104
105         [Test]
106         public void TestOpenStandardOutput()
107         {
108                 {
109                         Stream out1 = Console.OpenStandardOutput ();
110                         Assert.IsNotNull(out1, "Can't open output #1");
111                 }
112                 {
113                         Stream out1 = Console.OpenStandardOutput(512);
114                         Assert.IsNotNull (out1, "Can't open output #2");
115                 }
116                 // see commented-out tests in TestOpenStandardError
117         }
118
119         [Test]
120         public void TestRead()
121         {
122                 String testStr = "This is a readline test";
123                 Stream s = new MemoryStream();
124                 TextWriter w = new StreamWriter(s);
125                 ((StreamWriter)w).AutoFlush = true;
126                 TextReader r = new StreamReader(s);
127                 Console.SetIn(r);
128                 w.WriteLine(testStr);
129                 s.Position = 0;
130                 char val = (char) Console.Read();
131                 Assert.AreEqual ('T', val, "Wrong read");
132         }
133
134         [Test]
135         public void TestReadLine()
136         {
137                 String testStr = "This is a readline test";
138                 Stream s = new MemoryStream();
139                 TextWriter w = new StreamWriter(s);
140                 ((StreamWriter)w).AutoFlush = true;
141                 TextReader r = new StreamReader(s);
142                 Console.SetIn(r);
143                 w.WriteLine(testStr);
144                 s.Position = 0;
145                 String line = Console.ReadLine();
146                 Assert.AreEqual (testStr, line, "Wrong line");
147         }
148
149         [Test]
150         public void TestSetError()
151         {
152                 {
153                         bool errorThrown = false;
154                         try {
155                                 Console.SetError(null);
156                         } catch (ArgumentNullException) {
157                                 errorThrown = true;
158                         }
159                         Assert.IsTrue (errorThrown, "null error error not thrown");
160                 }
161                 {
162                         String testStr = "This is a stderr test";
163                         Stream s = new MemoryStream();
164                         TextWriter w = new StreamWriter(s);
165                         ((StreamWriter)w).AutoFlush = true;
166                         TextReader r = new StreamReader(s);
167                         Console.SetError(w);
168                         Console.Error.WriteLine(testStr);
169                         s.Position = 0;
170                         String line = r.ReadLine();
171                         Assert.AreEqual (testStr, line, "Wrong line");
172                 }
173         }
174
175         [Test]
176         public void TestSetIn()
177         {
178                 {
179                         bool errorThrown = false;
180                         try {
181                                 Console.SetIn(null);
182                         } catch (ArgumentNullException) {
183                                 errorThrown = true;
184                         }
185                         Assert.IsTrue (errorThrown, "null in error not thrown");
186                 }
187                 {
188                         String testStr = "This is a stdin test";
189                         Stream s = new MemoryStream();
190                         TextWriter w = new StreamWriter(s);
191                         ((StreamWriter)w).AutoFlush = true;
192                         TextReader r = new StreamReader(s);
193                         Console.SetIn(r);
194                         w.WriteLine(testStr);
195                         s.Position = 0;
196                         String line = Console.In.ReadLine();
197                         Assert.AreEqual (testStr, line, "Wrong line");
198                 }
199         }
200
201         [Test]
202         public void TestSetOut()
203         {
204                 {
205                         bool errorThrown = false;
206                         try {
207                                 Console.SetOut(null);
208                         } catch (ArgumentNullException) {
209                                 errorThrown = true;
210                         }
211                         Assert.IsTrue (errorThrown, "null out error not thrown");
212                 }
213                 {
214                         String testStr = "This is a stdout test";
215                         Stream s = new MemoryStream();
216                         TextWriter w = new StreamWriter(s);
217                         ((StreamWriter)w).AutoFlush = true;
218                         TextReader r = new StreamReader(s);
219                         Console.SetOut(w);
220                         Console.Out.WriteLine(testStr);
221                         s.Position = 0;
222                         String line = r.ReadLine();
223                         Assert.AreEqual (testStr, line, "Wrong line");
224                 }
225         }
226         
227         [Test]
228         public void TestWrite_Params()
229         {
230                 Console.Write ("text {0}", (object[]) null);
231         }
232
233         [Test]
234         public void TestWrite()
235         {
236                 Stream s = new MemoryStream();
237                 TextWriter w = new StreamWriter(s);
238                 ((StreamWriter)w).AutoFlush = true;
239                 TextReader r = new StreamReader(s);
240                 Console.SetOut(w);
241
242                 long endPos = 0;
243
244                 String testStr = "This is a stdout write test";
245                 Console.Write(testStr);
246                 s.Position = endPos;
247                 String line = r.ReadToEnd();
248                 Assert.AreEqual (testStr, line, "Wrong line");
249                 endPos = s.Position;
250
251                 Boolean[] booleans = {true, false};
252                 foreach (bool b in booleans) {
253                         Console.Write(b);
254                         s.Position = endPos;
255                         line = r.ReadToEnd();
256                         Assert.AreEqual (b.ToString(), line, "Wrong boolean");
257                         endPos = s.Position;
258                 }
259
260                 Char[] chars = {'a', ';', '?'};
261                 foreach (Char c in chars) {
262                         Console.Write(c);
263                         s.Position = endPos;
264                         line = r.ReadToEnd();
265                         Assert.AreEqual (c.ToString(), line, "Wrong char");
266                         endPos = s.Position;
267                 }
268
269                 // test writing a null value            
270                 string x = null;
271                 Console.Write (x);
272
273                 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
274                 // TODO - write with format string
275         }
276
277         [Test]
278         public void TestWriteLine()
279         {
280                 Stream s = new MemoryStream();
281                 TextWriter w = new StreamWriter(s);
282                 ((StreamWriter)w).AutoFlush = true;
283                 TextReader r = new StreamReader(s);
284                 Console.SetOut(w);
285
286                 long endPos = 0;
287
288                 String testStr = "This is a stdout writeline test";
289                 Console.WriteLine(testStr);
290                 s.Position = endPos;
291                 String line = r.ReadLine();
292                 Assert.AreEqual (testStr, line, "Wrong line");
293                 endPos = s.Position;
294
295                 Boolean[] booleans = {true, false};
296                 foreach (bool b in booleans) {
297                         Console.WriteLine(b);
298                         s.Position = endPos;
299                         line = r.ReadLine();
300                         Assert.AreEqual (b.ToString(), line, "Wrong boolean");
301                         endPos = s.Position;
302                 }
303
304                 Char[] chars = {'a', ';', '?'};
305                 foreach (Char c in chars) {
306                         Console.WriteLine(c);
307                         s.Position = endPos;
308                         line = r.ReadLine();
309                         Assert.AreEqual (c.ToString(), line, "Wrong char");
310                         endPos = s.Position;
311                 }
312
313                 // test writing a null value            
314                 string x = null;
315                 Console.WriteLine (x);
316
317                 // TODO - Likewise for char[], decimal, double, int, long, object, single, uint32, uint64
318                 // TODO - write with format string
319         }
320         
321         [Test]
322         public void TestWriteLine_Params()
323         {
324                 Stream s = new MemoryStream();
325                 TextWriter w = new StreamWriter(s);
326                 ((StreamWriter)w).AutoFlush = true;
327                 TextReader r = new StreamReader(s);
328                 Console.SetOut(w);
329
330                 Console.WriteLine ("text {0}", (object[]) null);
331         }
332
333 #if !MOBILE
334
335 #if NET_4_5
336         [Test]
337         public void RedirectedTest ()
338         {
339                 if (Console.IsErrorRedirected) {
340                         // Assert.Inconclusive ();
341                         return;
342                 }
343
344                 Console.SetError (TextWriter.Null);
345                 Assert.IsFalse (Console.IsErrorRedirected);
346         }
347 #endif
348
349         // Bug 678357
350         [Test]
351         public void EncodingTest ()
352         {
353                 Console.OutputEncoding = Encoding.ASCII;
354                 Assert.AreEqual (Console.OutputEncoding, Console.Out.Encoding);
355                 Console.OutputEncoding = Encoding.UTF8;
356                 Assert.AreEqual (Console.OutputEncoding, Console.Out.Encoding);
357         }
358 #endif
359 }
360 }