示例#1
0
        private static GSet <TestGSet.IntElement, TestGSet.IntElement> CreateGSet(TestGSet.IntElement
                                                                                  [] data)
        {
            GSet <TestGSet.IntElement, TestGSet.IntElement> gset = new LightWeightGSet <TestGSet.IntElement
                                                                                        , TestGSet.IntElement>(8);

            for (int i = 1; i < data.Length; i++)
            {
                gset.Put(data[i]);
            }
            return(gset);
        }
示例#2
0
        public virtual void TestRemoveAllViaIterator()
        {
            AList <int> list = GetRandomList(100, 123);
            LightWeightGSet <TestLightWeightGSet.TestElement, TestLightWeightGSet.TestElement>
            set = new LightWeightGSet <TestLightWeightGSet.TestElement, TestLightWeightGSet.TestElement
                                       >(16);

            foreach (int i in list)
            {
                set.Put(new TestLightWeightGSet.TestElement(i));
            }
            for (IEnumerator <TestLightWeightGSet.TestElement> iter = set.GetEnumerator(); iter
                 .HasNext();)
            {
                iter.Next();
                iter.Remove();
            }
            Assert.Equal(0, set.Size());
        }
示例#3
0
        public virtual void TestRemoveSomeViaIterator()
        {
            AList <int> list = GetRandomList(100, 123);
            LightWeightGSet <TestLightWeightGSet.TestElement, TestLightWeightGSet.TestElement>
            set = new LightWeightGSet <TestLightWeightGSet.TestElement, TestLightWeightGSet.TestElement
                                       >(16);

            foreach (int i in list)
            {
                set.Put(new TestLightWeightGSet.TestElement(i));
            }
            long sum = 0;

            for (IEnumerator <TestLightWeightGSet.TestElement> iter = set.GetEnumerator(); iter
                 .HasNext();)
            {
                sum += iter.Next().GetVal();
            }
            long mode = sum / set.Size();

            Log.Info("Removing all elements above " + mode);
            for (IEnumerator <TestLightWeightGSet.TestElement> iter_1 = set.GetEnumerator(); iter_1
                 .HasNext();)
            {
                int item = iter_1.Next().GetVal();
                if (item > mode)
                {
                    iter_1.Remove();
                }
            }
            for (IEnumerator <TestLightWeightGSet.TestElement> iter_2 = set.GetEnumerator(); iter_2
                 .HasNext();)
            {
                Assert.True(iter_2.Next().GetVal() <= mode);
            }
        }
示例#4
0
 public virtual void TestExceptionCases()
 {
     {
         //test contains
         LightWeightGSet <int, int> gset = new LightWeightGSet <int, int>(16);
         try
         {
             //test contains with a null element
             gset.Contains(null);
             NUnit.Framework.Assert.Fail();
         }
         catch (ArgumentNullException e)
         {
             LightWeightGSet.Log.Info("GOOD: getting " + e, e);
         }
     }
     {
         //test get
         LightWeightGSet <int, int> gset = new LightWeightGSet <int, int>(16);
         try
         {
             //test get with a null element
             gset.Get(null);
             NUnit.Framework.Assert.Fail();
         }
         catch (ArgumentNullException e)
         {
             LightWeightGSet.Log.Info("GOOD: getting " + e, e);
         }
     }
     {
         //test put
         LightWeightGSet <int, int> gset = new LightWeightGSet <int, int>(16);
         try
         {
             //test put with a null element
             gset.Put(null);
             NUnit.Framework.Assert.Fail();
         }
         catch (ArgumentNullException e)
         {
             LightWeightGSet.Log.Info("GOOD: getting " + e, e);
         }
         try
         {
             //test putting an element which is not implementing LinkedElement
             gset.Put(1);
             NUnit.Framework.Assert.Fail();
         }
         catch (ArgumentException e)
         {
             LightWeightGSet.Log.Info("GOOD: getting " + e, e);
         }
     }
     {
         //test iterator
         TestGSet.IntElement[] data = new TestGSet.IntElement[5];
         for (int i = 0; i < data.Length; i++)
         {
             data[i] = new TestGSet.IntElement(i, i);
         }
         for (int v = 1; v < data.Length - 1; v++)
         {
             {
                 //test remove while iterating
                 GSet <TestGSet.IntElement, TestGSet.IntElement> gset = CreateGSet(data);
                 foreach (TestGSet.IntElement i_1 in gset)
                 {
                     if (i_1.value == v)
                     {
                         //okay because data[0] is not in gset
                         gset.Remove(data[0]);
                     }
                 }
                 try
                 {
                     //exception because data[1] is in gset
                     foreach (TestGSet.IntElement i_2 in gset)
                     {
                         if (i_2.value == v)
                         {
                             gset.Remove(data[1]);
                         }
                     }
                     NUnit.Framework.Assert.Fail();
                 }
                 catch (ConcurrentModificationException e)
                 {
                     LightWeightGSet.Log.Info("GOOD: getting " + e, e);
                 }
             }
             {
                 //test put new element while iterating
                 GSet <TestGSet.IntElement, TestGSet.IntElement> gset = CreateGSet(data);
                 try
                 {
                     foreach (TestGSet.IntElement i_1 in gset)
                     {
                         if (i_1.value == v)
                         {
                             gset.Put(data[0]);
                         }
                     }
                     NUnit.Framework.Assert.Fail();
                 }
                 catch (ConcurrentModificationException e)
                 {
                     LightWeightGSet.Log.Info("GOOD: getting " + e, e);
                 }
             }
             {
                 //test put existing element while iterating
                 GSet <TestGSet.IntElement, TestGSet.IntElement> gset = CreateGSet(data);
                 try
                 {
                     foreach (TestGSet.IntElement i_1 in gset)
                     {
                         if (i_1.value == v)
                         {
                             gset.Put(data[3]);
                         }
                     }
                     NUnit.Framework.Assert.Fail();
                 }
                 catch (ConcurrentModificationException e)
                 {
                     LightWeightGSet.Log.Info("GOOD: getting " + e, e);
                 }
             }
         }
     }
 }