Skip to content

Commit

Permalink
style: use collection expression syntax where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Nov 13, 2024
1 parent ce18f6a commit bd546e6
Show file tree
Hide file tree
Showing 51 changed files with 260 additions and 251 deletions.
6 changes: 3 additions & 3 deletions X10D.Tests/src/Collections/ArrayTests.AsReadOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class AsReadOnlyTests
[Test]
public void AsReadOnly_ShouldReturnReadOnlyCollection_WhenArrayIsNotNull()
{
int[] array = {1, 2, 3};
int[] array = [1, 2, 3];
IReadOnlyCollection<int> result = array.AsReadOnly();
Assert.That(result, Is.InstanceOf<IReadOnlyCollection<int>>());
}
Expand All @@ -26,15 +26,15 @@ public void AsReadOnly_ShouldThrowArgumentNullException_WhenArrayIsNull()
[Test]
public void AsReadOnly_ShouldReturnCorrectCount_WhenArrayIsNotEmpty()
{
int[] array = {1, 2, 3};
int[] array = [1, 2, 3];
IReadOnlyCollection<int> result = array.AsReadOnly();
Assert.That(result, Has.Count.EqualTo(array.Length));
}

[Test]
public void AsReadOnly_ShouldReturnEmptyCollection_WhenArrayIsEmpty()
{
int[] array = Array.Empty<int>();
int[] array = [];
IReadOnlyCollection<int> result = array.AsReadOnly();
Assert.That(result, Is.Empty);
}
Expand Down
2 changes: 1 addition & 1 deletion X10D.Tests/src/Collections/ArrayTests.Clear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Clear_ShouldClearTheArray()
[Test]
public void Clear_ShouldDoNothing_WhenArrayIsEmpty()
{
int[] array = Array.Empty<int>();
int[] array = [];
array.Clear();
}

