Skip to content

Commit

Permalink
Merge pull request #76 from pfpack/feature/impl-chunk-method
Browse files Browse the repository at this point in the history
Implement Chunk method
  • Loading branch information
andreise authored Sep 20, 2024
2 parents fb03d25 + 81f44cb commit 69a4a34
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="PrimeFuncPack.UnitTest.Data" Version="3.1.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@ internal static void VerifyTruncatedState<T>(this FlatArray<T> actual, params T[

Assert.Equal(expectedItems, truncatedItems);
}

internal static void VerifyTruncatedState<T>(this FlatArray<FlatArray<T>> actual, params T[][]? expectedItems)
{
var actualLength = actual.GetStructFieldValue<int>("length");
Assert.StrictEqual(expectedItems?.Length ?? default, actualLength);

var actualItems = actual.GetFieldValue<FlatArray<T>[]?>("items");
if (actualItems is null)
{
Assert.Null(expectedItems);
return;
}

Assert.NotNull(expectedItems);

for (var i = 0; i < actualLength; i++)
{
actualItems[i].VerifyTruncatedState(expectedItems[i]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Theory]
[MemberData(nameof(Chunk_InvalidSizeTestData))]
public static void Chunk_SizeIsInvalid_ExpectArgumentOutOfRangeException(
FlatArray<string> source, int size)
{
var ex = Assert.Throws<ArgumentOutOfRangeException>(Test);
Assert.Equal("size", ex.ParamName);

void Test()
=>
_ = source.Chunk(size);
}

[Theory]
[MemberData(nameof(Chunk_ValidTestData))]
public static void Chunk_SizeIsValid_ExpectCorrectChunks(
FlatArray<string> source, int size, string[][]? expectedChunks)
{
var actual = source.Chunk(size);
actual.VerifyTruncatedState(expectedChunks);
}

public static TheoryData<FlatArray<string>, int> Chunk_InvalidSizeTestData
=>
new()
{
{
default,
0
},
{
default,
-1
},
{
new[] { SomeString, AnotherString }.InitializeFlatArray(),
0
},
{
new[] { SomeString, AnotherString }.InitializeFlatArray(),
-1
}
};

public static TheoryData<FlatArray<string>, int, string[][]?> Chunk_ValidTestData
=>
new()
{
{
default,
1,
null
},
{
new[] { EmptyString }.InitializeFlatArray(0),
1,
null
},
{
default,
3,
null
},
{
new[] { SomeString, EmptyString, WhiteSpaceString, AnotherString }.InitializeFlatArray(3),
2,
[
[SomeString, EmptyString],
[WhiteSpaceString]
]
},
{
new[] { SomeString, EmptyString, WhiteSpaceString, AnotherString }.InitializeFlatArray(4),
2,
[
[SomeString, EmptyString],
[WhiteSpaceString, AnotherString]
]
},
{
new[] { SomeString, EmptyString, WhiteSpaceString, AnotherString, TabString }.InitializeFlatArray(5),
2,
[
[SomeString, EmptyString],
[WhiteSpaceString, AnotherString],
[TabString]
]
},
{
new[] { ThreeWhiteSpacesString, AnotherString, SomeString, null! }.InitializeFlatArray(3),
3,
[
[ThreeWhiteSpacesString, AnotherString, SomeString]
]
},
{
new[] { ThreeWhiteSpacesString, AnotherString, SomeString, null! }.InitializeFlatArray(4),
3,
[
[ThreeWhiteSpacesString, AnotherString, SomeString],
[null!]
]
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace System;

partial struct FlatArray<T>
{
public FlatArray<FlatArray<T>> Chunk(int size)
{
if (size < 1)
{
throw InnerExceptionFactory.ChunkSizeOutsideBounds(nameof(size), size);
}

if (length == default)
{
return default;
}

var chunks = length % size == default ? new FlatArray<T>[length / size] : new FlatArray<T>[length / size + 1];
var start = 0;

for (var i = 0; i < chunks.Length; i++)
{
var effectiveSize = length - start;
if (effectiveSize > size)
{
effectiveSize = size;
}

var chunkItems = InnerArrayHelper.CopySegment(items!, start, effectiveSize);
chunks[i] = new(chunkItems, default);

start += effectiveSize;
}

return new(chunks, default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ internal static ArgumentOutOfRangeException SegmentOutsideBounds(int segmentStar
=>
new(null, Invariant($"Segment must be within the array bounds. Segment start was {segmentStart}. Segment length was {segmentLength}. Array length was {arrayLength}."));

internal static ArgumentOutOfRangeException ChunkSizeOutsideBounds(string paramName, int size)
=>
new(paramName, Invariant($"Chunk size must be greater than zero but was {size}."));

internal static IndexOutOfRangeException IndexOutOfRange(int index, int length)
=>
new(Invariant($"Index must be greater than or equal to zero and less than the array length. Index was {index}. Array length was {length}."));
Expand Down
2 changes: 1 addition & 1 deletion src/flatcollections-array/FlatArray/FlatArray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Description>PrimeFuncPack Core.FlatArray is a core library for .NET consisting of immutable FlatArray targeted for use in functional programming.</Description>
<RootNamespace>System</RootNamespace>
<AssemblyName>PrimeFuncPack.Core.FlatArray</AssemblyName>
<Version>1.4.0</Version>
<Version>1.5.0-preview.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/flatcollections/FlatCollections/FlatCollections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Description>PrimeFuncPack Core.FlatCollections is a set of immutable Flat collections for .NET designed for developing business applications based on functional programming.</Description>
<RootNamespace>System</RootNamespace>
<AssemblyName>PrimeFuncPack.Core.FlatCollections</AssemblyName>
<Version>1.4.0</Version>
<Version>1.5.0-preview.1</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,7 +33,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="PrimeFuncPack.Core.FlatArray" Version="1.4.0" />
<PackageReference Include="PrimeFuncPack.Core.FlatArray" Version="1.5.0-preview.1" />
</ItemGroup>

</Project>

0 comments on commit 69a4a34

Please sign in to comment.