public void TestTheBasics() { ISimple class1 = GoInterface <ISimple, SimpleClass> .From(new SimpleClass()); ISimple iface1 = GoInterface <ISimple, IDifferentName> .From(new SimpleClass()); ISimple struct1 = GoInterface <ISimple, SimpleStruct> .From(new SimpleStruct(10)); Assert.AreEqual(25, class1.Mutate(5)); Assert.AreEqual(25, iface1.Mutate(5)); Assert.AreEqual(70, struct1.Mutate(7)); SimpleBase class2 = GoInterface <SimpleBase, SimpleClass> .From(new SimpleClass()); SimpleBase iface2 = GoInterface <SimpleBase, IDifferentName> .From(new SimpleClass()); SimpleBase struct2 = GoInterface <SimpleBase, SimpleStruct> .From(new SimpleStruct(10)); for (int i = 0; i < 10; i++) { Assert.AreEqual(class1.Mutate(i), class2.Mutate(i)); Assert.AreEqual(iface1.Mutate(i), iface2.Mutate(i)); Assert.AreEqual(struct1.Mutate(i), struct2.Mutate(i)); } Assert.AreEqual("forwarded!", struct1.ToString()); Assert.AreEqual("forwarded!", struct2.ToString()); Assert.AreEqual(struct1.GetHashCode(), struct2.GetHashCode()); // Note that struct1.Equals(struct2) returns FALSE because struct2 is // not "really" a SimpleStruct. We must "unwrap" the right-hand side: Assert.That(struct1.Equals(GoInterface.Unwrap(struct2))); Assert.That(struct2.Equals(GoInterface.Unwrap(struct1))); }
public void ISimpleSourceTest() { ISimpleSource <string> list = GoInterface <ISimpleSource <string> > .From(new List <string>()); Assert.AreEqual(0, list.Count); Assert.AreEqual(false, list.GetEnumerator().MoveNext()); }
public void AliasesAndProperties() { ISimpleList list = GoInterface <ISimpleList> .From(new MyCollection(), CastOptions.AllowUnmatchedMethods); list.Add(10); Assert.That(list[0].Equals(10)); Assert.AreEqual(1, list.Count); }
public void InheritanceTest() { object something = new FooB(); FooBase foo = GoInterface <FooBase> .From(something); Assert.That(foo.Foo() == "Foo"); Assert.That(foo.Bar() == "Bar"); Assert.That(foo.Baz(1) == "Baz"); Assert.That(((IBaz)foo).Baz(false) == "Baz"); Assert.That(foo.Baz() == "Baz"); something = new FooB(); IBaz baz = GoInterface <IBaz> .ForceFrom(something); Assert.That(((IBar)baz).Bar() == "Bar"); Assert.That(((IBar2)baz).Bar() == "Bar"); Assert.That(baz.Baz() == "Baz"); }
public void TestAmbiguity() { IAmbig wrapped; try { wrapped = GoInterface <IAmbig> .From(new Ambig()); } catch (InvalidCastException e) { if (e.Message.Contains("4 ")) { return; // 4 ambiguous methods, just as expected } } wrapped = GoInterface <IAmbig> .ForceFrom(new Ambig()); int a = 0; Assert.ThrowsAny <MissingMethodException>(delegate() { wrapped.Strings("1", "2"); }); Assert.ThrowsAny <MissingMethodException>(delegate() { wrapped.RefMismatch(1, 2); }); Assert.ThrowsAny <MissingMethodException>(delegate() { wrapped.RefMismatch2(ref a, 2); }); Assert.ThrowsAny <MissingMethodException>(delegate() { wrapped.AmbigLarger(1); }); }
public static void DoBenchmark() { const int Iterations = 100000000; // Measure the time it takes to instantiate ten versions of // IReadOnlyList<T>. GoInterface is not able to create generic wrappers, // so every time you wrap the same generic type with a different type // parameter, GoInterface produces a completely separate wrapper. This // is not good for performance, but at least it makes it easy for our // benchmark to pick 10 "different" classes to create wrappers of. // // It is possible to wrap List<byte> not only as IReadOnlyList<byte> // but also as any larger integer type, such as IReadOnlyList<int>. // However, the wrapping of the GetEnumerator() methods doesn't work. // List<byte>.GetEnumerator() returns IEnumerator<byte>, but // IReadOnlyList<int>.GetEnumerator() returns IEnumerator<int>. There is // no implicit conversion from IEnumerator<byte> to IEnumerator<int>, // so GoInterface fails to wrap it. However, by using ForceFrom we get // around this limitation, which still allows us to use the indexer and // Count properties. If you call GetEnumerator(), though, you get a // MissingMethodException. // // Note that if you run this part of the benchmark twice without // exiting the program, the second time around it should take zero // milliseconds. And the benchmark generally runs more slowly right // after you reboot your computer. SimpleTimer timer = new SimpleTimer(); var dummy0 = GoInterface <IReadOnlyList <byte> > .ForceFrom(new List <byte>()); int firstOne = timer.Restart(); var dummy1 = GoInterface <IReadOnlyList <short> > .ForceFrom(new List <byte>()); var dummy2 = GoInterface <IReadOnlyList <ushort> > .ForceFrom(new List <byte>()); var dummy3 = GoInterface <IReadOnlyList <int> > .ForceFrom(new List <byte>()); var dummy4 = GoInterface <IReadOnlyList <uint> > .ForceFrom(new List <byte>()); var dummy5 = GoInterface <IReadOnlyList <long> > .ForceFrom(new List <byte>()); var dummy6 = GoInterface <IReadOnlyList <ulong> > .ForceFrom(new List <byte>()); var dummy7 = GoInterface <IReadOnlyList <float> > .ForceFrom(new List <byte>()); var dummy8 = GoInterface <IReadOnlyList <double> > .ForceFrom(new List <byte>()); var dummy9 = GoInterface <IReadOnlyList <object> > .ForceFrom(new List <byte>()); int nineMore = timer.Millisec; Console.WriteLine("First interface wrapped in {0}ms; nine more took {1}ms", firstOne, nineMore); // Second test: measure how long it takes to wrap the same List<int> // many times, using either GoInterface class. var list = new List <int>(); list.Add(0); list.Add(1); list.Add(2); list.Add(3); IList <int> ilist; IReadOnlyList <int> rolist; GoInterface <IReadOnlyList <int> > .From(list); // ignore first call timer.Restart(); int i = 0; do { ilist = list; // normal interface assignment is pretty much a no-op } while (++i < Iterations); int wrapTest0 = timer.Restart(); i = 0; do { rolist = GoInterface <IReadOnlyList <int> > .From(list); } while (++i < Iterations); int wrapTest1 = timer.Restart(); i = 0; do { rolist = GoInterface <IReadOnlyList <int>, List <int> > .From(list); } while (++i < Iterations); int wrapTest2 = timer.Restart(); Console.WriteLine("Wrapper creation speed ({0} million times):", Iterations / 1000000); Console.WriteLine("- {0} ms for normal .NET interfaces (no-op)", wrapTest0); Console.WriteLine("- {0} ms for GoInterface<IReadOnlyList<int>>.From()", wrapTest1); Console.WriteLine("- {0} ms for GoInterface<IReadOnlyList<int>,List<int>>.From()", wrapTest2); int total0 = 0, total1 = 0, total2 = 0; timer.Restart(); for (i = 0; i < Iterations; i++) { total0 += list[i & 3]; } int callTestDirectCall = timer.Restart(); for (i = 0; i < Iterations; i++) { total1 += ilist[i & 3]; } int callTestNormalInterface = timer.Restart(); for (i = 0; i < Iterations; i++) { total2 += rolist[i & 3]; } int callTestGoInterface = timer.Restart(); Debug.Assert(total0 == total1 && total1 == total2); Console.WriteLine("Time to call indexer of List<int> ({0} million times):", Iterations / 1000000); Console.WriteLine("- {0} ms for direct calls (not through an interface)", callTestDirectCall); Console.WriteLine("- {0} ms through IList<int> (normal interface)", callTestNormalInterface); Console.WriteLine("- {0} ms through IReadOnlyList<int> (GoInterface)", callTestGoInterface); }
public static ReverseView <T> From(IList <T> list) { return(GoInterface <ReverseView <T>, IList <T> > .From(list)); }