Author: gert
Date: 2007-05-25 16:22:40 -0400 (Fri, 25 May 2007)
New Revision: 77972

Modified:
   trunk/mcs/class/corlib/System.IO/ChangeLog
   trunk/mcs/class/corlib/System.IO/UnmanagedMemoryStream.cs
   trunk/mcs/class/corlib/Test/System.IO/ChangeLog
   trunk/mcs/class/corlib/Test/System.IO/UnmanagedMemoryStreamTest.cs
Log:
* UnmanagedMemoryStream.cs: In Read and ReadByte, use Marshal.ReadByte
to read bytes as this allows us to start reading from the current
position. In Read, return 0 when reading beyond the end of the stream
and only read bytes until the end of the stream (not capacity).
In ReadByte, return -1 when reading beyond the end of the stream.
In SetLength: changed argument validation (and reported exceptions) to
match MS, removed duplicate access check and changed the current
position if length is less than position. In Write: throw
NotSupportedException when attempting to write beyond capacity, use
Marshal.WriteByte since that allows us to start writing from the
current position. Adjust length when position moves beyond length
in both Write and WriteByte. Allow position to be moved beyond
capacity of stream. Fixed position using Seek (=+ typo). Changed 
CanRead to ignore current position. Allow Position to be used to move
beyond capacity of stream.
* UnmanagedMemoryStreamTest.cs: Added tests for CanRead, CanSeek,
CanWrite, Seek, Write. Improved existing tests for SetLength,
Position. Enabled tests that were previously failing.


Modified: trunk/mcs/class/corlib/System.IO/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.IO/ChangeLog  2007-05-25 20:21:45 UTC (rev 
77971)
+++ trunk/mcs/class/corlib/System.IO/ChangeLog  2007-05-25 20:22:40 UTC (rev 
77972)
@@ -1,3 +1,21 @@
+2007-05-25  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * UnmanagedMemoryStream.cs: In Read and ReadByte, use Marshal.ReadByte
+       to read bytes as this allows us to start reading from the current
+       position. In Read, return 0 when reading beyond the end of the stream
+       and only read bytes until the end of the stream (not capacity).
+       In ReadByte, return -1 when reading beyond the end of the stream.
+       In SetLength: changed argument validation (and reported exceptions) to
+       match MS, removed duplicate access check and changed the current
+       position if length is less than position. In Write: throw
+       NotSupportedException when attempting to write beyond capacity, use
+       Marshal.WriteByte since that allows us to start writing from the
+       current position. Adjust length when position moves beyond length
+       in both Write and WriteByte. Allow position to be moved beyond
+       capacity of stream. Fixed position using Seek (=+ typo). Changed
+       CanRead to ignore current position. Allow Position to be used to move
+       beyond capacity of stream.
+
 2007-05-24  Gert Driesen  <[EMAIL PROTECTED]>
 
        * UnmanagedMemoryStream.cs: Changed argument names and exceptions 

Modified: trunk/mcs/class/corlib/System.IO/UnmanagedMemoryStream.cs
===================================================================
--- trunk/mcs/class/corlib/System.IO/UnmanagedMemoryStream.cs   2007-05-25 
20:21:45 UTC (rev 77971)
+++ trunk/mcs/class/corlib/System.IO/UnmanagedMemoryStream.cs   2007-05-25 
20:22:40 UTC (rev 77972)
@@ -1,15 +1,12 @@
-//------------------------------------------------------------------------------
 //
 // System.IO.UnmanagedMemoryStream.cs
 //
 // Copyright (C) 2006 Sridhar Kulkarni, All Rights Reserved
 //
-// Author:         Sridhar Kulkarni ([EMAIL PROTECTED])
-// Created:        Monday, July 10, 2006
+// Authors:
+//     Sridhar Kulkarni ([EMAIL PROTECTED])
+//     Gert Driesen ([EMAIL PROTECTED])
 //
-//------------------------------------------------------------------------------
-
-//
 // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
@@ -38,7 +35,6 @@
 using System.IO;
 using System.Runtime.InteropServices;
 
-
 namespace System.IO
 {
        [CLSCompliantAttribute(false)]
@@ -109,14 +105,13 @@
                        get {
                                if (closed)
                                        return false;
-                               else
-                                       return ((fileaccess == FileAccess.Read 
|| fileaccess == FileAccess.ReadWrite)? current_position < capacity : false);
+                               return (fileaccess == FileAccess.Read || 
fileaccess == FileAccess.ReadWrite);
                        }
                }
 
                public override bool CanSeek {
                        get {
-                               return ((closed) ? false : true);
+                               return !closed;
                        }
                }
                
