Skip to content

Commit

Permalink
file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsmith1968 committed Jan 12, 2025
1 parent 5ba046e commit 5b8d93d
Show file tree
Hide file tree
Showing 14 changed files with 1,959 additions and 1,861 deletions.
27 changes: 25 additions & 2 deletions src/DNX.Extensions/Arrays/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,38 @@ public static T[] PadLeft<T>(this T[] input, int length)
return paddedArray;
}

public static T[] ShiftLeft<T>(this T[] input)
public static T[] ShiftLeft<T>(this T[] input, int by = 1, T fillValue = default)
{
if (input == null)
{
return [];
}

var shiftedArray = new T[input.Length];
Array.Copy(input, 1, shiftedArray, 0, input.Length - 1);
Array.Copy(input, by, shiftedArray, 0, input.Length - by);

for (var x = input.Length - by; x < input.Length; ++x)
{
shiftedArray[x] = fillValue;
}

return shiftedArray;
}

public static T[] ShiftRight<T>(this T[] input, int by = 1, T fillValue = default)
{
if (input == null)
{
return [];
}

var shiftedArray = new T[input.Length];
Array.Copy(input, 0, shiftedArray, by, input.Length - by);

for (var x = 0; x < by; ++x)
{
shiftedArray[x] = fillValue;
}

return shiftedArray;
}
Expand Down
163 changes: 125 additions & 38 deletions tests/DNX.Extensions.Tests/Arrays/ArrayExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,166 @@

#pragma warning disable IDE0290

namespace DNX.Extensions.Tests.Arrays
namespace DNX.Extensions.Tests.Arrays;

