Ejemplo n.º 1
0
        public void ID_Intern_derived_class()
        {
            Interner <int, A> interner = new Interner <int, A>();
            var obj1 = new A();
            var obj2 = new B();
            var obj3 = new B();

            var r1 = interner.InternAndUpdateWithMoreDerived(1, obj1);

            Assert.AreEqual(obj1, r1, "Objects should be equal");
            Assert.AreSame(obj1, r1, "Objects should be same / intern'ed");

            var r2 = interner.InternAndUpdateWithMoreDerived(2, obj2);

            Assert.AreEqual(obj2, r2, "Objects should be equal");
            Assert.AreSame(obj2, r2, "Objects should be same / intern'ed");

            // Interning should not replace instances of same class
            var r3 = interner.InternAndUpdateWithMoreDerived(2, obj3);

            Assert.AreSame(obj2, r3, "Interning should return previous object");
            Assert.AreNotSame(obj3, r3, "Interning should not replace previous object of same class");

            // Interning should return instances of most derived class
            var r4 = interner.InternAndUpdateWithMoreDerived(1, obj2);

            Assert.AreSame(obj2, r4, "Interning should return most derived object");
            Assert.AreNotSame(obj1, r4, "Interning should replace cached instances of less derived object");

            // Interning should not return instances of less derived class
            var r5 = interner.InternAndUpdateWithMoreDerived(2, obj1);

            Assert.AreNotSame(obj1, r5, "Interning should not return less derived object");
            Assert.AreSame(obj2, r5, "Interning should return previously cached instances of more derived object");
        }
Ejemplo n.º 2
0
        public void ID_Intern_derived_class()
        {
            Interner<int, A> interner = new Interner<int, A>();
            var obj1 = new A();
            var obj2 = new B();
            var obj3 = new B();

            var r1 = interner.InternAndUpdateWithMoreDerived(1, obj1);
            Assert.AreEqual(obj1, r1, "Objects should be equal");
            Assert.AreSame(obj1, r1, "Objects should be same / intern'ed");

            var r2 = interner.InternAndUpdateWithMoreDerived(2, obj2);
            Assert.AreEqual(obj2, r2, "Objects should be equal");
            Assert.AreSame(obj2, r2, "Objects should be same / intern'ed");

            // Interning should not replace instances of same class
            var r3 = interner.InternAndUpdateWithMoreDerived(2, obj3);
            Assert.AreSame(obj2, r3, "Interning should return previous object");
            Assert.AreNotSame(obj3, r3, "Interning should not replace previous object of same class");

            // Interning should return instances of most derived class
            var r4 = interner.InternAndUpdateWithMoreDerived(1, obj2);
            Assert.AreSame(obj2, r4, "Interning should return most derived object");
            Assert.AreNotSame(obj1, r4, "Interning should replace cached instances of less derived object");

            // Interning should not return instances of less derived class
            var r5 = interner.InternAndUpdateWithMoreDerived(2, obj1);
            Assert.AreNotSame(obj1, r5, "Interning should not return less derived object");
            Assert.AreSame(obj2, r5, "Interning should return previously cached instances of more derived object");
        }