Beispiel #1
0
        public void DifferentStringsDifferentDocuments()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";

            cache.Add(stringToAdd, document);
            Assert.Equal(1, cache.Count);

            stringToAdd = "Test2";
            XmlDocument document2 = new XmlDocument();
            string      return2   = cache.Add(stringToAdd, document2);

            // The second string gets its own instance.
            Assert.Equal(2, cache.Count);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();

            builder.Append("Test");
            builder.Append('2');
            XmlDocument document3 = new XmlDocument();
            string      return3   = cache.Add(builder.ToString(), document3);

            // The new string should be the same as the other one already in the collection.
            Assert.Same(return2, return3);

            // No new instances for string with the same content.
            Assert.Equal(2, cache.Count);
        }
Beispiel #2
0
        public void RemoveLastInstanceDeallocatesEntry()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";
            string return1     = cache.Add(stringToAdd, document);

            cache.Clear(document);

            // Should be no instances left.
            Assert.Equal(0, cache.Count);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();

            builder.Append("Test");
            builder.Append('1');
            XmlDocument document2 = new XmlDocument();

            string return2 = cache.Add(builder.ToString(), document2);

            // Returned references should NOT be the same
            Assert.NotSame(return1, return2);
        }
Beispiel #3
0
        public void AddReturnsSameInstanceForDifferentDocument()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";
            string return1     = cache.Add(stringToAdd, document);

            // Content of string should be the same.
            Assert.Equal(stringToAdd, return1);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();

            builder.Append("Test");
            builder.Append('1');
            XmlDocument document2 = new XmlDocument();

            string return2 = cache.Add(builder.ToString(), document2);

            // Content of string should be the same.
            Assert.Equal(builder.ToString(), return2);

            // Returned references should be the same
            Assert.Same(return1, return2);

            // Should not have added any new string instances to the cache.
            Assert.Equal(1, cache.Count);
        }
Beispiel #4
0
        public void RemoveOneInstance()
        {
            ProjectStringCache cache = new ProjectStringCache();

            XmlDocument document = new XmlDocument();

            string stringToAdd = "Test1";
            string return1     = cache.Add(stringToAdd, document);

            Assert.Equal(1, cache.Count);

            XmlDocument document2 = new XmlDocument();

            cache.Add(stringToAdd, document2);
            Assert.Equal(1, cache.Count);

            cache.Clear(document2);

            // Since there is still one document referencing the string, it should remain.
            Assert.Equal(1, cache.Count);

            // Build a new string guaranteed not to be optimized by the compiler into the same instance.
            StringBuilder builder = new StringBuilder();

            builder.Append("Test");
            builder.Append('1');
            XmlDocument document3 = new XmlDocument();

            string return3 = cache.Add(builder.ToString(), document3);

            // Returned references should be the same
            Assert.Same(return1, return3);

            // Still should only be one cached instance.
            Assert.Equal(1, cache.Count);
        }