public void TestElementClass() { var iComp = new IndexSet<TestClassB>( new TestClassB[] { new TestClassB() { T = 10 }, new TestClassB() { T = 20 }, new TestClassB() { T = 30 }, new TestClassB() { T = 40 } }, new IndexSpecification<TestClassB>().Add(q => q.T)); ; Assert.AreEqual(4, iComp.Where(c => c.T2 == 0).Count()); bool failed = false; try { iComp.WhereIndexed(c => c.T2 == 0).Count(); } catch { failed = true; } Assert.IsTrue(failed); }
public void PerformanceComparisonTest() { Stopwatch globalSw = new Stopwatch(); globalSw.Start(); List <SimpleClass> someItems = new List <SimpleClass> { new SimpleClass { Name = "Jason Jarett", Age = 25, FavoriteColor = Color.Aqua }, new SimpleClass { Name = "Aaron Erickson", Age = 37, FavoriteColor = Color.Green }, new SimpleClass { Name = "Erin Erickson", Age = 34, FavoriteColor = Color.Green }, new SimpleClass { Name = "Adriana Erickson", Age = 13, FavoriteColor = Color.Aqua }, }; var rnd = new Random(); var moreItems = Enumerable.Range(1, 100000).Select(r => new SimpleClass() { Name = "AdrBla" + rnd.NextDouble(), Age = rnd.Next(0, 100), FavoriteColor = new Color(rnd.Next(0, 0xFFFFFF)) }); someItems.AddRange(moreItems); someItems = someItems.OrderBy(r => rnd.Next()).ToList(); // Shuffle items Assert.AreEqual(100004, someItems.Count()); Console.WriteLine("Arrange test list : " + globalSw.ElapsedMilliseconds); globalSw.Reset(); var indexSpec = IndexSpecification <SimpleClass> .Build() .With(person => person.Name) .With(person => person.Age); var theIndexSet = new IndexSet <SimpleClass>(someItems, indexSpec); theIndexSet.UnableToUseIndex += (e, args) => { throw new Exception(args.Message); }; Console.WriteLine("Define index : " + globalSw.ElapsedMilliseconds); globalSw.Reset(); Stopwatch sw = new Stopwatch(); // Search a 1000 times in index globalSw.Start(); for (int i = 0; i < 1000; i++) { sw.Start(); var inIndexSearch = theIndexSet.Where(item => item.Age == 13 && item.Name == "Adriana Erickson"); Assert.AreEqual(1, inIndexSearch.Count()); sw.Stop(); if (i == 0) { Console.WriteLine("In index (first iteration) : " + sw.ElapsedMilliseconds); } if (i == 1) { Console.WriteLine("In index (second iteration) : " + sw.ElapsedMilliseconds); } //Console.WriteLine("In index : " + sw.ElapsedMilliseconds); sw.Reset(); } Console.WriteLine("In index global time : " + globalSw.ElapsedMilliseconds); globalSw.Reset(); sw.Reset(); // Search a 1000 times in list globalSw.Start(); for (int i = 0; i < 1000; i++) { sw.Start(); var inListSearch = someItems.Where(item => item.Age == 13 && item.Name == "Adriana Erickson"); Assert.AreEqual(1, inListSearch.Count()); sw.Stop(); //Console.WriteLine("In list : " + sw.ElapsedMilliseconds); sw.Reset(); } Console.WriteLine("In list global time : " + globalSw.ElapsedMilliseconds); }