From 701788ba92d0e306831cb0904e524a749f215bf1 Mon Sep 17 00:00:00 2001 From: YohskDista Date: Tue, 2 Oct 2018 22:51:55 +0200 Subject: [PATCH 1/3] First implementation of SequenceEqual --- ShittyLINQ/SequenceEqual.cs | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ShittyLINQ/SequenceEqual.cs diff --git a/ShittyLINQ/SequenceEqual.cs b/ShittyLINQ/SequenceEqual.cs new file mode 100644 index 0000000..965731c --- /dev/null +++ b/ShittyLINQ/SequenceEqual.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static bool SequenceEqual(this IEnumerable first, IEnumerable second) + { + if (first.Count() != second.Count()) + return false; + + var areSequenceEquals = true; + + for (int i = 0; i < first.Count(); i++) + { + areSequenceEquals &= (first.ElementAt(i).Equals(second.ElementAt(i)); + } + + return areSequenceEquals; + } + + public static bool SequenceEqual( + this IEnumerable first, + IEnumerable second, + IEqualityComparer comparer) + { + if (first.Count() != second.Count()) + return false; + + var areSequenceEquals = true; + + for (int i = 0; i < first.Count(); i++) + { + areSequenceEquals &= (comparer.Equals(first.ElementAt(i), second.ElementAt(i))); + } + + return areSequenceEquals; + } + } +} From 4aa7744019afdaad0b3c199222a113d620c2525e Mon Sep 17 00:00:00 2001 From: YohskDista Date: Tue, 2 Oct 2018 22:52:43 +0200 Subject: [PATCH 2/3] Forget a closing ) --- ShittyLINQ/SequenceEqual.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ShittyLINQ/SequenceEqual.cs b/ShittyLINQ/SequenceEqual.cs index 965731c..48438b6 100644 --- a/ShittyLINQ/SequenceEqual.cs +++ b/ShittyLINQ/SequenceEqual.cs @@ -15,7 +15,7 @@ public static bool SequenceEqual(this IEnumerable first, IEnumerable se for (int i = 0; i < first.Count(); i++) { - areSequenceEquals &= (first.ElementAt(i).Equals(second.ElementAt(i)); + areSequenceEquals &= (first.ElementAt(i).Equals(second.ElementAt(i))); } return areSequenceEquals; From 4138910c18b152a5fdb2cbb0a3ff2df48a429029 Mon Sep 17 00:00:00 2001 From: YohskDista Date: Tue, 2 Oct 2018 23:07:27 +0200 Subject: [PATCH 3/3] Test the `SequenceEqual` extensions Test the different implementation of the `SequenceEqual` extensions with the comparer and without it. Take the Microsoft example as samples --- ShittyLinqTests/SequenceEqualTests.cs | 93 +++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 ShittyLinqTests/SequenceEqualTests.cs diff --git a/ShittyLinqTests/SequenceEqualTests.cs b/ShittyLinqTests/SequenceEqualTests.cs new file mode 100644 index 0000000..fe09cbc --- /dev/null +++ b/ShittyLinqTests/SequenceEqualTests.cs @@ -0,0 +1,93 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyTests +{ + class Pet + { + public string Name { get; set; } + public int Age { get; set; } + } + + class ProductA + { + public string Name { get; set; } + public int Code { get; set; } + } + + class ProductComparer : IEqualityComparer + { + + public bool Equals(ProductA x, ProductA y) + { + if (ReferenceEquals(x, y)) return true; + + return x != null && y != null && x.Code.Equals(y.Code) && x.Name.Equals(y.Name); + } + + public int GetHashCode(ProductA obj) + { + int hashProductName = obj.Name == null ? 0 : obj.Name.GetHashCode(); + + int hashProductCode = obj.Code.GetHashCode(); + + return hashProductName ^ hashProductCode; + } + } + + /// + /// The code for the tests is the same than in the Microsoft example + /// for the SequenceEqual extension + /// + [TestClass] + public class SequenceEqualTests + { + [TestMethod] + public void When_Two_List_Are_Fill_With_Same_Objects_Then_Should_Be_Equals() + { + var pet1 = new Pet { Name = "Turbo", Age = 2 }; + var pet2 = new Pet { Name = "Peanut", Age = 8 }; + + var pets1 = new List { pet1, pet2 }; + var pets2 = new List { pet1, pet2 }; + + var equal = pets1.SequenceEqual(pets2); + + Assert.IsTrue(equal); + } + + [TestMethod] + public void When_Two_List_Are_Fill_With_Same_Objects_But_Different_References_Then_Should_Be_Not_Equals() + { + var pet1 = new Pet() { Name = "Turbo", Age = 2 }; + var pet2 = new Pet() { Name = "Peanut", Age = 8 }; + + // Create two lists of pets. + var pets1 = new List { pet1, pet2 }; + var pets2 = + new List { new Pet { Name = "Turbo", Age = 2 }, + new Pet { Name = "Peanut", Age = 8 } }; + + var equal = pets1.SequenceEqual(pets2); + + Assert.IsFalse(equal); + } + + [TestMethod] + public void When_Two_Lists_Are_Fill_With_Different_References_But_With_Comparer_Then_Should_Be_Equals() + { + ProductA[] storeA = { new ProductA { Name = "apple", Code = 9 }, + new ProductA { Name = "orange", Code = 4 } }; + + ProductA[] storeB = { new ProductA { Name = "apple", Code = 9 }, + new ProductA { Name = "orange", Code = 4 } }; + + bool equalAB = storeA.SequenceEqual(storeB, new ProductComparer()); + + Assert.IsTrue(equalAB); + } + } +}