Expand Down
2 changes: 1 addition & 1 deletion X10D.Tests/src/Collections/ByteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void Unpack_ShouldThrow_GivenTooSmallSpan()
Assert.Throws<ArgumentException>(() =>
{
const byte value = 0b11010100;
Span<bool> bits = stackalloc bool[0];
Span<bool> bits = [];
value.Unpack(bits);
});
}
Expand Down
2 changes: 1 addition & 1 deletion X10D.Tests/src/Collections/Int16Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void Unpack_ShouldThrow_GivenTooSmallSpan()
Assert.Throws<ArgumentException>(() =>
{
const short value = 0b11010100;
Span<bool> bits = stackalloc bool[0];
Span<bool> bits = [];
value.Unpack(bits);
});
}
Expand Down
2 changes: 1 addition & 1 deletion X10D.Tests/src/Collections/Int32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void Unpack_ShouldThrow_GivenTooSmallSpan()
Assert.Throws<ArgumentException>(() =>
{
const int value = 0b11010100;
Span<bool> bits = stackalloc bool[0];
Span<bool> bits = [];
value.Unpack(bits);
});
}
Expand Down
2 changes: 1 addition & 1 deletion X10D.Tests/src/Collections/Int64Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void UnpackBits_ShouldThrow_GivenTooSmallSpan()
{
Assert.Throws<ArgumentException>(() =>
{
Span<bool> bits = stackalloc bool[0];
Span<bool> bits = [];
0b11010100L.Unpack(bits);
});
}
Expand Down
30 changes: 15 additions & 15 deletions X10D.Tests/src/Collections/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void SlicedFill_ShouldLeaveFirstElement_GivenStartIndex1(params int[] arg
[Test]
public void Fill_ShouldThrow_GivenExceededCount()
{
int[] array = Array.Empty<int>();
int[] array = [];
var list = new List<int>();
Assert.Throws<ArgumentOutOfRangeException>(() => array.Fill(0, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => list.Fill(0, 0, 1));
Expand All @@ -51,7 +51,7 @@ public void Fill_ShouldThrow_GivenExceededCount()
[Test]
public void Fill_ShouldThrow_GivenNegativeCount()
{
int[] array = Array.Empty<int>();
int[] array = [];
var list = new List<int>();
Assert.Throws<ArgumentOutOfRangeException>(() => array.Fill(0, 0, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => list.Fill(0, 0, -1));
Expand All @@ -60,7 +60,7 @@ public void Fill_ShouldThrow_GivenNegativeCount()
[Test]
public void Fill_ShouldThrow_GivenNegativeStartIndex()
{
int[] array = Array.Empty<int>();
int[] array = [];
var list = new List<int>();
Assert.Throws<ArgumentOutOfRangeException>(() => array.Fill(0, -1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => list.Fill(0, -1, 0));
Expand All @@ -80,7 +80,7 @@ public void Fill_ShouldThrow_GivenNull()
[Test]
public void IndexOf_ShouldReturnCorrectValue_FromStartOfList()
{
int[] array = { 0, 1, 2, 3, 4 };
int[] array = [0, 1, 2, 3, 4];
Assert.Multiple(() =>
{
Assert.That(array.IndexOf(2), Is.EqualTo(2));
Expand All @@ -92,7 +92,7 @@ public void IndexOf_ShouldReturnCorrectValue_FromStartOfList()
[Test]
public void IndexOf_ShouldReturnCorrectValue_GivenSubRange()
{
int[] array = { 0, 1, 2, 3, 4, 0 };
int[] array = [0, 1, 2, 3, 4, 0];
Assert.Multiple(() =>
{
Assert.That(array.IndexOf(0), Is.Zero);
Expand All @@ -107,7 +107,7 @@ public void IndexOf_ShouldReturnCorrectValue_GivenSubRange()
[Test]
public void IndexOf_ShouldReturnNegative1_ForEmptyList()
{
int[] array = Array.Empty<int>();
int[] array = [];
Assert.Multiple(() =>
{
Assert.That(array.IndexOf(0), Is.EqualTo(-1));
Expand All @@ -131,14 +131,14 @@ public void IndexOf_ShouldThrowArgumentNullException_GivenNullList()
[Test]
public void IndexOf_ShouldThrowArgumentOutOfRangeException_GivenNegativeCount()
{
int[] array = Array.Empty<int>();
int[] array = [];
Assert.Throws<ArgumentOutOfRangeException>(() => array.IndexOf(0, 0, -1));
}

[Test]
public void IndexOf_ShouldThrowArgumentOutOfRangeException_GivenNegativeStartIndex()
{
int[] array = Array.Empty<int>();
int[] array = [];
Assert.Multiple(() =>
{
Assert.Throws<ArgumentOutOfRangeException>(() => array.IndexOf(0, -1));
Expand All @@ -149,7 +149,7 @@ public void IndexOf_ShouldThrowArgumentOutOfRangeException_GivenNegativeStartInd
[Test]
public void IndexOf_ShouldThrowArgumentOutOfRangeException_GivenInvalidStartIndexCountPair()
{
int[] array = { 0, 1, 2 };
int[] array = [0, 1, 2];
Assert.Throws<ArgumentOutOfRangeException>(() => array.IndexOf(0, 2, 4));
}

Expand Down Expand Up @@ -233,21 +233,21 @@ public void Shuffle_ShouldThrow_GivenNull()
[Test]
public void Slice_ShouldReturnCorrectValue_GivenStartIndex()
{
int[] array = { 0, 1, 2, 3, 4, 5 };
int[] array = [0, 1, 2, 3, 4, 5];
Assert.That(array.Slice(2).ToArray(), Is.EqualTo(new[] { 2, 3, 4, 5 }).AsCollection);
}

[Test]
public void Slice_ShouldReturnCorrectValue_GivenStartIndexAndLength()
{
int[] array = { 0, 1, 2, 3, 4, 5 };
int[] array = [0, 1, 2, 3, 4, 5];
Assert.That(array.Slice(2, 3).ToArray(), Is.EqualTo(new[] { 2, 3, 4 }).AsCollection);
}

[Test]
public void Slice_ShouldReturnEmptyList_ForEmptyList()
{
int[] array = Array.Empty<int>();
int[] array = [];
Assert.That(array.Slice(0).ToArray(), Is.EqualTo(Array.Empty<int>()).AsCollection);
Assert.That(array.Slice(0, 0).ToArray(), Is.EqualTo(Array.Empty<int>()).AsCollection);
}
Expand All @@ -263,22 +263,22 @@ public void Slice_ShouldThrowArgumentNullException_GivenNullList()
[Test]
public void Slice_ShouldThrowArgumentOutOfRangeException_GivenNegativeCount()
{
int[] array = Array.Empty<int>();
int[] array = [];
Assert.Throws<ArgumentOutOfRangeException>(() => array.Slice(0, -1));
}

[Test]
public void Slice_ShouldThrowArgumentOutOfRangeException_GivenNegativeStartIndex()
{
int[] array = Array.Empty<int>();
int[] array = [];
Assert.Throws<ArgumentOutOfRangeException>(() => array.Slice(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => array.Slice(-1, 0));
}

[Test]
public void Slice_ShouldThrowArgumentOutOfRangeException_GivenInvalidStartIndexCountPair()
{
int[] array = { 0, 1, 2 };
int[] array = [0, 1, 2];
Assert.Throws<ArgumentOutOfRangeException>(() => array.Slice(2, 4));
}

Expand Down
10 changes: 5 additions & 5 deletions X10D.Tests/src/Collections/SpanTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Count_ShouldReturn0_GivenEmptyReadOnlySpan()
[Test]
public void Count_ShouldReturn8_GivenSpanWith8MatchingElements()
{
Span<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };
Span<int> span = [1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2];

int count = span.Count(2);

Expand All @@ -41,7 +41,7 @@ public void Count_ShouldReturn8_GivenSpanWith8MatchingElements()
[Test]
public void Count_ShouldReturn8_GivenReadOnlySpanWith8MatchingElements()
{
ReadOnlySpan<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };
ReadOnlySpan<int> span = [1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2];

int count = span.Count(2);

Expand All @@ -51,23 +51,23 @@ public void Count_ShouldReturn8_GivenReadOnlySpanWith8MatchingElements()
[Test]
public void Replace_ShouldReplaceAllElements_GivenSpanOfInt32()
{
Span<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };
Span<int> span = [1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2];
span.Replace(2, 4);
Assert.That(span.ToArray(), Is.EqualTo(new[] { 1, 4, 3, 4, 5, 4, 7, 4, 9, 4, 11, 4, 13, 4, 15, 4 }));
}

[Test]
public void Replace_ShouldReplaceAllElements_GivenSpanOfChar()
{
Span<char> chars = stackalloc char[12] { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!' };
Span<char> chars = ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'];
chars.Replace('l', 'w');
Assert.That("Hewwo worwd!".ToCharArray(), Is.EqualTo(chars.ToArray()).AsCollection);
}

[Test]
public void Replace_ShouldDoNothing_GivenSpanWithNoMatchingElements()
{
Span<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };
Span<int> span = [1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2];
span.Replace(4, 8);
Assert.That(span.ToArray(), Is.EqualTo(new[] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 }));
}
Expand Down
Loading

0 comments on commit bd546e6

Please sign in to comment.