public class ArrayExtensionsTests(ITestOutputHelper outputHelper)
{
public class ArrayExtensionsTests(ITestOutputHelper outputHelper)
[Theory]
[InlineData("2,3,4", false)]
[InlineData("", true)]
[InlineData(null, true)]
public void Test_IsNullOrEmpty(string byteArray, bool isEmpty)
{
var bytes = byteArray?.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(x => Convert.ToByte(x))
.ToArray();

bytes.IsNullOrEmpty().Should().Be(isEmpty);
}

[Fact]
public void Test_PadLeft_when_greater_than_existing_array_length()
{
[Theory]
[InlineData("2,3,4", false)]
[InlineData("", true)]
[InlineData(null, true)]
public void Test_IsNullOrEmpty(string byteArray, bool isEmpty)
var values = "1,2,3,4,5,6".Split(",").ToArray();

var result = values.PadLeft(8);

values.Length.Should().Be(6);
result.Length.Should().Be(8);

for (var i = 1; i <= result.Length; ++i)
{
var bytes = byteArray?.Split(",", StringSplitOptions.RemoveEmptyEntries)
.Select(x => Convert.ToByte(x))
.ToArray();
outputHelper.WriteLine($"Index: {i}");
result[^i].Should().Be(i > values.Length ? default : values[^i]);
}
}

[Fact]
public void Test_PadLeft_when_less_than_existing_array_length()
{
var values = "1,2,3,4,5,6".Split(",").ToArray();

bytes.IsNullOrEmpty().Should().Be(isEmpty);
// Act
var result = values.PadLeft(4);

// Assert
values.Length.Should().Be(6);
result.Length.Should().Be(4);

for (var i = 1; i <= result.Length; ++i)
{
outputHelper.WriteLine($"Index: {i}");
result[^i].Should().Be(values[^i]);
}
}

public class ShiftLeft
{
[Fact]
public void Test_PadLeft_when_greater_than_existing_array_length()
public void Test_ShiftLeft_defaults_populated_array()
{
var values = "1,2,3,4,5,6".Split(",").ToArray();
string[] expected = ["2", "3", "4", "5", "6", null];

// Act
var result = values.ShiftLeft();

// Assert
result.Length.Should().Be(values.Length);
result.Should().BeEquivalentTo(expected);
}

var result = values.PadLeft(8);
[Fact]
public void Test_ShiftLeft_by_number_populated_array()
{
var values = "1,2,3,4,5,6".Split(",").ToArray();
string[] expected = ["4", "5", "6", null, null, null];

values.Length.Should().Be(6);
result.Length.Should().Be(8);
// Act
var result = values.ShiftLeft(by: 3);

for (var i = 1; i <= result.Length; ++i)
{
outputHelper.WriteLine($"Index: {i}");
result[^i].Should().Be(i > values.Length ? default : values[^i]);
}
// Assert
result.Length.Should().Be(values.Length);
result.Should().BeEquivalentTo(expected);
}

[Fact]
public void Test_PadLeft_when_less_than_existing_array_length()
public void Test_ShiftLeft_with_fill_value_populated_array()
{
var values = "1,2,3,4,5,6".Split(",").ToArray();
string[] expected = ["3", "4", "5", "6", "0", "0"];

// Act
var result = values.ShiftLeft(by: 2, fillValue: "0");

// Assert
result.Length.Should().Be(values.Length);
result.Should().BeEquivalentTo(expected);
}

var result = values.PadLeft(4);
[Fact]
public void Test_ShiftLeft_null_array()
{
string[] values = null;

values.Length.Should().Be(6);
result.Length.Should().Be(4);
var result = values.ShiftLeft();

for (var i = 1; i <= result.Length; ++i)
{
outputHelper.WriteLine($"Index: {i}");
result[^i].Should().Be(values[^i]);
}
result.Should().NotBeNull();
result.Length.Should().Be(0);
}
}


public class ShiftRight
{
[Fact]
public void Test_ShiftLeft_populated_array()
public void Test_ShiftRight_defaults_populated_array()
{
var values = "1,2,3,4,5,6".Split(",").ToArray();
string[] expected = [null, "1", "2", "3", "4", "5"];

var result = values.ShiftLeft();
// Act
var result = values.ShiftRight();

// Assert
result.Length.Should().Be(values.Length);
result.Should().BeEquivalentTo(expected);
}

for (var i = 0; i < result.Length; ++i)
{
outputHelper.WriteLine($"Index: {i}");
result[i].Should().Be(i >= values.Length - 1 ? default : values[i + 1]);
}
[Fact]
public void Test_ShiftRight_by_number_populated_array()
{
var values = "1,2,3,4,5,6".Split(",").ToArray();
string[] expected = [null, null, null, "1", "2", "3"];

// Act
var result = values.ShiftRight(by: 3);

// Assert
result.Length.Should().Be(values.Length);
result.Should().BeEquivalentTo(expected);
}

[Fact]
public void Test_ShiftLeft_null_array()
public void Test_ShiftRight_with_fill_value_populated_array()
{
var values = "1,2,3,4,5,6".Split(",").ToArray();
string[] expected = ["0", "0", "1", "2", "3", "4"];

// Act
var result = values.ShiftRight(by: 2, fillValue: "0");

// Assert
result.Length.Should().Be(values.Length);
result.Should().BeEquivalentTo(expected);
}

[Fact]
public void Test_ShiftRight_null_array()
{
string[] values = null;

var result = values.ShiftLeft();
var result = values.ShiftRight();

result.Should().NotBeNull();
result.Length.Should().Be(0);
Expand Down
53 changes: 26 additions & 27 deletions tests/DNX.Extensions.Tests/Arrays/ByteArrayExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@
using FluentAssertions;
using Xunit;

namespace DNX.Extensions.Tests.Arrays
namespace DNX.Extensions.Tests.Arrays;

public class ByteArrayExtensionsTests
{
public class ByteArrayExtensionsTests
[Theory]
[InlineData("65,66,67,68,69,70", "ABCDEF")]
[InlineData("97,98,99,100,101,102", "abcdef")]
[InlineData("", "")]
[InlineData(null, "")]
public void Test_GetAsciiString(string byteText, string expectedResult)
{
[Theory]
[InlineData("65,66,67,68,69,70", "ABCDEF")]
[InlineData("97,98,99,100,101,102", "abcdef")]
[InlineData("", "")]
[InlineData(null, "")]
public void Test_GetAsciiString(string byteText, string expectedResult)
{
var bytes = byteText == null
? []
: byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray();
var bytes = byteText == null
? []
: byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray();

var result = bytes.GetAsciiString();
var result = bytes.GetAsciiString();

result.Should().Be(expectedResult);
}
result.Should().Be(expectedResult);
}

[Theory]
[InlineData("1,2,3,4,5,6", "010203040506")]
[InlineData("", "")]
[InlineData(null, "")]
public void Test_ToHexString(string byteText, string expectedResult)
{
var bytes = byteText == null
? []
: byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray();
[Theory]
[InlineData("1,2,3,4,5,6", "010203040506")]
[InlineData("", "")]
[InlineData(null, "")]
public void Test_ToHexString(string byteText, string expectedResult)
{
var bytes = byteText == null
? []
: byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray();

var result = bytes.ToHexString();
var result = bytes.ToHexString();

result.Should().Be(expectedResult);
}
result.Should().Be(expectedResult);
}
}
Loading

0 comments on commit 5b8d93d

Please sign in to comment.