Browse files

Added reference implementation of EquatableStruct.

  • Loading branch information...
1 parent 4cc9eaf commit 75b6034fe71ff39878af0c6548f26b8b7b466429 @bgrainger bgrainger committed Feb 2, 2010
View
91 tests/Logos.Utility.Tests/EquatableStruct.cs
@@ -0,0 +1,91 @@
+
+using System;
+
+namespace Logos.Utility.Tests
+{
+ public struct EquatableStruct : IEquatable<EquatableStruct>
+ {
+ /// <summary>
+ /// Constructs a new instance of <see cref="EquatableStruct"/>.
+ /// </summary>
+ /// <param name="text">The text.</param>
+ /// <param name="count">The count.</param>
+ public EquatableStruct(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 obj is EquatableStruct && Equals((EquatableStruct) obj);
+ }
+
+ /// <summary>
+ /// Returns <c>true</c> if this object is equal to the specified <see cref="EquatableStruct"/>.
+ /// </summary>
+ /// <param name="obj">The <see cref="EquatableStruct"/> to compare to.</param>
+ /// <returns><c>true</c> if this object is equal to the specified <see cref="EquatableStruct"/>; otherwise <c>false</c>.</returns>
+ public bool Equals(EquatableStruct 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="EquatableStruct"/> objects have the same value.
+ /// </summary>
+ /// <param name="left">An <see cref="EquatableStruct"/> or a <c>null</c> reference.</param>
+ /// <param name="right">An <see cref="EquatableStruct"/> 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 ==(EquatableStruct left, EquatableStruct right)
+ {
+ return left.Equals(right);
+ }
+
+ /// <summary>
+ /// Determines whether two specified <see cref="EquatableStruct"/> objects have different value.
+ /// </summary>
+ /// <param name="left">An <see cref="EquatableStruct"/> or a <c>null</c> reference.</param>
+ /// <param name="right">An <see cref="EquatableStruct"/> 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 !=(EquatableStruct left, EquatableStruct right)
+ {
+ return !left.Equals(right);
+ }
+
+ readonly string m_text;
+ readonly int m_count;
+ }
+}
View
79 tests/Logos.Utility.Tests/EquatableStructTests.cs
@@ -0,0 +1,79 @@
+
+using NUnit.Framework;
+
+namespace Logos.Utility.Tests
+{
+ [TestFixture]
+ public class EquatableStructTests
+ {
+ [TestCase(null, 0)]
+ [TestCase("one", 1)]
+ [TestCase("million", 1000000)]
+ [TestCase("minus", -2)]
+ public void Constructor(string s, int i)
+ {
+ EquatableStruct ec = new EquatableStruct(s, i);
+ Assert.That(ec.Text, Is.EqualTo(s));
+ Assert.That(ec.Count, Is.EqualTo(i));
+ }
+
+ [Test]
+ public void Equals()
+ {
+ Assert.That(es_zero_0.Equals(es_zero_0), Is.True);
+ Assert.That(es_zero_0.Equals(es_zero_0_2), Is.True);
+ Assert.That(es_zero_0.Equals(es_zero_1), Is.False);
+ Assert.That(es_zero_0.Equals(es_one_0), Is.False);
+ Assert.That(es_zero_0.Equals(es_one_1), Is.False);
+ }
+
+ [Test]
+ public void EqualsNull()
+ {
+ Assert.That(es_zero_0.Equals(null), Is.False);
+ }
+
+ [Test]
+ public void EqualsObject()
+ {
+ Assert.That(es_zero_0.Equals((object) es_zero_0), Is.True);
+ Assert.That(es_zero_0.Equals((object) es_zero_0_2), Is.True);
+ Assert.That(es_zero_0.Equals((object) es_zero_1), Is.False);
+ Assert.That(es_zero_0.Equals((object) es_one_0), Is.False);
+ Assert.That(es_zero_0.Equals((object) es_one_1), Is.False);
+ }
+
+ [Test]
+ public void OperatorEquality()
+ {
+ Assert.That(es_zero_0 == es_zero_0_2, Is.True);
+ Assert.That(es_zero_0 == es_zero_1, Is.False);
+ Assert.That(es_zero_0 == es_one_0, Is.False);
+ Assert.That(es_zero_0 == es_one_1, Is.False);
+ }
+
+ [Test]
+ public void OperatorInequality()
+ {
+ Assert.That(es_zero_0 != es_zero_0_2, Is.False);
+ Assert.That(es_zero_0 != es_zero_1, Is.True);
+ Assert.That(es_zero_0 != es_one_0, Is.True);
+ Assert.That(es_zero_0 != es_one_1, Is.True);
+ }
+
+ [Test]
+ public void HashCode()
+ {
+ Assert.That(es_zero_0.GetHashCode(), Is.EqualTo(es_zero_0_2.GetHashCode()));
+ Assert.That(es_zero_0.GetHashCode(), Is.Not.EqualTo(es_zero_1.GetHashCode()));
+ Assert.That(es_zero_0.GetHashCode(), Is.Not.EqualTo(es_one_0.GetHashCode()));
+ Assert.That(es_zero_0.GetHashCode(), Is.Not.EqualTo(es_one_1.GetHashCode()));
+ }
+
+ EquatableStruct es_zero_0 = new EquatableStruct("zero", 0);
+ EquatableStruct es_zero_0_2 = new EquatableStruct("zero", 0);
+ EquatableStruct es_zero_1 = new EquatableStruct("zero", 1);
+ EquatableStruct es_one_0 = new EquatableStruct("one", 0);
+ EquatableStruct es_one_1 = new EquatableStruct("one", 1);
+ }
+}
View
2 tests/Logos.Utility.Tests/Logos.Utility.Tests.csproj
@@ -52,6 +52,8 @@
<Compile Include="DictionaryUtilityTests.cs" />
<Compile Include="EquatableClass.cs" />
<Compile Include="EquatableClassTests.cs" />
+ <Compile Include="EquatableStruct.cs" />
+ <Compile Include="EquatableStructTests.cs" />
<Compile Include="ObjectUtilityTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScopeTests.cs" />

0 comments on commit 75b6034

Please sign in to comment.