示例#1
0
        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);
            }
        }
示例#2
0
        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
        }
示例#3
0
        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()));
        }
示例#4
0
        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);
            }
        }
示例#5
0
        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));
        }
示例#6
0
        public void InternCache_CreateWeak_Trim_NotSupported()
        {
            var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Nop, Copy);

            Assert.ThrowsException <NotSupportedException>(() => cache.Trim());
        }
示例#7
0
        public void InternCache_CreateWeak_BadClone()
        {
            var cache = InternCache.CreateWeakInternCache <string>(MemoizationCacheFactory.Unbounded, s => s);

            Assert.ThrowsException <InvalidOperationException>(() => cache.Intern("foo"));
        }