@@ -124,8 +119,7 @@
                        get {
                                if (closed)
                                        return (false);
-                               else
-                                       return ((fileaccess == FileAccess.Write 
|| fileaccess == FileAccess.ReadWrite)? true:false);
+                               return (fileaccess == FileAccess.Write || 
fileaccess == FileAccess.ReadWrite);
                        }
                }
                public long Capacity {
@@ -148,16 +142,16 @@
                        get {
                                if (closed)
                                        throw new ObjectDisposedException("The 
stream is closed");
-                               else
-                                       return (current_position);
+                               return (current_position);
                        }
                        set {
                                if (closed)
                                        throw new ObjectDisposedException("The 
stream is closed");
-                               if (value < 0 || value > (long)Int32.MaxValue 
|| value > capacity)
-                                       throw new 
ArgumentOutOfRangeException("value that is less than zero, or the position is 
larger than Int32.MaxValue or capacity of the stream");
-                               else
-                                       current_position = value;
+                               if (value < 0)
+                                       throw new 
ArgumentOutOfRangeException("value", "Non-negative number required.");
+                               if (value > (long)Int32.MaxValue)
+                                       throw new 
ArgumentOutOfRangeException("value", "The position is larger than 
Int32.MaxValue.");
+                               current_position = value;
                        }
                }
 
@@ -189,14 +183,12 @@
                                if (fileaccess == FileAccess.Write)
                                        throw new NotSupportedException("Stream 
does not support reading");
                                else {
-                                       if (current_position == capacity)
+                                       if (current_position >= length)
                                                return (0);
                                        else {
-                                               int progress = current_position 
+ count < capacity ? count : (int) (capacity - current_position);
-                                               unsafe {
-                                                       
Marshal.Copy(initial_pointer, buffer, offset, progress);
-                                                       current_position += 
progress;
-                                               }
+                                               int progress = current_position 
+ count < length ? count : (int) (length - current_position);
+                                               for (int i = 0; i < progress; 
i++)
+                                                       buffer [offset + i] = 
Marshal.ReadByte (initial_pointer, (int) current_position++);
                                                return progress;
                                        }
                                }
@@ -204,30 +196,19 @@
                public override int ReadByte () {
                        if (closed)
                                throw new ObjectDisposedException("The stream 
is closed");
-                       if (current_position == capacity)
-                               throw new NotSupportedException("The current 
position is at the end of the stream");
 
-                       int byteread;
-
                        if (fileaccess== FileAccess.Write)
                                throw new NotSupportedException("Stream does 
not support reading");
                        else {
-                               if (current_position == length)
+                               if (current_position >= length)
                                        return (-1);
-                               else {
-                                       unsafe {
-                                               byteread = 
(int)Marshal.ReadByte(initial_pointer, (int)current_position);
-                                               current_position++;
-                                       }
-                                       return(byteread);
-                               }
+                               return (int) Marshal.ReadByte(initial_pointer, 
(int) current_position++);
                        }
                }
                public override long Seek (long offset, SeekOrigin loc) {
                        if (closed)
                                throw new ObjectDisposedException("The stream 
is closed");
-                       if (offset > capacity)
-                               throw new ArgumentOutOfRangeException("The 
offset value is larger than the maximum size of the stream");
+
                        long refpoint;
                        switch(loc) {
                        case SeekOrigin.Begin:
@@ -244,7 +225,7 @@
                        default:
                                throw new ArgumentException("Invalid SeekOrigin 
option");
                        }
-                       refpoint =+ (int)offset;
+                       refpoint += (int)offset;
                        if (refpoint < initial_position)
                                throw new IOException("An attempt was made to 
seek before the beginning of the stream");
                        current_position = refpoint;
@@ -255,14 +236,15 @@
                {
                        if (closed)
                                throw new ObjectDisposedException("The stream 
is closed");
-                       if (value < 0 || value > capacity)
-                               throw new ArgumentOutOfRangeException("The 
specified value is negative or exceeds the capacity of the stream");
+                       if (value < 0)
+                               throw new ArgumentOutOfRangeException("length", 
"Non-negative number required.");
+                       if (value > capacity)
+                               throw new IOException ("Unable to expand length 
of this stream beyond its capacity.");
                        if (fileaccess == FileAccess.Read)
-                               throw new NotSupportedException("write property 
is set to false");
-                       if (fileaccess == FileAccess.Read)
-                               throw new NotSupportedException("Length change 
not supported; see object construction");
-                       else
-                               length = value;
+                               throw new NotSupportedException ("Stream does 
not support writing.");
+                       length = value;
+                       if (length < current_position)
+                               current_position = length;
                }
 
                public override void Flush ()
@@ -285,19 +267,25 @@
                                throw new ObjectDisposedException("The stream 
is closed");
                        if (buffer == null)
                                throw new ArgumentNullException("The buffer 
parameter is a null reference");
-                       if (count > capacity)
-                               throw new ArgumentOutOfRangeException("The 
count value is greater than the capacity of the stream");
-                       if (offset < 0 || count < 0)
-                               throw new ArgumentOutOfRangeException("One of 
the specified parameters is less than zero");
+                       if ((current_position + count) > capacity)
+                               throw new NotSupportedException ("Unable to 
expand length of this stream beyond its capacity.");
+                       if (offset < 0)
+                               throw new ArgumentOutOfRangeException("offset", 
"Non-negative number required.");
+                       if (count < 0)
+                               throw new ArgumentOutOfRangeException("count", 
"Non-negative number required.");
                        if ((buffer.Length - offset) < count)
                                throw new ArgumentException("The length of the 
buffer array minus the offset parameter is less than the count parameter");
                        if (fileaccess == FileAccess.Read)
-                               throw new NotSupportedException("write property 
is set to false");
+                               throw new NotSupportedException ("Stream does 
not support writing.");
                        else {
                                unsafe {
-                                       //COPY data from managed buffer to 
unmanaged mem pointer
-                                       Marshal.Copy(buffer, offset, 
initial_pointer, (int)length);
-                                       current_position += length;
+                                       // use Marshal.WriteByte since that 
allow us to start writing
+                                       // from the current position
+                                       for (int i = 0; i < count; i++)
+                                               Marshal.WriteByte 
(initial_pointer, (int) current_position++, buffer [offset + i]);
+
+                                       if (current_position > length)
+                                               length = current_position;
                                }
                        }
                }
@@ -310,11 +298,13 @@
                        if (current_position == capacity)
                                throw new NotSupportedException("The current 
position is at the end of the capacity of the stream");
                        if (fileaccess == FileAccess.Read)
-                               throw new NotSupportedException("write property 
is set to false");
+                               throw new NotSupportedException("Stream does 
not support writing.");
                        else {
                                unsafe {
                                        Marshal.WriteByte(initial_pointer, 
(int)current_position, value);
                                        current_position++;
+                                       if (current_position > length)
+                                               length = current_position;
                                }
                        }
                }

Modified: trunk/mcs/class/corlib/Test/System.IO/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/ChangeLog     2007-05-25 20:21:45 UTC 
(rev 77971)
+++ trunk/mcs/class/corlib/Test/System.IO/ChangeLog     2007-05-25 20:22:40 UTC 
(rev 77972)
@@ -1,3 +1,9 @@
+2007-05-25  Gert Driesen  <[EMAIL PROTECTED]>
+
+       * UnmanagedMemoryStreamTest.cs: Added tests for CanRead, CanSeek,
+       CanWrite, Seek, Write. Improved existing tests for SetLength,
+       Position. Enabled tests that were previously failing.
+
 2007-05-24  Gert Driesen  <[EMAIL PROTECTED]>
 
        * UnmanagedMemoryStreamTest.cs: Fixed several tests and added several

Modified: trunk/mcs/class/corlib/Test/System.IO/UnmanagedMemoryStreamTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.IO/UnmanagedMemoryStreamTest.cs  
2007-05-25 20:21:45 UTC (rev 77971)
+++ trunk/mcs/class/corlib/Test/System.IO/UnmanagedMemoryStreamTest.cs  
2007-05-25 20:22:40 UTC (rev 77972)
@@ -8,14 +8,16 @@
 // (c) 2006 Sridhar Kulkarni.
 // Copyright (C) 2004 Novell (http://www.novell.com)
 //
+
 #if NET_2_0 && !TARGET_JVM
-using NUnit.Framework;
+using System;
+using System.Collections;
 using System.IO;
-using System;
+using System.Runtime.InteropServices;
 using System.Text;
-using System.Runtime.InteropServices;
-using System.Collections;
 
+using NUnit.Framework;
+
 namespace MonoTests.System.IO
 {
        [TestFixture]
@@ -31,10 +33,10 @@
                public void SetUp()
                {
                        testStreamData = UnicodeEncoding.Unicode.GetBytes("Here 
is some mono testdata");
-                       mem_intptr = 
Marshal.AllocHGlobal(testStreamData.Length);
+                       length = testStreamData.Length;
+                       capacity = testStreamData.Length * 2;
+                       mem_intptr = Marshal.AllocHGlobal(capacity);
                        mem_byteptr = (byte*)mem_intptr.ToPointer();
-                       length = testStreamData.Length;
-                       capacity = testStreamData.Length;
                        readData = new byte[length];
                }
 
@@ -49,7 +51,7 @@
                // Verify that the first count bytes in testBytes are the same 
as
                // the count bytes from index start in testStreamData
                //
-               void VerifyTestData(string id, byte[] testBytes, int start, int 
count)
+               void VerifyTestData (string id, byte [] testBytes, int start, 
int count)
                {
                        if (testBytes == null)
                                Assert.Fail(id + "+1 testBytes is null");
@@ -126,7 +128,6 @@
                }
 
                [Test]
-               [Category ("NotWorking")] // CanRead should return true even if 
we're at or beyond end of stream
                public void Constructor2 ()
                {
                        UnmanagedMemoryStream ums;
@@ -244,7 +245,7 @@
 
                [Test]
                [ExpectedException (typeof (ObjectDisposedException))]
-               public void Flush_Closed ()
+               public void Flush_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
@@ -253,28 +254,110 @@
                }
 
                [Test]
-               [Category ("NotWorking")]
+               public void CanRead ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.Read);
+                       Assert.IsTrue (ums.CanRead, "#1");
+                       ums.Seek (length, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanRead, "#2");
+                       ums.Seek (capacity, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanRead, "#3");
+                       ums.Seek (capacity + 1, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanRead, "#4");
+                       ums.Seek (0, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanRead, "#5");
+                       ums.Close ();
+               }
+
+               [Test]
+               public void CanRead_Stream_Closed ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                       ums.Close ();
+                       Assert.IsFalse (ums.CanRead);
+               }
+
+               [Test]
+               public void CanSeek ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.Read);
+                       Assert.IsTrue (ums.CanSeek, "#1");
+                       ums.Seek (length, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanSeek, "#2");
+                       ums.Seek (capacity, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanSeek, "#3");
+                       ums.Seek (capacity + 1, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanSeek, "#4");
+                       ums.Seek (0, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanSeek, "#5");
+                       ums.Close ();
+               }
+
+               [Test]
+               public void CanSeek_Stream_Closed ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                       Assert.IsTrue (ums.CanSeek, "#1");
+                       ums.Close ();
+                       Assert.IsFalse (ums.CanSeek, "#2");
+               }
+
+               [Test]
+               public void CanWrite ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                       Assert.IsTrue (ums.CanWrite, "#1");
+                       ums.Seek (length, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanWrite, "#2");
+                       ums.Seek (capacity, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanWrite, "#3");
+                       ums.Seek (capacity + 1, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanWrite, "#4");
+                       ums.Seek (0, SeekOrigin.Begin);
+                       Assert.IsTrue (ums.CanWrite, "#5");
+                       ums.Close ();
+               }
+
+               [Test]
+               public void CanWrite_Stream_Closed ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                       Assert.IsTrue (ums.CanWrite, "#1");
+                       ums.Close ();
+                       Assert.IsFalse (ums.CanWrite, "#2");
+               }
+
+               [Test]
                public void Read ()
                {
                        UnmanagedMemoryStream ums = new 
-                               UnmanagedMemoryStream(mem_byteptr, length, 
length, FileAccess.ReadWrite);
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
                        ums.Write (testStreamData, 0, testStreamData.Length);
                        ums.Position = 0;
 
-                       ums.Read (readData, 0, (length/2));
-                       VerifyTestData ("R1", readData, 0, (length / 2));
+                       Assert.AreEqual (length / 2, ums.Read (readData, 0, 
(length / 2)), "#1");
+                       VerifyTestData ("#2", readData, 0, (length / 2));
+                       Assert.AreEqual (length / 2, ums.Position, "#3");
                        
                        //Seek back to begining
                        ums.Seek (0, SeekOrigin.Begin);
                        
                        //Read complete stream
-                       ums.Read (readData, 0, length);
-                       VerifyTestData ("r2", readData, 0, length);
+                       Assert.AreEqual (length, ums.Read (readData, 0, 
length), "#4");
+                       VerifyTestData ("#5", readData, 0, length);
+                       Assert.AreEqual (length, ums.Position, "#6");
                        
                        //Seek to mid of the stream and read till end
                        ums.Seek ((length / 2), SeekOrigin.Begin);
                        ums.Read (readData, 0, (length / 2));
-                       VerifyTestData ("r3", readData, (length / 2), (length / 
2));
+                       VerifyTestData ("#7", readData, (length / 2), (length / 
2));
+                       Assert.AreEqual (length, ums.Position, "#8");
                        ums.Close ();
                }
 
@@ -298,7 +381,7 @@
 
                [Test]
                [ExpectedException (typeof (ObjectDisposedException))]
-               public void Read_Closed ()
+               public void Read_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
@@ -337,7 +420,9 @@
                                ums.Read (readData, 1, readData.Length);
                                Assert.Fail ("#1");
                        } catch (ArgumentException ex) {
-                               // Non-negative number required
+                               // Offset and length were out of bounds for the 
array or count
+                               // is greater than the number of elements from 
index to the end
+                               // of the source collection
                                Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
                                Assert.IsNull (ex.InnerException, "#3");
                                Assert.IsNotNull (ex.Message, "#4");
@@ -346,7 +431,7 @@
                }
 
                [Test]
-               [Category ("NotWorking")] // when reading on or beyond the end 
of the stream we must return 0
+               //[Category ("NotWorking")] // when reading on or beyond the 
end of the stream we must return 0
                public void Read_EndOfStream ()
                {
                        UnmanagedMemoryStream ums = new 
@@ -355,6 +440,10 @@
                        Assert.AreEqual (0, ums.Read (readData, 0, 1), "#1");
                        ums.Seek(length + 1, SeekOrigin.Begin);
                        Assert.AreEqual (0, ums.Read (readData, 0, 1), "#2");
+                       ums.Seek(length - 3, SeekOrigin.Begin);
+                       Assert.AreEqual (3, ums.Read (readData, 0, 5), "#3");
+                       ums.Seek(capacity + 1, SeekOrigin.Begin);
+                       Assert.AreEqual (0, ums.Read (readData, 0, 1), "#4");
                        ums.Close ();
                }
 
@@ -409,7 +498,7 @@
 
                [Test]
                [ExpectedException (typeof (ObjectDisposedException))]
-               public void ReadByte_Closed ()
+               public void ReadByte_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
@@ -418,17 +507,21 @@
                }
 
                [Test]
-               [Category ("NotWorking")] // when reading on or beyond the end 
of the stream we must return -1
+               //[Category ("NotWorking")] // when reading on or beyond the 
end of the stream we must return -1
                public void ReadByte_EndOfStream ()
                {
                        UnmanagedMemoryStream ums = new
-                               UnmanagedMemoryStream(mem_byteptr, length, 
length * 2, FileAccess.ReadWrite);
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
                        ums.Write (testStreamData, 0, testStreamData.Length);
                        ums.Position = 0;
                        ums.Seek(length, SeekOrigin.Begin);
                        Assert.AreEqual (-1, ums.ReadByte (), "#3");
                        ums.Seek(length + 1, SeekOrigin.Begin);
                        Assert.AreEqual (-1, ums.ReadByte (), "#4");
+                       ums.Seek(capacity, SeekOrigin.Begin);
+                       Assert.AreEqual (-1, ums.ReadByte (), "#5");
+                       ums.Seek(capacity + 1, SeekOrigin.Begin);
+                       Assert.AreEqual (-1, ums.ReadByte (), "#6");
                        ums.Close();
                }
 
@@ -436,7 +529,7 @@
                public void ReadByte_WriteOnly ()
                {
                        UnmanagedMemoryStream ums = new
-                               UnmanagedMemoryStream(mem_byteptr, length, 
length, FileAccess.Write);
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.Write);
                        try {
                                ums.ReadByte ();
                                Assert.Fail ("#1");
@@ -450,55 +543,139 @@
                }
 
                [Test]
-               [ExpectedException (typeof (ObjectDisposedException))]
-               public void Seek_Closed () 
+               public void Seek ()
                {
                        UnmanagedMemoryStream ums = new
-                               UnmanagedMemoryStream(mem_byteptr, length);
-                       ums.Close ();
-                       ums.Seek (0, SeekOrigin.Begin);
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.Write);
+                       Assert.AreEqual (5, ums.Seek (5, SeekOrigin.Begin), 
"#A1");
+                       Assert.AreEqual (5, ums.Position, "#A2");
+                       Assert.AreEqual (length, ums.Length, "#A3");
+
+                       ums.Seek (2, SeekOrigin.Current);
+                       //Assert.AreEqual (7, ums.Seek (2, SeekOrigin.Current), 
"#B1");
+                       Assert.AreEqual (7, ums.Position, "#B2");
+                       Assert.AreEqual (length, ums.Length, "#B3");
+
+                       Assert.AreEqual (length + 2, ums.Seek (2, 
SeekOrigin.End), "#C1");
+                       Assert.AreEqual (length + 2, ums.Position, "#C2");
+                       Assert.AreEqual (length, ums.Length, "#C3");
+
+                       Assert.AreEqual (0, ums.Seek (0, SeekOrigin.Begin), 
"#D1");
+                       Assert.AreEqual (0, ums.Position, "#D2");
+                       Assert.AreEqual (length, ums.Length, "#D3");
+
+                       Assert.AreEqual (length - 2, ums.Seek (-2, 
SeekOrigin.End), "#E1");
+                       Assert.AreEqual (length - 2, ums.Position, "#E2");
+                       Assert.AreEqual (length, ums.Length, "#E3");
+
+                       Assert.AreEqual (length - 5, ums.Seek (-3, 
SeekOrigin.Current), "#F1");
+                       Assert.AreEqual (length - 5, ums.Position, "#F2");
+                       Assert.AreEqual (length, ums.Length, "#F3");
+
+                       Assert.AreEqual (capacity + 5, ums.Seek (capacity + 5, 
SeekOrigin.Begin), "#G1");
+                       Assert.AreEqual (capacity + 5, ums.Position, "#G2");
+                       Assert.AreEqual (length, ums.Length, "#G3");
                }
 
                [Test]
-               public void Seek_EndOfStream ()
+               public void Seek_Origin_Invalid ()
                {
                        UnmanagedMemoryStream ums = new
                                UnmanagedMemoryStream(mem_byteptr, 5, 10, 
FileAccess.Read);
-                       ums.Seek(5, SeekOrigin.Begin);
+                       try {
+                               ums.Seek(1, (SeekOrigin) 666);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentException ex) {
+                               // Invalid seek origin
+                               Assert.AreEqual (typeof (ArgumentException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNull (ex.ParamName, "#5");
+                       }
                        ums.Close();
                }
 
                [Test]
-               [Category ("NotWorking")] // seeking beyond length or even 
beyond capacity must be allowed
-               public void Seek_Offset_Capacity ()
+               public void Seek_Offset_Invalid ()
                {
-                       UnmanagedMemoryStream ums;
+                       UnmanagedMemoryStream ums = new
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                       ums.Write (testStreamData, 0, testStreamData.Length);
+                       ums.Position = 0;
+                       try {
+                               ums.Seek(-1, SeekOrigin.Begin);
+                               Assert.Fail ("#A1");
+                       } catch (IOException ex) {
+                               // An attempt was made to move the position 
before the beginning
+                               // of the stream
+                               Assert.AreEqual (typeof (IOException), 
ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                       }
 
-                       // offset equals capacity
-                       ums = new UnmanagedMemoryStream(mem_byteptr, 5, 6, 
FileAccess.Read);
-                       ums.Seek(ums.Capacity, SeekOrigin.Begin);
-                       ums.Close();
+                       ums.Position = 2;
+                       try {
+                               ums.Seek(-3, SeekOrigin.Current);
+                               Assert.Fail ("#B1");
+                       } catch (IOException ex) {
+                               // An attempt was made to move the position 
before the beginning
+                               // of the stream
+                               Assert.AreEqual (typeof (IOException), 
ex.GetType (), "#B2");
+                               Assert.IsNull (ex.InnerException, "#B3");
+                               Assert.IsNotNull (ex.Message, "#B4");
+                       }
 
-                       // offset exceeds capacity
-                       ums = new UnmanagedMemoryStream(mem_byteptr, 5, 6, 
FileAccess.Read);
-                       ums.Seek(int.MaxValue, SeekOrigin.Begin);
                        ums.Close();
                }
 
                [Test]
-               public void Seek_Offset_Negative ()
+               [ExpectedException (typeof (ObjectDisposedException))]
+               public void Seek_Stream_Closed () 
                {
                        UnmanagedMemoryStream ums = new
-                               UnmanagedMemoryStream(mem_byteptr, length, 
length, FileAccess.ReadWrite);
-                       ums.Write (testStreamData, 0, testStreamData.Length);
+                               UnmanagedMemoryStream(mem_byteptr, length);
+                       ums.Close ();
+                       ums.Seek (0, SeekOrigin.Begin);
+               }
+
+               [Test]
+               public void Write ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                       ums.Write (testStreamData, 0, length);
+                       Assert.AreEqual (capacity, ums.Capacity, "#A1");
+                       Assert.AreEqual (length, ums.Position, "#A2");
+                       Assert.AreEqual (length, ums.Length, "#A3");
                        ums.Position = 0;
+                       ums.Read (readData, 0, length);
+                       Assert.AreEqual (capacity, ums.Capacity, "#B1");
+                       Assert.AreEqual (length, ums.Position, "#B2");
+                       Assert.AreEqual (length, ums.Length, "#B3");
+                       VerifyTestData ("#B4", readData, 0, length);
+                       ums.Write (testStreamData, 2, 2);
+                       Assert.AreEqual (capacity, ums.Capacity, "#C1");
+                       Assert.AreEqual (length + 2, ums.Position, "#C1");
+                       Assert.AreEqual (length + 2, ums.Length, "#C2");
+                       ums.Position = length;
+                       Assert.AreEqual (testStreamData [2], ums.ReadByte (), 
"#D1");
+                       Assert.AreEqual (testStreamData [3], ums.ReadByte (), 
"#D2");
+                       ums.Close();
+               }
+
+               [Test]
+               public void Write_Capacity_Exceeded ()
+               {
+                       UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, length, 
length + 2, FileAccess.ReadWrite);
+                       ums.Write (testStreamData, 0, length);
+                       ums.Write (testStreamData, 0, 2);
                        try {
-                               ums.Seek(-1, SeekOrigin.Begin);
+                               ums.Write (testStreamData, 0, 1);
                                Assert.Fail ("#1");
-                       } catch (IOException ex) {
-                               // An attempt was made to move the position 
before the beginning
-                               // of the stream
-                               Assert.AreEqual (typeof (IOException), 
ex.GetType (), "#2");
+                       } catch (NotSupportedException ex) {
+                               // Unable to expand length of this stream 
beyond its capacity
+                               Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#2");
                                Assert.IsNull (ex.InnerException, "#3");
                                Assert.IsNotNull (ex.Message, "#4");
                        }
@@ -506,20 +683,46 @@
                }
 
                [Test]
-               public void Write ()
+               public void Write_Count_Negative ()
                {
-                       UnmanagedMemoryStream ums = new 
+                       UnmanagedMemoryStream ums = new
                                UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
-                       ums.Write(testStreamData, 0, length);
-                       ums.Position = 0;
-                       ums.Read(readData, 0, length);
-                       VerifyTestData("RW1", readData, 0, length);
+                       try {
+                               ums.Write (testStreamData, 0, -1);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // Non-negative number required
+                               Assert.AreEqual (typeof 
(ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("count", ex.ParamName, "#6");
+                       }
                        ums.Close();
                }
 
                [Test]
+               public void Write_Offset_Negative ()
+               {
+                       UnmanagedMemoryStream ums = new
+                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                       try {
+                               ums.Write (testStreamData, -1, 
testStreamData.Length);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // Non-negative number required
+                               Assert.AreEqual (typeof 
(ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("offset", ex.ParamName, "#6");
+                       }
+                       ums.Close();
+               }
+
+               [Test]
                [ExpectedException (typeof (ObjectDisposedException))]
-               public void Write_Closed ()
+               public void Write_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
@@ -528,7 +731,7 @@
                }
 
                [Test]
-               public void Write_ReadOnly ()
+               public void Write_Stream_ReadOnly ()
                {
                        UnmanagedMemoryStream ums = new
                                UnmanagedMemoryStream(mem_byteptr, length);
@@ -548,16 +751,39 @@
                public void WriteByte ()
                {
                        UnmanagedMemoryStream ums = new 
-                               UnmanagedMemoryStream(mem_byteptr, length, 
capacity, FileAccess.ReadWrite);
+                               UnmanagedMemoryStream(mem_byteptr, 3, 5, 
FileAccess.ReadWrite);
                        ums.WriteByte (testStreamData [0]);
+                       Assert.AreEqual (5, ums.Capacity, "#A1");
+                       Assert.AreEqual (1, ums.Position, "#A2");
+                       Assert.AreEqual (3, ums.Length, "#A3");
+                       ums.WriteByte (testStreamData [1]);
+                       Assert.AreEqual (5, ums.Capacity, "#B1");
+                       Assert.AreEqual (2, ums.Position, "#B2");
+                       Assert.AreEqual (3, ums.Length, "#B3");
+                       ums.WriteByte (testStreamData [2]);
+                       Assert.AreEqual (5, ums.Capacity, "#C1");
+                       Assert.AreEqual (3, ums.Position, "#C2");
+                       Assert.AreEqual (3, ums.Length, "#C3");
+                       ums.WriteByte (testStreamData [3]);
+                       Assert.AreEqual (5, ums.Capacity, "#D1");
+                       Assert.AreEqual (4, ums.Position, "#D2");
+                       Assert.AreEqual (4, ums.Length, "#D3");
+                       ums.WriteByte (testStreamData [4]);
+                       Assert.AreEqual (5, ums.Capacity, "#E1");
+                       Assert.AreEqual (5, ums.Position, "#E2");
+                       Assert.AreEqual (5, ums.Length, "#E3");
                        ums.Seek (0, SeekOrigin.Begin);
-                       Assert.AreEqual (testStreamData [0], ums.ReadByte ());
+                       Assert.AreEqual (testStreamData [0], ums.ReadByte (), 
"#F1");
+                       Assert.AreEqual (testStreamData [1], ums.ReadByte (), 
"#F2");
+                       Assert.AreEqual (testStreamData [2], ums.ReadByte (), 
"#F3");
+                       Assert.AreEqual (testStreamData [3], ums.ReadByte (), 
"#F4");
+                       Assert.AreEqual (testStreamData [4], ums.ReadByte (), 
"#F5");
                        ums.Close ();
                }
 
                [Test]
                [ExpectedException (typeof (ObjectDisposedException))]
-               public void WriteByte_Closed ()
+               public void WriteByte_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
UnmanagedMemoryStream(mem_byteptr,
                                length, length, FileAccess.Write);
@@ -566,9 +792,28 @@
                }
 
                [Test]
-               public void WriteByte_ReadOnly ()
+               public void WriteByte_Capacity_Exceeded ()
                {
                        UnmanagedMemoryStream ums = new 
+                               UnmanagedMemoryStream(mem_byteptr, 1, 2, 
FileAccess.ReadWrite);
+                       ums.WriteByte (0x44);
+                       ums.WriteByte (0x45);
+                       try {
+                               ums.WriteByte (0x46);
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               // Unable to expand length of this stream 
beyond its capacity
+                               Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+                       ums.Close();
+               }
+
+               [Test]
+               public void WriteByte_Stream_ReadOnly ()
+               {
+                       UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
                        try {
                                ums.WriteByte (testStreamData [0]);
@@ -583,18 +828,90 @@
                }
 
                [Test]
-               [ExpectedException (typeof (NotSupportedException))]
-               public void _SetLength()
+               public void SetLength ()
                {
-                       UnmanagedMemoryStream ums = new 
-                               UnmanagedMemoryStream(mem_byteptr, length);
-                       ums.SetLength((length - 2));
+                       UnmanagedMemoryStream ums = new 
UnmanagedMemoryStream(mem_byteptr,
+                               length, capacity, FileAccess.ReadWrite);
+                       ums.Write (testStreamData, 0, testStreamData.Length);
+                       ums.SetLength (length - 1);
+                       Assert.AreEqual (capacity, ums.Capacity, "#A1");
+                       Assert.AreEqual (length - 1, ums.Length, "#A2");
+                       Assert.AreEqual (length - 1, ums.Position, "#A3");
+                       ums.SetLength (length + 1);
+                       Assert.AreEqual (capacity, ums.Capacity, "#B1");
+                       Assert.AreEqual (length + 1, ums.Length, "#B2");
+                       Assert.AreEqual (length - 1, ums.Position, "#B3");
+                       ums.SetLength (length);
+                       Assert.AreEqual (capacity, ums.Capacity, "#C1");
+                       Assert.AreEqual (length, ums.Length, "#C2");
+                       Assert.AreEqual (length - 1, ums.Position, "#C3");
+                       ums.SetLength (0);
+                       Assert.AreEqual (capacity, ums.Capacity, "#D1");
+                       Assert.AreEqual (0, ums.Length, "#D2");
+                       Assert.AreEqual (0, ums.Position, "#D3");
+                       ums.SetLength (capacity);
+                       Assert.AreEqual (capacity, ums.Capacity, "#E1");
+                       Assert.AreEqual (capacity, ums.Length, "#E2");
+                       Assert.AreEqual (0, ums.Position, "#E3");
                        ums.Close();
                }
 
                [Test]
+               public void SetLength_Capacity_Exceeded ()
+               {
+                       UnmanagedMemoryStream ums = new 
UnmanagedMemoryStream(mem_byteptr,
+                               length, capacity, FileAccess.ReadWrite);
+                       try {
+                               ums.SetLength (capacity + 1);
+                               Assert.Fail ("#1");
+                       } catch (IOException ex) {
+                               // Unable to expand length of this stream 
beyond its capacity
+                               Assert.AreEqual (typeof (IOException), 
ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+                       ums.Close();
+               }
+
+               [Test]
+               public void SetLength_Negative ()
+               {
+                       UnmanagedMemoryStream ums = new 
UnmanagedMemoryStream(mem_byteptr,
+                               length, capacity, FileAccess.ReadWrite);
+                       try {
+                               ums.SetLength(-1);
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // Non-negative number required
+                               Assert.AreEqual (typeof 
(ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("length", ex.ParamName, "#6");
+                       }
+                       ums.Close();
+               }
+
+               [Test]
+               public void SetLength_Stream_ReadOnly ()
+               {
+                       UnmanagedMemoryStream ums = new 
UnmanagedMemoryStream(mem_byteptr,
+                               length);
+                       try {
+                               ums.SetLength (length);
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               // Stream does not support writing
+                               Assert.AreEqual (typeof 
(NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+                       ums.Close();
+               }
+
+               [Test]
                [ExpectedException (typeof (ObjectDisposedException))]
-               public void Capacity_Disposed()
+               public void Capacity_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
@@ -604,55 +921,109 @@
 
                [Test]
                [ExpectedException (typeof (ObjectDisposedException))]
-               public void Close_get_Length()
+               public void Length_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
                        ums.Close();
                        long x = ums.Length;
                }
-               
+
                [Test]
-               [ExpectedException (typeof (ObjectDisposedException))]
-               public void Close_get_Position()
+               public void Position ()
                {
-                       UnmanagedMemoryStream ums = new 
-                               UnmanagedMemoryStream(mem_byteptr, length);
+                       UnmanagedMemoryStream ums = new 
UnmanagedMemoryStream(mem_byteptr,
+                               length, capacity, FileAccess.ReadWrite);
+                       Assert.AreEqual (0, ums.Position, "#1");
+                       ums.Position = capacity;
+                       Assert.AreEqual (capacity, ums.Position, "#2");
+                       ums.Position = length;
+                       Assert.AreEqual (length, ums.Position, "#3");
+                       ums.Position = int.MaxValue;
+                       Assert.AreEqual (int.MaxValue, ums.Position, "#4");
+                       ums.Position = 0;
+                       Assert.AreEqual (0, ums.Position, "#5");
                        ums.Close();
-                       long x = ums.Position;
                }
 
                [Test]
-               [ExpectedException (typeof (ObjectDisposedException))]
-               public void Close_set_Position()
+               public void Position_MaxValue_Exceeded ()
                {
+                       UnmanagedMemoryStream ums = new 
UnmanagedMemoryStream(mem_byteptr,
+                               length, capacity, FileAccess.ReadWrite);
+                       try {
+                               ums.Position = 0x80000000;
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // MemoryStream length must be non-negative and 
less than
+                               // 2^31 - 1 - origin
+                               Assert.AreEqual (typeof 
(ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("value", ex.ParamName, "#6");
+                       }
+                       ums.Close();
+               }
+
+               [Test]
+               public void Position_Negative ()
+               {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
-                       ums.Close();
-                       ums.Position = (length - 1);// Set to some acceptable 
value.
+                       try {
+                               ums.Position = -1;
+                               Assert.Fail ("#1");
+                       } catch (ArgumentOutOfRangeException ex) {
+                               // Non-negative number required
+                               Assert.AreEqual (typeof 
(ArgumentOutOfRangeException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                               Assert.IsNotNull (ex.ParamName, "#5");
+                               Assert.AreEqual ("value", ex.ParamName, "#6");
+                       }
+                       ums.Close ();
                }
+
                [Test]
-               [Category ("NotWorking")]
-               [ExpectedException (typeof (ObjectDisposedException))]
-               public void Close_get_PositionPointer()
+               public void Position_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
                        ums.Close();
-                       byte* bptr = ums.PositionPointer;
+                       try {
+                               long x = ums.Position;
+                               Assert.Fail ("#1: " + x);
+                       } catch (ObjectDisposedException) {
+                       }
+
+                       try {
+                               ums.Position = 0;
+                               Assert.Fail ("#2");
+                       } catch (ObjectDisposedException) {
+                       }
                }
 
                [Test]
                [Category ("NotWorking")]
-               [ExpectedException (typeof (ObjectDisposedException))]
-               public void Close_set_PositionPointer()
+               public void PositionPointer_Stream_Closed ()
                {
                        UnmanagedMemoryStream ums = new 
                                UnmanagedMemoryStream(mem_byteptr, length);
                        ums.Close();
-                       ums.PositionPointer = (byte *) (length - 1); //position 
pointer to somewhere within the length/capacity
+                       try {
+                               byte* bptr = ums.PositionPointer;
+                               Assert.Fail ("#1");
+                       } catch (ObjectDisposedException) {
+                       }
+
+                       try {
+                               // position pointer to somewhere within the 
capacity
+                               ums.PositionPointer = (byte*) (capacity - 1);
+                               Assert.Fail ("#2");
+                       } catch (ObjectDisposedException) {
+                       }
                }
        }
 }
 #endif
-

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to