Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for queue and stack updated #163

Merged
merged 11 commits into from
Mar 17, 2020
13 changes: 7 additions & 6 deletions pydatastructs/miscellaneous_data_structures/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from copy import deepcopy as dc

__all__ = [
'Stack'
'Stack',
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
]

class Stack(object):
Expand Down 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])
14 changes: 11 additions & 3 deletions pydatastructs/miscellaneous_data_structures/tests/test_queue.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from pydatastructs.miscellaneous_data_structures import Queue
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 type(q) is type(q1)
q2 = Queue(implementation='linked_list')
assert _check_type(q2,Queue) is True
assert raises(NotImplementedError, lambda: Queue(implementation=''))
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved

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 +26,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
24 changes: 15 additions & 9 deletions pydatastructs/miscellaneous_data_structures/tests/test_stack.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
from pydatastructs.miscellaneous_data_structures import Stack
from pydatastructs.linear_data_structures import OneDimensionalArray
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 type(s) is type(s1)
s2 = Stack(implementation='linked_list')
assert _check_type(s2,Stack) is True
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
assert raises(NotImplementedError, lambda: Stack(implementation=''))
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved

s = Stack()
def test_ArrayStack():
s = Stack(implementation='array')
s.push(1)
s.push(2)
s.push(3)
Expand All @@ -18,30 +26,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}))