@@ -22,6 +22,8 @@ public void Test_IBufferWriterStream_Lifecycle()
2222 {
2323 ArrayPoolBufferWriter < byte > writer = new ArrayPoolBufferWriter < byte > ( ) ;
2424
25+ // Get a stream from a buffer writer aand validate that it can only be written to.
26+ // This is to mirror the same functionality as the IBufferWriter<T> interface.
2527 Stream stream = ( ( IBufferWriter < byte > ) writer ) . AsStream ( ) ;
2628
2729 Assert . IsFalse ( stream . CanRead ) ;
@@ -31,6 +33,7 @@ public void Test_IBufferWriterStream_Lifecycle()
3133 Assert . ThrowsException < NotSupportedException > ( ( ) => stream . Length ) ;
3234 Assert . ThrowsException < NotSupportedException > ( ( ) => stream . Position ) ;
3335
36+ // Dispose the stream and check that no operation is now allowed
3437 stream . Dispose ( ) ;
3538
3639 Assert . IsFalse ( stream . CanRead ) ;
@@ -49,11 +52,14 @@ public void Test_IBufferWriterStream_Write_Array()
4952
5053 byte [ ] data = Test_MemoryStream . CreateRandomData ( 64 ) ;
5154
55+ // Write random data to the stream wrapping the buffer writer, and validate
56+ // that the state of the writer is consistent, and the written content matches.
5257 stream . Write ( data , 0 , data . Length ) ;
5358
5459 Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
5560 Assert . IsTrue ( writer . WrittenSpan . SequenceEqual ( data ) ) ;
5661
62+ // A few tests with invalid inputs (null buffers, invalid indices, etc.)
5763 Assert . ThrowsException < ArgumentNullException > ( ( ) => stream . Write ( null , 0 , 10 ) ) ;
5864 Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => stream . Write ( data , - 1 , 10 ) ) ;
5965 Assert . ThrowsException < ArgumentException > ( ( ) => stream . Write ( data , 200 , 10 ) ) ;
@@ -74,6 +80,7 @@ public async Task Test_IBufferWriterStream_WriteAsync_Array()
7480
7581 byte [ ] data = Test_MemoryStream . CreateRandomData ( 64 ) ;
7682
83+ // Same test as above, but using an asynchronous write instead
7784 await stream . WriteAsync ( data , 0 , data . Length ) ;
7885
7986 Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
@@ -102,11 +109,15 @@ public void Test_IBufferWriterStream_WriteByte()
102109
103110 foreach ( var item in data . Enumerate ( ) )
104111 {
112+ // Since we're enumerating, we can also double check the current written count
113+ // at each iteration, to ensure the writes are done correctly every time.
105114 Assert . AreEqual ( writer . WrittenCount , item . Index ) ;
106115
116+ // Write a number of bytes one by one to test this API as well
107117 stream . WriteByte ( item . Value ) ;
108118 }
109119
120+ // Validate the final written length and actual data
110121 Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
111122 Assert . IsTrue ( data . SequenceEqual ( writer . WrittenSpan ) ) ;
112123
@@ -140,6 +151,7 @@ public async Task Test_IBufferWriterStream_WriteAsync_Memory()
140151
141152 Memory < byte > data = Test_MemoryStream . CreateRandomData ( 64 ) ;
142153
154+ // Same as the other asynchronous test above, but writing from a Memory<T>
143155 await stream . WriteAsync ( data ) ;
144156
145157 Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
0 commit comments