public void InternCache_CreateWeak_Trim() { var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Unbounded, Copy); var set = new HashSet <string>(); for (var n = 0; n < 2; n++) { InternSome(cache, set); // Ensures lifetime of locals is not extended in DEBUG mode Assert.AreEqual(1, cache.Count); cache.Trim(); Assert.AreEqual(1, cache.Count); set.Clear(); GC.Collect(); GC.WaitForPendingFinalizers(); InternSome(cache, set); // Resurrects weak references Assert.AreEqual(1, cache.Count); cache.Trim(); Assert.AreEqual(1, cache.Count); set.Clear(); GC.Collect(); GC.WaitForPendingFinalizers(); cache.Trim(); Assert.AreEqual(0, cache.Count); } }
public void InternCache_CreateWeak_ArgumentChecking() { #pragma warning disable IDE0034 // Simplify 'default' expression (illustrative of method signature) Assert.ThrowsException <ArgumentNullException>(() => InternCache.CreateWeakInternCache <string>(default(IMemoizationCacheFactory), Copy)); Assert.ThrowsException <ArgumentNullException>(() => InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Unbounded, default(Func <string, string>))); #pragma warning restore IDE0034 // Simplify 'default' expression }
public void InternCache_CreateWeak_Dispose() { var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Unbounded, Copy); cache.Intern("bar".ToUpper()); cache.Intern("bar".ToUpper()); cache.Dispose(); Assert.ThrowsException <ObjectDisposedException>(() => cache.Intern("bar".ToUpper())); }
public void InternCache_CreateWeak_Simple2() { var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Unbounded, Copy); for (var n = 0; n < 2; n++) { for (var i = 0; i < 100; i++) { Assert.AreEqual("BAR", cache.Intern("bar".ToUpper())); } Assert.AreEqual(1, cache.Count); cache.Clear(); Assert.AreEqual(0, cache.Count); } }
public void InternCache_CreateWeak_Simple1() { var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Unbounded, Copy); var arg1 = "bar".ToUpper(); var res1 = cache.Intern(arg1); Assert.AreEqual("BAR", res1); Assert.AreNotSame(arg1, res1); // cloning behavior in the value space var arg2 = "bar".ToUpper(); var res2 = cache.Intern(arg2); Assert.AreEqual("BAR", res2); Assert.AreNotSame(arg2, res2); Assert.AreSame(res1, res2); Assert.IsFalse(string.IsNullOrEmpty(cache.DebugView)); }
public void InternCache_CreateWeak_Trim_NotSupported() { var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Nop, Copy); Assert.ThrowsException <NotSupportedException>(() => cache.Trim()); }
public void InternCache_CreateWeak_BadClone() { var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Unbounded, s => s); Assert.ThrowsException <InvalidOperationException>(() => cache.Intern("foo")); }