Esempio n. 1
0
        /// <summary>
        /// A generic test, regardless of the item type. Works by the following steps:
        /// 1. Adding the items in the list to the collection (show times if required)
        /// 2. Removing all items and test correctness by verifying that every item removed is smaller or
        /// equals to the one that was removed before it.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="col">The collection to test</param>
        /// <param name="list">A list of items of corresponded type</param>
        /// <param name="timeTrace">Determines whether to display the time elapsed for every operation</param>
        private void TestCollection <T>(IBaseCollection <T> col, List <T> list, bool timeTrace = true) where T : IComparable <T>
        {
            Stopwatch sw = new Stopwatch();

            foreach (T item in list)
            {
                sw.Start();
                col.Add(item);
                sw.Stop();
                if (timeTrace)
                {
                    _output.WriteLine($"Add time: {sw.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)}ms");
                }
            }

            sw.Start();
            T item1 = col.Remove();

            sw.Stop();
            if (timeTrace)
            {
                _output.WriteLine($"Remove time: {sw.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)}ms");
            }

            while (col.Count > 0)
            {
                sw.Start();
                T item2 = col.Remove();
                sw.Stop();
                if (timeTrace)
                {
                    _output.WriteLine($"Remove time: {sw.Elapsed.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)}ms");
                }

                // TEST CORRECTNESS
                // Verify that the previous item removed is not smaller than the current one.
                // If the test fails we display the item values and the current state.
                Assert.True(item1.CompareTo(item2) >= 0, $"{item1}, {item2}: cmp={item1.CompareTo(item2)} ({col.GetType().Name}, {col.Count} items left)");
                item1 = item2;
            }
        }