diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.Indexer.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.Indexer.cs index 89d71d72..75805cb1 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.Indexer.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.Indexer.cs @@ -8,9 +8,15 @@ namespace PrimeFuncPack.Core.Tests; partial class FlatArrayBuilderTest { [Theory] + [InlineData(int.MinValue)] + [InlineData(int.MinValue + 1)] + [InlineData(MinusFifteen)] [InlineData(-1)] [InlineData(0)] [InlineData(1)] + [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] public void Indexer_SourceIsDefault_ExpectIndexOutOfRangeException(int index) { var source = new FlatArray.Builder(); @@ -19,10 +25,16 @@ public void Indexer_SourceIsDefault_ExpectIndexOutOfRangeException(int index) } [Theory] - [InlineData(0, 1, TabString)] + [InlineData(0, 1, EmptyString)] + [InlineData(0, 2, AnotherString, SomeString, LowerSomeString)] [InlineData(1, 2, AnotherString, SomeString, LowerSomeString)] - [InlineData(2, 3, AnotherString, AnotherString, null, SomeString)] - [InlineData(3, 4, "Zero", "One", "Two", "Three")] + [InlineData(0, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(1, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(2, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, null, "One", "Two", "Three")] + [InlineData(1, 4, null, "One", "Two", "Three")] + [InlineData(2, 4, null, "One", "Two", "Three")] + [InlineData(3, 4, null, "One", "Two", "Three")] public void Indexer_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( int index, int sourceLength, params string?[] sourceItems) { @@ -35,10 +47,42 @@ public void Indexer_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( } [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(int.MinValue, 1, SomeString)] + [InlineData(int.MinValue + 1, 1, SomeString)] + [InlineData(MinusFifteen, 1, SomeString)] + [InlineData(-1, 1, SomeString)] [InlineData(1, 1, SomeString)] - [InlineData(1, 1, AnotherString, SomeString)] - [InlineData(5, 2, EmptyString, TabString)] + [InlineData(PlusFifteen, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(int.MinValue, 1, EmptyString, AnotherString)] + [InlineData(int.MinValue + 1, 1, EmptyString, AnotherString)] + [InlineData(MinusFifteen, 1, EmptyString, AnotherString)] + [InlineData(-1, 1, EmptyString, AnotherString)] + [InlineData(1, 1, EmptyString, AnotherString)] + [InlineData(PlusFifteen, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(int.MinValue, 2, EmptyString, SomeString)] + [InlineData(int.MinValue + 1, 2, EmptyString, SomeString)] + [InlineData(MinusFifteen, 2, EmptyString, SomeString)] + [InlineData(-1, 2, EmptyString, SomeString)] + [InlineData(2, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(int.MinValue, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MinValue + 1, 3, LowerSomeString, null, SomeString)] + [InlineData(MinusFifteen, 3, LowerSomeString, null, SomeString)] [InlineData(-1, 3, LowerSomeString, null, SomeString)] + [InlineData(3, 3, LowerSomeString, null, SomeString)] + [InlineData(PlusFifteen, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] public void Indexer_IndexIsOutOfRange_ExpectIndexOutOfRangeException( int index, int sourceLength, params string?[] sourceItems) { diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.IndexFromEnd.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.IndexFromEnd.cs new file mode 100644 index 00000000..04aca244 --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.IndexFromEnd.cs @@ -0,0 +1,76 @@ +using System; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayBuilderTest +{ + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] + public void IndexerRef_IndexFromEnd_SourceIsDefault_ExpectIndexOutOfRangeException(int indexFromEnd) + { + var fromEnd = Index.FromEnd(indexFromEnd); + + var source = new FlatArray.Builder(); + + _ = Assert.Throws(() => _ = source.ItemRef(fromEnd)); + } + + [Theory] + [InlineData(0, 1, 1, EmptyString)] + [InlineData(0, 2, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(0, 3, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, 4, null, "One", "Two", "Three")] + [InlineData(1, 1, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(2, 1, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(3, 1, 4, null, "One", "Two", "Three")] + public void IndexerRef_IndexFromEnd_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( + int index, int indexFromEnd, int sourceLength, params string?[] sourceItems) + { + var fromEnd = Index.FromEnd(indexFromEnd); + + var source = sourceItems.InitializeFlatArrayBuilder(sourceLength); + + var actual = source.ItemRef(fromEnd); + var expected = sourceItems[index]; + + Assert.Equal(expected, actual); + } + + [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(0, 1, SomeString)] + [InlineData(2, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(0, 1, EmptyString, AnotherString)] + [InlineData(2, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(0, 2, EmptyString, SomeString)] + [InlineData(3, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(0, 3, LowerSomeString, null, SomeString)] + [InlineData(4, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] + public void IndexerRef_IndexFromEnd_IndexIsOutOfRange_ExpectIndexOutOfRangeException( + int indexFromEnd, int sourceLength, params string?[] sourceItems) + { + var fromEnd = Index.FromEnd(indexFromEnd); + + var source = sourceItems.InitializeFlatArrayBuilder(sourceLength); + + _ = Assert.Throws(() => _ = source.ItemRef(fromEnd)); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.IndexFromStart.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.IndexFromStart.cs new file mode 100644 index 00000000..ace1253c --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.IndexFromStart.cs @@ -0,0 +1,79 @@ +using System; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayBuilderTest +{ + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] + public void IndexerRef_IndexFromStart_SourceIsDefault_ExpectIndexOutOfRangeException(int index) + { + var fromStart = Index.FromStart(index); + + var source = new FlatArray.Builder(); + + _ = Assert.Throws(() => _ = source.ItemRef(fromStart)); + } + + [Theory] + [InlineData(0, 1, EmptyString)] + [InlineData(0, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(1, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(0, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(1, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(2, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, null, "One", "Two", "Three")] + [InlineData(1, 4, null, "One", "Two", "Three")] + [InlineData(2, 4, null, "One", "Two", "Three")] + [InlineData(3, 4, null, "One", "Two", "Three")] + public void IndexerRef_IndexFromStart_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( + int index, int sourceLength, params string?[] sourceItems) + { + var fromStart = Index.FromStart(index); + + var source = sourceItems.InitializeFlatArrayBuilder(sourceLength); + + var actual = source.ItemRef(fromStart); + var expected = sourceItems[index]; + + Assert.Equal(expected, actual); + } + + [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(1, 1, SomeString)] + [InlineData(PlusFifteen, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(1, 1, EmptyString, AnotherString)] + [InlineData(PlusFifteen, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(2, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(3, 3, LowerSomeString, null, SomeString)] + [InlineData(PlusFifteen, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] + public void IndexerRef_IndexFromStart_IndexIsOutOfRange_ExpectIndexOutOfRangeException( + int index, int sourceLength, params string?[] sourceItems) + { + var fromStart = Index.FromStart(index); + + var source = sourceItems.InitializeFlatArrayBuilder(sourceLength); + + _ = Assert.Throws(() => _ = source.ItemRef(fromStart)); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.cs index af0e51f7..b433a7df 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.IndexerRef.cs @@ -8,9 +8,15 @@ namespace PrimeFuncPack.Core.Tests; partial class FlatArrayBuilderTest { [Theory] + [InlineData(int.MinValue)] + [InlineData(int.MinValue + 1)] + [InlineData(MinusFifteen)] [InlineData(-1)] [InlineData(0)] [InlineData(1)] + [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] public void IndexerRef_SourceIsDefault_ExpectIndexOutOfRangeException(int index) { var source = new FlatArray.Builder(); @@ -19,10 +25,16 @@ public void IndexerRef_SourceIsDefault_ExpectIndexOutOfRangeException(int index) } [Theory] - [InlineData(0, 1, TabString)] + [InlineData(0, 1, EmptyString)] + [InlineData(0, 2, AnotherString, SomeString, LowerSomeString)] [InlineData(1, 2, AnotherString, SomeString, LowerSomeString)] - [InlineData(2, 3, AnotherString, AnotherString, null, SomeString)] - [InlineData(3, 4, "Zero", "One", "Two", "Three")] + [InlineData(0, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(1, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(2, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, null, "One", "Two", "Three")] + [InlineData(1, 4, null, "One", "Two", "Three")] + [InlineData(2, 4, null, "One", "Two", "Three")] + [InlineData(3, 4, null, "One", "Two", "Three")] public void IndexerRef_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( int index, int sourceLength, params string?[] sourceItems) { @@ -35,10 +47,42 @@ public void IndexerRef_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( } [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(int.MinValue, 1, SomeString)] + [InlineData(int.MinValue + 1, 1, SomeString)] + [InlineData(MinusFifteen, 1, SomeString)] + [InlineData(-1, 1, SomeString)] [InlineData(1, 1, SomeString)] - [InlineData(1, 1, AnotherString, SomeString)] - [InlineData(5, 2, EmptyString, TabString)] + [InlineData(PlusFifteen, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(int.MinValue, 1, EmptyString, AnotherString)] + [InlineData(int.MinValue + 1, 1, EmptyString, AnotherString)] + [InlineData(MinusFifteen, 1, EmptyString, AnotherString)] + [InlineData(-1, 1, EmptyString, AnotherString)] + [InlineData(1, 1, EmptyString, AnotherString)] + [InlineData(PlusFifteen, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(int.MinValue, 2, EmptyString, SomeString)] + [InlineData(int.MinValue + 1, 2, EmptyString, SomeString)] + [InlineData(MinusFifteen, 2, EmptyString, SomeString)] + [InlineData(-1, 2, EmptyString, SomeString)] + [InlineData(2, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(int.MinValue, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MinValue + 1, 3, LowerSomeString, null, SomeString)] + [InlineData(MinusFifteen, 3, LowerSomeString, null, SomeString)] [InlineData(-1, 3, LowerSomeString, null, SomeString)] + [InlineData(3, 3, LowerSomeString, null, SomeString)] + [InlineData(PlusFifteen, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] public void IndexerRef_IndexIsOutOfRange_ExpectIndexOutOfRangeException( int index, int sourceLength, params string?[] sourceItems) { diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.CollectionExpression.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.CollectionExpression.cs new file mode 100644 index 00000000..49cddd08 --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.CollectionExpression.cs @@ -0,0 +1,26 @@ +#if NET8_0_OR_GREATER +using System; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayBuilderTest +{ + [Fact] + public static void ConstructFromCollectionExpression_SourceIsEmpty_ExpectDefault() + { + FlatArray.Builder actual = []; + actual.VerifyInnerState([], default); + } + + [Fact] + public static void ConstructFromCollectionExpression_SourceIsNotEmpty_ExpectInnerStateIsSourceCollection() + { + FlatArray.Builder actual = [ SomeString, null, EmptyString, WhiteSpaceString, AnotherString ]; + string?[] expectedItems = [ SomeString, null, EmptyString, WhiteSpaceString, AnotherString ]; + + actual.VerifyInnerState(expectedItems, expectedItems.Length); + } +} +#endif \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.Array.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.Array.cs new file mode 100644 index 00000000..367a646f --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.Array.cs @@ -0,0 +1,52 @@ +using System; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayBuilderTest +{ + [Fact] + public void FromArray_SourceIsNull_ExpectInnerStateIsDefault() + { + StructType[]? source = null; + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Fact] + public void FromArray_SourceIsEmpty_ExpectInnerStateIsDefault() + { + var source = Array.Empty(); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Theory] + [InlineData(Zero)] + [InlineData(PlusFifteen, Zero, MinusFifteen, PlusFifteen)] + [InlineData(12, 15, 1, 91, 7, -95, 975, 0, 5, 6, 7, 901, 98, -266, 78, 62, 21, 35, 75, 212, 51)] + public void FromArray_SourceIsNotEmpty_ExpectInnerStateIsSourceArray( + params int[] source) + { + var coppied = source.GetCopy(); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(coppied, coppied.Length); + } + + [Fact] + public void FromArray_ThenModifySource_ExpectInnerStateHasNotChanged() + { + var sourceArray = new[] { "One", "Two", "Three" }; + var actual = FlatArray.Builder.From(sourceArray); + + sourceArray[0] = "2"; + var expectedItems = new[] { "One", "Two", "Three" }; + + actual.VerifyInnerState(expectedItems, expectedItems.Length); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.FlatArray.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.FlatArray.cs new file mode 100644 index 00000000..2f5846ed --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.FlatArray.cs @@ -0,0 +1,61 @@ +using System; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayBuilderTest +{ + [Fact] + public void FromFlatArray_SourceIsDefault_ExpectInnerStateIsDefault() + { + var source = default(FlatArray); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Theory] + [InlineData(SomeString, AnotherString)] + [InlineData(LowerSomeString, null, SomeString, EmptyString, WhiteSpaceString)] + public void FromFlatArray_SourceIsNotDefault_ExpectInnerStateIsSourceArray( + params string?[] sourceArray) + { + var coppied = sourceArray.GetCopy(); + + var source = sourceArray.InitializeFlatArray(); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(coppied, coppied.Length); + } + + [Fact] + public void FromNullableFlatArray_SourceIsNull_ExpectInnerStateIsDefault() + { + var source = default(FlatArray?); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Fact] + public void FromNullableFlatArray_SourceIsDefault_ExpectInnerStateIsDefault() + { + FlatArray? source = default(FlatArray); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Fact] + public void FromNullableFlatArray_SourceIsNotDefault_ExpectInnerStateIsSourceArray() + { + FlatArray? source = new bool?[] { false, null, true }.InitializeFlatArray(2); + + var actual = FlatArray.Builder.From(source); + var expectedItems = new bool?[] { false, null }; + + actual.VerifyInnerState(expectedItems, expectedItems.Length); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.ImmutableArray.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.ImmutableArray.cs new file mode 100644 index 00000000..d459664f --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.ImmutableArray.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Immutable; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayBuilderTest +{ + [Fact] + public void FromImmutableArray_SourceIsDefault_ExpectInnerStateIsDefault() + { + var source = default(ImmutableArray); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Theory] + [InlineData(SomeString)] + [InlineData(LowerSomeString, null, AnotherString)] + public void FromImmutableArray_SourceIsNotDefault_ExpectInnerStateAreSourceItems( + params string?[] sourceArray) + { + var coppied = sourceArray.GetCopy(); + + var source = sourceArray.ToImmutableArray(); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(coppied, coppied.Length); + } + + [Fact] + public void FromNullableImmutableArray_SourceIsNull_ExpectInnerStateIsDefault() + { + ImmutableArray? source = null; + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Fact] + public void FromNullableImmutableArray_SourceIsDefault_ExpectInnerStateIsDefault() + { + ImmutableArray? source = new ImmutableArray();; + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Theory] + [InlineData(PlusFifteen)] + [InlineData(null, MinusFifteen, Zero)] + public void FromNullableImmutableArray_SourceIsNotDefault_ExpectInnerStateAreSourceItems( + params int?[] sourceItems) + { + var coppied = sourceItems.GetCopy(); + + var source = sourceItems.ToImmutableArray(); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(coppied, coppied.Length); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.List.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.List.cs new file mode 100644 index 00000000..fc3284b1 --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.List.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayBuilderTest +{ + [Fact] + public void FromList_SourceIsNull_ExpectInnerStateIsDefault() + { + List? source = null; + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Fact] + public void FromList_SourceIsEmpty_ExpectInnerStateIsDefault() + { + var source = new List(); + var actual = FlatArray.Builder.From(source); + + actual.VerifyInnerState(Array.Empty(), default); + } + + [Fact] + public void FromList_SourceIsNotEmpty_ExpectInnerStateAreSourceItems() + { + var source = new List + { + SomeTextRecordStruct, null, AnotherTextRecordStruct + }; + + var actual = FlatArray.Builder.From(source); + + const int expectedLength = 3; + var expectedItems = new RecordStruct?[] + { + SomeTextRecordStruct, null, AnotherTextRecordStruct + }; + + actual.VerifyInnerState(expectedItems, expectedLength); + } + + [Fact] + public void FromList_ThenModifySource_ExpectInnerStateHasNotChanged() + { + var sourceList = new List + { + MinusFifteenIdSomeStringNameRecord, ZeroIdNullNameRecord, PlusFifteenIdSomeStringNameRecord + }; + + var actual = FlatArray.Builder.From(sourceList); + + sourceList[0] = PlusFifteenIdLowerSomeStringNameRecord; + sourceList.Add(MinusFifteenIdNullNameRecord); + + const int expectedLength = 3; + var expectedItems = new[] + { + MinusFifteenIdSomeStringNameRecord, ZeroIdNullNameRecord, PlusFifteenIdSomeStringNameRecord + }; + + actual.VerifyInnerState(expectedItems, expectedLength); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.cs deleted file mode 100644 index 61c15a7c..00000000 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Factory/Factory.From.cs +++ /dev/null @@ -1,220 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using PrimeFuncPack.UnitTest; -using Xunit; -using static PrimeFuncPack.UnitTest.TestData; - -namespace PrimeFuncPack.Core.Tests; - -partial class FlatArrayBuilderTest -{ - [Fact] - public void FromArray_SourceIsNull_ExpectInnerStateIsDefault() - { - StructType[]? source = null; - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Fact] - public void FromArray_SourceIsEmpty_ExpectInnerStateIsDefault() - { - var source = Array.Empty(); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Theory] - [InlineData(Zero)] - [InlineData(PlusFifteen, Zero, MinusFifteen, PlusFifteen)] - [InlineData(12, 15, 1, 91, 7, -95, 975, 0, 5, 6, 7, 901, 98, -266, 78, 62, 21, 35, 75, 212, 51)] - public void FromArray_SourceIsNotEmpty_ExpectInnerStateIsSourceArray( - params int[] source) - { - var coppied = source.GetCopy(); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(coppied, coppied.Length); - } - - [Fact] - public void FromArray_ThenModifySource_ExpectInnerStateHasNotChanged() - { - var sourceArray = new[] { "One", "Two", "Three" }; - var actual = FlatArray.Builder.From(sourceArray); - - sourceArray[0] = "2"; - var expectedItems = new[] { "One", "Two", "Three" }; - - actual.VerifyInnerState(expectedItems, expectedItems.Length); - } - - [Fact] - public void FromFlatArray_SourceIsDefault_ExpectInnerStateIsDefault() - { - var source = default(FlatArray); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Theory] - [InlineData(SomeString, AnotherString)] - [InlineData(LowerSomeString, null, SomeString, EmptyString, WhiteSpaceString)] - public void FromFlatArray_SourceIsNotDefault_ExpectInnerStateIsSourceArray( - params string?[] sourceArray) - { - var coppied = sourceArray.GetCopy(); - - var source = sourceArray.InitializeFlatArray(); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(coppied, coppied.Length); - } - - [Fact] - public void FromNullableFlatArray_SourceIsNull_ExpectInnerStateIsDefault() - { - var source = default(FlatArray?); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Fact] - public void FromNullableFlatArray_SourceIsDefault_ExpectInnerStateIsDefault() - { - FlatArray? source = default(FlatArray); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Fact] - public void FromNullableFlatArray_SourceIsNotDefault_ExpectInnerStateIsSourceArray() - { - FlatArray? source = new bool?[] { false, null, true }.InitializeFlatArray(2); - - var actual = FlatArray.Builder.From(source); - var expectedItems = new bool?[] { false, null }; - - actual.VerifyInnerState(expectedItems, expectedItems.Length); - } - - [Fact] - public void FromList_SourceIsNull_ExpectInnerStateIsDefault() - { - List? source = null; - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Fact] - public void FromList_SourceIsEmpty_ExpectInnerStateIsDefault() - { - var source = new List(); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Fact] - public void FromList_SourceIsNotEmpty_ExpectInnerStateAreSourceItems() - { - var source = new List - { - SomeTextRecordStruct, null, AnotherTextRecordStruct - }; - - var actual = FlatArray.Builder.From(source); - - const int expectedLength = 3; - var expectedItems = new RecordStruct?[] - { - SomeTextRecordStruct, null, AnotherTextRecordStruct - }; - - actual.VerifyInnerState(expectedItems, expectedLength); - } - - [Fact] - public void FromList_ThenModifySource_ExpectInnerStateHasNotChanged() - { - var sourceList = new List - { - MinusFifteenIdSomeStringNameRecord, ZeroIdNullNameRecord, PlusFifteenIdSomeStringNameRecord - }; - - var actual = FlatArray.Builder.From(sourceList); - - sourceList[0] = PlusFifteenIdLowerSomeStringNameRecord; - sourceList.Add(MinusFifteenIdNullNameRecord); - - const int expectedLength = 3; - var expectedItems = new[] - { - MinusFifteenIdSomeStringNameRecord, ZeroIdNullNameRecord, PlusFifteenIdSomeStringNameRecord - }; - - actual.VerifyInnerState(expectedItems, expectedLength); - } - - [Fact] - public void FromImmutableArray_SourceIsDefault_ExpectInnerStateIsDefault() - { - var source = default(ImmutableArray); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Theory] - [InlineData(SomeString)] - [InlineData(LowerSomeString, null, AnotherString)] - public void FromImmutableArray_SourceIsNotDefault_ExpectInnerStateAreSourceItems( - params string?[] sourceArray) - { - var coppied = sourceArray.GetCopy(); - - var source = sourceArray.ToImmutableArray(); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(coppied, coppied.Length); - } - - [Fact] - public void FromNullableImmutableArray_SourceIsNull_ExpectInnerStateIsDefault() - { - ImmutableArray? source = null; - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Fact] - public void FromNullableImmutableArray_SourceIsDefault_ExpectInnerStateIsDefault() - { - ImmutableArray? source = new ImmutableArray();; - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(Array.Empty(), default); - } - - [Theory] - [InlineData(PlusFifteen)] - [InlineData(null, MinusFifteen, Zero)] - public void FromNullableImmutableArray_SourceIsNotDefault_ExpectInnerStateAreSourceItems( - params int?[] sourceItems) - { - var coppied = sourceItems.GetCopy(); - - var source = sourceItems.ToImmutableArray(); - var actual = FlatArray.Builder.From(source); - - actual.VerifyInnerState(coppied, coppied.Length); - } -} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.Indexer.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.Indexer.cs index a85a2523..576f9798 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.Indexer.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.Indexer.cs @@ -8,9 +8,15 @@ namespace PrimeFuncPack.Core.Tests; partial class FlatArrayTest { [Theory] + [InlineData(int.MinValue)] + [InlineData(int.MinValue + 1)] [InlineData(MinusFifteen)] - [InlineData(Zero)] + [InlineData(-1)] + [InlineData(0)] + [InlineData(1)] [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] public void Indexer_SourceIsDefault_ExpectIndexOutOfRangeException(int index) { var source = default(FlatArray); @@ -23,8 +29,14 @@ void Test() [Theory] [InlineData(0, 1, EmptyString)] + [InlineData(0, 2, AnotherString, SomeString, LowerSomeString)] [InlineData(1, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(0, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(1, 3, SomeString, AnotherString, null, LowerSomeString)] [InlineData(2, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, null, "One", "Two", "Three")] + [InlineData(1, 4, null, "One", "Two", "Three")] + [InlineData(2, 4, null, "One", "Two", "Three")] [InlineData(3, 4, null, "One", "Two", "Three")] public void Indexer_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( int index, int sourceLength, params string?[] sourceItems) @@ -38,10 +50,42 @@ public void Indexer_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( } [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(int.MinValue, 1, SomeString)] + [InlineData(int.MinValue + 1, 1, SomeString)] + [InlineData(MinusFifteen, 1, SomeString)] + [InlineData(-1, 1, SomeString)] [InlineData(1, 1, SomeString)] + [InlineData(PlusFifteen, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(int.MinValue, 1, EmptyString, AnotherString)] + [InlineData(int.MinValue + 1, 1, EmptyString, AnotherString)] + [InlineData(MinusFifteen, 1, EmptyString, AnotherString)] + [InlineData(-1, 1, EmptyString, AnotherString)] [InlineData(1, 1, EmptyString, AnotherString)] - [InlineData(5, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(int.MinValue, 2, EmptyString, SomeString)] + [InlineData(int.MinValue + 1, 2, EmptyString, SomeString)] + [InlineData(MinusFifteen, 2, EmptyString, SomeString)] + [InlineData(-1, 2, EmptyString, SomeString)] + [InlineData(2, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(int.MinValue, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MinValue + 1, 3, LowerSomeString, null, SomeString)] + [InlineData(MinusFifteen, 3, LowerSomeString, null, SomeString)] [InlineData(-1, 3, LowerSomeString, null, SomeString)] + [InlineData(3, 3, LowerSomeString, null, SomeString)] + [InlineData(PlusFifteen, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] public void Indexer_IndexIsOutOfRange_ExpectIndexOutOfRangeException( int index, int sourceLength, params string?[] sourceItems) { diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.IndexFromEnd.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.IndexFromEnd.cs new file mode 100644 index 00000000..5c77cf80 --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.IndexFromEnd.cs @@ -0,0 +1,82 @@ +using System; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayTest +{ + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] + public void IndexerRef_IndexFromEnd_SourceIsDefault_ExpectIndexOutOfRangeException(int indexFromEnd) + { + var fromEnd = Index.FromEnd(indexFromEnd); + + var source = default(FlatArray); + var ex = Assert.Throws(Test); + + void Test() + => + _ = source.ItemRef(fromEnd); + } + + [Theory] + [InlineData(0, 1, 1, EmptyString)] + [InlineData(0, 2, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(0, 3, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, 4, null, "One", "Two", "Three")] + [InlineData(1, 1, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(2, 1, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(3, 1, 4, null, "One", "Two", "Three")] + public void IndexerRef_IndexFromEnd_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( + int index, int indexFromEnd, int sourceLength, params string?[] sourceItems) + { + var fromEnd = Index.FromEnd(indexFromEnd); + + var source = sourceItems.InitializeFlatArray(sourceLength); + + var actual = source.ItemRef(fromEnd); + var expected = sourceItems[index]; + + Assert.Equal(expected, actual); + } + + [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(0, 1, SomeString)] + [InlineData(2, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(0, 1, EmptyString, AnotherString)] + [InlineData(2, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(0, 2, EmptyString, SomeString)] + [InlineData(3, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(0, 3, LowerSomeString, null, SomeString)] + [InlineData(4, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] + public void IndexerRef_IndexFromEnd_IndexIsOutOfRange_ExpectIndexOutOfRangeException( + int indexFromEnd, int sourceLength, params string?[] sourceItems) + { + var fromEnd = Index.FromEnd(indexFromEnd); + + var source = sourceItems.InitializeFlatArray(sourceLength); + var ex = Assert.Throws(Test); + + void Test() + => + _ = source.ItemRef(fromEnd); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.IndexFromStart.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.IndexFromStart.cs new file mode 100644 index 00000000..fcc1ab7f --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.IndexFromStart.cs @@ -0,0 +1,85 @@ +using System; +using PrimeFuncPack.UnitTest; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayTest +{ + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] + public void IndexerRef_IndexFromStart_SourceIsDefault_ExpectIndexOutOfRangeException(int index) + { + var fromStart = Index.FromStart(index); + + var source = default(FlatArray); + var ex = Assert.Throws(Test); + + void Test() + => + _ = source.ItemRef(fromStart); + } + + [Theory] + [InlineData(0, 1, EmptyString)] + [InlineData(0, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(1, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(0, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(1, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(2, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, null, "One", "Two", "Three")] + [InlineData(1, 4, null, "One", "Two", "Three")] + [InlineData(2, 4, null, "One", "Two", "Three")] + [InlineData(3, 4, null, "One", "Two", "Three")] + public void IndexerRef_IndexFromStart_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( + int index, int sourceLength, params string?[] sourceItems) + { + var fromStart = Index.FromStart(index); + + var source = sourceItems.InitializeFlatArray(sourceLength); + + var actual = source.ItemRef(fromStart); + var expected = sourceItems[index]; + + Assert.Equal(expected, actual); + } + + [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(1, 1, SomeString)] + [InlineData(PlusFifteen, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(1, 1, EmptyString, AnotherString)] + [InlineData(PlusFifteen, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(2, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(3, 3, LowerSomeString, null, SomeString)] + [InlineData(PlusFifteen, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] + public void IndexerRef_IndexFromStart_IndexIsOutOfRange_ExpectIndexOutOfRangeException( + int index, int sourceLength, params string?[] sourceItems) + { + var fromStart = Index.FromStart(index); + + var source = sourceItems.InitializeFlatArray(sourceLength); + var ex = Assert.Throws(Test); + + void Test() + => + _ = source.ItemRef(fromStart); + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.cs index 90141ca1..277b3a99 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Array/Array.IndexerRef.cs @@ -8,9 +8,15 @@ namespace PrimeFuncPack.Core.Tests; partial class FlatArrayTest { [Theory] + [InlineData(int.MinValue)] + [InlineData(int.MinValue + 1)] [InlineData(MinusFifteen)] - [InlineData(Zero)] + [InlineData(-1)] + [InlineData(0)] + [InlineData(1)] [InlineData(PlusFifteen)] + [InlineData(int.MaxValue - 1)] + [InlineData(int.MaxValue)] public void IndexerRef_SourceIsDefault_ExpectIndexOutOfRangeException(int index) { var source = default(FlatArray); @@ -23,8 +29,14 @@ void Test() [Theory] [InlineData(0, 1, EmptyString)] + [InlineData(0, 2, AnotherString, SomeString, LowerSomeString)] [InlineData(1, 2, AnotherString, SomeString, LowerSomeString)] + [InlineData(0, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(1, 3, SomeString, AnotherString, null, LowerSomeString)] [InlineData(2, 3, SomeString, AnotherString, null, LowerSomeString)] + [InlineData(0, 4, null, "One", "Two", "Three")] + [InlineData(1, 4, null, "One", "Two", "Three")] + [InlineData(2, 4, null, "One", "Two", "Three")] [InlineData(3, 4, null, "One", "Two", "Three")] public void IndexerRef_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( int index, int sourceLength, params string?[] sourceItems) @@ -38,10 +50,42 @@ public void IndexerRef_IndexIsInRange_ExpectItemIsFromSourceItemsByIndex( } [Theory] + // Effective Length: 1, Inner array length: 1 + [InlineData(int.MinValue, 1, SomeString)] + [InlineData(int.MinValue + 1, 1, SomeString)] + [InlineData(MinusFifteen, 1, SomeString)] + [InlineData(-1, 1, SomeString)] [InlineData(1, 1, SomeString)] + [InlineData(PlusFifteen, 1, SomeString)] + [InlineData(int.MaxValue - 1, 1, SomeString)] + [InlineData(int.MaxValue, 1, SomeString)] + // Effective Length: 1, Inner array length: 2 + [InlineData(int.MinValue, 1, EmptyString, AnotherString)] + [InlineData(int.MinValue + 1, 1, EmptyString, AnotherString)] + [InlineData(MinusFifteen, 1, EmptyString, AnotherString)] + [InlineData(-1, 1, EmptyString, AnotherString)] [InlineData(1, 1, EmptyString, AnotherString)] - [InlineData(5, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue - 1, 1, EmptyString, AnotherString)] + [InlineData(int.MaxValue, 1, EmptyString, AnotherString)] + // Effective Length: 2, Inner array length: 2 + [InlineData(int.MinValue, 2, EmptyString, SomeString)] + [InlineData(int.MinValue + 1, 2, EmptyString, SomeString)] + [InlineData(MinusFifteen, 2, EmptyString, SomeString)] + [InlineData(-1, 2, EmptyString, SomeString)] + [InlineData(2, 2, EmptyString, SomeString)] + [InlineData(PlusFifteen, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue - 1, 2, EmptyString, SomeString)] + [InlineData(int.MaxValue, 2, EmptyString, SomeString)] + // Effective Length: 3, Inner array length: 3 + [InlineData(int.MinValue, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MinValue + 1, 3, LowerSomeString, null, SomeString)] + [InlineData(MinusFifteen, 3, LowerSomeString, null, SomeString)] [InlineData(-1, 3, LowerSomeString, null, SomeString)] + [InlineData(3, 3, LowerSomeString, null, SomeString)] + [InlineData(PlusFifteen, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue - 1, 3, LowerSomeString, null, SomeString)] + [InlineData(int.MaxValue, 3, LowerSomeString, null, SomeString)] public void IndexerRef_IndexIsOutOfRange_ExpectIndexOutOfRangeException( int index, int sourceLength, params string?[] sourceItems) { diff --git a/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.Array.cs b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.Array.cs new file mode 100644 index 00000000..1b71fc57 --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.Array.cs @@ -0,0 +1,13 @@ +using System.Diagnostics.CodeAnalysis; + +namespace System; + +partial class FlatArray +{ + partial class Builder + { + public static FlatArray.Builder From([AllowNull] params T[] source) + => + new(source); + } +} diff --git a/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.FlatArray.cs b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.FlatArray.cs new file mode 100644 index 00000000..ca964104 --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.FlatArray.cs @@ -0,0 +1,15 @@ +namespace System; + +partial class FlatArray +{ + partial class Builder + { + public static FlatArray.Builder From(FlatArray source) + => + FlatArray.Builder.From(source); + + public static FlatArray.Builder From(FlatArray? source) + => + FlatArray.Builder.From(source); + } +} diff --git a/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.ImmutableArray.cs b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.ImmutableArray.cs new file mode 100644 index 00000000..3ef1332c --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.ImmutableArray.cs @@ -0,0 +1,17 @@ +using System.Collections.Immutable; + +namespace System; + +partial class FlatArray +{ + partial class Builder + { + public static FlatArray.Builder From(ImmutableArray source) + => + FlatArray.Builder.From(source); + + public static FlatArray.Builder From(ImmutableArray? source) + => + FlatArray.Builder.From(source); + } +} diff --git a/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.List.cs b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.List.cs new file mode 100644 index 00000000..13b5dc31 --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.List.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +namespace System; + +partial class FlatArray +{ + partial class Builder + { + public static FlatArray.Builder From([AllowNull] List source) + => + FlatArray.Builder.From(source); + } +} diff --git a/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.cs b/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.cs deleted file mode 100644 index 89d532d2..00000000 --- a/src/flatcollections-array/FlatArray/FlatArray.Builder/FlatArray.Builder.Factory.From.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Diagnostics.CodeAnalysis; - -namespace System; - -partial class FlatArray -{ - partial class Builder - { - public static FlatArray.Builder From([AllowNull] params T[] source) - => - new(source); - - public static FlatArray.Builder From(FlatArray source) - => - FlatArray.Builder.From(source); - - public static FlatArray.Builder From(FlatArray? source) - => - FlatArray.Builder.From(source); - - public static FlatArray.Builder From([AllowNull] List source) - => - FlatArray.Builder.From(source); - - public static FlatArray.Builder From(ImmutableArray source) - => - FlatArray.Builder.From(source); - - public static FlatArray.Builder From(ImmutableArray? source) - => - FlatArray.Builder.From(source); - } -} diff --git a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.Indexers.cs b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.Indexers.cs index 8d6f79ed..6d8b6cc2 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.Indexers.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.Indexers.cs @@ -31,8 +31,9 @@ public T this[int index] } } - // ItemRef is implemented readonly to prevent modification of FlatArray instances + // ItemRef are implemented readonly to prevent modification of FlatArray instances // initialized by calling 'MoveTo' on the Builder + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref readonly T ItemRef(int index) { @@ -43,5 +44,10 @@ public ref readonly T ItemRef(int index) throw InnerExceptionFactory.IndexOutOfRange(index, length); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ref readonly T ItemRef(Index index) + => + ref ItemRef(index.GetOffset(length)); } } diff --git a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.cs b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.cs index 77919351..66523cb8 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.cs @@ -5,6 +5,9 @@ namespace System; partial struct FlatArray { +#if NET8_0_OR_GREATER + [CollectionBuilder(typeof(FlatArray.Builder), nameof(FlatArray.Builder.From))] +#endif [DebuggerDisplay($"{nameof(Length)} = {{{nameof(Length)}}}")] public sealed partial class Builder { diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.FlatArray.cs b/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.FlatArray.cs index da294659..2c6a2e7f 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.FlatArray.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.FlatArray.cs @@ -8,6 +8,10 @@ public static FlatArray From(FlatArray source) => new(source); + public static FlatArray From(FlatArray? source) + => + new(source); + // TODO: Add the tests and make public internal static FlatArray From(FlatArray source, int start, int length) { @@ -28,10 +32,6 @@ void InnerValidateRange() } } - public static FlatArray From(FlatArray? source) - => - new(source); - // TODO: Add the tests and make public internal static FlatArray From(FlatArray? source, int start, int length) => diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.ImmutableArray.cs b/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.ImmutableArray.cs index a008bdde..6d7befbe 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.ImmutableArray.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Factory/Factory.Explicit.From.ImmutableArray.cs @@ -9,6 +9,10 @@ public static FlatArray From(ImmutableArray source) => new(source); + public static FlatArray From(ImmutableArray? source) + => + new(source); + // TODO: Add the tests and make public internal static FlatArray From(ImmutableArray source, int start, int length) { @@ -36,10 +40,6 @@ void InnerValidateRange() } } - public static FlatArray From(ImmutableArray? source) - => - new(source); - // TODO: Add the tests and make public internal static FlatArray From(ImmutableArray? source, int start, int length) => diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.Indexers.cs b/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.Indexers.cs index 3fbabc6a..81bf1c59 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.Indexers.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.Indexers.cs @@ -28,4 +28,9 @@ public ref readonly T ItemRef(int index) throw InnerExceptionFactory.IndexOutOfRange(index, length); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ref readonly T ItemRef(Index index) + => + ref ItemRef(index.GetOffset(length)); } diff --git a/src/flatcollections-array/FlatArray/FlatArray.csproj b/src/flatcollections-array/FlatArray/FlatArray.csproj index 38900595..bc0fd588 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.csproj +++ b/src/flatcollections-array/FlatArray/FlatArray.csproj @@ -18,7 +18,7 @@ PrimeFuncPack Core.FlatArray is a core library for .NET consisting of immutable FlatArray targeted for use in functional programming. System PrimeFuncPack.Core.FlatArray - 1.3.0-rc.1 + 1.3.0-rc.2 diff --git a/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.FlatArray.cs b/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.FlatArray.cs index eafbec1a..c183c066 100644 --- a/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.FlatArray.cs +++ b/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.FlatArray.cs @@ -6,15 +6,15 @@ public static FlatArray From(FlatArray source) => new(source); + public static FlatArray From(FlatArray? source) + => + new(source); + // TODO: Add the tests and make public internal static FlatArray From(FlatArray source, int start, int length) => FlatArray.From(source, start, length); - public static FlatArray From(FlatArray? source) - => - new(source); - // TODO: Add the tests and make public internal static FlatArray From(FlatArray? source, int start, int length) => diff --git a/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.ImmutableArray.cs b/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.ImmutableArray.cs index 94003f9c..56f14e1b 100644 --- a/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.ImmutableArray.cs +++ b/src/flatcollections-array/FlatArray/FlatArray/FlatArray.Factory.From.ImmutableArray.cs @@ -8,15 +8,15 @@ public static FlatArray From(ImmutableArray source) => new(source); + public static FlatArray From(ImmutableArray? source) + => + new(source); + // TODO: Add the tests and make public internal static FlatArray From(ImmutableArray source, int start, int length) => FlatArray.From(source, start, length); - public static FlatArray From(ImmutableArray? source) - => - new(source); - // TODO: Add the tests and make public internal static FlatArray From(ImmutableArray? source, int start, int length) => diff --git a/src/flatcollections/FlatCollections/FlatCollections.csproj b/src/flatcollections/FlatCollections/FlatCollections.csproj index 24abd215..be97a320 100644 --- a/src/flatcollections/FlatCollections/FlatCollections.csproj +++ b/src/flatcollections/FlatCollections/FlatCollections.csproj @@ -18,7 +18,7 @@ PrimeFuncPack Core.FlatCollections is a set of immutable Flat collections for .NET designed for developing business applications based on functional programming. System PrimeFuncPack.Core.FlatCollections - 1.3.0-rc.1 + 1.3.0-rc.2 @@ -33,7 +33,7 @@ - +