コード例 #1
0
        public void TrimOrderTest()
        {
            var dict = new LRUCacheDictionary <string, string>()
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
                ["key4"] = "value4",
                ["key5"] = "value5",
            };

            // 4 -> 2 -> 3 -> 1 -> 5 の順で参照
            _ = dict["key4"];
            _ = dict["key2"];
            _ = dict["key3"];
            _ = dict["key1"];
            _ = dict["key5"];

            // 3 個までに縮小
            dict.TrimLimit = 3;
            dict.Trim();

            // 直近に参照された 3 -> 1 -> 5 のみ残っているはず
            Assert.True(dict.ContainsKey("key1"));
            Assert.False(dict.ContainsKey("key2"));
            Assert.True(dict.ContainsKey("key3"));
            Assert.False(dict.ContainsKey("key4"));
            Assert.True(dict.ContainsKey("key5"));
        }
コード例 #2
0
        public void CopyToTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            var array = new KeyValuePair <string, string> [5];

            dict.CopyTo(array, 0);
            Assert.Equal(new KeyValuePair <string, string>("key1", "value1"), array[0]);
            Assert.Equal(new KeyValuePair <string, string>("key2", "value2"), array[1]);
            Assert.Equal(new KeyValuePair <string, string>("key3", "value3"), array[2]);
            Assert.Equal(new KeyValuePair <string, string>(), array[3]);
            Assert.Equal(new KeyValuePair <string, string>(), array[4]);

            array = new KeyValuePair <string, string> [5];
            dict.CopyTo(array, 1);
            Assert.Equal(new KeyValuePair <string, string>(), array[0]);
            Assert.Equal(new KeyValuePair <string, string>("key1", "value1"), array[1]);
            Assert.Equal(new KeyValuePair <string, string>("key2", "value2"), array[2]);
            Assert.Equal(new KeyValuePair <string, string>("key3", "value3"), array[3]);
            Assert.Equal(new KeyValuePair <string, string>(), array[4]);

            array = new KeyValuePair <string, string> [5];
            Assert.Throws <ArgumentException>(() => dict.CopyTo(array, 3));
            Assert.Throws <ArgumentException>(() => dict.CopyTo(array, 5));
            Assert.Throws <ArgumentOutOfRangeException>(() => dict.CopyTo(array, -1));
            Assert.Throws <ArgumentNullException>(() => dict.CopyTo(null !, 0));
        }
コード例 #3
0
        public void InnerListTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            var node = dict.innerList.First;

            Assert.That(node.Value.Key, Is.EqualTo("key3"));
            node = node.Next;
            Assert.That(node.Value.Key, Is.EqualTo("key2"));
            node = node.Next;
            Assert.That(node.Value.Key, Is.EqualTo("key1"));

            // 2 -> 3 -> 1 の順に値を参照
            var x = dict["key2"];

            x = dict["key3"];
            x = dict["key1"];

            // 直近に参照した順で並んでいるかテスト
            node = dict.innerList.First;
            Assert.That(node.Value.Key, Is.EqualTo("key1"));
            node = node.Next;
            Assert.That(node.Value.Key, Is.EqualTo("key3"));
            node = node.Next;
            Assert.That(node.Value.Key, Is.EqualTo("key2"));
        }
コード例 #4
0
        public void TrimOrderTest()
        {
            var dict = new LRUCacheDictionary <string, string>()
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
                { "key4", "value4" },
                { "key5", "value5" },
            };

            // 4 -> 2 -> 3 -> 1 -> 5 の順で参照
            var x = dict["key4"];

            x = dict["key2"];
            x = dict["key3"];
            x = dict["key1"];
            x = dict["key5"];

            // 3 個までに縮小
            dict.TrimLimit = 3;
            dict.Trim();

            // 直近に参照された 3 -> 1 -> 5 のみ残っているはず
            Assert.That(dict.ContainsKey("key1"), Is.True);
            Assert.That(dict.ContainsKey("key2"), Is.False);
            Assert.That(dict.ContainsKey("key3"), Is.True);
            Assert.That(dict.ContainsKey("key4"), Is.False);
            Assert.That(dict.ContainsKey("key5"), Is.True);
        }
