Exemplo n.º 1
0
	def test_StackLinked(self):
		nodestack = stacks.StackLinked(3) #Create Stacked Linked List
		with self.assertRaises(IndexError): #Tests Error, cannot pop an empty stack
			nodestack.pop()
		self.assertEqual(nodestack.is_empty(), True) #Tests if the stack is empty
		self.assertEqual(nodestack.size(), 0) #Tests if the size of the stack is 0
		nodestack.push(12) #Push first value to the stack
		self.assertEqual(nodestack.is_empty(), False) #Tests when something is pushed if the stack is empty
		self.assertEqual(nodestack.size(), 1)#Tests size function
		self.assertEqual(nodestack.peek(), 12)#Tests peek function
		self.assertEqual(nodestack.pop(), 12) #Tests pop function
		self.assertEqual(nodestack.peek(),None)#Tests peek function and returns None when there is nothing to peek
		self.assertEqual(nodestack.is_full(), False) #Tests if the Stack is full
		nodestack.push(15)
		nodestack.push(17)
		self.assertEqual(nodestack.size(), 2) #Tests the size after pushing 2 items
		self.assertEqual(nodestack.peek(),17) #Tests peek function
		self.assertEqual(nodestack.pop(),17) #Tests pop function 
		self.assertEqual(nodestack.peek(),15)#Tests peek to see if once the top is redirected if it returns the new top
		nodestack.push(12)
		nodestack.push(21)
		self.assertEqual(nodestack.size(), 3)
		self.assertEqual(nodestack.is_full(), True)#Tests when the Stack is full 
		with self.assertRaises(IndexError): #Tests pushing additional item after it is full
			nodestack.push(12)
Exemplo n.º 2
0
 def test_linked_is_empty_2(self):
     stack = stacks.StackLinked(10)
     stack.push(2)
     self.assertFalse(stack.is_empty())
Exemplo n.º 3
0
import unittest
import stacks
"""Tests all of the functions for both the StackArray class and the StackLinked class. In the beginning I create test arrays and
and test linked lists that will demonstrate the working functions. Both of the implementations take in the exact same parameters
so there are no differences when initializing the classes. They work as if they were the same implementations
"""

#test arrays and linked lists
#empty stacks
array1 = stacks.StackArray(10)
linked1 = stacks.StackLinked(10)

#full stacks
array2 = stacks.StackArray(10)
for i in range(10):
    array2.items[i] = i

linked2 = stacks.StackLinked(10)
for i in range(10):
    stacks.StackLinked.push(linked2, i)
    #print(linked2.items)

#array test stacks
array3 = stacks.StackArray(10)
for i in range(5):
    array3.items[i] = i
array3pushed = stacks.StackArray(10)
for i in range(6):
    array3pushed.items[i] = i

array4 = stacks.StackArray(10)
Exemplo n.º 4
0
 def test_is_full_2(self):
     stack = stacks.StackLinked(10)  #Creates an empty stack
     for i in range(10):
         stack.push(i)  #Puts 10 values in the stack
     self.assertTrue(stack.is_full())
Exemplo n.º 5
0
 def test_linked_is_empty_1(self):
     stack = stacks.StackLinked(10)
     self.assertTrue(stack.is_empty())
Exemplo n.º 6
0
 def test_linked_size_1(self):
     stack = stacks.StackLinked(10)
     for i in range(5):
         stack.push(i)
     self.assertEqual(stack.size(), 5)
Exemplo n.º 7
0
 def test_linked_size_1(self):
     stack = stacks.StackLinked(10)
     self.assertEqual(stack.size(), 0)
Exemplo n.º 8
0
 def test_linked_peek_1(self):
     stack = stacks.StackLinked(10)
     stack.push(45)
     self.assertEqual(stack.peek(), 45)
     self.assertEqual(stack.size(), 1)
Exemplo n.º 9
0
 def test_linked_peek_2(self):
     stack = stacks.StackLinked(10)
     with self.assertRaises(IndexError):
         stack.peek()
Exemplo n.º 10
0
 def test_linked_pop_2(self):
     stack = stacks.StackLinked(10)
     stack.push(25)
     self.assertEqual(stack.pop(), 25)
     self.assertTrue(stack.is_empty())
Exemplo n.º 11
0
 def test_linked_pop_3(self):
     stack = stacks.StackLinked(10)
     with self.assertRaises(IndexError):
         stack.pop()
Exemplo n.º 12
0
 def test_linked_pop_1(self):
     stack = stacks.StackLinked(10)
     stack.push(25)
     stack.push(45)
     self.assertEqual(stack.pop(), 45)
     self.assertEqual(stack.pop(), 25)
Exemplo n.º 13
0
 def test_linked_push_2(self):
     stack = stacks.StackLinked(10)
     for i in range(10):
         stack.push(i)
     with self.assertRaises(IndexError):
         stack.push(10)
Exemplo n.º 14
0
 def test_linked_push_1(self):
     stack = stacks.StackLinked(10)
     stack.push(25)
     self.assertEqual(stack.size(), 1)
Exemplo n.º 15
0
 def test_linked_is_full_2(self):
     stack = stacks.StackLinked(10)
     for i in range(10):
         stack.push(i)
     self.assertTrue(stack.is_full())
Exemplo n.º 16
0
 def test_linked_is_full_1(self):
     stack = stacks.StackLinked(10)
     self.assertFalse(stack.is_full())