Skip to content

Commit bd59190

Browse files
committed
exact on custom type in Edge IEquatable<> interface
1 parent d4c8b54 commit bd59190

File tree

9 files changed

+102
-9
lines changed

9 files changed

+102
-9
lines changed

src/Dijkstra.NET/Dijkstra.NET.Tests/Dijkstra.NET.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</Choose>
7272
<ItemGroup>
7373
<Compile Include="DijkstraTest.cs" />
74+
<Compile Include="EdgeTest.cs" />
7475
<Compile Include="Properties\AssemblyInfo.cs" />
7576
</ItemGroup>
7677
<ItemGroup>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
namespace Dijkstra.NET.Tests
2+
{
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Model;
5+
6+
[TestClass]
7+
public class EdgeTest
8+
{
9+
[TestMethod]
10+
public void Two_Edges_Should_Be_Equal()
11+
{
12+
var a = new Edge<string, int>(new Node<string, int>(0, "node1"), 1, 1);
13+
var b = new Edge<string, int>(new Node<string, int>(0, "node2"),1, 1);
14+
15+
bool act = a.Equals(b);
16+
17+
Assert.IsTrue(act);
18+
}
19+
20+
[TestMethod]
21+
public void Two_Edges_Should_Be_Equal_With_Null_References()
22+
{
23+
var a = new Edge<string, string>(new Node<string, string>(0, "node1"), 1, null);
24+
var b = new Edge<string, string>(new Node<string, string>(0, "node2"),1, null);
25+
26+
bool act = a.Equals(b);
27+
28+
Assert.IsTrue(act);
29+
}
30+
31+
[TestMethod]
32+
public void Two_Edges_Should_Be_Diffrent_With_Null_Reference()
33+
{
34+
var a = new Edge<string, string>(new Node<string, string>(0, "node1"), 1, null);
35+
var b = new Edge<string, string>(new Node<string, string>(0, "node2"),1, "a");
36+
37+
bool act = a.Equals(b);
38+
bool act2 = b.Equals(a);
39+
40+
Assert.IsFalse(act);
41+
Assert.IsFalse(act2);
42+
}
43+
44+
[TestMethod]
45+
public void Two_Edges_Should_Be_Diffrent_With_Diffrent_Parameter()
46+
{
47+
var a = new Edge<string, string>(new Node<string, string>(0, "node1"), 1, "b");
48+
var b = new Edge<string, string>(new Node<string, string>(0, "node2"),1, "a");
49+
50+
bool act = a.Equals(b);
51+
bool act2 = b.Equals(a);
52+
53+
Assert.IsFalse(act);
54+
Assert.IsFalse(act2);
55+
}
56+
57+
[TestMethod]
58+
public void Two_Edges_Should_Be_Diffrent_With_Diffrent_Costs()
59+
{
60+
var a = new Edge<string, string>(new Node<string, string>(0, "node1"), 3, "a");
61+
var b = new Edge<string, string>(new Node<string, string>(0, "node2"),1, "a");
62+
63+
bool act = a.Equals(b);
64+
bool act2 = b.Equals(a);
65+
66+
Assert.IsFalse(act);
67+
Assert.IsFalse(act2);
68+
}
69+
70+
[TestMethod]
71+
public void Two_Edges_Should_Be_Diffrent_With_Diffrent_Nodes()
72+
{
73+
var a = new Edge<string, string>(new Node<string, string>(0, "node1"), 1, "a");
74+
var b = new Edge<string, string>(new Node<string, string>(1, "node2"),1, "a");
75+
76+
bool act = a.Equals(b);
77+
bool act2 = b.Equals(a);
78+
79+
Assert.IsFalse(act);
80+
Assert.IsFalse(act2);
81+
}
82+
}
83+
}

src/Dijkstra.NET/Dijkstra.NET/Contract/IGraph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace Dijkstra.NET.Contract
22
{
3-
using System.Collections.Generic;
3+
using System;
44

5-
public interface IGraph<T, TEdgeCustom> where TEdgeCustom: class
5+
public interface IGraph<T, TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
66
{
77
INode<T, TEdgeCustom> this[uint node] { get; }
88
}

src/Dijkstra.NET/Dijkstra.NET/Contract/INode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace Dijkstra.NET.Contract
22
{
3+
using System;
34
using System.Collections.Generic;
45
using Model;
56

6-
public interface INode<T,TEdgeCustom> where TEdgeCustom: class
7+
public interface INode<T,TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
78
{
89
IList<Edge<T, TEdgeCustom>> Children { get; }
910
T Item { get; }

src/Dijkstra.NET/Dijkstra.NET/Dijkstra.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
namespace Dijkstra.NET
22
{
3+
using System;
34
using System.Collections.Generic;
45
using Contract;
56
using Extensions;
67
using Model;
78
using Utility;
89

9-
public class Dijkstra<T, TEdgeCustom> where TEdgeCustom: class
10+
public class Dijkstra<T, TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
1011
{
1112
private readonly IGraph<T, TEdgeCustom> _graph;
1213

src/Dijkstra.NET/Dijkstra.NET/Model/Edge.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
namespace Dijkstra.NET.Model
22
{
33
using System;
4+
using System.Collections.Generic;
45
using Contract;
5-
public struct Edge<T, TCustom>: IEquatable<Edge<T, TCustom>> where TCustom: class
6+
public struct Edge<T, TCustom>: IEquatable<Edge<T, TCustom>> where TCustom: IEquatable<TCustom>
67
{
78
public Edge(INode<T, TCustom> node, uint cost, TCustom custom)
89
{
@@ -15,7 +16,11 @@ public Edge(INode<T, TCustom> node, uint cost, TCustom custom)
1516
public uint Cost { get; }
1617
public TCustom Item { get; }
1718

18-
public bool Equals(Edge<T, TCustom> other) => Node.Key == other.Node.Key && Cost == other.Cost && ReferenceEquals(Item, other.Item);
19+
public bool Equals(Edge<T, TCustom> other) => Node.Key == other.Node.Key && Cost == other.Cost &&
20+
(EqualityComparer<TCustom>.Default.Equals(Item, default(TCustom)) &&
21+
EqualityComparer<TCustom>.Default.Equals(other.Item, default(TCustom)) ||
22+
!EqualityComparer<TCustom>.Default.Equals(Item, default(TCustom)) && !EqualityComparer<TCustom>.Default.Equals(other.Item, default(TCustom))
23+
&& Item.Equals(other.Item));
1924

2025
public override int GetHashCode()
2126
{

src/Dijkstra.NET/Dijkstra.NET/Model/Graph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Contract;
8-
public class Graph<T, TEdgeCustom>: IGraph<T, TEdgeCustom>, IEnumerable<INode<T, TEdgeCustom>>, ICloneable where TEdgeCustom: class
8+
public class Graph<T, TEdgeCustom>: IGraph<T, TEdgeCustom>, IEnumerable<INode<T, TEdgeCustom>>, ICloneable where TEdgeCustom : IEquatable<TEdgeCustom>
99
{
1010
private readonly IDictionary<uint, INode<T, TEdgeCustom>> _nodes = new Dictionary<uint, INode<T, TEdgeCustom>>();
1111

src/Dijkstra.NET/Dijkstra.NET/Model/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using Contract;
6-
public class Node<T, TEdgeCustom>: INode<T, TEdgeCustom> where TEdgeCustom: class
6+
public class Node<T, TEdgeCustom>: INode<T, TEdgeCustom> where TEdgeCustom: IEquatable<TEdgeCustom>
77
{
88
public Node(uint key, T item)
99
{

src/Dijkstra.NET/Dijkstra.NET/Utility/NodeComparer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace Dijkstra.NET.Utility
22
{
3+
using System;
34
using System.Collections.Generic;
45
using Contract;
5-
internal class NodeComparer<T, TEdgeCustom> : IComparer<INode<T, TEdgeCustom>> where TEdgeCustom: class
6+
7+
internal class NodeComparer<T, TEdgeCustom> : IComparer<INode<T, TEdgeCustom>> where TEdgeCustom : IEquatable<TEdgeCustom>
68
{
79
public int Compare(INode<T, TEdgeCustom> x, INode<T, TEdgeCustom> y)
810
{

0 commit comments

Comments
 (0)