public void TestRegisterInstance() { var container = GetContainer(); var foo = new Foo(); container.RegisterInstance<IFoo>(foo); var foo2 = container.Resolve<IFoo>(); Assert.AreSame(foo, foo2); }
public void with() { var foo = new Foo { Id = 1, Name = "a" }; var foo1 = foo.With(new Foo { Id = 2 }, nameof(Foo.Id)); Assert.Equal(2, foo1.Id); Assert.Equal("a", foo1.Name); Assert.Equal(1, foo.Id); Assert.Equal("a", foo.Name); Assert.NotEqual(foo, foo1); var foo2 = foo.With(new Foo { Id = 3, Name = "b" }, nameof(Foo.Id), nameof(Foo.Name)); Assert.Equal(3, foo2.Id); Assert.Equal("b", foo2.Name); Assert.Equal(1, foo.Id); Assert.Equal("a", foo.Name); Assert.NotEqual(foo, foo2); }
public void perf() { var foo = new Foo(); var sw = Stopwatch.StartNew(); for (var i = 0; i < 100 * 1000; i++) { new Foo().Merge(foo); foo.Id++; } Console.WriteLine(sw.ElapsedMilliseconds); sw.Stop(); sw = Stopwatch.StartNew(); for (var i = 0; i < 100 * 1000; i++) { foo.MemberwiseClone(); foo.Id++; } Console.WriteLine(sw.ElapsedMilliseconds); sw.Stop(); sw = Stopwatch.StartNew(); for (var i = 0; i < 100 * 1000; i++) { foo.Clone(); foo.Id++; } Console.WriteLine(sw.ElapsedMilliseconds); var bar = new Bar(); var barz = new Bar(); sw.Stop(); sw = Stopwatch.StartNew(); for (var i = 0; i < 100 * 1000; i++) { barz = bar; barz.Id++; } Console.WriteLine(sw.ElapsedMilliseconds); }
public static void TestContainsValue_EqualObjects() { var hash1 = new Hashtable(); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { var foo1 = new Foo() { StringValue = "Goodbye" }; var foo2 = new Foo() { StringValue = "Goodbye" }; hash2.Add(101, foo1); Assert.True(hash2.ContainsValue(foo2)); }); }
public static void TestContainsKey_EqualObjects() { var hash1 = new Hashtable(); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { var foo1 = new Foo() { StringValue = "Goodbye" }; var foo2 = new Foo() { StringValue = "Goodbye" }; hash2.Add(foo1, 101); Assert.True(hash2.ContainsKey(foo2)); Assert.True(hash2.Contains(foo2)); int i1 = 0x10; int i2 = 0x100; long l1 = (((long)i1) << 32) + i2; // Create two longs with same hashcode long l2 = (((long)i2) << 32) + i1; hash2.Add(l1, 101); hash2.Add(l2, 101); // This will cause collision bit of the first entry to be set Assert.True(hash2.ContainsKey(l1)); Assert.True(hash2.Contains(l1)); hash2.Remove(l1); // Remove the first item Assert.False(hash2.ContainsKey(l1)); Assert.False(hash2.Contains(l1)); Assert.True(hash2.ContainsKey(l2)); Assert.True(hash2.Contains(l2)); }); }
public static void TestAdd_ReferenceType() { var hash1 = new Hashtable(); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { // Value is a reference var foo = new Foo(); hash2.Add("Key", foo); Assert.Equal("Hello World", ((Foo)hash2["Key"]).StringValue); // Changing original object should change the object stored in the Hashtable foo.StringValue = "Goodbye"; Assert.Equal("Goodbye", ((Foo)hash2["Key"]).StringValue); }); }
public static void Clone_IsShallowCopy() { var sortList = new SortedList(); for (int i = 0; i < 10; i++) { sortList.Add(i, new Foo()); } SortedList sortListClone = (SortedList)sortList.Clone(); string stringValue = "Hello World"; for (int i = 0; i < 10; i++) { Assert.Equal(stringValue, ((Foo)sortListClone[i]).StringValue); } // Now we remove an object from the original list, but this should still be present in the clone sortList.RemoveAt(9); Assert.Equal(stringValue, ((Foo)sortListClone[9]).StringValue); stringValue = "Good Bye"; ((Foo)sortList[0]).StringValue = stringValue; Assert.Equal(stringValue, ((Foo)sortList[0]).StringValue); Assert.Equal(stringValue, ((Foo)sortListClone[0]).StringValue); // If we change the object, of course, the previous should not happen sortListClone[0] = new Foo(); Assert.Equal(stringValue, ((Foo)sortList[0]).StringValue); stringValue = "Hello World"; Assert.Equal(stringValue, ((Foo)sortListClone[0]).StringValue); }
public void DemoAction(Foo foo, Bar bar, Baz baz) { }