コード例 #5
0
        public void CopyToTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            var array = new KeyValuePair <string, string> [5];

            dict.CopyTo(array, 0);
            Assert.That(array[0], Is.EqualTo(new KeyValuePair <string, string>("key1", "value1")));
            Assert.That(array[1], Is.EqualTo(new KeyValuePair <string, string>("key2", "value2")));
            Assert.That(array[2], Is.EqualTo(new KeyValuePair <string, string>("key3", "value3")));
            Assert.That(array[3], Is.EqualTo(new KeyValuePair <string, string>()));
            Assert.That(array[4], Is.EqualTo(new KeyValuePair <string, string>()));

            array = new KeyValuePair <string, string> [5];
            dict.CopyTo(array, 1);
            Assert.That(array[0], Is.EqualTo(new KeyValuePair <string, string>()));
            Assert.That(array[1], Is.EqualTo(new KeyValuePair <string, string>("key1", "value1")));
            Assert.That(array[2], Is.EqualTo(new KeyValuePair <string, string>("key2", "value2")));
            Assert.That(array[3], Is.EqualTo(new KeyValuePair <string, string>("key3", "value3")));
            Assert.That(array[4], Is.EqualTo(new KeyValuePair <string, string>()));

            array = new KeyValuePair <string, string> [5];
            Assert.Throws <ArgumentException>(() => dict.CopyTo(array, 3));
            Assert.Throws <ArgumentException>(() => dict.CopyTo(array, 5));
            Assert.Throws <ArgumentOutOfRangeException>(() => dict.CopyTo(array, -1));
            Assert.Throws <ArgumentNullException>(() => dict.CopyTo(null, 0));
        }
コード例 #6
0
        public void Remove2Test()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            var ret = dict.Remove(new KeyValuePair <string, string>("key1", "value1"));

            Assert.That(ret, Is.True);
            Assert.That(dict.innerDict.Count, Is.EqualTo(2));
            Assert.That(dict.innerList.Count, Is.EqualTo(2));
            Assert.That(dict.innerDict.ContainsKey("key1"), Is.False);
            Assert.That(dict.innerDict.ContainsKey("key2"), Is.True);
            Assert.That(dict.innerDict.ContainsKey("key3"), Is.True);

            ret = dict.Remove(new KeyValuePair <string, string>("key2", "hogehoge"));
            Assert.That(ret, Is.False);

            dict.Remove(new KeyValuePair <string, string>("key2", "value2"));
            dict.Remove(new KeyValuePair <string, string>("key3", "value3"));

            Assert.That(dict.innerDict, Is.Empty);
            Assert.That(dict.innerList, Is.Empty);

            ret = dict.Remove(new KeyValuePair <string, string>("hogehoge", "hogehoge"));
            Assert.That(ret, Is.False);
        }
コード例 #7
0
        public void Remove2Test()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            var ret = dict.Remove(new KeyValuePair <string, string>("key1", "value1"));

            Assert.True(ret);
            Assert.Equal(2, dict.innerDict.Count);
            Assert.Equal(2, dict.innerList.Count);
            Assert.False(dict.innerDict.ContainsKey("key1"));
            Assert.True(dict.innerDict.ContainsKey("key2"));
            Assert.True(dict.innerDict.ContainsKey("key3"));

            ret = dict.Remove(new KeyValuePair <string, string>("key2", "hogehoge"));
            Assert.False(ret);

            dict.Remove(new KeyValuePair <string, string>("key2", "value2"));
            dict.Remove(new KeyValuePair <string, string>("key3", "value3"));

            Assert.Empty(dict.innerDict);
            Assert.Empty(dict.innerList);

            ret = dict.Remove(new KeyValuePair <string, string>("hogehoge", "hogehoge"));
            Assert.False(ret);
        }
