diff --git a/ShittyLINQ/OfType.cs b/ShittyLINQ/OfType.cs new file mode 100644 index 0000000..3bf6f47 --- /dev/null +++ b/ShittyLINQ/OfType.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static IEnumerable OfType(this IEnumerable self) + { + if (self == null) throw new ArgumentNullException(); + return OfTypeIterator(self); + } + + private static IEnumerable OfTypeIterator(IEnumerable source) + { + foreach (object obj in source) + { + if (obj is TResult) yield return (TResult)obj; + } + } + } +} diff --git a/ShittyLINQ/ToArray.cs b/ShittyLINQ/ToArray.cs new file mode 100644 index 0000000..6da7eda --- /dev/null +++ b/ShittyLINQ/ToArray.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static TSource[] ToArray(this IEnumerable source) + { + if (source == null) throw new ArgumentNullException(); + return new Buffer(source).ToArray(); + } + } + + internal struct Buffer + { + internal TElement[] items; + internal int count; + + internal Buffer(IEnumerable source) + { + TElement[] items = null; + int count = 0; + ICollection collection = source as ICollection; + if (collection != null) + { + count = collection.Count; + if (count > 0) + { + items = new TElement[count]; + collection.CopyTo(items, 0); + } + } + else + { + foreach (TElement item in source) + { + if (items == null) + { + items = new TElement[4]; + } + else if (items.Length == count) + { + TElement[] newItems = new TElement[checked(count * 2)]; + Array.Copy(items, 0, newItems, 0, count); + items = newItems; + } + items[count] = item; + count++; + } + } + this.items = items; + this.count = count; + } + + internal TElement[] ToArray() + { + if (count == 0) return new TElement[0]; + if (items.Length == count) return items; + TElement[] result = new TElement[count]; + Array.Copy(items, 0, result, 0, count); + return result; + } + } +} diff --git a/ShittyLinqTests/OfTypeTests.cs b/ShittyLinqTests/OfTypeTests.cs new file mode 100644 index 0000000..e89c58e --- /dev/null +++ b/ShittyLinqTests/OfTypeTests.cs @@ -0,0 +1,53 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using System; +using System.Collections.Generic; + +namespace ShittyTests +{ + [TestClass] + public class OfTypeTests + { + [TestMethod] + public void OfType_OfTypeFound() + { + object[] numbers = new object[] { 1, 2, 3, 4, 5 }; + int expectedResult = 5; + + IEnumerable result = numbers.OfType(); + + Assert.AreEqual(expectedResult, result.Count()); + } + + [TestMethod] + public void OfType_OfTypeNotFound() + { + object[] numbers = new object[] { 1, 2, 3, 4, 5 }; + int expectedResult = 0; + + IEnumerable result = numbers.OfType(); + + Assert.AreEqual(expectedResult, result.Count()); + } + + [TestMethod] + public void OfType_OfTypeSomeFound() + { + object[] numbers = new object[] { 1, 2, "3", 4, 5 }; + int expectedResult = 1; + + IEnumerable result = numbers.OfType(); + + Assert.AreEqual(expectedResult, result.Count()); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void OfType_SourceIsNull() + { + int[] numbers = null; + + IEnumerable result = numbers.OfType(); + } + } +} diff --git a/ShittyLinqTests/ToArrayTests.cs b/ShittyLinqTests/ToArrayTests.cs new file mode 100644 index 0000000..6084fd8 --- /dev/null +++ b/ShittyLinqTests/ToArrayTests.cs @@ -0,0 +1,35 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using ShittyTests.TestHelpers; +using System; +using System.Collections.Generic; + +namespace ShittyTests +{ + [TestClass] + public class ToArrayTests + { + [TestMethod] + public void ToArray_SequenceIsNull() + { + IEnumerable nums = null; + Assert.ThrowsException(() => nums.ToArray()); + } + + [TestMethod] + public void ToArray_SequenceEquals() + { + IEnumerable expected = new List { 0, 1, 2 }; + int[] actual = expected.ToArray(); + TestHelper.AssertCollectionsAreSame(expected, actual); + } + + [TestMethod] + public void ToArray_SequenceIsEmpty() + { + IEnumerable expected = new List(); + int[] actual = expected.ToArray(); + TestHelper.AssertCollectionsAreSame(expected, actual); + } + } +}