@@ -22,6 +22,8 @@ public void Test_IBufferWriterStream_Lifecycle()
22
22
{
23
23
ArrayPoolBufferWriter < byte > writer = new ArrayPoolBufferWriter < byte > ( ) ;
24
24
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.
25
27
Stream stream = ( ( IBufferWriter < byte > ) writer ) . AsStream ( ) ;
26
28
27
29
Assert . IsFalse ( stream . CanRead ) ;
@@ -31,6 +33,7 @@ public void Test_IBufferWriterStream_Lifecycle()
31
33
Assert . ThrowsException < NotSupportedException > ( ( ) => stream . Length ) ;
32
34
Assert . ThrowsException < NotSupportedException > ( ( ) => stream . Position ) ;
33
35
36
+ // Dispose the stream and check that no operation is now allowed
34
37
stream . Dispose ( ) ;
35
38
36
39
Assert . IsFalse ( stream . CanRead ) ;
@@ -49,11 +52,14 @@ public void Test_IBufferWriterStream_Write_Array()
49
52
50
53
byte [ ] data = Test_MemoryStream . CreateRandomData ( 64 ) ;
51
54
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.
52
57
stream . Write ( data , 0 , data . Length ) ;
53
58
54
59
Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
55
60
Assert . IsTrue ( writer . WrittenSpan . SequenceEqual ( data ) ) ;
56
61
62
+ // A few tests with invalid inputs (null buffers, invalid indices, etc.)
57
63
Assert . ThrowsException < ArgumentNullException > ( ( ) => stream . Write ( null , 0 , 10 ) ) ;
58
64
Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => stream . Write ( data , - 1 , 10 ) ) ;
59
65
Assert . ThrowsException < ArgumentException > ( ( ) => stream . Write ( data , 200 , 10 ) ) ;
@@ -74,6 +80,7 @@ public async Task Test_IBufferWriterStream_WriteAsync_Array()
74
80
75
81
byte [ ] data = Test_MemoryStream . CreateRandomData ( 64 ) ;
76
82
83
+ // Same test as above, but using an asynchronous write instead
77
84
await stream . WriteAsync ( data , 0 , data . Length ) ;
78
85
79
86
Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
@@ -102,11 +109,15 @@ public void Test_IBufferWriterStream_WriteByte()
102
109
103
110
foreach ( var item in data . Enumerate ( ) )
104
111
{
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.
105
114
Assert . AreEqual ( writer . WrittenCount , item . Index ) ;
106
115
116
+ // Write a number of bytes one by one to test this API as well
107
117
stream . WriteByte ( item . Value ) ;
108
118
}
109
119
120
+ // Validate the final written length and actual data
110
121
Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
111
122
Assert . IsTrue ( data . SequenceEqual ( writer . WrittenSpan ) ) ;
112
123
@@ -140,6 +151,7 @@ public async Task Test_IBufferWriterStream_WriteAsync_Memory()
140
151
141
152
Memory < byte > data = Test_MemoryStream . CreateRandomData ( 64 ) ;
142
153
154
+ // Same as the other asynchronous test above, but writing from a Memory<T>
143
155
await stream . WriteAsync ( data ) ;
144
156
145
157
Assert . AreEqual ( writer . WrittenCount , data . Length ) ;
0 commit comments