Ejemplo n.º 1
0
        public static ListIterator <T> FillN <T>(IList <T> list, int startIndex, int count, T value)
        {
            ListIterator <T> begin = IteratorUtil.Begin(list);

            begin.OffsetBy(startIndex);
            return((ListIterator <T>)FillN(begin, count, value));
        }
Ejemplo n.º 2
0
        [Test] public void RotateCopy_Iterator()
        {
            int[] src  = new int[10];
            int[] dest = new int[12];
            dest[0] = Int32.MinValue;
            dest[dest.Length - 1] = Int32.MinValue;
            for (int i = 0; i < src.Length; ++i)
            {
                src[i] = i + 1;
            }

            ListIterator <int> middle = new ListIterator <int>(src, 3);

            Algorithm.RotateCopy(IteratorUtil.Begin(src), middle, IteratorUtil.End(src), new ListIterator <int>(dest, 1));

            Assert.AreEqual(Int32.MinValue, dest[0]);
            Assert.AreEqual(Int32.MinValue, dest[dest.Length - 1]);
            for (int i = 1, c = 4; i < 8; ++i, ++c)
            {
                Assert.AreEqual(c, dest[i]);
            }

            for (int i = 8, c = 1; i < 11; ++i, ++c)
            {
                Assert.AreEqual(c, dest[i]);
            }
        }
Ejemplo n.º 3
0
        [Test] public void FindIf_IntIterator()
        {
            int[] array             = Constants.TEST_INT_ARRAY;
            ListIterator <int> iter = Algorithm.FindIf(IteratorUtil.Begin(array), IteratorUtil.End(array), IsMinus1);

            Assert.AreEqual(8, iter.Position);

            iter = Algorithm.FindIf(IteratorUtil.Begin(array), IteratorUtil.End(array), Never);
            Assert.IsNull(iter);
//        Assert.IsTrue(IteratorUtil.End(array).Equals(iter));

            RandomAccessIterator <int> beginR = IteratorUtil.Begin(array);
            RandomAccessIterator <int> endR   = IteratorUtil.End(array);
            RandomAccessIterator <int> iterR  = Algorithm.FindIf(beginR, endR, IsMinus1);

            Assert.AreEqual(8, ((ListIterator <int>)iterR).Position);

            ForwardIterator <int> beginF = IteratorUtil.Begin(array);
            ForwardIterator <int> endF   = IteratorUtil.End(array);
            ForwardIterator <int> iterF  = Algorithm.FindIf(beginF, endF, IsMinus1);

            Assert.AreEqual(8, ((ListIterator <int>)iterF).Position);

            InputIterator <int> beginI = IteratorUtil.Begin(array);
            InputIterator <int> endI   = IteratorUtil.End(array);
            InputIterator <int> iterI  = Algorithm.FindIf(beginI, endI, IsMinus1);

            Assert.AreEqual(8, ((ListIterator <int>)iterI).Position);
        }
Ejemplo n.º 4
0
        [Test] public void NthElement_Iterator_Comparison()
        {
            int[] copy = new int[src.Length];
            Algorithm.Copy(src, copy);
            Array.Sort(copy);

            ListIterator <int> begin = IteratorUtil.Begin(src);
            ListIterator <int> end   = IteratorUtil.End(src);
            ListIterator <int> nth   = IteratorUtil.AdvanceCopy(begin, 4);
            int pivotValue           = nth.Read();

            Algorithm.NthElement(begin, nth, end, Functional.Compare);
            Assert.AreEqual(0, begin.Position);
            Assert.AreEqual(src.Length, end.Position);

            Assert.AreEqual(copy[4], src[4]);

            for (int i = 0; i < 4; ++i)
            {
                Assert.IsTrue(src[i] < src[4]);
            }

            for (int i = 5; i < src.Length; ++i)
            {
                Assert.IsTrue(src[i] >= src[4]);
            }
        }
