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); }
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); }