Beispiel #1
0
        public void Remove()
        {
            int LCacheSize = 5;

            FTestCache = new FixedSizeCache <string, string>(LCacheSize);
            for (int i = 1; i <= FTestCache.Size; i++)
            {
                FTestCache.Add(GetKey(i), GetValue(i));
            }

            int LTestKey = 3;

            FTestCache.Remove(GetKey(LTestKey));
            Assert.False(FTestCache.ContainsKey(GetValue(LTestKey)));
            Assert.AreEqual(LCacheSize - 1, FTestCache.Count);

            LTestKey = 1;
            FTestCache.Remove(GetKey(LTestKey));
            Assert.False(FTestCache.ContainsKey(GetValue(LTestKey)));
            Assert.AreEqual(LCacheSize - 2, FTestCache.Count);

            LTestKey = 5;
            FTestCache.Remove(GetKey(LTestKey));
            Assert.False(FTestCache.ContainsKey(GetValue(LTestKey)));
            Assert.AreEqual(LCacheSize - 3, FTestCache.Count);
            Assert.True(ValidateList());
        }
Beispiel #2
0
        public void Run()
        {
            Random LRandom = new Random();

            for (int j = 0; j < FIterations; j++)
            {
                int LEntry;
                for (int i = 0; i < FAccessCount; i++)
                {
                    LEntry = LRandom.Next(1, FSeed);
                    switch (LRandom.Next(1, 10))
                    {
                    case 1:
                    case 2:
                    case 3:
                        FFixedSizeCache.Add(FixedSizeCacheTest.GetKey(LEntry), FixedSizeCacheTest.GetValue(LEntry));
                        break;

                    case 4:
                    case 5:
                    case 6:
                        FFixedSizeCache[FixedSizeCacheTest.GetKey(LEntry)] = FixedSizeCacheTest.GetValue(LRandom.Next(1, FSeed));
                        break;

                    case 8:
                        FFixedSizeCache.Reference(FixedSizeCacheTest.GetKey(LEntry), FixedSizeCacheTest.GetValue(LEntry));
                        break;

                    default:
                        FFixedSizeCache.Remove(FixedSizeCacheTest.GetKey(LEntry));
                        break;
                    }
                }
            }
        }
Beispiel #3
0
        public void InternalRemove()
        {
            try
            {
                //add four remove middle
                //add four remove tail

                ReflectionPermission LReflectionPermission = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.AllFlags);
                LReflectionPermission.Assert();

                FixedSizeCache <string, string> .Entry LHead;
                FixedSizeCache <string, string> .Entry LCutoff;
                FixedSizeCache <string, string> .Entry LTail;

                //add one remove one
                FTestCache = new FixedSizeCache <string, string>(2);
                IncrementTestEntry();

                FTestCache.Remove(GetKey(1));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(0, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.IsNull(LHead);
                Assert.IsNull(LCutoff);
                Assert.IsNull(LTail);

                //add two remove head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(2);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                string LKey = GetKey(2);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(1));
                Assert.AreEqual(LCutoff.Key, GetKey(1));
                Assert.AreEqual(LTail.Key, GetKey(1));

                Assert.IsNull(GetNext(LHead));
                Assert.IsNull(GetPrior(LHead));
                Assert.False(GetPreCutoff(LHead));

                //add two remove tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(2);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(1);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add three remove head, head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(3);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.IsNull(GetNext(LHead));
                Assert.AreEqual(LTail, GetPrior(LHead));
                Assert.False(GetPreCutoff(LHead));

                Assert.AreEqual(LTail.Key, GetKey(1));
                Assert.AreEqual(LHead, GetNext(LTail));
                Assert.IsNull(GetPrior(LTail));
                Assert.False(GetPreCutoff(LTail));

                FTestCache.Remove(GetKey(2));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(1));
                Assert.AreEqual(LCutoff.Key, GetKey(1));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add three remove head, tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(3);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(1));

                FTestCache.Remove(GetKey(1));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add three remove tail, middle
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(1);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.IsNull(GetNext(LHead));
                Assert.AreEqual(LTail, GetPrior(LHead));
                Assert.False(GetPreCutoff(LHead));

                Assert.AreEqual(LTail.Key, GetKey(2));
                Assert.AreEqual(LHead, GetNext(LTail));
                Assert.IsNull(GetPrior(LTail));
                Assert.False(GetPreCutoff(LTail));

                FTestCache.Remove(GetKey(2));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(3));

                //add three remove tail, head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(1);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(2));

                FTestCache.Remove(GetKey(3));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add four remove head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetHead().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());


                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.IsNull(GetNext(LHead));
                //Assert.AreEqual(LTail, GetPrior(LHead));
                //Assert.False(GetPreCutoff(LHead));

                //Assert.AreEqual(LTail.Key, GetKey(2));
                //Assert.AreEqual(LHead, GetNext(LTail));
                //Assert.IsNull(GetPrior(LTail));
                //Assert.False(GetPreCutoff(LTail));


                //add four remove tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetTail().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(4));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add four remove cutoff
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetCutoff().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(4));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add four remove middle
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetKey(2));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(4));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetHead().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(5));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetTail().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(5));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add five remove cutoff
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetCutoff().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove middle, next to cutoff
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetKey(3));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(5));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove middle, next to tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetKey(3));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(5));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //FTestCache = new FixedSizeCache<string, string>(2);
                //while (TestEntry < FTestCache.Size)
                //    IncrementTestEntry();


                //FTestCache = new FixedSizeCache<string, string>(25);
                //while (TestEntry < FTestCache.Size)
                //    IncrementTestEntry();

                //FTestCache.Remove(GetHead().Key);
                //Assert.True(ValidateList());

                //FTestCache.Remove(GetCutoff().Key);
                //Assert.True(ValidateList());

                //FTestCache.Remove(GetTail().Key);
                //Assert.True(ValidateList());

                //while (FTestCache.Count > 0)
                //{
                //    FTestCache.Remove(GetCutoff().Key);
                //    Assert.True(ValidateList());
                //}
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
        }