Ejemplo n.º 5
0
        [Test] public void Mismatch_RandomIterator()
        {
            array1[7]++;
            ListIterator <int> cmpIter = IteratorUtil.Begin(array2);

            cmpIter.MoveNext();
            cmpIter.MoveNext();

            RandomAccessIterator <int> beginR = IteratorUtil.Begin(array1);
            RandomAccessIterator <int> endR   = IteratorUtil.End(array1);
            RandomAccessIterator <int> cmpR   = cmpIter;
            Pair <RandomAccessIterator <int>, RandomAccessIterator <int> > pR = Algorithm.Mismatch(beginR, endR, cmpR);

            Assert.AreEqual(7, pR.First.Position);
            Assert.AreEqual(array1[7], pR.First.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pR.First);
            Assert.AreEqual(9, pR.Second.Position);
            Assert.AreEqual(array2[9], pR.Second.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pR.Second);

            pR = Algorithm.Mismatch(beginR, endR, cmpR, EqualityComparer <int> .Default);
            Assert.AreEqual(7, pR.First.Position);
            Assert.AreEqual(array1[7], pR.First.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pR.First);
            Assert.AreEqual(9, pR.Second.Position);
            Assert.AreEqual(array2[9], pR.Second.Read());
            Assert.IsInstanceOfType(typeof(ListIterator <int>), pR.Second);
        }
Ejemplo n.º 6
0
        [Test] public void RandomShuffle_Iterator_Func()
        {
            Random rand = new Random(System.Environment.TickCount);

            Algorithm.RandomShuffle(IteratorUtil.Begin(src), IteratorUtil.End(src), delegate(int max) { return(rand.Next(max)); });
            Algorithm.RandomShuffle(IteratorUtil.Begin(src), IteratorUtil.End(src), delegate(int max) { return(rand.Next(max)); });
        }
Ejemplo n.º 7
0
        [Test] public void MaxElement_StringIteratorComparer()
        {
            string[] array             = Constants.TEST_STRING_ARRAY;
            ListIterator <string> iter = Algorithm.MaxElement(IteratorUtil.Begin(array), IteratorUtil.End(array), Comparer <string> .Default);

            Assert.AreEqual("world", iter.Read());
            Assert.AreEqual(1, iter.Position);
        }
Ejemplo n.º 8
0
        [Test] public void MaxElement_IntIteratorComparer()
        {
            int[] array             = Constants.TEST_INT_ARRAY;
            ListIterator <int> iter = Algorithm.MaxElement(IteratorUtil.Begin(array), IteratorUtil.End(array), Comparer <int> .Default);

            Assert.AreEqual(100, iter.Read());
            Assert.AreEqual(10, iter.Position);
        }
Ejemplo n.º 9
0
        [Test] public void MinElement_StringIterator()
        {
            string[] array             = Constants.TEST_STRING_ARRAY;
            ListIterator <string> iter = Algorithm.MinElement(IteratorUtil.Begin(array), IteratorUtil.End(array));

            Assert.AreEqual("Bob", iter.Read());
            Assert.AreEqual(9, iter.Position);
        }
Ejemplo n.º 10
0
 [Test] public void CopyBackward_IteratorToList()
 {
     Algorithm.CopyBackward(IteratorUtil.Begin(src), IteratorUtil.End(src), dest);
     for (int s = src.Length - 1, d = dest.Length - 1; s >= 0; --s, --d)
     {
         Assert.AreEqual(src[s], dest[d]);
     }
 }
Ejemplo n.º 11
0
 [Test] public void Copy_IteratorToList()
 {
     Algorithm.Copy(IteratorUtil.Begin(src), IteratorUtil.End(src), dest);
     for (int i = 0; i < count; ++i)
     {
         Assert.AreEqual(dest[i], src[i]);
     }
 }
Ejemplo n.º 12
0
        [Test] public void Unique_Iterator_Comparer()
        {
            ListIterator <int> result = Algorithm.Unique(IteratorUtil.Begin(src), IteratorUtil.End(src), EqualityComparer <int> .Default);

            VerifyOutput();

            Assert.IsNotNull(result);
            Assert.AreEqual(5, result.Position);
        }
Ejemplo n.º 13
0
        [Test] public void Unique_Iterator_Predicate()
        {
            ListIterator <int> result = Algorithm.Unique(IteratorUtil.Begin(src), IteratorUtil.End(src), delegate(int lhs, int rhs) { return(lhs == rhs); });

            VerifyOutput();

            Assert.IsNotNull(result);
            Assert.AreEqual(5, result.Position);
        }
Ejemplo n.º 14
0
        [Test] public void Unique_RandomAccessIterator_Predicate()
        {
            RandomAccessIterator <int> begin  = IteratorUtil.Begin(src);
            RandomAccessIterator <int> end    = IteratorUtil.End(src);
            RandomAccessIterator <int> result = Algorithm.Unique(begin, end, delegate(int lhs, int rhs) { return(lhs == rhs); });

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(5, IteratorUtil.Distance(begin, result));
        }
