Skip to content

Commit 8a70ace

Browse files
committed
Added comments to unit tests
1 parent 2626c7d commit 8a70ace

File tree

8 files changed

+216
-29
lines changed

8 files changed

+216
-29
lines changed

UnitTests/UnitTests.HighPerformance.Shared/Extensions/Test_ArrayExtensions.2D.cs

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public void Test_ArrayExtensions_2D_AsSpan2DAndFillArrayMid()
8888
{
8989
bool[,] test = new bool[4, 5];
9090

91+
// To fill an array we now go through the Span2D<T> type, which includes all
92+
// the necessary logic to perform the operation. In these tests we just create
93+
// one through the extension, slice it and then fill it. For instance in this
94+
// one, we're creating a Span2D<bool> from coordinates (1, 1), with a height of
95+
// 2 and a width of 2, and then filling it. Then we just compare the results.
9196
test.AsSpan2D(1, 1, 2, 3).Fill(true);
9297

9398
var expected = new[,]
@@ -173,12 +178,16 @@ public void Test_ArrayExtensions_2D_GetRow_Rectangle()
173178
{ 9, 10, 11, 12 }
174179
};
175180

181+
// Here we use the enumerator on the RefEnumerator<T> type to traverse items in a row
182+
// by reference. For each one, we check that the reference does in fact point to the
183+
// item we expect in the underlying array (in this case, items on row 1).
176184
int j = 0;
177185
foreach (ref int value in array.GetRow(1))
178186
{
179187
Assert.IsTrue(Unsafe.AreSame(ref value, ref array[1, j++]));
180188
}
181189

190+
// Check that RefEnumerable<T>.ToArray() works correctly
182191
CollectionAssert.AreEqual(array.GetRow(1).ToArray(), new[] { 5, 6, 7, 8 });
183192

184193
// Test an empty array
@@ -189,12 +198,40 @@ public void Test_ArrayExtensions_2D_GetRow_Rectangle()
189198
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetRow(20));
190199
}
191200

201+
[TestCategory("ArrayExtensions")]
202+
[TestMethod]
203+
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1312", Justification = "Dummy loop variable")]
204+
[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1501", Justification = "Empty test loop")]
205+
public void Test_ArrayExtensions_2D_GetColumn_Rectangle()
206+
{
207+
int[,] array =
208+
{
209+
{ 1, 2, 3, 4 },
210+
{ 5, 6, 7, 8 },
211+
{ 9, 10, 11, 12 }
212+
};
213+
214+
// Same as above, but this time we iterate a column instead (so non contiguous items)
215+
int i = 0;
216+
foreach (ref int value in array.GetColumn(1))
217+
{
218+
Assert.IsTrue(Unsafe.AreSame(ref value, ref array[i++, 1]));
219+
}
220+
221+
CollectionAssert.AreEqual(array.GetColumn(1).ToArray(), new[] { 2, 6, 10 });
222+
223+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(-1));
224+
225+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(20));
226+
}
227+
192228
[TestCategory("ArrayExtensions")]
193229
[TestMethod]
194230
public void Test_ArrayExtensions_2D_GetRow_Empty()
195231
{
196232
int[,] array = new int[0, 0];
197233

234+
// Try to get a row from an empty array (the row index isn't in range)
198235
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetRow(0).ToArray());
199236
}
200237

@@ -212,6 +249,8 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers()
212249
{ 13, 14, 15, 16 }
213250
};
214251

252+
// Get a row and test the Clear method. Note that the Span2D<T> here is sliced
253+
// starting from the second column, so this method should clear the row from index 1.
215254
array.AsSpan2D(1, 1, 3, 3).GetRow(0).Clear();
216255

217256
int[,] expected =
@@ -224,6 +263,7 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers()
224263

225264
CollectionAssert.AreEqual(array, expected);
226265

266+
// Same as before, but this time we fill a column with a value
227267
array.GetColumn(2).Fill(42);
228268

229269
expected = new[,]
@@ -238,31 +278,36 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers()
238278

239279
int[] copy = new int[4];
240280

281+
// Get a row and copy items to a target span (in this case, wrapping an array)
241282
array.GetRow(2).CopyTo(copy);
242283

243284
int[] result = { 9, 10, 42, 12 };
244285

245286
CollectionAssert.AreEqual(copy, result);
246287

288+
// Same as above, but copying from a column (so we test non contiguous sequences too)
247289
array.GetColumn(1).CopyTo(copy);
248290

249291
result = new[] { 2, 0, 10, 14 };
250292

251293
CollectionAssert.AreEqual(copy, result);
252294

295+
// Some invalid attempts to copy to an empty span or sequence
253296
Assert.ThrowsException<ArgumentException>(() => array.GetRow(0).CopyTo(default(RefEnumerable<int>)));
254297
Assert.ThrowsException<ArgumentException>(() => array.GetRow(0).CopyTo(default(Span<int>)));
255298

256299
Assert.ThrowsException<ArgumentException>(() => array.GetColumn(0).CopyTo(default(RefEnumerable<int>)));
257300
Assert.ThrowsException<ArgumentException>(() => array.GetColumn(0).CopyTo(default(Span<int>)));
258301

302+
// Same as CopyTo, but this will fail gracefully with an invalid target
259303
Assert.IsTrue(array.GetRow(2).TryCopyTo(copy));
260304
Assert.IsFalse(array.GetRow(0).TryCopyTo(default(Span<int>)));
261305

262306
result = new[] { 9, 10, 42, 12 };
263307

264308
CollectionAssert.AreEqual(copy, result);
265309

310+
// Also fill a row and then further down clear a column (trying out all possible combinations)
266311
array.GetRow(2).Fill(99);
267312

