From 77ee2d268d9ae2fad1893e5ad0488a2757ccdf12 Mon Sep 17 00:00:00 2001 From: Rafael Escoto Date: Tue, 2 Oct 2018 13:35:51 +0200 Subject: [PATCH] Adding ToHashSet Method --- ShittyLINQ/ToHashSet.cs | 22 ++++++++++++++++++++++ ShittyLinqTests/ToHashSetTests.cs | 27 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 ShittyLINQ/ToHashSet.cs create mode 100644 ShittyLinqTests/ToHashSetTests.cs diff --git a/ShittyLINQ/ToHashSet.cs b/ShittyLINQ/ToHashSet.cs new file mode 100644 index 0000000..01cd198 --- /dev/null +++ b/ShittyLINQ/ToHashSet.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + /// + /// Returns a of the source items. + /// + /// Type of elements in source sequence. + /// Source sequence + /// A hash set of the items in the sequence. + /// is null + public static HashSet ToHashSet(this IEnumerable source) + { + if (source == null) throw new ArgumentNullException(); + return new HashSet(source); + } + } +} diff --git a/ShittyLinqTests/ToHashSetTests.cs b/ShittyLinqTests/ToHashSetTests.cs new file mode 100644 index 0000000..abf25ff --- /dev/null +++ b/ShittyLinqTests/ToHashSetTests.cs @@ -0,0 +1,27 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using ShittyTests.TestHelpers; +using System; +using System.Collections.Generic; + +namespace ShittyLinqTests +{ + [TestClass] + public class ToHashSetTests + { + [TestMethod] + public void ToHashSet_SequenceIsNull() + { + IEnumerable nums = null; + Assert.ThrowsException(() => nums.ToHashSet()); + } + + [TestMethod] + public void ToHashSet_SequenceEquals() + { + var expected = new int[] { 0, 1, 2 }; + var actual = expected.ToHashSet(); + TestHelper.AssertCollectionsAreSame(expected, actual); + } + } +}