⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interfacetests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 5 页
字号:
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                object dummy = coll[int.MaxValue];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            // Check bad type.
            if (typeof(T) != typeof(object)) {
                int index = coll.IndexOf(new object());
                Assert.AreEqual(-1, index);

                bool b = coll.Contains(new object());
                Assert.IsFalse(b);
            }
        }

        /// <summary>
        ///  Test a read-write IList&lt;T&gt; that should contain the given values, possibly in order. Destroys the collection in the process.
        /// </summary>
        /// <param name="coll">IList&lt;T&gt; to test. </param>
        /// <param name="valueArray">The values that should be in the list.</param>
        public static void TestReadWriteListGeneric<T>(IList<T> coll, T[] valueArray)
        {
            TestReadWriteListGeneric<T>(coll, valueArray, null);
        }

        public static void TestReadWriteListGeneric<T>(IList<T> coll, T[] valueArray, BinaryPredicate<T> equals)
        {
            if (equals == null)
                equals = delegate(T x, T y) { return object.Equals(x, y); };

            TestListGeneric(coll, valueArray, equals);     // Basic read-only list stuff.

            // Check the indexer getter.
            T[] save = new T[coll.Count];
            for (int i = coll.Count - 1; i >= 0; --i) {
                Assert.AreEqual(valueArray[i], coll[i]);
                int index = coll.IndexOf(valueArray[i]);
                Assert.IsTrue(index >= 0);
                Assert.IsTrue(index == i || (index < i && object.Equals(coll[index], valueArray[i])));
                save[i] = coll[i];
            }

            // Check the setter by reversing the list.
            for (int i = 0; i < coll.Count / 2; ++i) {
                T temp = coll[i];
                coll[i] = coll[coll.Count - 1 - i];
                coll[coll.Count - 1 - i] = temp;
            }

            for (int i = 0; i < coll.Count; ++i ) {
                Assert.AreEqual(valueArray[coll.Count - 1 - i], coll[i]);
                int index = coll.IndexOf(valueArray[coll.Count - 1 - i]);
                Assert.IsTrue(index >= 0);
                Assert.IsTrue(index == i || (index < i && object.Equals(coll[index], valueArray[coll.Count - 1 - i])));
            }

            // Reverse back
            for (int i = 0; i < coll.Count / 2; ++i) {
                T temp = coll[i];
                coll[i] = coll[coll.Count - 1 - i];
                coll[coll.Count - 1 - i] = temp;
            }

            T item = valueArray.Length > 0 ? valueArray[valueArray.Length / 2] : default(T);
            // Check exceptions from index out of range.
            try {
                coll[-1] = item;
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll[int.MinValue] = item;
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                T dummy = coll[-2];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll[coll.Count] = item;
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                T dummy = coll[coll.Count];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                T dummy = coll[int.MaxValue];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll[int.MaxValue] = item;
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll.Insert(-1, item);
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll.Insert(coll.Count + 1, item);
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll.Insert(int.MaxValue, item);
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll.RemoveAt(coll.Count);
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll.RemoveAt(-1);
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll.RemoveAt(int.MaxValue);
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll.RemoveAt(coll.Count);
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            // Insert at the beginning.
            coll.Insert(0, item);
            Assert.AreEqual(coll[0], item);
            Assert.AreEqual(valueArray.Length + 1, coll.Count);
            for (int i = 0; i < valueArray.Length; ++i)
                Assert.AreEqual(valueArray[i], coll[i + 1]);

            // Insert at the end
            coll.Insert(valueArray.Length + 1, item);
            Assert.AreEqual(coll[valueArray.Length + 1], item);
            Assert.AreEqual(valueArray.Length + 2, coll.Count);
            for (int i = 0; i < valueArray.Length; ++i)
                Assert.AreEqual(valueArray[i], coll[i + 1]);

            // Delete at the beginning.
            coll.RemoveAt(0);
            Assert.AreEqual(coll[valueArray.Length], item);
            Assert.AreEqual(valueArray.Length + 1, coll.Count);
            for (int i = 0; i < valueArray.Length; ++i)
                Assert.AreEqual(valueArray[i], coll[i]);

            // Delete at the end.
            coll.RemoveAt(valueArray.Length);
            Assert.AreEqual(valueArray.Length, coll.Count);
            for (int i = 0; i < valueArray.Length; ++i)
                Assert.AreEqual(valueArray[i], coll[i]);

            // Insert at the middle.
            coll.Insert(valueArray.Length / 2, item);
            Assert.AreEqual(valueArray.Length + 1, coll.Count);
            Assert.AreEqual(item, coll[valueArray.Length / 2]);
            for (int i = 0; i < valueArray.Length; ++i) {
                if (i < valueArray.Length / 2)
                    Assert.AreEqual(valueArray[i], coll[i]);
                else
                    Assert.AreEqual(valueArray[i], coll[i+1]);
            }

            // Delete at the middle.
            coll.RemoveAt(valueArray.Length / 2);
            Assert.AreEqual(valueArray.Length, coll.Count);
            for (int i = 0; i < valueArray.Length; ++i)
                Assert.AreEqual(valueArray[i], coll[i]);

            // Delete all from the middle.
            for (int i = 0; i < valueArray.Length; ++i)
                coll.RemoveAt(coll.Count / 2);
            Assert.AreEqual(0, coll.Count);

            // Build up in order.
            for (int i = 0; i < save.Length; ++i) {
                coll.Insert(i, save[i]);
            }

            TestListGeneric(coll, valueArray, equals);     // Basic read-only list stuff.

            coll.Clear();
            Assert.AreEqual(0, coll.Count);

            // Build up in reverse order.
            for (int i = 0; i < save.Length; ++i) {
                coll.Insert(0, save[save.Length - 1 - i]);
            }
            TestListGeneric(coll, valueArray, equals);     // Basic read-only list stuff.

            // Check read-write collection stuff.
            TestReadWriteCollectionGeneric<T>(coll, valueArray, true);
        }

        /// <summary>
        ///  Test a read-write non-generic IList that should contain the given values, possibly in order. Destroys the collection in the process.
        /// </summary>
        /// <param name="coll">IList to test. </param>
        /// <param name="valueArray">The values that should be in the list.</param>
        public static void TestReadWriteList<T>(IList coll, T[] valueArray)
        {
            TestList(coll, valueArray);     // Basic read-only list stuff.

            // Check read only
            Assert.IsFalse(coll.IsReadOnly);
            Assert.IsFalse(coll.IsReadOnly);

            // Check the indexer getter.
            T[] save = new T[coll.Count];
            for (int i = coll.Count - 1; i >= 0; --i) {
                Assert.AreEqual(valueArray[i], coll[i]);
                int index = coll.IndexOf(valueArray[i]);
                Assert.IsTrue(index >= 0);
                Assert.IsTrue(index == i || (index < i && object.Equals(coll[index], valueArray[i])));
                save[i] = (T) coll[i];
            }

            // Check the setter by reversing the list.
            for (int i = 0; i < coll.Count / 2; ++i) {
                T temp = (T) coll[i];
                coll[i] = coll[coll.Count - 1 - i];
                coll[coll.Count - 1 - i] = temp;
            }

            for (int i = 0; i < coll.Count; ++i ) {
                Assert.AreEqual(valueArray[coll.Count - 1 - i], coll[i]);
                int index = coll.IndexOf(valueArray[coll.Count - 1 - i]);
                Assert.IsTrue(index >= 0);
                Assert.IsTrue(index == i || (index < i && object.Equals(coll[index], valueArray[coll.Count - 1 - i])));
            }

            // Reverse back
            for (int i = 0; i < coll.Count / 2; ++i) {
                T temp = (T) coll[i];
                coll[i] = coll[coll.Count - 1 - i];
                coll[coll.Count - 1 - i] = temp;
            }

            T item = valueArray.Length > 0 ? valueArray[valueArray.Length / 2] : default(T);
            // Check exceptions from index out of range.
            try {
                coll[-1] = item;
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll[int.MinValue] = item;
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                object dummy = coll[-2];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll[coll.Count] = item;
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                object dummy = coll[coll.Count];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                object dummy = coll[int.MaxValue];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                coll[int.MaxValue] = item;
                Assert.Fail("Should throw");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -