Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added reference implementation of EquatableClass.

  • Loading branch information...
commit 4cc9eaf7d4119a837fe47a917aa5d91ce20066d2 1 parent b303705
@bgrainger bgrainger authored
View
94 tests/Logos.Utility.Tests/EquatableClass.cs
@@ -0,0 +1,94 @@
+
+using System;
+
+namespace Logos.Utility.Tests
+{
+ /// <summary>
+ /// Gives a reference implementation of an equatable class.
+ /// </summary>
+ public sealed class EquatableClass : IEquatable<EquatableClass>
+ {
+ /// <summary>
+ /// Constructs a new instance of <see cref="EquatableClass"/>.
+ /// </summary>
+ /// <param name="text">The text.</param>
+ /// <param name="count">The count.</param>
+ public EquatableClass(string text, int count)
+ {
+ m_text = text;
+ m_count = count;
+ }
+
+ /// <summary>
+ /// Returns the text.
+ /// </summary>
+ public string Text
+ {
+ get { return m_text; }
+ }
+
+ /// <summary>
+ /// Returns the count.
+ /// </summary>
+ public int Count
+ {
+ get { return m_count; }
+ }
+
+ /// <summary>
+ /// Returns <c>true</c> if this object is equal to the specified object.
+ /// </summary>
+ /// <param name="obj">The object to compare to.</param>
+ /// <returns><c>true</c> if this object is equal to the specified object; otherwise <c>false</c>.</returns>
+ public override bool Equals(object obj)
+ {
+ return Equals(obj as EquatableClass);
+ }
+
+ /// <summary>
+ /// Returns <c>true</c> if this object is equal to the specified <see cref="EquatableClass"/>.
+ /// </summary>
+ /// <param name="obj">The <see cref="EquatableClass"/> to compare to.</param>
+ /// <returns><c>true</c> if this object is equal to the specified <see cref="EquatableClass"/>; otherwise <c>false</c>.</returns>
+ public bool Equals(EquatableClass other)
+ {
+ // this is the core implementation of Equals; other methods delegate to this
+ return other != null && m_text == other.m_text && m_count == other.m_count;
+ }
+
+ /// <summary>
+ /// Returns a hash code for the current object.
+ /// </summary>
+ /// <returns>A hash code for the current object.</returns>
+ public override int GetHashCode()
+ {
+ // combine the hash codes of the various components of this class
+ return HashCodeUtility.CombineHashCodes(ObjectUtility.GetHashCode(m_text), m_count);
+ }
+
+ /// <summary>
+ /// Determines whether two specified <see cref="EquatableClass"/> objects have the same value.
+ /// </summary>
+ /// <param name="left">An <see cref="EquatableClass"/> or a <c>null</c> reference.</param>
+ /// <param name="right">An <see cref="EquatableClass"/> or a <c>null</c> reference.</param>
+ /// <returns><c>true</c> if the value of <paramref name="left"/> is the same as the value of <paramref name="right"/>; otherwise, <c>false</c>.</returns>
+ public static bool operator ==(EquatableClass left, EquatableClass right)
+ {
+ return ObjectImpl.OperatorEquality(left, right);
+ }
+
+ /// <summary>
+ /// Determines whether two specified <see cref="EquatableClass"/> objects have different value.
+ /// </summary>
+ /// <param name="left">An <see cref="EquatableClass"/> or a <c>null</c> reference.</param>
+ /// <param name="right">An <see cref="EquatableClass"/> or a <c>null</c> reference.</param>
+ /// <returns><c>true</c> if the value of <paramref name="left"/> is different from the value of <paramref name="right"/>; otherwise, <c>false</c>.</returns>
+ public static bool operator !=(EquatableClass left, EquatableClass right)
+ {
+ return ObjectImpl.OperatorInequality(left, right);
+ }
+
+ readonly string m_text;
+ readonly int m_count;
+ }
+}
View
85 tests/Logos.Utility.Tests/EquatableClassTests.cs
@@ -0,0 +1,85 @@
+
+using NUnit.Framework;
+
+namespace Logos.Utility.Tests
+{
+ [TestFixture]
+ public class EquatableClassTests
+ {
+ [TestCase(null, 0)]
+ [TestCase("one", 1)]
+ [TestCase("million", 1000000)]
+ [TestCase("minus", -2)]
+ public void Constructor(string s, int i)
+ {
+ EquatableClass ec = new EquatableClass(s, i);
+ Assert.That(ec.Text, Is.EqualTo(s));
+ Assert.That(ec.Count, Is.EqualTo(i));
+ }
+
+ [Test]
+ public void Equals()
+ {
+ Assert.That(ec_zero_0.Equals(ec_zero_0), Is.True);
+ Assert.That(ec_zero_0.Equals(ec_zero_0_2), Is.True);
+ Assert.That(ec_zero_0.Equals(ec_zero_1), Is.False);
+ Assert.That(ec_zero_0.Equals(ec_one_0), Is.False);
+ Assert.That(ec_zero_0.Equals(ec_one_1), Is.False);
+ }
+
+ [Test]
+ public void EqualsNull()
+ {
+ Assert.That(ec_zero_0.Equals(null), Is.False);
+ Assert.That(ec_zero_0.Equals(ec_null), Is.False);
+ }
+
+ [Test]
+ public void EqualsObject()
+ {
+ Assert.That(ec_zero_0.Equals((object) ec_zero_0), Is.True);
+ Assert.That(ec_zero_0.Equals((object) ec_zero_0_2), Is.True);
+ Assert.That(ec_zero_0.Equals((object) ec_zero_1), Is.False);
+ Assert.That(ec_zero_0.Equals((object) ec_one_0), Is.False);
+ Assert.That(ec_zero_0.Equals((object) ec_one_1), Is.False);
+ }
+
+ [Test]
+ public void OperatorEquality()
+ {
+ Assert.That(ec_zero_0 == ec_zero_0_2, Is.True);
+ Assert.That(ec_zero_0 == ec_zero_1, Is.False);
+ Assert.That(ec_zero_0 == ec_one_0, Is.False);
+ Assert.That(ec_zero_0 == ec_one_1, Is.False);
+ Assert.That(ec_zero_0 == ec_null, Is.False);
+ Assert.That(ec_null == ec_zero_0, Is.False);
+ }
+
+ [Test]
+ public void OperatorInequality()
+ {
+ Assert.That(ec_zero_0 != ec_zero_0_2, Is.False);
+ Assert.That(ec_zero_0 != ec_zero_1, Is.True);
+ Assert.That(ec_zero_0 != ec_one_0, Is.True);
+ Assert.That(ec_zero_0 != ec_one_1, Is.True);
+ Assert.That(ec_zero_0 != ec_null, Is.True);
+ Assert.That(ec_null != ec_zero_0, Is.True);
+ }
+
+ [Test]
+ public void HashCode()
+ {
+ Assert.That(ec_zero_0.GetHashCode(), Is.EqualTo(ec_zero_0_2.GetHashCode()));
+ Assert.That(ec_zero_0.GetHashCode(), Is.Not.EqualTo(ec_zero_1.GetHashCode()));
+ Assert.That(ec_zero_0.GetHashCode(), Is.Not.EqualTo(ec_one_0.GetHashCode()));
+ Assert.That(ec_zero_0.GetHashCode(), Is.Not.EqualTo(ec_one_1.GetHashCode()));
+ }
+
+ EquatableClass ec_zero_0 = new EquatableClass("zero", 0);
+ EquatableClass ec_zero_0_2 = new EquatableClass("zero", 0);
+ EquatableClass ec_zero_1 = new EquatableClass("zero", 1);
+ EquatableClass ec_one_0 = new EquatableClass("one", 0);
+ EquatableClass ec_one_1 = new EquatableClass("one", 1);
+ EquatableClass ec_null = null;
+ }
+}
View
2  tests/Logos.Utility.Tests/Logos.Utility.Tests.csproj
@@ -50,6 +50,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DictionaryUtilityTests.cs" />
+ <Compile Include="EquatableClass.cs" />
+ <Compile Include="EquatableClassTests.cs" />
<Compile Include="ObjectUtilityTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScopeTests.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.