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

📄 dequetests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 3 页
字号:

        [Test]
        public void IndexerExceptions()
        {
            string s = "foo";
            Deque<string> d = new Deque<string>();

            d.AddToFront("c");
            d.AddToFront("b");
            d.AddToFront("a");
            d.AddToBack("d");
            d.AddToBack("e");
            d.AddToBack("f");

            try {
                s = d[-1];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                s = d[6];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                s = d[int.MaxValue];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                s = d[int.MinValue];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[-1] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[6] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[int.MaxValue] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[int.MinValue] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            d.Clear();
            d.AddToBack("a");
            d.AddToBack("b");
            d.AddToBack("c");
            d.AddToBack("d");

            try {
                s = d[-1];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                s = d[4];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                s = d[int.MaxValue];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                s = d[int.MinValue];
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[-1] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[4] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[int.MaxValue] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                d[int.MinValue] = s;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
        }

        [Test]
        public void EmptyExceptions()
        {
            Deque<double> d = new Deque<double>();

            try {
                d.GetAtFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.GetAtBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            d.AddToBack(2.3);
            d.RemoveFromFront();

            try {
                d.GetAtFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.GetAtBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromFront();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                d.RemoveFromBack();
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

        }

        [Test]
        public void AddMany()
        {
            Deque<string> deque1 = new Deque<string>(new string[] { "A", "B", "C", "D" });
            deque1.AddManyToFront(new string[] { "Q", "R", "S" });
            deque1.AddManyToBack(new string[] { "L", "M", "N", "O" });
            InterfaceTests.TestReadWriteListGeneric(deque1, new string[] { "Q", "R", "S", "A", "B", "C", "D", "L", "M", "N", "O" });
        }

        [Test]
        public void FailFastEnumerator()
        {
            Deque<string> deque1 = new Deque<string>(new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" });
            int i = 0;

            try {
                foreach (string s in deque1) {
                    ++i;
                    Assert.IsTrue(i < 4);
                    if (i == 3)
                        deque1.AddToBack("hi");
                }
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            i = 0;
            try {
                foreach (string s in deque1) {
                    ++i;
                    Assert.IsTrue(i < 4);
                    if (i == 3)
                        deque1.AddToFront("hi");
                }
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            i = 0;
            try {
                foreach (string s in deque1) {
                    ++i;
                    Assert.IsTrue(i < 4);
                    if (i == 3)
                        deque1.RemoveRange(2, 4);
                }
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            i = 0;
            try {
                foreach (string s in deque1) {
                    ++i;
                    Assert.IsTrue(i < 4);
                    if (i == 3)
                        deque1[5] = "hi";
                }
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            i = 0;
            try {
                foreach (string s in deque1) {
                    ++i;
                    Assert.IsTrue(i < 4);
                    if (i == 3)
                        deque1.Clear();
                }
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }
        }

        [Test]
        public void Initialize()
        {
            Deque<string> deque1 = new Deque<string>(new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" });
            InterfaceTests.TestReadWriteListGeneric(deque1, new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" });
            Deque<string> deque2 = new Deque<string>(new string[] {});
            InterfaceTests.TestReadWriteListGeneric(deque2, new string[] {});
            Deque<string> deque3 = new Deque<string>();
            InterfaceTests.TestReadWriteListGeneric(deque3, new string[] {});
        }

        [Test]
        public void Capacity()
        {
            Deque<int> deque1 = new Deque<int>();

            Assert.AreEqual(0, deque1.Capacity);
            deque1.Add(4);
            Assert.AreEqual(7, deque1.Capacity);
            for (int i = 0; i < 100; ++i)
                deque1.Add(i);
            Assert.AreEqual(127, deque1.Capacity);

            deque1.Clear();
            Assert.AreEqual(0, deque1.Capacity);
            deque1.Capacity = 4;
            Assert.AreEqual(4, deque1.Capacity);
            for (int i = 0; i < 12; ++i)
                deque1.Add(i);
            Assert.AreEqual(19, deque1.Capacity);
            deque1.Capacity = 12;
            Assert.AreEqual(deque1.Capacity, 12);
            InterfaceTests.TestReadWriteListGeneric(deque1, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });

            deque1.Clear();
            for (int i = 0; i < 12; ++i)
                deque1.Add(i);
            try {
                deque1.Capacity = 11;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                deque1.Capacity = -1;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
            try {
                deque1.Capacity = int.MaxValue;
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
        }

        [Test]
        public void TrimToSize()
        {
            Deque<int> deque1 = new Deque<int>();
            deque1.TrimToSize();
            Assert.AreEqual(deque1.Count, deque1.Capacity);

            for (int i = 0; i < 12; ++i)
                deque1.Add(i);

            deque1.TrimToSize();
            Assert.AreEqual(deque1.Count, deque1.Capacity);
            InterfaceTests.TestReadWriteListGeneric(deque1, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
        }

        [Test]
        public void RemoveRangeExceptions()
        {
            Deque<int> deque1 = new Deque<int>();
            for (int i = 0; i < 100; ++i)
                deque1.AddToBack(i);

            try {
                deque1.RemoveRange(3, 98);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
                Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
            }

            try {
                deque1.RemoveRange(-1, 1);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
                Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
            }

            try {
                deque1.RemoveRange(0, int.MaxValue);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
                Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
            }

⌨️ 快捷键说明

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