Ejemplo n.º 15
0
        [Test] public void SearchN_Iterator()
        {
            int[] array = Constants.TEST_BIG_INT_ARRAY;
            ForwardIterator <int> result = Algorithm.SearchN(IteratorUtil.Begin(array), IteratorUtil.End(array), 3, 1);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(typeof(ListIterator <int>), result);
            Assert.AreEqual(5, IteratorUtil.Distance(IteratorUtil.Begin(array), result));
            Assert.AreEqual(array[5], result.Read());
        }
Ejemplo n.º 16
0
        [Test] public void FindFirstOf_Iterator()
        {
            ForwardIterator <int> result = Algorithm.FindFirstOf(IteratorUtil.Begin(array), IteratorUtil.End(array),
                                                                 IteratorUtil.Begin(search), IteratorUtil.End(search));

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(typeof(ListIterator <int>), result);
            Assert.AreEqual(5, IteratorUtil.Distance(IteratorUtil.Begin(array), result));
            Assert.AreEqual(33, result.Read());
        }
Ejemplo n.º 17
0
        [Test] public void Equal_IteratorComparer()
        {
            int[]      range1 = Constants.TEST_INT_ARRAY;
            List <int> range2 = new List <int>(range1);

            Assert.IsTrue(Algorithm.Equal(IteratorUtil.Begin(range1), IteratorUtil.End(range1), IteratorUtil.Begin(range2), EqualityComparer <int> .Default));

            --range2[5];
            Assert.IsFalse(Algorithm.Equal(IteratorUtil.Begin(range1), IteratorUtil.End(range1), IteratorUtil.Begin(range2), EqualityComparer <int> .Default));
        }
Ejemplo n.º 18
0
 [Test] public void ReverseCopy_Iterator2List()
 {
     Algorithm.ReverseCopy(IteratorUtil.Begin(src), IteratorUtil.End(src), dest);
     Assert.AreEqual(dest[dest.Length - 1], Int32.MinValue);
     Assert.AreEqual(dest[dest.Length - 2], Int32.MinValue);
     for (int i = 0; i < count; ++i)
     {
         Assert.AreEqual(src[i], dest[count - 1 - i]);
     }
 }
Ejemplo n.º 19
0
        [Test] public void Unique_ForwardIterator()
        {
            ForwardIterator <int> begin  = IteratorUtil.Begin(src);
            ForwardIterator <int> end    = IteratorUtil.End(src);
            ForwardIterator <int> result = Algorithm.Unique(begin, end);

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(5, IteratorUtil.Distance(begin, result));
        }
Ejemplo n.º 20
0
        [Test] public void Unique_RandomAccessIterator_Comparer()
        {
            RandomAccessIterator <int> begin  = IteratorUtil.Begin(src);
            RandomAccessIterator <int> end    = IteratorUtil.End(src);
            RandomAccessIterator <int> result = Algorithm.Unique(begin, end, EqualityComparer <int> .Default);

            VerifyOutput();
            Assert.IsNotNull(result);
            Assert.AreEqual(5, IteratorUtil.Distance(begin, result));
        }
Ejemplo n.º 21
0
        [Test] public void StableSort_Iterator_Comparer()
        {
            ListIterator <int> begin = IteratorUtil.Begin(src);
            ListIterator <int> end   = IteratorUtil.End(src);

            Algorithm.StableSort(begin, end, Comparer <int> .Default);

            Assert.AreEqual(0, begin.Position);
            Assert.AreEqual(src.Length, end.Position);
            ValidateOutput();
        }
Ejemplo n.º 22
0
        [Test] public void FindEnd_Comparer()
        {
            ForwardIterator <int> result = Algorithm.FindEnd(IteratorUtil.Begin(array), IteratorUtil.End(array),
                                                             IteratorUtil.Begin(search), IteratorUtil.End(search),
                                                             EqualityComparer <int> .Default);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(typeof(ListIterator <int>), result);
            Assert.AreEqual(array.Length - 9, IteratorUtil.Distance(IteratorUtil.Begin(array), result));
            Assert.AreEqual(5, result.Read());
        }
