예제 #1
0
        public void Resource_IncRef()
        {
            Resource resurrection;

            using (Resource resource = new TestResource()) {
                // Keep reference outside the using statement
                resurrection = resource;

                Assert.IsFalse(resource.IsDisposed);

                // IncRef increment the reference count
                resource.IncRef();
                Assert.AreEqual(1u, resource.RefCount);
            }
            Assert.IsTrue(resurrection.IsDisposed);

            using (Resource resource = new TestResource()) {
                // Keep reference outside the using statement
                resurrection = resource;

                Assert.IsFalse(resource.IsDisposed);

                // IncRef increment the reference count
                resource.IncRef();
                resource.IncRef();
                Assert.AreEqual(2u, resource.RefCount);

                // The resource shall not be disposed even is exiting from a using statement
            }
            Assert.IsFalse(resurrection.IsDisposed);

            // Really dispose, since decrementing reference count
            resurrection.DecRef();
            Assert.IsTrue(resurrection.IsDisposed);
        }
예제 #2
0
        public void Resource_DecRef()
        {
            Resource resource;

            // Calling DecRef with a RefCount equal to 0 dispose
            resource = new TestResource();
            Assert.IsFalse(resource.IsDisposed);
            Assert.AreEqual(0u, resource.RefCount);

            resource.DecRef();
            Assert.IsTrue(resource.IsDisposed);

            // Calling DecRef with a RefCount equal to 1 dispose all the same
            resource = new TestResource();
            Assert.IsFalse(resource.IsDisposed);
            Assert.AreEqual(0u, resource.RefCount);

            resource.IncRef();
            Assert.AreEqual(1u, resource.RefCount);

            resource.DecRef();
            Assert.IsTrue(resource.IsDisposed);
        }