예제 #1
0
        public void PooledDictionary_Serialization()
        {
            var ms = new MemoryStream();

            using (var obj = PooledDictionary <string, int> .New())
            {
                var map = obj.Dictionary;

                map.Add("qux", 42);
                map.Add("foo", 43);
                map.Add("bar", 44);
                map.Add("baz", 45);

                var fmt = new BinaryFormatter();
                fmt.Serialize(ms, map);
            }

            ms.Position = 0;

            {
                var fmt = new BinaryFormatter();
                var map = (PooledDictionary <string, int>)fmt.Deserialize(ms);

                Assert.IsTrue(map.TryGetValue("qux", out var val1) && val1 == 42);
                Assert.IsTrue(map.TryGetValue("foo", out var val2) && val2 == 43);
                Assert.IsTrue(map.TryGetValue("bar", out var val3) && val3 == 44);
                Assert.IsTrue(map.TryGetValue("baz", out var val4) && val4 == 45);

                map.Free(); // no-op but doesn't throw
            }
        }
예제 #2
0
        public void PooledDictionary_GlobalPool()
        {
            for (var i = 0; i < 100; i++)
            {
                using var obj = PooledDictionary <string, int> .New();

                var map = obj.Dictionary;

                Assert.AreEqual(0, map.Count);

                map.Add("qux", 42);
                map.Add("foo", 43);
                map.Add("bar", 44);
                map.Add("baz", 45);
            }
        }
예제 #3
0
        private static void PooledDictionary_Simple_Impl(DictionaryPool <string, int> pool)
        {
            for (var i = 0; i < 100; i++)
            {
                using var obj = i % 2 == 0 ? pool.New() : PooledDictionary <string, int> .New(pool);

                var map = obj.Dictionary;

                Assert.AreEqual(0, map.Count);

                map.Add("qux", 42);
                map.Add("foo", 43);
                map.Add("bar", 44);
                map.Add("baz", 45);
            }
        }
예제 #4
0
        public void PooledDictionary_GottenTooBig()
        {
            var bigPool = DictionaryPool <int, string> .Create(1, 16, EqualityComparer <int> .Default, 2048);

            var smallPool = DictionaryPool <int, string> .Create(1, 16, EqualityComparer <int> .Default, 16);

            TooBig(() => PooledDictionary <int, string> .New(), h => h.Dictionary, (h, n) => { for (var i = 0; i < n; i++)
                                                                                               {
                                                                                                   h.Add(i, "");
                                                                                               }
                   }, 1024);
            TooBig(() => bigPool.New(), h => h.Dictionary, (h, n) => { for (var i = 0; i < n; i++)
                                                                       {
                                                                           h.Add(i, "");
                                                                       }
                   }, 2048);
            TooBig(() => smallPool.New(), h => h.Dictionary, (h, n) => { for (var i = 0; i < n; i++)
                                                                         {
                                                                             h.Add(i, "");
                                                                         }
                   }, 16);
        }