MemoryStreamTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.IO.StringWriter
  3. //
  4. // Author: Marcin Szczepanski ([email protected])
  5. //
  6. // TODO: Add some testing for exceptions
  7. //
  8. // TODO: Add some testing for the CanXXX properties, exceptions,
  9. // various different constructors.
  10. //
  11. using NUnit.Framework;
  12. using System.IO;
  13. using System;
  14. using System.Text;
  15. namespace MonoTests.System.IO
  16. {
  17. public class MemoryStreamTest : TestCase {
  18. private MemoryStream testStream;
  19. private byte[] testStreamData;
  20. public MemoryStreamTest() : base ("MonoTests.System.IO.MemoryStream testcase") { }
  21. public MemoryStreamTest( string name ): base(name) { }
  22. public static ITest Suite {
  23. get {
  24. return new TestSuite(typeof(MemoryStreamTest));
  25. }
  26. }
  27. protected override void SetUp() {
  28. testStreamData = new byte[100];
  29. for( int i = 0; i < 100; i++ ) {
  30. testStreamData[i] = (byte)(100 - i);
  31. }
  32. testStream = new MemoryStream( testStreamData );
  33. }
  34. //
  35. // Verify that the first count bytes in testBytes are the same as
  36. // the count bytes from index start in testStreamData
  37. //
  38. private void VerifyTestData( byte[] testBytes, int start, int count) {
  39. if( testBytes == null ) {
  40. throw new ArgumentNullException();
  41. } else if( ( start < 0 || count < 0 ) || start + count > testStreamData.Length || start > testStreamData.Length ) {
  42. throw new ArgumentOutOfRangeException();
  43. }
  44. for( int test = 0; test < count; test++ ) {
  45. if( testBytes[ test ] != testStreamData[ start + test ] ) {
  46. string failStr = String.Format( "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>", test, start+test,
  47. testBytes[ test ], testStreamData[ start+test] );
  48. Fail( failStr );
  49. }
  50. }
  51. }
  52. public void TestConstructors() {
  53. MemoryStream ms = new MemoryStream();
  54. AssertEquals("A1", 0L, ms.Length );
  55. AssertEquals("A2", 0, ms.Capacity );
  56. AssertEquals("A3", true, ms.CanWrite );
  57. ms = new MemoryStream( 10 );
  58. AssertEquals("A4", 0L, ms.Length );
  59. AssertEquals("A5", 10, ms.Capacity );
  60. }
  61. public void TestRead() {
  62. byte[] readBytes = new byte[20];
  63. try {
  64. /* Test simple read */
  65. testStream.Read( readBytes, 0, 10 );
  66. VerifyTestData( readBytes, 0, 10 );
  67. /* Seek back to beginning */
  68. testStream.Seek( 0, SeekOrigin.Begin );
  69. /* Read again, bit more this time */
  70. testStream.Read( readBytes, 0, 20 );
  71. VerifyTestData( readBytes, 0, 20 );
  72. /* Seek to 20 bytes from End */
  73. testStream.Seek( -20, SeekOrigin.End );
  74. testStream.Read( readBytes, 0, 20);
  75. VerifyTestData( readBytes, 80, 20);
  76. int readByte = testStream.ReadByte();
  77. AssertEquals( -1, readByte );
  78. }
  79. catch(Exception e){
  80. Fail("Threw an unexpected exception:"+e.ToString());
  81. return;
  82. }
  83. }
  84. public void TestWriteBytes() {
  85. byte[] readBytes = new byte[100];
  86. try {
  87. MemoryStream ms = new MemoryStream( 100 );
  88. for( int i = 0; i < 100; i++ ) {
  89. ms.WriteByte( testStreamData[i] );
  90. }
  91. ms.Seek( 0, SeekOrigin.Begin);
  92. testStream.Read( readBytes, 0, 100 );
  93. VerifyTestData( readBytes, 0, 100 );
  94. }
  95. catch(Exception e){
  96. Fail("Threw an unexpected exception:"+e.ToString());
  97. return;
  98. }
  99. }
  100. public void TestWriteBlock() {
  101. byte[] readBytes = new byte[100];
  102. try {
  103. MemoryStream ms = new MemoryStream( 100 );
  104. ms.Write( testStreamData, 0, 100 );
  105. ms.Seek( 0, SeekOrigin.Begin);
  106. testStream.Read( readBytes, 0, 100 );
  107. VerifyTestData( readBytes, 0, 100 );
  108. byte[] arrayBytes = testStream.ToArray();
  109. VerifyTestData( arrayBytes, 0, 100 );
  110. }
  111. catch(Exception e){
  112. Fail("Threw an unexpected exception:"+e.ToString());
  113. return;
  114. }
  115. }
  116. }
  117. }