1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | """
Fifo class
"""
class Fifo:
def __init__(self):
""" the fifo queue """
self.qu = []
def push(self, val):
""" push onto stack """
self.qu.append(val)
def pop(self):
""" Pops the last pushed on stack """
if len(self.qu):
val = self.qu.pop()
return val
else:
return None
def shift(self):
""" removed the first from queue """
if len(self.qu):
val = self.qu[0]
self.qu.remove(val)
return val
else:
return None
def unshift(self, val):
""" add to front of queue """
self.qu.insert(0, val)
def count(self):
return len(self.qu)
def bump(nn):
""" example class function, no self """
return nn + 1
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """
pip install pytest
"""
from fifo import Fifo
def test_stack():
fifo = Fifo()
assert fifo.count() == 0
fifo.push(1)
fifo.push(2)
fifo.push(3)
fifo.push(4)
fifo.push(5)
assert fifo.count() == 5
val = fifo.pop()
assert fifo.count() == 4
assert val == 5
def test_fifo():
fifo = Fifo()
assert fifo.count() == 0
fifo.push(1)
fifo.push(2)
fifo.push(3)
fifo.push(4)
fifo.push(5)
assert fifo.count() == 5
val = fifo.shift()
assert fifo.count() == 4
assert val == 1
val = fifo.shift()
assert fifo.count() == 3
assert val == 2
def test_reverse_fifo():
fifo = Fifo()
assert fifo.count() == 0
fifo.unshift(1)
fifo.unshift(2)
fifo.unshift(3)
assert fifo.count() == 3
val = fifo.pop()
assert fifo.count() == 2
assert val == 1
val = fifo.pop()
assert fifo.count() == 1
assert val == 2
val = fifo.pop()
assert fifo.count() == 0
assert val == 3
val = fifo.pop()
assert fifo.count() == 0
assert val == None
def test_class_function():
assert Fifo.bump(2) == 3
|
1 2 3 4 5 6 7 8 9 10 | pytest test_fifo.py
=======================test session starts ===========================
platform darwin -- Python 3.6.1, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: /Users/phil/titan/python, inifile:
collected 4 items
test_fifo.py ....
=======================4 passed in 0.02 seconds ======================
|