Skip to content

Commit

Permalink
Add test_queue to tests (keon#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hai Hoang Dang authored Apr 29, 2018
1 parent 0dd1f72 commit 1768997
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 119 deletions.
23 changes: 0 additions & 23 deletions queues/max_sliding_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"""

import collections
import unittest


def max_sliding_window(arr, k):
qi = collections.deque() # queue storing indexes of elements
result = []
Expand All @@ -33,23 +30,3 @@ def max_sliding_window(arr, k):
if i >= k - 1:
result.append(arr[qi[0]])
return result


class TestSuite(unittest.TestCase):

def test_max_sliding_window(self):

array = [1, 3, -1, -3, 5, 3, 6, 7]
self.assertEqual(max_sliding_window(array, k=5), [5, 5, 6, 7])
self.assertEqual(max_sliding_window(array, k=3), [3, 3, 5, 5, 6, 7])
self.assertEqual(max_sliding_window(array, k=7), [6, 7])

array = [8, 5, 10, 7, 9, 4, 15, 12, 90, 13]
self.assertEqual(max_sliding_window(array, k=4), [10, 10, 10, 15, 15, 90, 90])
self.assertEqual(max_sliding_window(array, k=7), [15, 15, 90, 90])
self.assertEqual(max_sliding_window(array, k=2), [8, 10, 10, 9, 9, 15, 15, 90, 90])


if __name__ == '__main__':

unittest.main()
93 changes: 0 additions & 93 deletions queues/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
It needs no parameters and returns an integer.
* peek() returns the front element of the queue.
"""
import unittest
from abc import ABCMeta, abstractmethod


class AbstractQueue(metaclass=ABCMeta):

def __init__(self):
Expand Down Expand Up @@ -99,7 +96,6 @@ def __init__(self, value):
self.value = value
self.next = None


class LinkedListQueue(AbstractQueue):

def __init__(self):
Expand Down Expand Up @@ -142,92 +138,3 @@ def peek(self):
if self.is_empty():
raise IndexError("Queue is empty")
return self._front.value


# TODO
class HeapPriorityQueue(AbstractQueue):

def __init__(self):
super().__init__()
pass

def __iter__(self):
pass

def enqueue(self, value):
pass

def dequeue(self):
pass

def peek(self):
pass


class TestSuite(unittest.TestCase):
"""
Test suite for the Queue data structures.
"""

def test_ArrayQueue(self):
queue = ArrayQueue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

# test __iter__()
it = iter(queue)
self.assertEqual(1, next(it))
self.assertEqual(2, next(it))
self.assertEqual(3, next(it))
self.assertRaises(StopIteration, next, it)

# test __len__()
self.assertEqual(3, len(queue))

# test is_empty()
self.assertFalse(queue.is_empty())

# test peek()
self.assertEqual(1, queue.peek())

# test dequeue()
self.assertEqual(1, queue.dequeue())
self.assertEqual(2, queue.dequeue())
self.assertEqual(3, queue.dequeue())

self.assertTrue(queue.is_empty())

def test_LinkedListQueue(self):
queue = LinkedListQueue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

# test __iter__()
it = iter(queue)
self.assertEqual(1, next(it))
self.assertEqual(2, next(it))
self.assertEqual(3, next(it))
self.assertRaises(StopIteration, next, it)

# test __len__()
self.assertEqual(3, len(queue))

# test is_empty()
self.assertFalse(queue.is_empty())

# test peek()
self.assertEqual(1, queue.peek())

# test dequeue()
self.assertEqual(1, queue.dequeue())
self.assertEqual(2, queue.dequeue())
self.assertEqual(3, queue.dequeue())

self.assertTrue(queue.is_empty())


if __name__ == "__main__":

unittest.main()
4 changes: 1 addition & 3 deletions queues/reconstruct_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ def reconstruct_queue(people):
queue = []
people.sort(key=lambda x: (-x[0], x[1]))
for h, k in people:
queue.insert(k, (h, k))
queue.insert(k, [h, k])
return queue


90 changes: 90 additions & 0 deletions tests/test_queues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from queues.queue import ArrayQueue, LinkedListQueue
from queues.max_sliding_window import max_sliding_window
from queues.reconstruct_queue import reconstruct_queue

import unittest

class TestQueue(unittest.TestCase):
"""
Test suite for the Queue data structures.
"""

def test_ArrayQueue(self):
queue = ArrayQueue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

# test __iter__()
it = iter(queue)
self.assertEqual(1, next(it))
self.assertEqual(2, next(it))
self.assertEqual(3, next(it))
self.assertRaises(StopIteration, next, it)

# test __len__()
self.assertEqual(3, len(queue))

# test is_empty()
self.assertFalse(queue.is_empty())

# test peek()
self.assertEqual(1, queue.peek())

# test dequeue()
self.assertEqual(1, queue.dequeue())
self.assertEqual(2, queue.dequeue())
self.assertEqual(3, queue.dequeue())

self.assertTrue(queue.is_empty())

def test_LinkedListQueue(self):
queue = LinkedListQueue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

# test __iter__()
it = iter(queue)
self.assertEqual(1, next(it))
self.assertEqual(2, next(it))
self.assertEqual(3, next(it))
self.assertRaises(StopIteration, next, it)

# test __len__()
self.assertEqual(3, len(queue))

# test is_empty()
self.assertFalse(queue.is_empty())

# test peek()
self.assertEqual(1, queue.peek())

# test dequeue()
self.assertEqual(1, queue.dequeue())
self.assertEqual(2, queue.dequeue())
self.assertEqual(3, queue.dequeue())

self.assertTrue(queue.is_empty())

class TestSuite(unittest.TestCase):

def test_max_sliding_window(self):

array = [1, 3, -1, -3, 5, 3, 6, 7]
self.assertEqual(max_sliding_window(array, k=5), [5, 5, 6, 7])
self.assertEqual(max_sliding_window(array, k=3), [3, 3, 5, 5, 6, 7])
self.assertEqual(max_sliding_window(array, k=7), [6, 7])

array = [8, 5, 10, 7, 9, 4, 15, 12, 90, 13]
self.assertEqual(max_sliding_window(array, k=4), [10, 10, 10, 15, 15, 90, 90])
self.assertEqual(max_sliding_window(array, k=7), [15, 15, 90, 90])
self.assertEqual(max_sliding_window(array, k=2), [8, 10, 10, 9, 9, 15, 15, 90, 90])

def test_reconstruct_queue(self):
self.assertEqual([[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]],
reconstruct_queue([[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]))

if __name__ == "__main__":

unittest.main()

0 comments on commit 1768997

Please sign in to comment.