-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pmosk
committed
Sep 19, 2024
1 parent
fb03d25
commit 29bb373
Showing
7 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Chunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!] | ||
] | ||
} | ||
}; | ||
} |
40 changes: 40 additions & 0 deletions
40
src/flatcollections-array/FlatArray/FlatArray.T/Functional/Function.Chunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace System; | ||
|
||
partial struct FlatArray<T> | ||
{ | ||
public FlatArray<FlatArray<T>> Chunk(int size) | ||
{ | ||
if (size < 1) | ||
{ | ||
throw InnerExceptionFactory.SizeOutsideBounds(nameof(size), size); | ||
} | ||
|
||
if (length == default) | ||
{ | ||
return default; | ||
} | ||
|
||
var chunksLength = length / size; | ||
if (length % size != default) | ||
{ | ||
chunksLength++; | ||
} | ||
|
||
var chunks = new FlatArray<T>[chunksLength]; | ||
for (var i = 0; i < chunks.Length; i++) | ||
{ | ||
var start = i * size; | ||
|
||
var chunkLength = length - start; | ||
if (chunkLength > size) | ||
{ | ||
chunkLength = size; | ||
} | ||
|
||
var chunkItems = InnerArrayHelper.CopySegment(items!, start, chunkLength); | ||
chunks[i] = new(chunkLength, chunkItems); | ||
} | ||
|
||
return chunks; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters