示例#1
0
        public void ConcurrentCacheDictionary_Null()
        {
            var cd = new ConcurrentCacheDictionary <string, int>(EqualityComparer <string> .Default);

            Assert.AreEqual(0, cd.Count);

            var values = cd.Values;

            Assert.AreEqual(4, cd.GetOrAdd(key: null, x => (x ?? "null").Length));
            Assert.AreEqual(1, cd.Count);
            Assert.AreEqual(1, values.Count());
            Assert.IsTrue(values.Contains(4));

            Assert.AreEqual(4, cd.GetOrAdd(key: null, x => (x ?? "null").Length));
            Assert.AreEqual(1, cd.Count);
            Assert.AreEqual(1, values.Count());
            Assert.IsTrue(values.Contains(4));

            Assert.IsTrue(cd.Remove(key: null));
            Assert.AreEqual(0, cd.Count);
            Assert.AreEqual(0, values.Count());

            cd.Clear();
            Assert.AreEqual(0, cd.Count);
            Assert.AreEqual(0, values.Count());
        }
示例#2
0
        public void ConcurrentCacheDictionary_Simple()
        {
            var cd = new ConcurrentCacheDictionary <int, int>(EqualityComparer <int> .Default);

            Assert.AreEqual(0, cd.Count);

            var values = cd.Values;

            Assert.AreEqual(42, cd.GetOrAdd(21, x => x * 2));
            Assert.AreEqual(1, cd.Count);
            Assert.AreEqual(1, values.Count());
            Assert.IsTrue(values.Contains(42));

            Assert.AreEqual(42, cd.GetOrAdd(21, x => x * 2));
            Assert.AreEqual(1, cd.Count);
            Assert.AreEqual(1, values.Count());
            Assert.IsTrue(values.Contains(42));

            Assert.AreEqual(44, cd.GetOrAdd(22, x => x * 2));
            Assert.AreEqual(2, cd.Count);
            Assert.AreEqual(2, values.Count());
            Assert.IsTrue(values.Contains(42) && values.Contains(44));

            Assert.IsTrue(cd.Remove(21));
            Assert.AreEqual(1, cd.Count);
            Assert.AreEqual(1, values.Count());
            Assert.IsTrue(values.Contains(44));

            Assert.IsFalse(cd.Remove(21));
            Assert.AreEqual(1, cd.Count);
            Assert.AreEqual(1, values.Count());
            Assert.IsTrue(values.Contains(44));

            Assert.IsFalse(cd.Remove(-1));
            Assert.AreEqual(1, cd.Count);
            Assert.AreEqual(1, values.Count());
            Assert.IsTrue(values.Contains(44));

            cd.Clear();
            Assert.AreEqual(0, cd.Count);
            Assert.AreEqual(0, values.Count());
        }