MemoryStreamTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. protected override void SetUp() {
  21. testStreamData = new byte[100];
  22. for( int i = 0; i < 100; i++ ) {
  23. testStreamData[i] = (byte)(100 - i);
  24. }
  25. testStream = new MemoryStream( testStreamData );
  26. }
  27. //
  28. // Verify that the first count bytes in testBytes are the same as
  29. // the count bytes from index start in testStreamData
  30. //
  31. private void VerifyTestData( byte[] testBytes, int start, int count) {
  32. if( testBytes == null ) {
  33. throw new ArgumentNullException();
  34. } else if( ( start < 0 || count < 0 ) || start + count > testStreamData.Length || start > testStreamData.Length ) {
  35. throw new ArgumentOutOfRangeException();
  36. }
  37. for( int test = 0; test < count; test++ ) {
  38. if( testBytes[ test ] != testStreamData[ start + test ] ) {
  39. string failStr = String.Format( "testByte {0} (testStream {1}) was <{2}>, expecting <{3}>", test, start+test,
  40. testBytes[ test ], testStreamData[ start+test] );
  41. Fail( failStr );
  42. }
  43. }
  44. }
  45. public void TestConstructors() {
  46. MemoryStream ms = new MemoryStream();
  47. AssertEquals("A1", 0L, ms.Length );
  48. AssertEquals("A2", 0, ms.Capacity );
  49. AssertEquals("A3", true, ms.CanWrite );
  50. ms = new MemoryStream( 10 );
  51. AssertEquals("A4", 0L, ms.Length );
  52. AssertEquals("A5", 10, ms.Capacity );
  53. }
  54. public void TestRead() {
  55. byte[] readBytes = new byte[20];
  56. try {
  57. /* Test simple read */
  58. testStream.Read( readBytes, 0, 10 );
  59. VerifyTestData( readBytes, 0, 10 );
  60. /* Seek back to beginning */
  61. testStream.Seek( 0, SeekOrigin.Begin );
  62. /* Read again, bit more this time */
  63. testStream.Read( readBytes, 0, 20 );
  64. VerifyTestData( readBytes, 0, 20 );
  65. /* Seek to 20 bytes from End */
  66. testStream.Seek( -20, SeekOrigin.End );
  67. testStream.Read( readBytes, 0, 20);
  68. VerifyTestData( readBytes, 80, 20);
  69. int readByte = testStream.ReadByte();
  70. AssertEquals( -1, readByte );
  71. }
  72. catch(Exception e){
  73. Fail("Threw an unexpected exception:"+e.ToString());
  74. return;
  75. }
  76. }
  77. public void TestWriteBytes() {
  78. byte[] readBytes = new byte[100];
  79. try {
  80. MemoryStream ms = new MemoryStream( 100 );
  81. for( int i = 0; i < 100; i++ ) {
  82. ms.WriteByte( testStreamData[i] );
  83. }
  84. ms.Seek( 0, SeekOrigin.Begin);
  85. testStream.Read( readBytes, 0, 100 );
  86. VerifyTestData( readBytes, 0, 100 );
  87. }
  88. catch(Exception e){
  89. Fail("Threw an unexpected exception:"+e.ToString());
  90. return;
  91. }
  92. }
  93. public void TestWriteBlock() {
  94. byte[] readBytes = new byte[100];
  95. try {
  96. MemoryStream ms = new MemoryStream( 100 );
  97. ms.Write( testStreamData, 0, 100 );
  98. ms.Seek( 0, SeekOrigin.Begin);
  99. testStream.Read( readBytes, 0, 100 );
  100. VerifyTestData( readBytes, 0, 100 );
  101. byte[] arrayBytes = testStream.ToArray();
  102. VerifyTestData( arrayBytes, 0, 100 );
  103. }
  104. catch(Exception e){
  105. Fail("Threw an unexpected exception:"+e.ToString());
  106. return;
  107. }
  108. }
  109. }
  110. }