コード例 #8
0
        public void InnerListTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            var node = dict.innerList.First;

            Assert.Equal("key3", node.Value.Key);
            node = node.Next;
            Assert.Equal("key2", node.Value.Key);
            node = node.Next;
            Assert.Equal("key1", node.Value.Key);

            // 2 -> 3 -> 1 の順に値を参照
            var x = dict["key2"];

            x = dict["key3"];
            x = dict["key1"];

            // 直近に参照した順で並んでいるかテスト
            node = dict.innerList.First;
            Assert.Equal("key1", node.Value.Key);
            node = node.Next;
            Assert.Equal("key3", node.Value.Key);
            node = node.Next;
            Assert.Equal("key2", node.Value.Key);
        }
コード例 #9
0
ファイル: ImageCache.cs プロジェクト: kuyagic/OpenTween
        public ImageCache()
        {
            this.innerDictionary = new LRUCacheDictionary <string, Task <MemoryImage> >(trimLimit: 300, autoTrimCount: 100);
            this.innerDictionary.CacheRemoved += (s, e) => {
                // まだ参照されている場合もあるのでDisposeはファイナライザ任せ
                this.CacheRemoveCount++;
            };

            this.cancelTokenSource = new CancellationTokenSource();
        }
コード例 #10
0
        public void ContainsTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            Assert.That(dict.Contains(new KeyValuePair <string, string>("key1", "value1")), Is.True);
            Assert.That(dict.Contains(new KeyValuePair <string, string>("key3", "value2")), Is.False);
            Assert.That(dict.Contains(new KeyValuePair <string, string>("value3", "key3")), Is.False);
            Assert.That(dict.Contains(new KeyValuePair <string, string>("hogehoge", "hogehoge")), Is.False);
        }
コード例 #11
0
        public void ContainsTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            Assert.Contains(new KeyValuePair <string, string>("key1", "value1"), dict);
            Assert.DoesNotContain(new KeyValuePair <string, string>("key3", "value2"), dict);
            Assert.DoesNotContain(new KeyValuePair <string, string>("value3", "key3"), dict);
            Assert.DoesNotContain(new KeyValuePair <string, string>("hogehoge", "hogehoge"), dict);
        }
コード例 #12
0
        public void ContainsKeyTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            Assert.True(dict.ContainsKey("key1"));
            Assert.True(dict.ContainsKey("key3"));
            Assert.False(dict.ContainsKey("value1"));
            Assert.False(dict.ContainsKey("hogehoge"));
        }
コード例 #13
0
        public void GetterTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            Assert.Equal("value1", dict["key1"]);
            Assert.Equal("value2", dict["key2"]);
            Assert.Equal("value3", dict["key3"]);

            Assert.Throws <KeyNotFoundException>(() => { var x = dict["hogehoge"]; });
        }
コード例 #14
0
        public void GetterTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            Assert.That(dict["key1"], Is.EqualTo("value1"));
            Assert.That(dict["key2"], Is.EqualTo("value2"));
            Assert.That(dict["key3"], Is.EqualTo("value3"));

            Assert.Throws <KeyNotFoundException>(() => { var x = dict["hogehoge"]; });
        }
コード例 #15
0
        public void SetterTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            dict["key1"] = "foo";
            Assert.Equal("foo", dict.innerDict["key1"].Value.Value);

            dict["hogehoge"] = "bar";
            Assert.True(dict.innerDict.ContainsKey("hogehoge"));
            Assert.Equal("bar", dict.innerDict["hogehoge"].Value.Value);
        }
コード例 #16
0
        public void SetterTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            dict["key1"] = "foo";
            Assert.That(dict.innerDict["key1"].Value.Value, Is.EqualTo("foo"));

            dict["hogehoge"] = "bar";
            Assert.That(dict.innerDict.ContainsKey("hogehoge"), Is.True);
            Assert.That(dict.innerDict["hogehoge"].Value.Value, Is.EqualTo("bar"));
        }
