diff --git a/Local.Tests/Collections/AddShould.cs b/Local.Tests/Collections/AddShould.cs index 4932600..231a4a2 100644 --- a/Local.Tests/Collections/AddShould.cs +++ b/Local.Tests/Collections/AddShould.cs @@ -31,7 +31,7 @@ public void AddItems(int itemsCount) Assert.True(vector.Contains(item)); } } - + [Theory, MemberData(nameof(ItemsCount))] public void AddRangeOfItems(int itemsCount) { @@ -45,7 +45,7 @@ public void AddRangeOfItems(int itemsCount) Assert.True(vector.Contains(item)); } } - + [Theory, MemberData(nameof(ItemsCount))] public void AddRangeOfItemsCollection(int itemsCount) { @@ -66,13 +66,7 @@ public void AddRangeOfItemsCollection(int itemsCount) public static TheoryData ItemsCount() => new TheoryData { - 0, - 1, - 5, - 10, - 20, - 100, - 200 + 0, 1, 5, 10, 20, 100, 200 }; } } \ No newline at end of file diff --git a/Local.Tests/Collections/ExtensionsShould.cs b/Local.Tests/Collections/ExtensionsShould.cs index 120fc30..d74d424 100644 --- a/Local.Tests/Collections/ExtensionsShould.cs +++ b/Local.Tests/Collections/ExtensionsShould.cs @@ -105,7 +105,7 @@ public void ThrowIfCalculateMaxByEmptyVector() => Assert.Throws(); vector.Max(); }); - + [Fact] public void ThrowIfCalculateMinByEmptyVector() => Assert.Throws(() => { diff --git a/Local.Tests/Collections/FirstOrDefaultShould.cs b/Local.Tests/Collections/FirstOrDefaultShould.cs new file mode 100644 index 0000000..0a9f4d1 --- /dev/null +++ b/Local.Tests/Collections/FirstOrDefaultShould.cs @@ -0,0 +1,63 @@ +using System; +using System.Linq; +using FluentAssertions; +using Local.Collections; +using Xunit; + +namespace Local.Tests.Collections +{ + public class FirstOrDefaultShould : LinqTests + { + [Theory, MemberData(nameof(Items))] + public void FindFirstByPredicate(int[] items) + { + static bool Predicate(int i) => i == 1; + + var vector = new LocalVector(items); + + vector + .FirstOrDefault(Predicate) + .Should().Be(items.FirstOrDefault(Predicate)); + } + + [Theory, MemberData(nameof(Items))] + public void FindFirstByPredicateWithArg(int[] items) + { + const int argument = 1; + + var vector = new LocalVector(items); + + vector + .FirstOrDefault((i, arg) => i == arg, argument) + .Should().Be(items.FirstOrDefault(i => i == argument)); + } + + [Fact] + public void NotThrowIfNotFoundByPredicate() + { + new LocalVector() + .FirstOrDefault(i => i == 1) + .Should().Be(0); + } + + [Fact] + public void NotThrowIfNotFoundByPredicateWithArg() + { + const int argument = 1; + + new LocalVector() + .FirstOrDefault((i, arg) => i == arg, argument) + .Should().Be(0); + } + + [Theory, MemberData(nameof(Items))] + public void ReturnFirstOrDefaultElement(int[] items) + { + var vector = new LocalVector(items); + + vector + .FirstOrDefault() + .Should().Be(items.FirstOrDefault()); + } + } +} \ No newline at end of file diff --git a/Local.Tests/Local.Tests.csproj b/Local.Tests/Local.Tests.csproj index dccfc39..72fb3fd 100644 --- a/Local.Tests/Local.Tests.csproj +++ b/Local.Tests/Local.Tests.csproj @@ -9,18 +9,18 @@ - - - - - - - + + + + + + + - + diff --git a/Local/Collections/LocalVector.Linq.cs b/Local/Collections/LocalVector.Linq.cs index 4a925b0..fbc5c9a 100644 --- a/Local/Collections/LocalVector.Linq.cs +++ b/Local/Collections/LocalVector.Linq.cs @@ -137,6 +137,43 @@ public readonly T First(Func predicate, TArg arg) #endregion + #region FirstOrDefault + + public readonly T FirstOrDefault() + { + return _length == 0 ? default! : _element0; + } + + public readonly T FirstOrDefault(Func predicate) + { + for (var i = 0; i < _length; i++) + { + var element = Get(i); + if (predicate(element)) + { + return element; + } + } + + return default!; + } + + public readonly T FirstOrDefault(Func predicate, TArg arg) + { + for (var i = 0; i < _length; i++) + { + var element = Get(i); + if (predicate(element, arg)) + { + return element; + } + } + + return default!; + } + + #endregion + public readonly T Last() { EnsureNotEmpty(); diff --git a/Local/Collections/LocalVector.cs b/Local/Collections/LocalVector.cs index 00b083e..12362cf 100644 --- a/Local/Collections/LocalVector.cs +++ b/Local/Collections/LocalVector.cs @@ -205,7 +205,7 @@ public readonly bool Contains(T element, EqualityComparer? comparer = null) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly Enumerator GetEnumerator() => new Enumerator(in this); + public readonly Enumerator GetEnumerator() => new Enumerator(this); public void Reverse() { diff --git a/Local/Local.csproj b/Local/Local.csproj index f84df21..35e7e6a 100644 --- a/Local/Local.csproj +++ b/Local/Local.csproj @@ -10,11 +10,11 @@ Local Simple collection on stack - 0.8.1 + 0.8.2 git git://github.com/teoadal/local Locals - collection;list;performance + collection;stack;list;performance https://github.com/teoadal/local MIT true