Skip to content

Commit

Permalink
Tests for queue and stack updated (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarsheetKakar authored Mar 17, 2020
1 parent 34a2eaf commit fbbd987
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
11 changes: 6 additions & 5 deletions pydatastructs/miscellaneous_data_structures/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ def is_empty(self):

@property
def peek(self):
return self.top.data

@property
def top(self):
return self.stack.head

@property
Expand All @@ -164,4 +160,9 @@ def __len__(self):
return self.stack.size

def __str__(self):
return str(self.stack)
elements = []
current_node = self.peek
while current_node is not None:
elements.append(current_node.data)
current_node = current_node.next
return str(elements[::-1])
16 changes: 13 additions & 3 deletions pydatastructs/miscellaneous_data_structures/tests/test_queue.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
from pydatastructs.miscellaneous_data_structures import Queue
from pydatastructs.miscellaneous_data_structures.queue import ArrayQueue, LinkedListQueue
from pydatastructs.utils.raises_util import raises
from pydatastructs.utils.misc_util import _check_type

def test_Queue():
q = Queue(implementation='array')
q1 = Queue()
assert _check_type(q, ArrayQueue) is True
assert _check_type(q1, ArrayQueue) is True
q2 = Queue(implementation='linked_list')
assert _check_type(q2, LinkedListQueue) is True
assert raises(NotImplementedError, lambda: Queue(implementation=''))

def test_ArrayQueue():
q1 = Queue()
raises(IndexError, lambda: q1.popleft())
q1 = Queue(implementation='array', items=[0])
q1.append(1)
q1.append(2)
Expand All @@ -16,9 +28,7 @@ def test_Queue():
assert q1.popleft() == 3
assert len(q1) == 0

q1 = Queue()
raises(IndexError, lambda: q1.popleft())

def test_LinkedListQueue():
q1 = Queue(implementation='linked_list')
q1.append(1)
assert raises(TypeError, lambda: Queue(implementation='linked_list', items={0, 1}))
Expand Down
27 changes: 17 additions & 10 deletions pydatastructs/miscellaneous_data_structures/tests/test_stack.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
from pydatastructs.miscellaneous_data_structures import Stack
from pydatastructs.linear_data_structures import OneDimensionalArray
from pydatastructs.miscellaneous_data_structures.stack import ArrayStack, LinkedListStack
from pydatastructs.utils.raises_util import raises
from pydatastructs.utils.misc_util import _check_type

def test_Stack():
s = Stack(implementation='array')
s1 = Stack()
assert _check_type(s, ArrayStack) is True
assert _check_type(s1, ArrayStack) is True
s2 = Stack(implementation='linked_list')
assert _check_type(s2, LinkedListStack) is True
assert raises(NotImplementedError, lambda: Stack(implementation=''))

s = Stack()
def test_ArrayStack():
s = Stack(implementation='array')
s.push(1)
s.push(2)
s.push(3)
Expand All @@ -18,30 +27,28 @@ def test_Stack():
_s = Stack(items=[1, 2, 3])
assert str(_s) == '[1, 2, 3]'
assert len(_s) == 3
assert raises(NotImplementedError, lambda: Stack(implementation=''))

def test_LinkedListStack():
s = Stack(implementation='linked_list')
s.push(1)
s.push(2)
s.push(3)
assert s.peek == 3
assert str(s) == '[3, 2, 1]'
assert s.peek.data == 3
assert str(s) == '[1, 2, 3]'
assert s.pop().data == 3
assert s.pop().data == 2
assert s.pop().data == 1
assert s.is_empty is True
assert raises(IndexError, lambda : s.pop())
assert str(s) == '[]'
_s = Stack(implementation='linked_list',items=[1, 2, 3])
assert str(_s) == '[3, 2, 1]'
assert str(_s) == '[1, 2, 3]'
assert len(_s) == 3

s = Stack(implementation='linked_list',items=['a',None,type,{}])
assert len(s) == 4
assert s.size == 4

top = s.top
assert top.data == s.pop().data

peek = s.peek
assert peek == s.pop().data
assert peek.data == s.pop().data
assert raises(TypeError, lambda: Stack(implementation='linked_list', items={0, 1}))

0 comments on commit fbbd987

Please sign in to comment.