268313
expected = new[,]
@@ -302,6 +347,9 @@ public void Test_ArrayExtensions_2D_ReadOnlyGetRowOrColumn_Helpers()
302347
{ 13, 14, 15, 16 }
303348
};
304349

350+
// This test pretty much does the same things as the method above, but this time
351+
// using a source ReadOnlySpan2D<T>, so that the sequence type being tested is
352+
// ReadOnlyRefEnumerable<T> instead (which shares most features but is separate).
305353
ReadOnlySpan2D<int> span2D = array;
306354

307355
int[] copy = new int[4];
@@ -332,32 +380,6 @@ public void Test_ArrayExtensions_2D_ReadOnlyGetRowOrColumn_Helpers()
332380
CollectionAssert.AreEqual(copy, result);
333381
}
334382

335-
[TestCategory("ArrayExtensions")]
336-
[TestMethod]
337-
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1312", Justification = "Dummy loop variable")]
338-
[SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1501", Justification = "Empty test loop")]
339-
public void Test_ArrayExtensions_2D_GetColumn_Rectangle()
340-
{
341-
int[,] array =
342-
{
343-
{ 1, 2, 3, 4 },
344-
{ 5, 6, 7, 8 },
345-
{ 9, 10, 11, 12 }
346-
};
347-
348-
int i = 0;
349-
foreach (ref int value in array.GetColumn(1))
350-
{
351-
Assert.IsTrue(Unsafe.AreSame(ref value, ref array[i++, 1]));
352-
}
353-
354-
CollectionAssert.AreEqual(array.GetColumn(1).ToArray(), new[] { 2, 6, 10 });
355-
356-
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(-1));
357-
358-
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(20));
359-
}
360-
361383
[TestCategory("ArrayExtensions")]
362384
[TestMethod]
363385
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1312", Justification = "Dummy loop variable")]
@@ -432,6 +454,7 @@ public void Test_ArrayExtensions_2D_AsSpan_Empty()
432454

433455
Span<int> span = array.AsSpan();
434456

457+
// Check that the empty array was loaded properly
435458
Assert.AreEqual(span.Length, array.Length);
436459
Assert.IsTrue(span.IsEmpty);
437460
}
@@ -449,11 +472,14 @@ public void Test_ArrayExtensions_2D_AsSpan_Populated()
449472

450473
Span<int> span = array.AsSpan();
451474

475+
// Test the total length of the span
452476
Assert.AreEqual(span.Length, array.Length);
453477

454478
ref int r0 = ref array[0, 0];
455479
ref int r1 = ref span[0];
456480

481+
// Similarly to the top methods, here we compare a given reference to
482+
// ensure they point to the right element back in the original array.
457483
Assert.IsTrue(Unsafe.AreSame(ref r0, ref r1));
458484
}
459485
#endif

UnitTests/UnitTests.HighPerformance.Shared/Extensions/Test_BoolExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class Test_BoolExtensions
1515
[TestMethod]
1616
public void Test_BoolExtensions_True()
1717
{
18+
// There tests all just run a couple of boolean expressions and validate that the extension
19+
// correctly produces either 1 or 0 depending on whether the expression was true or false.
1820
Assert.AreEqual(1, true.ToByte(), nameof(Test_BoolExtensions_True));
1921
Assert.AreEqual(1, (DateTime.Now.Year > 0).ToByte(), nameof(Test_BoolExtensions_True));
2022
}

UnitTests/UnitTests.HighPerformance.Shared/Helpers/Test_ParallelHelper.ForEach.In2D.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public unsafe void Test_ParallelHelper_ForEach_In2D(
3535
{
3636
int[,] data = CreateRandomData2D(sizeY, sizeX);
3737

38+
// Create a memory wrapping the random array with the given parameters
3839
ReadOnlyMemory2D<int> memory = data.AsMemory2D(row, column, height, width);
3940

4041
Assert.AreEqual(memory.Length, height * width);
@@ -43,13 +44,16 @@ public unsafe void Test_ParallelHelper_ForEach_In2D(
4344

4445
int sum = 0;
4546

47+
// Sum all the items in parallel. The Summer type takes a pointer to a target value
48+
// and adds values to it in a thread-safe manner (with an interlocked add).
4649
ParallelHelper.ForEach(memory, new Summer(&sum));
4750

4851
int expected = 0;
4952

53+
// Calculate the sum iteratively as a baseline for comparison
5054
foreach (int n in memory.Span)
5155
{
52-
Interlocked.Add(ref Unsafe.AsRef<int>(&expected), n);
56+
expected += n;
5357
}
5458

5559
Assert.AreEqual(sum, expected, $"The sum doesn't match, was {sum} instead of {expected}");

UnitTests/UnitTests.HighPerformance.Shared/Helpers/Test_ParallelHelper.ForEach.Ref2D.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void Test_ParallelHelper_ForEach_Ref2D(
3333
data = CreateRandomData2D(sizeY, sizeX),
3434
copy = (int[,])data.Clone();
3535

36+
// Prepare the target data iteratively
3637
foreach (ref int n in copy.AsSpan2D(row, column, height, width))
3738
{
3839
n = unchecked(n * 397);
@@ -44,6 +45,7 @@ public void Test_ParallelHelper_ForEach_Ref2D(
4445
Assert.AreEqual(memory.Height, height);
4546
Assert.AreEqual(memory.Width, width);
4647

48+
// Do the same computation in paralellel, then compare the two arrays
4749
ParallelHelper.ForEach(memory, new Multiplier(397));
4850

4951
CollectionAssert.AreEqual(data, copy);

0 commit comments

Comments
 (0)