示例#1
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]);
            }
        }
示例#2
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);
        }
示例#3
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)); });
        }
示例#4
0
        [Test] public void Rotate_Iterator()
        {
            int[] array = new int[12];
            array[0] = Int32.MinValue;
            array[array.Length - 1] = Int32.MinValue;
            for (int i = 1; i <= 10; ++i)
            {
                array[i] = i;
            }

            ListIterator <int> begin = new ListIterator <int>(array, 1);
            ListIterator <int> end   = IteratorUtil.End(array);

            end.MovePrev();
            ListIterator <int> newBeginIter = new ListIterator <int>(array, 4);

            Algorithm.Rotate(begin, newBeginIter, end);
            Assert.AreEqual(Int32.MinValue, array[0]);
            Assert.AreEqual(Int32.MinValue, array[array.Length - 1]);
            for (int i = 1, c = 4; i < 8; ++i, ++c)
            {
                Assert.AreEqual(c, array[i]);
            }

            for (int i = 8, c = 1; i < 11; ++i, ++c)
            {
                Assert.AreEqual(c, array[i]);
            }
        }
示例#5
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]);
            }
        }
示例#6
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);
        }
示例#7
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]);
     }
 }
示例#8
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]);
     }
 }
示例#9
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);
        }
示例#10
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);
        }
示例#11
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));
        }
示例#12
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));
        }
示例#13
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));
        }
示例#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));
        }
示例#15
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]);
     }
 }
示例#16
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));
        }
示例#17
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));
        }
示例#18
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();
        }
示例#19
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();
        }
示例#20
0
 [SetUp] public void SetUp()
 {
     array1 = new int[] { 1, 2, 3, 4, 5 };
     array2 = new int[] { 1, 2, 3, 4, 5 };
     array3 = new int[] { 1, 2, 3, 4, 5, 1 };
     begin1 = IteratorUtil.Begin(array1);
     end1   = IteratorUtil.End(array1);
     begin2 = IteratorUtil.Begin(array2);
     end2   = IteratorUtil.End(array2);
     begin3 = IteratorUtil.Begin(array3);
     end3   = IteratorUtil.End(array3);
 }
示例#21
0
        [Test] public void Count_Iterator()
        {
            int[]    intArray    = Constants.TEST_INT_ARRAY;
            string[] stringArray = Constants.TEST_STRING_ARRAY;

            int count = Algorithm.Count(IteratorUtil.Begin(intArray), IteratorUtil.End(intArray), 29);

            Assert.AreEqual(4, count);

            count = Algorithm.Count(IteratorUtil.Begin(stringArray), IteratorUtil.End(stringArray), "Frank");
            Assert.AreEqual(3, count);
        }
示例#22
0
文件: CopyTest.cs 项目: ggeurts/nhive
        [Test] public void Copy_IteratorIntToList()
        {
            // test using simple int arrays
            int[] src  = Constants.TEST_INT_ARRAY;
            int[] dest = new int[src.Length + 2];

            Algorithm.Copy(IteratorUtil.Begin(src), IteratorUtil.End(src), dest);
            for (int i = 0; i < src.Length; ++i)
            {
                Assert.AreEqual(dest[i], src[i]);
            }
        }
示例#23
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));
        }
示例#24
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);
        }
示例#25
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);
        }
示例#26
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]));
            }
        }
示例#27
0
        public void TestConstructors()
        {
            Vector<int> v = new Vector<int>();
            Assert.AreEqual(0, v.Count);
            Assert.AreEqual(0, v.Capacity);
            Assert.IsTrue(v.Begin().Equals(v.End()));
            Assert.AreEqual(0, IteratorUtil.Distance(v.Begin(), v.End()));

            int[] src = new int[]{1,2,3,4,5};
            v = new Vector<int>(src);
            Assert.AreEqual(src.Length, v.Count);
            Assert.IsFalse(v.Begin().Equals(v.End()));
            Assert.AreEqual(src.Length, IteratorUtil.Distance(v.Begin(), v.End()));
            for(int i=0; i<src.Length; ++i)
            {
            Assert.AreEqual(src[i], v[i]);
            }

            v = new Vector<int>(IteratorUtil.Begin(src), IteratorUtil.End(src));
            Assert.AreEqual(src.Length, v.Count);
            Assert.IsFalse(v.Begin().Equals(v.End()));
            Assert.AreEqual(src.Length, IteratorUtil.Distance(v.Begin(), v.End()));
            for(int i=0; i<src.Length; ++i)
            {
            Assert.AreEqual(src[i], v[i]);
            }

            v = new Vector<int>(v);
            Assert.AreEqual(src.Length, v.Count);
            Assert.IsFalse(v.Begin().Equals(v.End()));
            Assert.AreEqual(src.Length, IteratorUtil.Distance(v.Begin(), v.End()));
            for(int i=0; i<src.Length; ++i)
            {
            Assert.AreEqual(src[i], v[i]);
            }

            v = new Vector<int>(10);
            Assert.AreEqual(10, v.Count);
            Assert.IsFalse(v.Begin().Equals(v.End()));
            Assert.AreEqual(10, IteratorUtil.Distance(v.Begin(), v.End()));
            Algorithm.ForEach(v.Begin(), v.End(), delegate(int x) {
            Assert.AreEqual(0, x);
            });

            v = new Vector<int>(10, 42);
            Assert.AreEqual(10, v.Count);
            Assert.IsFalse(v.Begin().Equals(v.End()));
            Assert.AreEqual(10, IteratorUtil.Distance(v.Begin(), v.End()));
            Algorithm.ForEach(v.Begin(), v.End(), delegate(int x) {
            Assert.AreEqual(42, x);
            });
        }
示例#28
0
        [Test] public void Transform_Binary()
        {
            ListIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();
            Algorithm.Transform(IteratorUtil.Begin(src), IteratorUtil.End(src), IteratorUtil.Begin(src), destIter, AddEm);
            Assert.AreEqual(dest[0], Int32.MinValue);
            Assert.AreEqual(dest[dest.Length - 1], Int32.MinValue);
            for (int i = 0; i < count; ++i)
            {
                Assert.AreEqual(src[i] * 2, dest[i + 1]);
            }
        }
示例#29
0
        [Test] public void ReverseCopy_Iterator()
        {
            ListIterator <int> destIter = IteratorUtil.Begin(dest);

            destIter.MoveNext();

            Algorithm.ReverseCopy(IteratorUtil.Begin(src), IteratorUtil.End(src), destIter);
            Assert.AreEqual(dest[0], Int32.MinValue);
            Assert.AreEqual(dest[dest.Length - 1], Int32.MinValue);
            for (int i = 0; i < count; ++i)
            {
                Assert.AreEqual(src[i], dest[dest.Length - 2 - i]);
            }
        }
示例#30
0
        [Test] public void Accumulate_Iterator()
        {
            int[] array = Constants.TEST_INT_ARRAY;
            int   sum   = Algorithm.Accumulate(IteratorUtil.Begin(array), IteratorUtil.End(array), 0, Add);

            int expectedTotal = 0;

            foreach (int value in array)
            {
                expectedTotal += value;
            }

            Assert.AreEqual(expectedTotal, sum);
        }