예제 #1
0
        public void TestParallelCacheAddGet()
        {
            var cache  = new ObjectCache <HashedKey, int>(16);
            var random = new Random(0);

            var expectedValues = new int[16 * 1024];

            for (int i = 0; i < expectedValues.Length; i++)
            {
                // Use some arbitrarily chose sampling sets to
                // give a test of interspersed hits and misses
                if ((i % 9) == 0)
                {
                    expectedValues[i] = random.Next(-5, 5);
                }
                else if ((i % 7) == 0)
                {
                    expectedValues[i] = random.Next(16, 32);
                }
                else
                {
                    expectedValues[i] = random.Next(-64, 64);
                }
            }

            // Skip zero since table cache will find HashedKey with key = 0 and hash = 0.
            // since this is equal to default(HashedKey).
            Parallel.For(
                1,
                expectedValues.Length,
                i =>
            {
                int expectedValue = expectedValues[i];

                var key = new HashedKey()
                {
                    Key  = expectedValue,
                    Hash = expectedValue
                };

                long misses = cache.Misses;
                long hits   = cache.Hits;

                int value;
                if (!cache.TryGetValue(key, out value))
                {
                    XAssert.IsTrue(cache.Misses > misses);
                    cache.AddItem(key, expectedValue);
                }
                else
                {
                    XAssert.IsTrue(cache.Hits > hits);
                    XAssert.AreEqual(expectedValue, value);
                }
            });
        }
예제 #2
0
        public void TestCacheAddGet()
        {
            var cache = new ObjectCache <HashedKey, int>(16);

            long hits   = 0;
            long misses = 0;

            // Loop twice. We know that cache should still return
            // false on first TryGetValue because the entries must have
            // been evicted by the end the time the same index is reached twice
            for (int j = 0; j < 2; j++)
            {
                // Skip zero since table cache will find HashedKey with key = 0 and hash = 0.
                // since this is equal to default(HashedKey).
                for (int i = 1; i < 64; i++)
                {
                    var key = new HashedKey()
                    {
                        Key  = i,
                        Hash = i
                    };

                    int value;
                    XAssert.IsFalse(cache.TryGetValue(key, out value));
                    cache.AddItem(key, i);

                    misses++;
                    XAssert.AreEqual(hits, cache.Hits);
                    XAssert.AreEqual(misses, cache.Misses);

                    XAssert.IsTrue(cache.TryGetValue(key, out value));
                    XAssert.AreEqual(i, value);

                    hits++;
                    XAssert.AreEqual(hits, cache.Hits);
                    XAssert.AreEqual(misses, cache.Misses);
                }
            }
        }