Ejemplo n.º 23
0
        [Test] public void FindEnd_Predicate()
        {
            ForwardIterator <int> result = Algorithm.FindEnd(IteratorUtil.Begin(array), IteratorUtil.End(array),
                                                             IteratorUtil.Begin(search), IteratorUtil.End(search),
                                                             Functional.EqualTo <int>);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(typeof(ListIterator <int>), result);
            Assert.AreEqual(array.Length - 9, IteratorUtil.Distance(IteratorUtil.Begin(array), result));
            Assert.AreEqual(5, result.Read());
        }
Ejemplo n.º 24
0
        [Test] public void StableSort_Iterator_Predicate()
        {
            ListIterator <int> begin = IteratorUtil.Begin(src);
            ListIterator <int> end   = IteratorUtil.End(src);

            Algorithm.StableSort(begin, end, Functional.Compare <int>);

            Assert.AreEqual(0, begin.Position);
            Assert.AreEqual(src.Length, end.Position);
            ValidateOutput();
        }
Ejemplo n.º 25
0
        [Test] public void Find_StringIterator()
        {
            string[] array             = Constants.TEST_STRING_ARRAY;
            ListIterator <string> iter = Algorithm.Find(IteratorUtil.Begin(array), IteratorUtil.End(array), "Sue");

            Assert.AreEqual(7, iter.Position);

            iter = Algorithm.Find(IteratorUtil.Begin(array), IteratorUtil.End(array), "python");
            Assert.IsNull(iter);
//        Assert.IsTrue(IteratorUtil.End(array ).Equals(iter));
        }
Ejemplo n.º 26
0
        [Test] public void Find_IntIterator()
        {
            int[] array             = Constants.TEST_INT_ARRAY;
            ListIterator <int> iter = Algorithm.Find(IteratorUtil.Begin(array), IteratorUtil.End(array), -1);

            Assert.AreEqual(8, iter.Position);

            iter = Algorithm.Find(IteratorUtil.Begin(array), IteratorUtil.End(array), -217);
            Assert.IsNull(iter);
//        Assert.IsTrue(IteratorUtil.End(array).Equals(iter));
        }
Ejemplo n.º 27
0
        [Test] public void Remove_Comparer_ForwardIterator()
        {
            ForwardIterator <int> begin = IteratorUtil.Begin(src);
            ForwardIterator <int> end   = IteratorUtil.End(src);

            ForwardIterator <int> result = Algorithm.Remove(begin, end, 29, EqualityComparer <int> .Default);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, (begin as ListIterator <int>).Position);
            Assert.AreEqual(src.Length, IteratorUtil.Distance(begin, end));
            Assert.AreEqual(src.Length - 4, IteratorUtil.Distance(begin, result));
        }
Ejemplo n.º 28
0
        [Test] public void Remove_Comparer_RandomAccessIterator()
        {
            RandomAccessIterator <int> begin = IteratorUtil.Begin(src);
            RandomAccessIterator <int> end   = IteratorUtil.End(src);

            RandomAccessIterator <int> result = Algorithm.Remove(begin, end, 29, EqualityComparer <int> .Default);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, begin.Position);
            Assert.AreEqual(src.Length, end.Position);
            Assert.AreEqual(src.Length - 4, result.Position);
        }
Ejemplo n.º 29
0
        [Test] public void Remove_ListIterator()
        {
            ListIterator <int> begin = IteratorUtil.Begin(src);
            ListIterator <int> end   = IteratorUtil.End(src);

            ListIterator <int> result = Algorithm.Remove(begin, end, 29);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, begin.Position);
            Assert.AreEqual(src.Length, end.Position);
            Assert.AreEqual(src.Length - 4, result.Position);
        }
Ejemplo n.º 30
0
        [Test] public void Merge_Functor()
        {
            int[] output = new int[array1.Length + array2.Length];
            Assert.AreEqual(output.Length, expected.Length, "Bug in test code. expected array not the correct length");

            Algorithm.Merge <int>(IteratorUtil.Begin(array1), IteratorUtil.End(array1), IteratorUtil.Begin(array2), IteratorUtil.End(array2),
                                  IteratorUtil.Begin(output), Functional.Compare);
            for (int i = 0; i < output.Length; ++i)
            {
                Assert.AreEqual(expected[i], output[i], string.Format("Failure comparing index {0} : {1} != {2}", i, output[i], expected[i]));
            }
        }