コード例 #17
0
        public void TryGetValueTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            var ret = dict.TryGetValue("key1", out var value);

            Assert.True(ret);
            Assert.Equal("value1", value);

            ret = dict.TryGetValue("hogehoge", out value);
            Assert.False(ret);
            Assert.Null(value);
        }
コード例 #18
0
        public void CountTest()
        {
            var dict = new LRUCacheDictionary <string, string>();

            Assert.That(dict.Count, Is.EqualTo(0));

            dict.Add("key1", "value1");
            Assert.That(dict.Count, Is.EqualTo(1));

            dict.Add("key2", "value2");
            Assert.That(dict.Count, Is.EqualTo(2));

            dict.Remove("key1");
            Assert.That(dict.Count, Is.EqualTo(1));

            dict.Clear();
            Assert.That(dict.Count, Is.EqualTo(0));
        }
コード例 #19
0
        public void CountTest()
        {
            var dict = new LRUCacheDictionary <string, string>();

            Assert.Empty(dict);

            dict.Add("key1", "value1");
            Assert.Single(dict);

            dict.Add("key2", "value2");
            Assert.Equal(2, dict.Count);

            dict.Remove("key1");
            Assert.Single(dict);

            dict.Clear();
            Assert.Empty(dict);
        }
コード例 #20
0
        public void TryGetValueTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            string value;
            var    ret = dict.TryGetValue("key1", out value);

            Assert.That(ret, Is.True);
            Assert.That(value, Is.EqualTo("value1"));

            ret = dict.TryGetValue("hogehoge", out value);
            Assert.That(ret, Is.False);
            Assert.That(value, Is.Null);
        }
コード例 #21
0
        public void Enumerator2Test()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            var enumerator = ((IEnumerable)dict).GetEnumerator();

            Assert.That(enumerator.MoveNext(), Is.True);
            Assert.That(enumerator.Current, Is.EqualTo(new KeyValuePair <string, string>("key1", "value1")));
            Assert.That(enumerator.MoveNext(), Is.True);
            Assert.That(enumerator.Current, Is.EqualTo(new KeyValuePair <string, string>("key2", "value2")));
            Assert.That(enumerator.MoveNext(), Is.True);
            Assert.That(enumerator.Current, Is.EqualTo(new KeyValuePair <string, string>("key3", "value3")));
            Assert.That(enumerator.MoveNext(), Is.False);
        }
コード例 #22
0
        public void Enumerator2Test()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            var enumerator = ((IEnumerable)dict).GetEnumerator();

            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <string, string>("key1", "value1"), enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <string, string>("key2", "value2"), enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <string, string>("key3", "value3"), enumerator.Current);
            Assert.False(enumerator.MoveNext());
        }
コード例 #23
0
        public void EnumeratorTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            var enumerator = dict.GetEnumerator();

            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <string, string>("key1", "value1"), enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <string, string>("key2", "value2"), enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <string, string>("key3", "value3"), enumerator.Current);
            Assert.False(enumerator.MoveNext());
        }
コード例 #24
0
        public void KeysTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            Assert.Equal(new[] { "key1", "key2", "key3" }, dict.Keys, collComparer);

            dict.Add("foo", "bar");
            Assert.Equal(new[] { "key1", "key2", "key3", "foo" }, dict.Keys, collComparer);

            dict.Remove("key2");
            Assert.Equal(new[] { "key1", "key3", "foo" }, dict.Keys, collComparer);

            dict.Clear();
            Assert.Empty(dict.Keys);
        }
コード例 #25
0
        public void ValuesTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
            };

            Assert.That(dict.Values, Is.EquivalentTo(new[] { "value1", "value2", "value3" }));

            dict.Add("foo", "bar");
            Assert.That(dict.Values, Is.EquivalentTo(new[] { "value1", "value2", "value3", "bar" }));

            dict.Remove("key2");
            Assert.That(dict.Values, Is.EquivalentTo(new[] { "value1", "value3", "bar" }));

            dict.Clear();
            Assert.That(dict.Values, Is.Empty);
        }
コード例 #26
0
        public void ValuesTest()
        {
            var dict = new LRUCacheDictionary <string, string>
            {
                ["key1"] = "value1",
                ["key2"] = "value2",
                ["key3"] = "value3",
            };

            Assert.Equal(new[] { "value1", "value2", "value3" }, dict.Values, collComparer);

            dict.Add("foo", "bar");
            Assert.Equal(new[] { "value1", "value2", "value3", "bar" }, dict.Values, collComparer);

            dict.Remove("key2");
            Assert.Equal(new[] { "value1", "value3", "bar" }, dict.Values, collComparer);

            dict.Clear();
            Assert.Empty(dict.Values);
        }
コード例 #27
0
        public void CacheRemovedEventTest()
        {
            var dict = new LRUCacheDictionary <string, string>();

            dict["key1"] = "value1";
            dict["key2"] = "value2";
            dict["key3"] = "value3";
            dict["key4"] = "value4";

            // イベント設定
            var removedList = new List <string>();

            dict.CacheRemoved += (s, e) => removedList.Add(e.Item.Key);

            // 2 個までに縮小
            dict.TrimLimit = 2;
            dict.Trim();

            // 直近に参照された 3, 4 以外のアイテムに対してイベントが発生しているはず
            Assert.Equal(new[] { "key1", "key2" }, removedList, collComparer);
        }
コード例 #28
0
        public void AddTest()
        {
            var dict = new LRUCacheDictionary <string, string>();

            dict.Add("key1", "value1");

            Assert.Single(dict.innerDict);
            Assert.True(dict.innerDict.ContainsKey("key1"));
            var internalNode = dict.innerDict["key1"];

            Assert.Equal("key1", internalNode.Value.Key);
            Assert.Equal("value1", internalNode.Value.Value);

            dict.Add("key2", "value2");

            Assert.Equal(2, dict.innerDict.Count);
            Assert.True(dict.innerDict.ContainsKey("key2"));
            internalNode = dict.innerDict["key2"];
            Assert.Equal("key2", internalNode.Value.Key);
            Assert.Equal("value2", internalNode.Value.Value);
        }
コード例 #29
0
        public void AddTest()
        {
            var dict = new LRUCacheDictionary <string, string>();

            dict.Add("key1", "value1");

            Assert.That(dict.innerDict.Count, Is.EqualTo(1));
            Assert.That(dict.innerDict.ContainsKey("key1"), Is.True);
            var internalNode = dict.innerDict["key1"];

            Assert.That(internalNode.Value.Key, Is.EqualTo("key1"));
            Assert.That(internalNode.Value.Value, Is.EqualTo("value1"));

            dict.Add("key2", "value2");

            Assert.That(dict.innerDict.Count, Is.EqualTo(2));
            Assert.That(dict.innerDict.ContainsKey("key2"), Is.True);
            internalNode = dict.innerDict["key2"];
            Assert.That(internalNode.Value.Key, Is.EqualTo("key2"));
            Assert.That(internalNode.Value.Value, Is.EqualTo("value2"));
        }
コード例 #30
0
        public ImageCache()
        {
            lock (this.lockObject)
            {
                this.innerDictionary = new LRUCacheDictionary <string, Task <MemoryImage> >(trimLimit: 300, autoTrimCount: 100);

                this.innerDictionary.CacheRemoved += (s, e) => {
                    var imageTask = e.Item.Value;

                    if (imageTask.Status == TaskStatus.RanToCompletion)
                    {
                        imageTask.Result.Dispose();
                    }

                    imageTask.Dispose();

                    this.CacheRemoveCount++;
                };

                this.cancelTokenSource = new CancellationTokenSource();
            }
        }