Browse Source

2004-05-24 Sebastien Pouliot <[email protected]>

	* StringWriterTest.cs: New tests for integer overflow and disposed
	exceptions.

svn path=/trunk/mcs/; revision=27985
Sebastien Pouliot 21 năm trước cách đây
mục cha
commit
2785a192ff

+ 2 - 0
mcs/class/corlib/Test/System.IO/ChangeLog

@@ -3,6 +3,8 @@
 	* MemoryStreamTest.cs: Test that we zeroize old data when we manipulate
 	the length of the stream. Verify that we throw the same exceptions as
 	MS fx.
+	* StringWriterTest.cs: New tests for integer overflow and disposed 
+	exceptions.
 
 2004-05-20  Sebastien Pouliot  <[email protected]>
 

+ 138 - 25
mcs/class/corlib/Test/System.IO/StringWriterTest.cs

@@ -1,16 +1,17 @@
 //
 // System.IO.StringWriter
 //
-// Authors:
+// Authors:
 //	Marcin Szczepanski ([email protected])
-//	Ben Maurer <[email protected]>
+//	Ben Maurer <[email protected]>
+//	Sebastien Pouliot  <[email protected]>
 //
-// TODO: Add some testing for exceptions
+// Copyright (C) 2004 Novell (http://www.novell.com)
 //
 
 using NUnit.Framework;
 using System.IO;
-using System;
+using System;
 using System.Globalization;
 using System.Text;
 
@@ -28,27 +29,27 @@ public class StringWriterTest : Assertion {
         public void TestCultureInfoConstructor() {
 
 		StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
-		AssertNotNull( writer.GetStringBuilder() );
-		
-		AssertEquals( String.Empty, writer.ToString() );
-		
-		writer.Write( 'A' );
-		AssertEquals( "A", writer.ToString() );
-		
-		writer.Write( " foo" );
-		AssertEquals( "A foo", writer.ToString() );
-		
-		
-		char[] testBuffer = "Test String".ToCharArray();
-		
-		writer.Write( testBuffer, 0, 4 );
-		AssertEquals( "A fooTest", writer.ToString() );
-		
-		writer.Write( testBuffer, 5, 6 );
-		AssertEquals( "A fooTestString", writer.ToString() );
-		
-		writer = new StringWriter(CultureInfo.InvariantCulture);
-		writer.Write(null as string);
+		AssertNotNull( writer.GetStringBuilder() );
+		
+		AssertEquals( String.Empty, writer.ToString() );
+		
+		writer.Write( 'A' );
+		AssertEquals( "A", writer.ToString() );
+		
+		writer.Write( " foo" );
+		AssertEquals( "A foo", writer.ToString() );
+		
+		
+		char[] testBuffer = "Test String".ToCharArray();
+		
+		writer.Write( testBuffer, 0, 4 );
+		AssertEquals( "A fooTest", writer.ToString() );
+		
+		writer.Write( testBuffer, 5, 6 );
+		AssertEquals( "A fooTestString", writer.ToString() );
+		
+		writer = new StringWriter(CultureInfo.InvariantCulture);
+		writer.Write(null as string);
 		AssertEquals( "", writer.ToString() );
         }
 
@@ -160,7 +161,119 @@ public class StringWriterTest : Assertion {
         	}        	        	
         }
 
+	[Test]
+	// strangely this is accepted [ExpectedException (typeof (ArgumentNullException))]
+	public void WriteString_Null ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Write (null);
+	}
+
+	[Test]
+	[ExpectedException (typeof (ArgumentNullException))]
+	public void WriteChars_Null ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Write (null, 0, 0);
+	}
+
+	[Test]
+	[ExpectedException (typeof (ArgumentOutOfRangeException))]
+	public void WriteChars_IndexNegative ()
+	{
+		char[] c = new char [2] { 'a', 'b' };
+        	StringWriter writer = new StringWriter ();
+		writer.Write (c, -1, 0);
+	}
+
+	[Test]
+	[ExpectedException (typeof (ArgumentOutOfRangeException))]
+	public void WriteChars_CountNegative ()
+	{
+		char[] c = new char [2] { 'a', 'b' };
+        	StringWriter writer = new StringWriter ();
+		writer.Write (c, 0, -1);
+	}
+
+	[Test]
+	[ExpectedException (typeof (ArgumentException))]
+	public void WriteChars_IndexOverflow ()
+	{
+		char[] c = new char [2] { 'a', 'b' };
+        	StringWriter writer = new StringWriter ();
+		writer.Write (c, Int32.MaxValue, 0);
+	}
+
+	[Test]
+	[ExpectedException (typeof (ArgumentException))]
+	public void WriteChars_CountOverflow ()
+	{
+		char[] c = new char [2] { 'a', 'b' };
+        	StringWriter writer = new StringWriter ();
+		writer.Write (c, 0, Int32.MaxValue);
+	}
+
+	[Test]
+	public void Disposed_Encoding ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Close ();
+		AssertNotNull ("Disposed-Encoding", writer.Encoding);
+	}
+
+	[Test]
+	public void Disposed_DoubleClose ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Close ();
+		writer.Close ();
+	}
+
+	[Test]
+	public void Disposed_GetStringBuilder ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Write ("Mono");
+		writer.Close ();
+		AssertNotNull ("Disposed-GetStringBuilder", writer.GetStringBuilder ());
+	}
+
+	[Test]
+	public void Disposed_ToString ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Write ("Mono");
+		writer.Close ();
+		AssertEquals ("Disposed-ToString", "Mono", writer.ToString ());
+	}
+
+	[Test]
+	[ExpectedException (typeof (ObjectDisposedException))]
+	public void Disposed_WriteChar ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Close ();
+		writer.Write ('c');
+	}
 
+	[Test]
+	[ExpectedException (typeof (ObjectDisposedException))]
+	public void Disposed_WriteString ()
+	{
+        	StringWriter writer = new StringWriter ();
+		writer.Close ();
+		writer.Write ("mono");
+	}
+
+	[Test]
+	[ExpectedException (typeof (ObjectDisposedException))]
+	public void Disposed_WriteChars ()
+	{
+		char[] c = new char [2] { 'a', 'b' };
+        	StringWriter writer = new StringWriter ();
+		writer.Close ();
+		writer.Write (c, 0, 2);
+	}
 }
 
 }