2016-12-19 15:38:30 +00:00
|
|
|
'''
|
|
|
|
Created on Dec 19, 2016
|
|
|
|
|
|
|
|
@author: klaehn
|
|
|
|
'''
|
|
|
|
import unittest
|
|
|
|
import random
|
2016-12-20 21:09:01 +00:00
|
|
|
from data_buffer import DataBuffer
|
2016-12-19 15:38:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Test(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
def test_zero_space(self):
|
2016-12-20 21:09:01 +00:00
|
|
|
dut = DataBuffer(0)
|
2016-12-19 15:38:30 +00:00
|
|
|
result = dut.push(10)
|
|
|
|
self.assertFalse(result, "test_zero_space - exp: False res: True")
|
|
|
|
|
|
|
|
def test_max_data(self):
|
|
|
|
buffer_size = 10
|
2016-12-20 21:09:01 +00:00
|
|
|
dut = DataBuffer(buffer_size)
|
2016-12-19 15:38:30 +00:00
|
|
|
for _ in range(11):
|
|
|
|
dut.push(random.random())
|
|
|
|
result = dut.length()
|
|
|
|
self.assertEqual(result, buffer_size, "test_max_data - exp: " + \
|
|
|
|
str(buffer_size) + " res: " + str(result))
|
|
|
|
|
|
|
|
def test_average_min(self):
|
2016-12-20 21:09:01 +00:00
|
|
|
dut = DataBuffer(10)
|
2016-12-19 15:38:30 +00:00
|
|
|
rnd = random.random()
|
|
|
|
dut.push(rnd)
|
|
|
|
result = dut.average()
|
|
|
|
self.assertEqual(result, None, "test_average_min - exp: None" + \
|
|
|
|
" res: " + str(result))
|
|
|
|
|
|
|
|
def test_average_mid(self):
|
|
|
|
buffer_size = 10
|
|
|
|
res_buffer = []
|
2016-12-20 21:09:01 +00:00
|
|
|
dut = DataBuffer(buffer_size)
|
2016-12-19 15:38:30 +00:00
|
|
|
for _ in range(buffer_size / 2):
|
|
|
|
rnd = random.random()
|
|
|
|
dut.push(rnd)
|
|
|
|
res_buffer.append(rnd)
|
|
|
|
result = dut.average()
|
|
|
|
self.assertEqual(result, None, "test_average_mid - exp: None" + \
|
|
|
|
" res: " + str(result))
|
|
|
|
|
|
|
|
def test_average_max(self):
|
|
|
|
buffer_size = 10
|
|
|
|
res_buffer = []
|
2016-12-20 21:09:01 +00:00
|
|
|
dut = DataBuffer(buffer_size)
|
2016-12-19 15:38:30 +00:00
|
|
|
for _ in range(buffer_size):
|
|
|
|
rnd = random.random()
|
|
|
|
dut.push(rnd)
|
|
|
|
res_buffer.append(rnd)
|
|
|
|
result = dut.average()
|
|
|
|
self.assertEqual(result, sum(res_buffer) / buffer_size, \
|
|
|
|
"test_average_mid - exp: " + \
|
|
|
|
str(sum(res_buffer) / buffer_size) + \
|
|
|
|
" res: " + str(result))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
#import sys;sys.argv = ['', 'Test.testName']
|
|
|
|
unittest.main()
|