コード例 #1
0
        public void TestDoublyLinkedList()
        {
            int retVal;
            DoublyLinkedList dll = new DoublyLinkedList(5);

            // Push()
            dll.DeleteAt(0);
            dll.Push(1);    // LL is empty
            Assert.AreEqual(new List<int>() { 1 }, dll.ReturnList());
            dll.Push(2);    // LL.count == 1
            Assert.AreEqual(new List<int>() { 2, 1 }, dll.ReturnList());
            dll.Push(3);    // LL.count > 1
            Assert.AreEqual(new List<int>() { 3, 2, 1 }, dll.ReturnList());

            // Delete()
            dll.InitializeDLL();
            retVal = dll.Delete(111); // doesn't exist
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1,0 }, dll.ReturnList());
            Assert.AreEqual(-1, retVal);
            retVal = dll.Delete(0); // exists at tail
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(0, retVal);
            retVal = dll.Delete(5); // exists at head
            Assert.AreEqual(new List<int>() { 4, 3, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(5, retVal);
            retVal = dll.Delete(3); // exists between head and tail
            Assert.AreEqual(new List<int>() { 4, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(3, retVal);

            // DeleteAt()
            dll.InitializeDLL();
            retVal = dll.DeleteAt(-23); // invalid index
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1, 0 }, dll.ReturnList());
            Assert.AreEqual(-1, retVal);
            retVal = dll.DeleteAt(7); // invalid index
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1, 0 }, dll.ReturnList());
            Assert.AreEqual(-1, retVal);
            retVal = dll.DeleteAt(0); // exists at head
            Assert.AreEqual(new List<int>() { 4, 3, 2, 1, 0 }, dll.ReturnList());
            Assert.AreEqual(5, retVal);
            retVal = dll.DeleteAt(4); // exists at tail
            Assert.AreEqual(new List<int>() { 4, 3, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(0, retVal);
            retVal = dll.DeleteAt(1); // exists between head and tail
            Assert.AreEqual(new List<int>() { 4, 2, 1 }, dll.ReturnList());
            Assert.AreEqual(3, retVal);

            // Search()
            dll.InitializeDLL();
            Assert.AreEqual(-1, dll.Search(111)); // doesn't exist
            Assert.AreEqual(5, dll.Search(5)); // exists at head
            Assert.AreEqual(0, dll.Search(0)); // exists at tail
            Assert.AreEqual(3, dll.Search(3)); // exists in between head and tail
            Assert.AreEqual(new List<int>() { 5, 4, 3, 2, 1, 0 }, dll.ReturnList());

            // Append()
            dll = new DoublyLinkedList(0);
            dll.DeleteAt(0);
            dll.Append(0); // LL is empty
            Assert.AreEqual(new List<int>() { 0 }, dll.ReturnList());
            dll.Append(1); // LL.count = 1
            Assert.AreEqual(new List<int>() { 0, 1 }, dll.ReturnList());
            dll.Append(2);
            Assert.AreEqual(new List<int>() { 0, 1, 2 }, dll.ReturnList());
            dll.Append(3); // LL.count > 1
            Assert.AreEqual(new List<int>() { 0, 1, 2, 3 }, dll.ReturnList());

            // Pop()
            dll = new DoublyLinkedList(0);
            dll.Append(1);
            dll.Append(2);
            Assert.AreEqual(2, dll.Pop()); // LL.count > 1;
            Assert.AreEqual(new List<int>() { 0, 1 }, dll.ReturnList());
            Assert.AreEqual(1, dll.Pop()); // LL.count > 1;
            Assert.AreEqual(new List<int>() { 0 }, dll.ReturnList());
            Assert.AreEqual(0, dll.Pop()); // LL.count == 1;
            Assert.AreEqual(new List<int>(), dll.ReturnList());
            Boolean isException = false;
            try
            {
                dll.Pop();
            }
            catch
            {
                isException = true;
            }
            Assert.IsTrue(isException);
        }