Skip to content

Commit

Permalink
Added FirstOrDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
teoadal committed Nov 24, 2020
1 parent 8c446c1 commit a94696d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 21 deletions.
12 changes: 3 additions & 9 deletions Local.Tests/Collections/AddShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void AddItems(int itemsCount)
Assert.True(vector.Contains(item));
}
}

[Theory, MemberData(nameof(ItemsCount))]
public void AddRangeOfItems(int itemsCount)
{
Expand All @@ -45,7 +45,7 @@ public void AddRangeOfItems(int itemsCount)
Assert.True(vector.Contains(item));
}
}

[Theory, MemberData(nameof(ItemsCount))]
public void AddRangeOfItemsCollection(int itemsCount)
{
Expand All @@ -66,13 +66,7 @@ public void AddRangeOfItemsCollection(int itemsCount)

public static TheoryData<int> ItemsCount() => new TheoryData<int>
{
0,
1,
5,
10,
20,
100,
200
0, 1, 5, 10, 20, 100, 200
};
}
}
2 changes: 1 addition & 1 deletion Local.Tests/Collections/ExtensionsShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void ThrowIfCalculateMaxByEmptyVector() => Assert.Throws<InvalidOperation
var vector = new LocalVector<double>();
vector.Max();
});

[Fact]
public void ThrowIfCalculateMinByEmptyVector() => Assert.Throws<InvalidOperationException>(() =>
{
Expand Down
63 changes: 63 additions & 0 deletions Local.Tests/Collections/FirstOrDefaultShould.cs
Original file line number Diff line number Diff line change
@@ -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<int>(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<int>(items);

vector
.FirstOrDefault((i, arg) => i == arg, argument)
.Should().Be(items.FirstOrDefault(i => i == argument));
}

[Fact]
public void NotThrowIfNotFoundByPredicate()
{
new LocalVector<int>()
.FirstOrDefault(i => i == 1)
.Should().Be(0);
}

[Fact]
public void NotThrowIfNotFoundByPredicateWithArg()
{
const int argument = 1;

new LocalVector<int>()
.FirstOrDefault((i, arg) => i == arg, argument)
.Should().Be(0);
}

[Theory, MemberData(nameof(Items))]
public void ReturnFirstOrDefaultElement(int[] items)
{
var vector = new LocalVector<int>(items);

vector
.FirstOrDefault()
.Should().Be(items.FirstOrDefault());
}
}
}
16 changes: 8 additions & 8 deletions Local.Tests/Local.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture.Xunit2" Version="4.11.0"/>
<PackageReference Include="FluentAssertions" Version="5.10.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1"/>
<PackageReference Include="Moq" Version="4.14.1"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"/>
<PackageReference Include="coverlet.collector" Version="1.2.1"/>
<PackageReference Include="AutoFixture.Xunit2" Version="4.11.0" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="coverlet.collector" Version="1.2.1" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Local\Local.csproj"/>
<ProjectReference Include="..\Local\Local.csproj" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions Local/Collections/LocalVector.Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ public readonly T First<TArg>(Func<T, TArg, bool> predicate, TArg arg)

#endregion

#region FirstOrDefault

public readonly T FirstOrDefault()
{
return _length == 0 ? default! : _element0;
}

public readonly T FirstOrDefault(Func<T, bool> predicate)
{
for (var i = 0; i < _length; i++)
{
var element = Get(i);
if (predicate(element))
{
return element;
}
}

return default!;
}

public readonly T FirstOrDefault<TArg>(Func<T, TArg, bool> 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();
Expand Down
2 changes: 1 addition & 1 deletion Local/Collections/LocalVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public readonly bool Contains(T element, EqualityComparer<T>? comparer = null)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Enumerator GetEnumerator() => new Enumerator(in this);
public readonly Enumerator GetEnumerator() => new Enumerator(this);

public void Reverse()
{
Expand Down
4 changes: 2 additions & 2 deletions Local/Local.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

<Title>Local</Title>
<Description>Simple collection on stack</Description>
<Version>0.8.1</Version>
<Version>0.8.2</Version>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/teoadal/local</RepositoryUrl>
<PackageId>Locals</PackageId>
<PackageTags>collection;list;performance</PackageTags>
<PackageTags>collection;stack;list;performance</PackageTags>
<PackageProjectUrl>https://github.com/teoadal/local</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down

0 comments on commit a94696d

Please sign in to comment.