Skip to content

Commit ba19b76

Browse files
committed
Added comments to unit tests
1 parent 7c3269a commit ba19b76

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

UnitTests/UnitTests.HighPerformance.Shared/Buffers/Test_ArrayPoolBufferWriter{T}.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Buffers;
76
using System.Diagnostics.CodeAnalysis;
87
using System.IO;
98
using System.Linq;
@@ -150,28 +149,43 @@ public void Test_ArrayPoolBufferWriterOfT_MultipleDispose()
150149
[TestMethod]
151150
public void Test_ArrayPoolBufferWriterOfT_AsStream()
152151
{
153-
var writer = new ArrayPoolBufferWriter<byte>();
154-
155-
Span<byte> data = Guid.NewGuid().ToByteArray();
152+
const int GuidSize = 16;
156153

157-
data.CopyTo(writer.GetSpan(data.Length));
154+
var writer = new ArrayPoolBufferWriter<byte>();
155+
var guid = Guid.NewGuid();
158156

159-
writer.Advance(data.Length);
157+
// Here we first get a stream with the extension targeting ArrayPoolBufferWriter<T>.
158+
// This will wrap it into a custom internal stream type and produce a write-only
159+
// stream that essentially mirrors the IBufferWriter<T> functionality as a stream.
160+
using (Stream writeStream = writer.AsStream())
161+
{
162+
writeStream.Write(guid);
163+
}
160164

161-
Assert.AreEqual(writer.WrittenCount, data.Length);
165+
Assert.AreEqual(writer.WrittenCount, GuidSize);
162166

163-
Stream stream = ((IMemoryOwner<byte>)writer).AsStream();
167+
// Here we get a readable stream instead, and read from it to ensure
168+
// the previous data was written correctly from the writeable stream.
169+
using (Stream stream = writer.WrittenMemory.AsStream())
170+
{
171+
Assert.AreEqual(stream.Length, GuidSize);
164172

165-
Assert.AreEqual(stream.Length, data.Length);
173+
byte[] result = new byte[GuidSize];
166174

167-
byte[] result = new byte[16];
175+
stream.Read(result, 0, result.Length);
168176

169-
stream.Read(result, 0, result.Length);
177+
// Read the guid data and ensure it matches our initial guid
178+
Assert.IsTrue(new Guid(result).Equals(guid));
179+
}
170180

171-
Assert.IsTrue(data.SequenceEqual(result));
181+
// Do a dummy write just to ensure the writer isn't disposed here.
182+
// This is because we got a stream from a memory, not a memory owner.
183+
writer.Write((byte)42);
184+
writer.Advance(1);
172185

173-
stream.Dispose();
186+
writer.Dispose();
174187

188+
// Now check that the writer is actually disposed instead
175189
Assert.ThrowsException<ObjectDisposedException>(() => writer.Capacity);
176190
}
177191
}

UnitTests/UnitTests.HighPerformance.Shared/Streams/Test_IBufferWriterStream.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)