diff --git a/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs b/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs index 676a274e..023fcdd3 100644 --- a/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs +++ b/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs @@ -13,4 +13,13 @@ internal static void VerifyInnerState(this FlatArray actual, T[]? expected var actualItems = actual.GetFieldValue("items"); Assert.Equal(expectedItems, actualItems); } + + internal static void VerifyInnerState_TheSameAssert(this FlatArray actual, T[]? expectedItems, int expectedLength) + { + var actualLength = actual.GetStructFieldValue("length"); + Assert.StrictEqual(expectedLength, actualLength); + + var actualItems = actual.GetFieldValue("items"); + Assert.Same(expectedItems, actualItems); + } } \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/TestData/ReadingCollectionsTestSource.cs b/src/flatcollections-array/FlatArray.Tests/TestData/ReadingCollectionsTestSource.cs new file mode 100644 index 00000000..25cf341d --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/TestData/ReadingCollectionsTestSource.cs @@ -0,0 +1,226 @@ +using System; +using System.Collections.Generic; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests.TestData; + +internal class ReadingCollectionsTestSource +{ + public static IEnumerable EnumerateStringNonEmptyCases() + { + yield return new object[] + { + new string?[] { null }, + new string?[] { null, null, null, null } + }; + yield return new object[] + { + new string[] { SomeString }, + new string?[] { SomeString, null, null, null } + }; + yield return new object[] + { + new string?[] { SomeString, null }, + new string?[] { SomeString, null, null, null } + }; + yield return new object[] + { + new string?[] { null, SomeString }, + new string?[] { null, SomeString, null, null } + }; + yield return new object[] + { + new string?[] { "0" }, + new string?[] { "0", null, null, null } + }; + yield return new object[] + { + new string?[] { "0", "1" }, + new string?[] { "0", "1", null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2" }, + new string?[] { "0", "1", "2", null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3" }, + new string?[] { "0", "1", "2", "3" } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4" }, + new string?[] { "0", "1", "2", "3", "4", null, null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5" }, + new string?[] { "0", "1", "2", "3", "4", "5", null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7" } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", null, null, null, null, null, null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", null, null, null, null, null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", null, null, null, null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", null, null, null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", null, null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", null, null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", null } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" } + }; + yield return new object[] + { + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16" }, + new string?[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null } + }; + } + + public static IEnumerable EnumerateInt32NullableNonEmptyCases() + { + yield return new object[] + { + new int?[] { null }, + new int?[] { null, null, null, null } + }; + yield return new object[] + { + new int?[] { MinusFifteen }, + new int?[] { MinusFifteen, null, null, null } + }; + yield return new object[] + { + new int?[] { MinusFifteen, null }, + new int?[] { MinusFifteen, null, null, null } + }; + yield return new object[] + { + new int?[] { null, MinusFifteen }, + new int?[] { null, MinusFifteen, null, null } + }; + yield return new object[] + { + new int?[] { 0 }, + new int?[] { 0, null, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1 }, + new int?[] { 0, 1, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2 }, + new int?[] { 0, 1, 2, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3 }, + new int?[] { 0, 1, 2, 3 } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4 }, + new int?[] { 0, 1, 2, 3, 4, null, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5 }, + new int?[] { 0, 1, 2, 3, 4, 5, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7 } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, null, null, null, null, null, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, null, null, null, null, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, null, null, null, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, null, null, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, null, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, null, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, null } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } + }; + yield return new object[] + { + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, + new int?[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null } + }; + } +} diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.MoveToFlatArray.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.MoveToFlatArray.cs index fe0472d5..6bcbb7ed 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.MoveToFlatArray.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Builder/Builder/Builder.MoveToFlatArray.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using PrimeFuncPack.UnitTest; using Xunit; using static PrimeFuncPack.UnitTest.TestData; @@ -25,17 +26,26 @@ public void MoveToFlatArray_SourceIsDefault_ExpectBuilderStateIsDefault() source.VerifyInnerState(Array.Empty(), default); } - [Fact] - public void MoveToFlatArray_SourceIsNotDefault_ExpectArrayItemsAreBuilderItems() + [Theory] + [MemberData(nameof(MoveToFlatArray_SourceIsNotDefault_ExpectInnerStateTheSameAsBuilderState_CaseSource))] + public void MoveToFlatArray_SourceIsNotDefault_ExpectInnerStateTheSameAsBuilderState( + int length, + RefType[] sourceItems) { - const int length = 3; - - var sourceItems = new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, ZeroIdRefType }; var source = sourceItems.InitializeFlatArrayBuilder(length); - var actual = source.MoveToFlatArray(); - var expectedItems = new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, ZeroIdRefType }; + actual.VerifyInnerState_TheSameAssert(sourceItems, length); + } + [Theory] + [MemberData(nameof(MoveToFlatArray_SourceIsNotDefault_WithHugeCapacity_ExpectInnerStateCorrespondToBuilderState_CaseSource))] + public void MoveToFlatArray_SourceIsNotDefault_WithHugeCapacity_ExpectInnerStateCorrespondToBuilderState( + int length, + RefType[] sourceItems, + RefType[] expectedItems) + { + var source = sourceItems.InitializeFlatArrayBuilder(length); + var actual = source.MoveToFlatArray(); actual.VerifyInnerState(expectedItems, length); } @@ -47,4 +57,62 @@ public void MoveToFlatArray_SourceIsNotDefault_ExpectBuilderStateIsDefault() source.VerifyInnerState(Array.Empty(), default); } -} \ No newline at end of file + + public static IEnumerable MoveToFlatArray_SourceIsNotDefault_ExpectInnerStateTheSameAsBuilderState_CaseSource() + { + yield return new object[] + { + 2, + new[] { PlusFifteenIdRefType, MinusFifteenIdRefType } + }; + yield return new object[] + { + 2, + new[] { PlusFifteenIdRefType, MinusFifteenIdRefType, null } + }; + yield return new object[] + { + 3, + new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType } + }; + yield return new object[] + { + 3, + new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, null } + }; + yield return new object[] + { + 3, + new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, null, null } + }; + } + + + public static IEnumerable MoveToFlatArray_SourceIsNotDefault_WithHugeCapacity_ExpectInnerStateCorrespondToBuilderState_CaseSource() + { + yield return new object[] + { + 2, + new[] { PlusFifteenIdRefType, MinusFifteenIdRefType, null, null }, + new[] { PlusFifteenIdRefType, MinusFifteenIdRefType } + }; + yield return new object[] + { + 2, + new[] { PlusFifteenIdRefType, MinusFifteenIdRefType, null, null, null }, + new[] { PlusFifteenIdRefType, MinusFifteenIdRefType } + }; + yield return new object[] + { + 3, + new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, null, null, null }, + new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType } + }; + yield return new object[] + { + 3, + new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, null, null, null, null }, + new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType } + }; + } +} diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Json/Test.Deserialize.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Json/Test.Deserialize.cs index a201c9c4..e5c997ea 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Json/Test.Deserialize.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T.Json/Test.Deserialize.cs @@ -1,8 +1,8 @@ +using PrimeFuncPack.Core.Tests.TestData; using System; using System.Text.Json; using Xunit; using static PrimeFuncPack.Core.Tests.JsonSerializerTestSource; -using static PrimeFuncPack.UnitTest.TestData; namespace PrimeFuncPack.Core.Tests; @@ -13,7 +13,7 @@ partial class FlatArrayJsonSerializerTest public void Deserialize_SourceIsNullJsonValue_ExpectDefaultState( JsonSerializerOptions? options) { - var source = "null"; + const string source = "null"; var actual = JsonSerializer.Deserialize>(source, options); actual.VerifyInnerState(default, default); @@ -24,10 +24,9 @@ public void Deserialize_SourceIsNullJsonValue_ExpectDefaultState( public void Deserialize_SourceArrayIsEmpty_ExpectDefaultState( JsonSerializerOptions? options) { - var sourceArray = Array.Empty(); - var source = JsonSerializer.Serialize(sourceArray, options); - + const string source = "[]"; var actual = JsonSerializer.Deserialize>(source, options); + actual.VerifyInnerState(default, default); } @@ -91,32 +90,24 @@ public void Deserialize_SourceArrayIsNotEmpty_OptionsCases_ExpectInnerItemsAreEq } [Theory] - [InlineData(AnotherString)] - [InlineData(null, AnotherString)] - [InlineData(AnotherString, null)] - [InlineData(null, null)] - // Test cases for testing doubling the buffer - [InlineData("01")] - [InlineData("01", "02")] - [InlineData("01", "02", "03")] - [InlineData("01", "02", "03", "04")] - [InlineData("01", "02", "03", "04", "05")] - [InlineData("01", "02", "03", "04", "05", "06")] - [InlineData("01", "02", "03", "04", "05", "06", "07")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17")] - public void Deserialize_SourceArrayIsNotEmpty_ExpectInnerItemsAreEqualToSourceArrayItems( - params string?[] sourceItems) + [MemberData(nameof(ReadingCollectionsTestSource.EnumerateStringNonEmptyCases), MemberType = typeof(ReadingCollectionsTestSource))] + public void Deserialize_SourceArrayIsNotEmpty_Strings_ExpectInnerStateCorrespondsToSource( + string?[] sourceItems, string?[] expectedItems) { - var copied = sourceItems.GetCopy(); - var source = JsonSerializer.Serialize(sourceItems); var actual = JsonSerializer.Deserialize>(source); - actual.VerifyInnerState(copied, copied.Length); + actual.VerifyInnerState(expectedItems, sourceItems.Length); + } + + [Theory] + [MemberData(nameof(ReadingCollectionsTestSource.EnumerateInt32NullableNonEmptyCases), MemberType = typeof(ReadingCollectionsTestSource))] + public void Deserialize_SourceArrayIsNotEmpty_Int32Nullable_ExpectInnerStateCorrespondsToSource( + int?[] sourceItems, int?[] expectedItems) + { + var source = JsonSerializer.Serialize(sourceItems); + var actual = JsonSerializer.Deserialize>(source); + + actual.VerifyInnerState(expectedItems, sourceItems.Length); } } \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Factory/Factory.Explicit.From.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Factory/Factory.Explicit.From.cs index 0a66973e..590bc2cf 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Factory/Factory.Explicit.From.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Factory/Factory.Explicit.From.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using PrimeFuncPack.Core.Tests.TestData; using PrimeFuncPack.UnitTest; using Xunit; using static PrimeFuncPack.UnitTest.TestData; @@ -238,33 +239,14 @@ public void FromEnumerable_SourceIsEmpty_ExpectInnerStateIsDefault() } [Theory] - [InlineData(AnotherString)] - [InlineData(null, AnotherString)] - [InlineData(AnotherString, null)] - [InlineData(null, null)] - // Test cases for testing doubling the buffer - [InlineData("01")] - [InlineData("01", "02")] - [InlineData("01", "02", "03")] - [InlineData("01", "02", "03", "04")] - [InlineData("01", "02", "03", "04", "05")] - [InlineData("01", "02", "03", "04", "05", "06")] - [InlineData("01", "02", "03", "04", "05", "06", "07")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16")] - [InlineData("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17")] - public void FromEnumerable_SourceIsNotEmpty_ExpectInnerStateAreSourceItems( - params string?[] sourceItems) + [MemberData(nameof(ReadingCollectionsTestSource.EnumerateStringNonEmptyCases), MemberType = typeof(ReadingCollectionsTestSource))] + public void FromEnumerable_SourceIsNotEmpty_ExpectInnerStateCorrespondsToSource( + string?[] sourceItems, string?[] expectedItems) { - var copied = sourceItems.GetCopy(); - var source = GetSource(); var actual = FlatArray.From(source); - actual.VerifyInnerState(copied, copied.Length); + actual.VerifyInnerState(expectedItems, sourceItems.Length); IEnumerable GetSource() { diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray/Test.From.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray/Test.From.cs index 4de12a46..ab2d2f42 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray/Test.From.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray/Test.From.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using PrimeFuncPack.Core.Tests.TestData; using PrimeFuncPack.UnitTest; using Xunit; using static PrimeFuncPack.UnitTest.TestData; @@ -237,17 +238,14 @@ public void FromEnumerable_SourceIsEmpty_ExpectInnerStateIsDefault() } [Theory] - [InlineData(SomeString)] - [InlineData("01", "02", "03", "04", "05", "06", null, "08", "09", "10", "11", "12")] - public void FromEnumerable_SourceIsNotEmpty_ExpectInnerStateAreSourceItems( - params string?[] sourceItems) + [MemberData(nameof(ReadingCollectionsTestSource.EnumerateStringNonEmptyCases), MemberType = typeof(ReadingCollectionsTestSource))] + public void FromEnumerable_SourceIsNotEmpty_ExpectInnerStateCorrespondsToSource( + string?[] sourceItems, string?[] expectedItems) { - var coppied = sourceItems.GetCopy(); - var source = GetSource(); var actual = FlatArray.From(source); - actual.VerifyInnerState(coppied, coppied.Length); + actual.VerifyInnerState(expectedItems, sourceItems.Length); IEnumerable GetSource() { diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArrayExtensions/Test.ToFlatArray.Enumerable.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArrayExtensions/Test.ToFlatArray.Enumerable.cs index d9153295..47871ad0 100644 --- a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArrayExtensions/Test.ToFlatArray.Enumerable.cs +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArrayExtensions/Test.ToFlatArray.Enumerable.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using PrimeFuncPack.Core.Tests.TestData; using PrimeFuncPack.UnitTest; using Xunit; using static PrimeFuncPack.UnitTest.TestData; @@ -28,19 +29,16 @@ public void ToFlatArray_SourceIsEmptyEnumerable_ExpectInnerStateIsDefault() } [Theory] - [InlineData(PlusFifteen)] - [InlineData(27, null, 34, -18, 191)] - public void ToFlatArray_FromEnumerable_SourceIsNotEmpty_ExpectInnerStateAreSourceItems( - params int?[] sourceItems) + [MemberData(nameof(ReadingCollectionsTestSource.EnumerateStringNonEmptyCases), MemberType = typeof(ReadingCollectionsTestSource))] + public void ToFlatArray_FromEnumerable_SourceIsNotEmpty_ExpectInnerStateCorrespondsToSource( + string?[] sourceItems, string?[] expectedItems) { - var coppied = sourceItems.GetCopy(); - var source = GetSource(); var actual = source.ToFlatArray(); - actual.VerifyInnerState(coppied, coppied.Length); + actual.VerifyInnerState(expectedItems, sourceItems.Length); - IEnumerable GetSource() + IEnumerable GetSource() { foreach (var item in sourceItems) { diff --git a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.TrimExcess.cs b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.TrimExcess.cs index fcf39dd0..3c0795c6 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.TrimExcess.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T.Builder/FlatArray.Builder.TrimExcess.cs @@ -1,6 +1,4 @@ -using System.Runtime.CompilerServices; - -namespace System; +namespace System; partial struct FlatArray { @@ -14,7 +12,7 @@ internal void TrimExcess() var length = this.length; var items = this.items; - if (length == items.Length) + if (items.Length == length) { return; } diff --git a/src/flatcollections-array/FlatArray/FlatArray.T.JsonConverter/FlatArray.JsonConverter.Read.cs b/src/flatcollections-array/FlatArray/FlatArray.T.JsonConverter/FlatArray.JsonConverter.Read.cs index 654f1612..1a56b18f 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T.JsonConverter/FlatArray.JsonConverter.Read.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T.JsonConverter/FlatArray.JsonConverter.Read.cs @@ -42,12 +42,7 @@ public override FlatArray Read(ref Utf8JsonReader reader, Type typeToConvert, array[actualCount++] = InnerReadItem(ref reader, options); } - if (actualCount < array.Length) - { - InnerArrayHelper.TruncateUnchecked(ref array, actualCount); - } - - return new(array, default); + return new(actualCount, array); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.TrimExcess.cs b/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.TrimExcess.cs new file mode 100644 index 00000000..4ea9ff60 --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.TrimExcess.cs @@ -0,0 +1,9 @@ +namespace System; + +partial struct FlatArray +{ + // TODO: Add the tests and make public + internal FlatArray TrimExcess() + => + items is null || items.Length == length ? this : new(InnerArrayHelper.Copy(items, length), default); +} diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIEnumerable.cs b/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIEnumerable.cs index 25e8422e..0023e7b4 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIEnumerable.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIEnumerable.cs @@ -31,12 +31,7 @@ internal static FlatArray FromIEnumerable(IEnumerable source, int estimate array[actualCount++] = enumerator.Current; } - if (actualCount < array.Length) - { - InnerArrayHelper.TruncateUnchecked(ref array, actualCount); - } - - return new(array, default); + return new(actualCount, array); } } } diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIReadOnlyList.cs b/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIReadOnlyList.cs index 1451ebf4..6491c9cb 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIReadOnlyList.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/InnerFactory/FlatArray.InnerFactory.FromIReadOnlyList.cs @@ -31,12 +31,7 @@ internal static FlatArray FromIReadOnlyList(IReadOnlyList source) actualCount++; } - if (actualCount < array.Length) - { - InnerArrayHelper.TruncateUnchecked(ref array, actualCount); - } - - return new(array, default); + return new(actualCount, array); } } }