Permalink
Browse files

more fixup for i4o2->i4o. Started porting some of the old tests back …

…into i4o 2
  • Loading branch information...
1 parent 13ebf4e commit f833ba9fc20def3be8d93026d856d4f4daf3252e @staxmanade staxmanade committed Feb 15, 2011
View
@@ -7,5 +7,5 @@ if %errorlevel% neq 0 exit /b %errorlevel%
.\packages\StatLight.1.3.3981\tools\StatLight.exe -x=.\i4o.Silverlight.Tests\Bin\Debug\i4o.Silverlight.Tests.xap
if %errorlevel% neq 0 exit /b %errorlevel%
-.\packages\NUnit.2.5.7.10213\Tools\nunit-console.exe .\i4o.Tests\bin\Debug\i4o2.Tests.dll
+.\packages\NUnit.2.5.7.10213\Tools\nunit-console.exe .\i4o.Tests\bin\Debug\i4o.Tests.dll
if %errorlevel% neq 0 exit /b %errorlevel%
View
@@ -7,5 +7,5 @@ if %errorlevel% neq 0 exit /b %errorlevel%
.\packages\StatLight.1.3.3981\tools\StatLight.exe -x=.\i4o.Silverlight.Tests\Bin\Release\i4o.Silverlight.Tests.xap
if %errorlevel% neq 0 exit /b %errorlevel%
-.\packages\NUnit.2.5.7.10213\Tools\nunit-console.exe .\i4o.Tests\bin\Release\i4o2.Tests.dll
+.\packages\NUnit.2.5.7.10213\Tools\nunit-console.exe .\i4o.Tests\bin\Release\i4o.Tests.dll
if %errorlevel% neq 0 exit /b %errorlevel%
@@ -0,0 +1,187 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using NUnit.Framework;
+
+namespace i4o.Tests
+{
+ public class TestClass
+ {
+ public string TestProperty1 { get; set; }
+ public string TestProperty2 { get; set; }
+
+ public static IndexSpecification<TestClass> GetIndexSpec()
+ {
+ return new IndexSpecification<TestClass>()
+ .Add(p => p.TestProperty1)
+ .Add(p => p.TestProperty2);
+ }
+ }
+
+ internal class TestIndexableCollection<T> : IndexSet<T>
+ {
+ public TestIndexableCollection(IEnumerable<T> items, IndexSpecification<T> indexSpecification)
+ : base(items, indexSpecification)
+ { }
+
+ //public Dictionary<string, Dictionary<int, List<T>>> CurrentIndexes { get { return Indexes; } }
+
+ public int CountOfValuesIndexedFor<TProperty>(Expression<Func<T, TProperty>> propertyExpression)
+ {
+ var propertyName = propertyExpression.GetMemberName();
+ return base.IndexDictionary[propertyName].Count();
+ }
+
+ public IIndex<T> GetIndexFor<TParameter>(Expression<Func<T, TParameter>> propertyExpression)
+ {
+ var propertyName = propertyExpression.GetMemberName();
+ return base.IndexDictionary[propertyName];
+ }
+ }
+
+ [TestFixture]
+ [Ignore("Old version used to support ICollection & IList - not anymore?")]
+ public class CollectionModificationTests
+ {
+ [Test]
+ public void when_adding_an_item_using_the_base_type_does_the_index_get_updated()
+ {
+ var testClasses = new List<TestClass>
+ {
+ new TestClass {TestProperty1 = "Hello1"},
+ new TestClass {TestProperty1 = "Hello2"},
+ new TestClass {TestProperty1 = "Hello3"},
+ };
+
+ var indexedTestClasses = new TestIndexableCollection<TestClass>(testClasses, TestClass.GetIndexSpec());
+
+ var baseCollection = indexedTestClasses as ICollection<TestClass>;
+ baseCollection.Add(new TestClass { TestProperty1 = "Hello4" });
+
+ Assert.AreEqual(4, indexedTestClasses.CountOfValuesIndexedFor(p => p.TestProperty1));
+ }
+
+ [Test]
+ public void when_inserting_an_item_at_a_particular_index_the_new_item_should_get_indexed()
+ {
+ var testClasses = new List<TestClass>
+ {
+ new TestClass {TestProperty1 = "Hello1"},
+ new TestClass {TestProperty1 = "Hello2"},
+ new TestClass {TestProperty1 = "Hello3"},
+ };
+
+ var indexedTestClasses = new TestIndexableCollection<TestClass>(testClasses, TestClass.GetIndexSpec());
+
+ var baseCollection = indexedTestClasses as IList<TestClass>;
+ baseCollection.Insert(0, new TestClass { TestProperty1 = "Hello4" });
+
+ Assert.AreEqual(4, indexedTestClasses.CountOfValuesIndexedFor(p => p.TestProperty1));
+ }
+
+ [Test]
+ public void when_removing_an_item_at_a_particular_index_the_item_should_be_removed_from_the_index()
+ {
+ var testClasses = new List<TestClass>
+ {
+ new TestClass {TestProperty1 = "Hello1"},
+ new TestClass {TestProperty1 = "Hello2"},
+ new TestClass {TestProperty1 = "Hello3"},
+ };
+
+ var indexedTestClasses = new TestIndexableCollection<TestClass>(testClasses, TestClass.GetIndexSpec());
+
+ throw new NotImplementedException();
+ //indexedTestClasses.RemoveAt(0);
+
+ Assert.AreEqual(2, indexedTestClasses.CountOfValuesIndexedFor(p => p.TestProperty1));
+ }
+
+ [Test]
+ public void when_changing_the_value_at_a_particular_index_the_old_item_should_be_removed_from_the_index_and_the_new_value_should_be_added_to_the_index()
+ {
+ var t = new TestClass { TestProperty1 = "Hello1" };
+ var testClasses = new List<TestClass>
+ {
+ t,
+ new TestClass {TestProperty1 = "Hello2"},
+ new TestClass {TestProperty1 = "Hello3"},
+ };
+
+ var indexedTestClasses = new TestIndexableCollection<TestClass>(testClasses, TestClass.GetIndexSpec());
+
+ var testProperty2Index = indexedTestClasses.GetIndexFor(s => s.TestProperty2);
+
+ throw new NotImplementedException();
+ //indexedTestClasses[0] = new TestClass { TestProperty1 = "Hello4", TestProperty2 = "a" };
+
+ //testProperty2Index.ItemsWithKey(Consts.NullKeyHashCode)
+ // .Count().ShouldEqual(2);
+ }
+
+
+ [Test]
+ public void when_clearing_the_collection_it_should_also_clear_the_internal_indicies()
+ {
+ var testClasses = new List<TestClass>
+ {
+ new TestClass {TestProperty1 = "Hello1"},
+ new TestClass {TestProperty1 = "Hello2"},
+ new TestClass {TestProperty1 = "Hello3"},
+ };
+
+ var indexedTestClasses = new TestIndexableCollection<TestClass>(testClasses, TestClass.GetIndexSpec());
+
+ throw new NotImplementedException();
+ //indexedTestClasses.Clear();
+
+ //Assert.AreEqual(0, indexedTestClasses.Count);
+ //Assert.AreEqual(0, indexedTestClasses.CountOfValuesIndexedFor(p => p.TestProperty1));
+ //Assert.AreEqual(0, indexedTestClasses.CountOfValuesIndexedFor(p => p.TestProperty2));
+ }
+
+ [Test]
+ public void the_IList_Contains_method_should_work_correctly()
+ {
+ var testClasses = new List<TestClass>
+ {
+ new TestClass {TestProperty1 = "Hello1"},
+ new TestClass {TestProperty1 = "Hello2"},
+ new TestClass {TestProperty1 = "Hello3"},
+ };
+
+ var item = testClasses.Last();
+
+ var indexedTestClasses = new TestIndexableCollection<TestClass>(testClasses, TestClass.GetIndexSpec());
+ Assert.IsTrue(indexedTestClasses.Contains(item));
+ }
+
+ [Test]
+ public void the_IList_IndexOf_method_should_work_correctly()
+ {
+ var testClasses = new List<TestClass>
+ {
+ new TestClass {TestProperty1 = "Hello1"},
+ new TestClass {TestProperty1 = "Hello2"},
+ new TestClass {TestProperty1 = "Hello3"},
+ };
+
+ var item = testClasses.Last();
+ throw new NotImplementedException();
+ //var indexedTestClasses = new TestIndexableCollection<TestClass>(testClasses, TestClass.GetIndexSpec());
+ //Assert.AreEqual(2, indexedTestClasses.IndexOf(item));
+ }
+
+ [Test]
+ public void should_be_able_to_get_an_enumerator_of_the_indexable_collection()
+ {
+ var indexedTestClasses = new IndexSet<TestClass>(TestClass.GetIndexSpec());
+
+ Assert.IsNotNull((indexedTestClasses as IEnumerable<TestClass>).GetEnumerator());
+ Assert.IsNotNull((indexedTestClasses as IEnumerable).GetEnumerator());
+ }
+
+ }
+}
@@ -0,0 +1,180 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using NUnit.Framework;
+
+namespace i4o.Tests
+{
+ [TestFixture]
+ public class PropertyReaderTests
+ {
+
+ [Test]
+ public void Should_throw_ArgumentNullException_if_given_null_propertyName()
+ {
+ typeof(ArgumentNullException)
+ .ShouldBeThrownBy(() => { new PropertyReader<Type>(null); });
+ }
+
+ [Test]
+ public void Should_throw_ArgumentException_if_given_empty_propertyName()
+ {
+ typeof(ArgumentException)
+ .ShouldBeThrownBy(() => { new PropertyReader<Type>(string.Empty); });
+ }
+
+ [Test]
+ public void Should_throw_exception_if_given_propertyName_not_existing_on_object()
+ {
+ typeof(ArgumentException)
+ .ShouldBeThrownBy(() => { new PropertyReader<Type>("someRandomPropertyNameHEREEEEEE"); });
+ }
+
+ [Test]
+ public void Should_be_able_to_read_the_value_of_a_type_using_the_DelegateMethod_strategy()
+ {
+ var t = new TestClass { TestValue = "Hi" };
+ var p = new PropertyReader<TestClass>(PropertyReadStrategy.DelegateMethod, "TestValue");
+ p.ReadValue(t).ShouldEqual("Hi");
+ }
+
+ [Test]
+ public void Should_be_able_to_read_the_value_of_a_type_using_the_Reflection_strategy()
+ {
+ var t = new TestClass { TestValue = "Hi" };
+ var p = new PropertyReader<TestClass>(PropertyReadStrategy.Reflection, "TestValue");
+ p.ReadValue(t).ShouldEqual("Hi");
+ }
+
+ [Test]
+ public void Should_return_zero_when_trying_to_get_a_null_objects_HashCode()
+ {
+ var @class = new TestClass();
+ var reader = new PropertyReader<TestClass>("TestValue");
+ reader.GetItemHashCode(@class).ShouldEqual(0);
+ }
+
+ [Test]
+ public void Should_return_actual_objects_HashCode()
+ {
+ const string testString = "Hello World!";
+ var @class = new TestClass { TestValue = testString };
+ var reader = new PropertyReader<TestClass>("TestValue");
+ reader.GetItemHashCode(@class).ShouldEqual(testString.GetHashCode());
+ }
+
+ public class TestClass
+ {
+ public string TestValue { get; set; }
+ }
+
+#if !SILVERLIGHT
+ [Test]
+ [TestCase(10, PropertyReadStrategy.Reflection)]
+ [TestCase(250, PropertyReadStrategy.Reflection)]
+ [TestCase(450, PropertyReadStrategy.DelegateMethod)]
+ [TestCase(1000, PropertyReadStrategy.DelegateMethod)]
+ [Explicit]
+ public void Property_read_strategy_perf_test(int max, PropertyReadStrategy expectedToBeFaster)
+ {
+ var random = new Random();
+ var stats = new List<IndexReadTimingResult>();
+
+ for (int i = 0; i < 1000; i++)
+ {
+ var numberOfItems = random.Next() % max;
+ if (numberOfItems == 0)
+ continue;
+ var stat = GetIndexReadTimingResult(numberOfItems);
+ stats.Add(stat);
+ }
+
+ IndexReadTimingResult.
+ WhoWasGenerallyFaster(stats).ShouldEqual(expectedToBeFaster);
+ }
+#endif
+
+ public class IndexReadTimingResult
+ {
+ public long DelegateMethodTime { get; set; }
+ public long ReflectionTime { get; set; }
+ public int Iterations { get; set; }
+
+ public PropertyReadStrategy WhoWon()
+ {
+ if (DelegateMethodTime > ReflectionTime)
+ return PropertyReadStrategy.Reflection;
+ return PropertyReadStrategy.DelegateMethod;
+ }
+
+ public static PropertyReadStrategy WhoWasGenerallyFaster(IEnumerable<IndexReadTimingResult> testSamples)
+ {
+
+ var statSummary = new Dictionary<PropertyReadStrategy, int>
+ {
+ {PropertyReadStrategy.Reflection, 0},
+ {PropertyReadStrategy.DelegateMethod, 0}
+ };
+
+ foreach (var stat in testSamples)
+ {
+ statSummary[stat.WhoWon()]++;
+ }
+
+ var totalItmes = statSummary.Select(s => s.Value).Sum();
+
+ // If "generally" faster is too close co call. Fail...
+ var percentageDiff = Math.Abs(.5d - (statSummary[PropertyReadStrategy.Reflection] / (double)totalItmes));
+ if (percentageDiff < .05)
+ throw new Exception(
+ string.Format(
+ "Item stats were too close to call Reflection={0}, DelegateMethod={1}, percentage={2}",
+ statSummary[PropertyReadStrategy.Reflection],
+ statSummary[PropertyReadStrategy.DelegateMethod],
+ percentageDiff));
+
+ PropertyReadStrategy winner;
+ if (statSummary[PropertyReadStrategy.Reflection] > statSummary[PropertyReadStrategy.DelegateMethod])
+ winner = PropertyReadStrategy.Reflection;
+ else
+ winner = PropertyReadStrategy.DelegateMethod;
+
+ return winner;
+ }
+ }
+
+ private static IndexReadTimingResult GetIndexReadTimingResult(int numberOfItems)
+ {
+ var dummyCollectionOfData = (from x in Enumerable.Range(1, numberOfItems)
+ select new TestClass { TestValue = Guid.NewGuid().ToString() })
+ .ToList();
+
+ Func<PropertyReadStrategy, int, long> timeFor = (readerStrategy, iterations) =>
+ {
+ var stopwatch = Stopwatch.StartNew();
+ var reader = new PropertyReader<TestClass>(readerStrategy, "TestValue");
+ for (int i = 0; i < iterations; i++)
+ {
+ reader.ReadValue(dummyCollectionOfData[i]);
+ }
+ stopwatch.Stop();
+ return stopwatch.ElapsedTicks;
+ };
+
+ // Run each once to initialize any system level (possibly jitting???) or eronous code...
+ timeFor(PropertyReadStrategy.Reflection, 1);
+ timeFor(PropertyReadStrategy.DelegateMethod, 1);
+
+ var reflectionTime = timeFor(PropertyReadStrategy.Reflection, numberOfItems);
+ var delegateMethodTime = timeFor(PropertyReadStrategy.DelegateMethod, numberOfItems);
+
+ return new IndexReadTimingResult
+ {
+ DelegateMethodTime = delegateMethodTime,
+ ReflectionTime = reflectionTime,
+ Iterations = numberOfItems,
+ };
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit f833ba9

Please sign in to comment.