Merge pull request #1248 from kumpera/kill_the_interpreter
[mono.git] / mcs / class / corlib / Test / System.IO / StreamReaderTest.cs
index e1bdbac1c2aa907470ed3b09322797dcb8838868..54e5e04243583a489dff3547f3eb5d601d8d08fa 100644 (file)
@@ -9,6 +9,9 @@
 using System;
 using System.IO;
 using System.Text;
+#if NET_4_5
+using System.Threading.Tasks;
+#endif
 
 using NUnit.Framework;
 
@@ -363,12 +366,12 @@ public class StreamReaderTest
                m.Close();
        }
 
-       public void TestCurrentEncoding() {
+       public void TestCurrentEncoding()
+       {
                Byte[] b = {};
                MemoryStream m = new MemoryStream(b);
                StreamReader r = new StreamReader(m);
-               Assert.AreEqual (Encoding.UTF8.GetType (), r.CurrentEncoding.GetType (),
-                       "wrong encoding");
+               Assert.AreSame (Encoding.UTF8, r.CurrentEncoding, "wrong encoding");
        }
 
        // TODO - Close - annoying spec - won't commit to any exceptions. How to test?
@@ -726,12 +729,33 @@ public class StreamReaderTest
                        Assert.Fail ("Failed to detect UTF16LE encoded string");
                if (!CheckEncodingDetected(Encoding.BigEndianUnicode))
                        Assert.Fail ("Failed to detect UTF16BE encoded string");
-#if NET_2_0
                if (!CheckEncodingDetected(Encoding.UTF32))
                        Assert.Fail ("Failed to detect UTF32LE encoded string");
                if (!CheckEncodingDetected(new UTF32Encoding(true, true)))
                        Assert.Fail ("Failed to detect UTF32BE encoded string");
-#endif
+       }
+
+       // This is a special case, where the StreamReader has less than 4 bytes at 
+       // encoding detection time, so it tries to check for Unicode encoding, instead of
+       // waiting for more bytes to test against the UTF32 BOM.
+       [Test]
+       public void EncodingDetectionUnicode ()
+       {
+               byte [] bytes = new byte [3];
+               bytes [0] = 0xff;
+               bytes [1] = 0xfe;
+               bytes [2] = 0;
+               MemoryStream inStream = new MemoryStream (bytes);
+               StreamReader reader = new StreamReader (inStream, Encoding.UTF8, true);
+
+               // It should start with the encoding we used in the .ctor
+               Assert.AreEqual (Encoding.UTF8, reader.CurrentEncoding, "#A1");
+
+               reader.Read ();
+               //reader.Read ();
+               Assert.AreEqual (Encoding.Unicode, reader.CurrentEncoding, "#B1");
+
+               reader.Close ();
        }
 
        private bool CheckEncodingDetected(Encoding encoding)
@@ -749,6 +773,21 @@ public class StreamReaderTest
                return decodedString == TestString;
        }
     
+    [Test] // Bug445326
+       [Category ("MobileNotWorking")]
+       public void EndOfBufferIsCR ()
+       {
+               using (StreamReader reader = new StreamReader ("Test/resources/Fergie.GED")) {
+                       string line;
+                       int count = 0;
+                       while ((line = reader.ReadLine ()) != null) {
+                               Assert.IsFalse (line.Length > 1000, "#1 " + count);
+                               count++;
+                       }
+                       Assert.AreEqual (16107, count, "#2");
+               }
+       }
+
        [Test]
        public void bug75526 ()
        {
@@ -766,5 +805,125 @@ public class StreamReaderTest
                        return 2;
                }
        }
+
+       [Test]
+       public void PeekWhileBlocking ()
+       {
+               StreamReader reader = new StreamReader (new MyStream ());
+               int c = reader.Read ();
+               Assert.IsFalse (reader.EndOfStream);
+               string str = reader.ReadToEnd ();
+               Assert.AreEqual ("bc", str);
+       }
+
+       [Test]
+       public void EncodingChangedAuto ()
+       {
+               int testlines = 2048; // all data should larger than stream reader default buffer size
+               string testdata = "test";
+               MemoryStream ms = new MemoryStream();
+               // write utf8 encoding data.
+               using (StreamWriter sw = new StreamWriter (ms, Encoding.UTF8)) {
+                       for (int i = 0; i < testlines; i++)
+                               sw.WriteLine (testdata);
+               }
+
+               MemoryStream readms = new MemoryStream (ms.GetBuffer());
+               using (StreamReader sr = new StreamReader(readms, Encoding.Unicode, true)) {
+                       for (int i = 0; i < testlines; i++) {
+                               string line = sr.ReadLine ();
+                               if (line != testdata)
+                                       Assert.Fail ("Wrong line content");
+                       }
+               }
+       }
+
+       [Test]
+       public void NullStream ()
+       {
+               var buffer = new char[2];
+               Assert.AreEqual (0, StreamReader.Null.ReadBlock (buffer, 0, buffer.Length));
+       }
+
+#if NET_4_5
+       [Test]
+       public void ReadLineAsync ()
+       {
+               MemoryStream ms = new MemoryStream ();
+               StreamWriter sw = new StreamWriter (ms, Encoding.UTF8);
+               sw.WriteLine ("a");
+               sw.WriteLine ("b");
+               sw.Flush ();
+               ms.Seek (0, SeekOrigin.Begin);
+
+               Func<Task<string>> res = async () => {
+                       using (StreamReader reader = new StreamReader (ms)) {
+                               return await reader.ReadLineAsync () + await reader.ReadToEndAsync () + await reader.ReadToEndAsync ();
+                       }
+               };
+
+               var result = res ();
+               Assert.IsTrue (result.Wait (3000), "#1");
+               Assert.AreEqual ("ab" + Environment.NewLine, result.Result);
+       }
+#endif
+}
+
+class MyStream : Stream {
+       int n;
+
+       public override int Read (byte [] buffer, int offset, int size)
+       {
+               if (n == 0) {
+                       buffer [offset] = (byte) 'a';
+                       n++;
+                       return 1;
+               } else if (n == 1) {
+                       buffer [offset] = (byte) 'b';
+                       buffer [offset + 1] = (byte) 'c';
+                       n++;
+                       return 2;
+               }
+               return 0;
+       }
+
+       public override bool CanRead {
+               get { return true; }
+       }
+
+       public override bool CanSeek {
+               get { return false; }
+       }
+
+       public override bool CanWrite {
+               get { return false; }
+       }
+
+       public override long Length {
+               get { return 0; }
+       }
+
+       public override long Position {
+               get { return 0; }
+                       set { }
+       }
+
+       public override void Flush ()
+       {
+       }
+
+       public override long Seek (long offset, SeekOrigin origin)
+       {
+               return 0;
+       }
+
+       public override void SetLength (long value)
+       {
+       }
+
+       public override void Write (byte[] buffer, int offset, int count)
+       {
+       }
 }
+
 }