Revision: 46293
Updated Code
at May 17, 2011 01:07 by joelsand
Updated Code
/*** Source code ***/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace Snippets { public static class IEnumerableExtensions { /// <summary> /// Finds the index in the collection where the predicate evaluates to true. /// /// Returns -1 if no matching item found /// </summary> /// <typeparam name="TSource">Type of collection</typeparam> /// <param name="source">Source collection</param> /// <param name="predicate">Function to evaluate</param> /// <returns>Index where predicate is true, or -1 if not found.</returns> public static int IndexWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { var enumerator = source.GetEnumerator(); int index = 0; while (enumerator.MoveNext()) { TSource obj = enumerator.Current; if (predicate(obj)) return index; index++; } return -1; } /// <summary> /// Finds the index in the collection where the predicate evaluates to true. /// /// Returns -1 if no matching item found /// </summary> /// <typeparam name="TSource">Type of collection</typeparam> /// <param name="source">Source collection</param> /// <param name="predicate">Function to evaluate</param> /// <returns>Index where predicate is true, or -1 if not found.</returns> public static int IndexWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate) { var enumerator = source.GetEnumerator(); int index = 0; while (enumerator.MoveNext()) { TSource obj = enumerator.Current; if (predicate(obj, index)) return index; index++; } return -1; } } } /*** Unit Tests ***/ using ProtectedTrust.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; namespace SnippetsTests { /// <summary> ///This is a test class for IEnumerableExtensionsTest and is intended ///to contain all IEnumerableExtensionsTest Unit Tests ///</summary> [TestClass()] public class IEnumerableExtensionsTest { private TestContext testContextInstance; /// <summary> ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///</summary> public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } #region Additional test attributes // //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion /// <summary> ///A test for IndexWhere ///</summary> public void IndexWhereTestHelper<TSource>() { IEnumerable<TSource> source = null; // TODO: Initialize to an appropriate value Func<TSource, int, bool> predicate = null; // TODO: Initialize to an appropriate value int expected = 0; // TODO: Initialize to an appropriate value int actual; actual = IEnumerableExtensions.IndexWhere<TSource>(source, predicate); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); } [TestMethod()] public void IndexWhereTest() { // create list of numbers List<int> list = new List<int>(); for (int i = 0; i < 100; i++) { list.Add(i); } // test method with various inputs int actual = list.IndexWhere((n) => n == 5); Assert.AreEqual(5, actual); actual = list.IndexWhere((n) => n == 99); Assert.AreEqual(99, actual); actual = list.IndexWhere((n) => n == 0); Assert.AreEqual(0, actual); actual = list.IndexWhere((n) => n == 1); Assert.AreEqual(1, actual); // check one not found actual = list.IndexWhere((n) => n == 23423); Assert.AreEqual(-1, actual); } [TestMethod()] public void IndexWhereTest1() { // create list of numbers List<int> list = new List<int>(); for (int i = 0; i < 100; i++) { list.Add(i); } // test method with various inputs int actual = list.IndexWhere((n, actualRunningIndex) => n == 5); Assert.AreEqual(5, actual); actual = list.IndexWhere((n, actualRunningIndex) => n == 99); Assert.AreEqual(99, actual); actual = list.IndexWhere((n, actualRunningIndex) => n == 0); Assert.AreEqual(0, actual); actual = list.IndexWhere((n, actualRunningIndex) => n == 1); Assert.AreEqual(1, actual); // check one not found actual = list.IndexWhere((n, actualRunningIndex) => n == 23423); Assert.AreEqual(-1, actual); } [TestMethod()] public void IndexWhereTestRunningIndex() { // create list of numbers List<int> list = new List<int>(); for (int i = 0; i < 100; i++) { list.Add(i); } int expectedRunningIndex = 0; list.IndexWhere((n, actualRunningIndex) => AssertRunningIndex(expectedRunningIndex++, actualRunningIndex)); } private static bool AssertRunningIndex(int expectedRunningIndex, int actualRunningIndex) { Assert.AreEqual(expectedRunningIndex, actualRunningIndex, "Index passed in predicate method is incorrect."); // always return false so the loop doesn't stop return false; } } }
Revision: 46292
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 17, 2011 01:06 by joelsand
Initial Code
/*** Source code ***/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace SnippetsTests { public static class IEnumerableExtensions { /// <summary> /// Finds the index in the collection where the predicate evaluates to true. /// /// Returns -1 if no matching item found /// </summary> /// <typeparam name="TSource">Type of collection</typeparam> /// <param name="source">Source collection</param> /// <param name="predicate">Function to evaluate</param> /// <returns>Index where predicate is true, or -1 if not found.</returns> public static int IndexWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { var enumerator = source.GetEnumerator(); int index = 0; while (enumerator.MoveNext()) { TSource obj = enumerator.Current; if (predicate(obj)) return index; index++; } return -1; } /// <summary> /// Finds the index in the collection where the predicate evaluates to true. /// /// Returns -1 if no matching item found /// </summary> /// <typeparam name="TSource">Type of collection</typeparam> /// <param name="source">Source collection</param> /// <param name="predicate">Function to evaluate</param> /// <returns>Index where predicate is true, or -1 if not found.</returns> public static int IndexWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate) { var enumerator = source.GetEnumerator(); int index = 0; while (enumerator.MoveNext()) { TSource obj = enumerator.Current; if (predicate(obj, index)) return index; index++; } return -1; } } } /*** Unit Tests ***/ using ProtectedTrust.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; namespace ProtectedTrust.Tests.Unit { /// <summary> ///This is a test class for IEnumerableExtensionsTest and is intended ///to contain all IEnumerableExtensionsTest Unit Tests ///</summary> [TestClass()] public class IEnumerableExtensionsTest { private TestContext testContextInstance; /// <summary> ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///</summary> public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } #region Additional test attributes // //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion /// <summary> ///A test for IndexWhere ///</summary> public void IndexWhereTestHelper<TSource>() { IEnumerable<TSource> source = null; // TODO: Initialize to an appropriate value Func<TSource, int, bool> predicate = null; // TODO: Initialize to an appropriate value int expected = 0; // TODO: Initialize to an appropriate value int actual; actual = IEnumerableExtensions.IndexWhere<TSource>(source, predicate); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); } [TestMethod()] public void IndexWhereTest() { // create list of numbers List<int> list = new List<int>(); for (int i = 0; i < 100; i++) { list.Add(i); } // test method with various inputs int actual = list.IndexWhere((n) => n == 5); Assert.AreEqual(5, actual); actual = list.IndexWhere((n) => n == 99); Assert.AreEqual(99, actual); actual = list.IndexWhere((n) => n == 0); Assert.AreEqual(0, actual); actual = list.IndexWhere((n) => n == 1); Assert.AreEqual(1, actual); // check one not found actual = list.IndexWhere((n) => n == 23423); Assert.AreEqual(-1, actual); } [TestMethod()] public void IndexWhereTest1() { // create list of numbers List<int> list = new List<int>(); for (int i = 0; i < 100; i++) { list.Add(i); } // test method with various inputs int actual = list.IndexWhere((n, actualRunningIndex) => n == 5); Assert.AreEqual(5, actual); actual = list.IndexWhere((n, actualRunningIndex) => n == 99); Assert.AreEqual(99, actual); actual = list.IndexWhere((n, actualRunningIndex) => n == 0); Assert.AreEqual(0, actual); actual = list.IndexWhere((n, actualRunningIndex) => n == 1); Assert.AreEqual(1, actual); // check one not found actual = list.IndexWhere((n, actualRunningIndex) => n == 23423); Assert.AreEqual(-1, actual); } [TestMethod()] public void IndexWhereTestRunningIndex() { // create list of numbers List<int> list = new List<int>(); for (int i = 0; i < 100; i++) { list.Add(i); } int expectedRunningIndex = 0; list.IndexWhere((n, actualRunningIndex) => AssertRunningIndex(expectedRunningIndex++, actualRunningIndex)); } private static bool AssertRunningIndex(int expectedRunningIndex, int actualRunningIndex) { Assert.AreEqual(expectedRunningIndex, actualRunningIndex, "Index passed in predicate method is incorrect."); // always return false so the loop doesn't stop return false; } } }
Initial URL
Initial Description
Find the index of an item in a collection with a simple statement. For example: int index = myList.IndexWhere(item => item.Something == someOtherThing);
Initial Title
LINQ Index of item - IndexWhere()
Initial Tags
Initial Language
C#