Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add EqualByValues<T> little class #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Value.Net45/Value.Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RootNamespace>Value</RootNamespace>
<AssemblyName>Value</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
Expand Down Expand Up @@ -58,4 +58,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
30 changes: 30 additions & 0 deletions Value.Tests/EqualByValuesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Value.Shared;

namespace Value.Tests
{
[TestFixture]
public class EqualByValuesTest
{
[Test]
public void Should_be_equal_by_values()
{
var coll1 = new string[] { "Hello", "Apple", "C#" };
var coll2 = new string[] { "C#", "Hello", "Apple" };
Assert.IsTrue(new EqualByValues<string>(coll1, coll2));
}

[Test]
public void Should_not_be_equal_by_values()
{
var coll1 = new string[] { "Hello", "Apple", "C#", "Java" };
var coll2 = new string[] { "C#", "Hello", "Apple" };
Assert.IsTrue(new EqualByValues<string>(coll1, coll2));
}
}
}
7 changes: 4 additions & 3 deletions Value.Tests/Value.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<Compile Include="AmountTests.cs" />
<Compile Include="DictionaryByValueTests.cs" />
<Compile Include="EqualByValuesTest.cs" />
<Compile Include="SetByValueTests.cs" />
<Compile Include="Samples\ThreeCards.cs" />
<Compile Include="Samples\Amount.cs" />
Expand All @@ -66,10 +67,10 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand All @@ -79,4 +80,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
2 changes: 1 addition & 1 deletion Value.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<package id="NFluent" version="1.3.1.0" targetFramework="net45" />
<package id="NUnit" version="3.6.1" targetFramework="net45" />
<package id="NUnit.ConsoleRunner" version="3.6.1" targetFramework="net45" />
</packages>
</packages>
41 changes: 41 additions & 0 deletions Value/EqualByValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

namespace Value.Shared
{
using System.Collections.Generic;

/// <summary>
/// Determines two collections are equal by values, i.e. ignoring the order of an elements,
/// but the exact elements contained.
/// </summary>
public class EqualByValues<T>
{
private IEnumerable<T> _first;
private IEnumerable<T> _second;

public EqualByValues(IEnumerable<T> first, IEnumerable<T> second)
{
_first = first;
_second = second;
}

public bool Value()
{
bool ret = true;
HashSet<T> set = new HashSet<T>(_first);
foreach (var item in _second)
{
if (!set.Contains(item))
{
ret = false;
break;
}
}
return ret;
}

public static implicit operator bool(EqualByValues<T> equalByValues)
{
return equalByValues.Value();
}
}
}
1 change: 1 addition & 0 deletions Value/Value.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Compile Include="$(MSBuildThisFileDirectory)EquatableByValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)EquatableByValueWithoutOrder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ListByValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)EqualByValues.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SetByValue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ValueType.cs" />
</ItemGroup>
Expand Down