public void IndexArray()
 {
     for (int count = 0; count < 10; count++)
     {
         int[] list = CollectionsUtility.IndexArray(count);
         for (int i = 0; i < list.Length; i++)
         {
             Assert.AreEqual(i, list[i]);
         }
     }
 }
        public void Randomize()
        {
            for (int count = 0; count < 10; count++)
            {
                int[] list = CollectionsUtility.IndexArray(count);
                CollectionsUtility.Randomize(list);

                // check that all elements still occur exactly once
                bool[] found = new bool[count];
                for (int i = 0; i < list.Length; i++)
                {
                    Assert.IsFalse(found[list[i]]);
                    found[list[i]] = true;
                }
                for (int i = 0; i < list.Length; i++)
                {
                    Assert.IsTrue(found[list[i]]);
                }
            }
        }
Beispiel #3
0
        private void CollectionTest()
        {
            Stopwatch timer     = new Stopwatch();
            var       testCases = _collectionTestCases;

            Output(String.Format("{0,8}", " "));
            foreach (TestCase test in testCases)
            {
                Output(String.Format("{0,14}", test.Name));
            }
            Output("\n");

            // count units of size x operation in milliseconds,
            // rather than individual operations in microseconds
            const int outerLoop = 100, size = 200000;
            const int iterations = outerLoop * 1000;

            long[] addTicks = new long[testCases.Length],
            iterateTicks = new long[testCases.Length],
            searchTicks  = new long[testCases.Length],
            removeTicks  = new long[testCases.Length];

            // generate random permutation of items
            int[] array = CollectionsUtility.IndexArray(size);
            CollectionsUtility.Randomize(array);

            // trigger JIT compilation
            foreach (TestCase test in testCases)
            {
                test.Collection.Clear();
                foreach (int item in array)
                {
                    test.Collection.Add(item);
                }
            }

            for (int i = 0; i < outerLoop; i++)
            {
                for (int j = 0; j < testCases.Length; j++)
                {
                    TestCase test = testCases[j];
                    test.Collection.Clear();

                    timer.Restart();
                    foreach (int item in array)
                    {
                        test.Collection.Add(item);
                    }
                    timer.Stop();
                    addTicks[j] += timer.ElapsedTicks;

                    int sum = 0;
                    timer.Restart();
                    foreach (int item in test.Collection)
                    {
                        unchecked { sum += item; }
                    }
                    timer.Stop();
                    iterateTicks[j] += timer.ElapsedTicks;

                    int key = MersenneTwister.Default.Next(size - 1);
                    timer.Restart();
                    for (int k = 0; k < size; k++)
                    {
                        test.Collection.Contains((key + k) % size);
                    }
                    timer.Stop();
                    searchTicks[j] += timer.ElapsedTicks;

                    timer.Restart();
                    foreach (int item in array)
                    {
                        test.Collection.Remove(item);
                    }
                    timer.Stop();
                    removeTicks[j] += timer.ElapsedTicks;
                }
            }

            Output(String.Format("{0,8}", "Add"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(addTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Iterate"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(iterateTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Search"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(searchTicks[i], iterations)));
            }

            Output(String.Format("\n{0,8}", "Remove"));
            for (int i = 0; i < testCases.Length; i++)
            {
                Output(String.Format("{0,14:N2}", AverageMicrosecs(removeTicks[i], iterations)));
            }

            Output(String.Format(
                       "\n\nTimes are msec averages for {0:N0} integer items in random order.\n", size));
        }