public void TestUserInfoEquality() { // Object setup UserInfo u1 = new UserInfo("Foo", "Bar", "Baz"); UserInfo u2 = new UserInfo("Foo", "Bar", "Baz"); UserInfo u3 = new UserInfo("Hello", "World", "Foobar"); UserInfo u4 = new UserInfo("Hello", "World", "Foobar"); UserInfo u5 = null; UserInfo u6 = u1; // Equality Assert.IsTrue(UserInfo.Equals(u1, u2)); Assert.IsTrue(u1.Equals(u2)); Assert.IsTrue(u1 == u2); Assert.IsTrue(UserInfo.Equals(u3, u4)); Assert.IsTrue(u3.Equals(u4)); Assert.IsTrue(u3 == u4); // Inequality Assert.IsFalse(UserInfo.Equals(u1, u3)); Assert.IsFalse(u1.Equals(u3)); Assert.IsTrue(u1 != u3); Assert.IsFalse(UserInfo.Equals(u2, u4)); Assert.IsFalse(u2.Equals(u4)); Assert.IsTrue(u2 != u4); // Checks against null objects Assert.IsFalse(UserInfo.Equals(u1, u5)); Assert.IsFalse(UserInfo.Equals(u5, u1)); Assert.IsTrue(u1 != u5); Assert.IsTrue(u5 != u1); // Checks against null Assert.IsFalse(u1.Equals(null)); Assert.IsTrue(UserInfo.Equals(u5, null)); Assert.IsTrue(UserInfo.Equals(null, u5)); Assert.IsTrue(u5 == null); Assert.IsTrue(null == u5); // Checks against self Assert.IsTrue(u1.Equals(u6)); // Hash codes are equal; Assert.IsTrue(u1.GetHashCode() == u2.GetHashCode()); Assert.IsTrue(u3.GetHashCode() == u4.GetHashCode()); }
private static bool EqualsHelper(UserInfo a, UserInfo b) { Debug.Assert(a != null && b != null); return a.Nick == b.Nick && a.User == b.User && a.RealName == b.RealName; }
public static bool Equals(UserInfo a, UserInfo b) { return Object.ReferenceEquals(a, b) || (!Object.ReferenceEquals(a, null) && !Object.ReferenceEquals(b, null) && UserInfo.EqualsHelper(a, b)); }