def add(self, value): """Adds value to buffer, overwrite as needed.""" if self.isFull(): self.total -= self.buffer[self.low] self.total += value CircularBuffer.add(self,value)
def add(self, value): """Adds value to buffer, overwrite as needed.""" if self.isFull(): delta = self.buffer[self.low] else: delta = 0 self.total += value - delta self.sumSq += value*value - delta*delta CircularBuffer.add(self,value)
def add(self, value): """Adds value to buffer, overwrite as needed.""" if self.isFull(): delta = self.buffer[self.low] else: delta = 0 self.total += value - delta self.sumSq += value * value - delta * delta CircularBuffer.add(self, value)
def setUp(self): self.cb = CircularBuffer(SIZE)
class TestCircBuffer(unittest.TestCase): def setUp(self): self.cb = CircularBuffer(SIZE) def tearDown(self): self.cb = None def test_basic(self): """Basic test.""" self.assertTrue(self.cb.isEmpty()) self.assertFalse(self.cb.isFull()) self.assertEqual(0, len(self.cb)) def test_fill(self): """Fill up the buffer.""" for _ in range(SIZE): self.cb.add(_) self.assertFalse(self.cb.isEmpty()) self.assertTrue(self.cb.isFull()) self.assertEqual(5, len(self.cb)) def test_overFill(self): """Validate can deal with overfill.""" high = 15 for _ in range(high): self.cb.add(_) self.assertFalse(self.cb.isEmpty()) self.assertTrue(self.cb.isFull()) self.assertEqual(5, len(self.cb)) # check all are still present for _ in range(high - 1, high - SIZE - 1, -1): self.assertTrue(_ in self.cb) def test_fillWithRemove(self): """Validate can deal with removals.""" high = 15 for _ in range(high): self.cb.add(_) self.cb.remove() self.assertTrue(self.cb.isEmpty()) self.assertFalse(self.cb.isFull()) self.assertEqual(0, len(self.cb)) def test_randomUsage(self): """Try random behavior.""" for _ in range(1000): if random.random() < 0.75: self.cb.add(random.random()) elif not self.cb.isEmpty(): self.cb.remove()
def __init__(self, size): """Store buffer in given storage.""" CircularBuffer.__init__(self, size) self.total = 0
def remove(self): """Removes oldest value from non-empty buffer.""" removed = CircularBuffer.remove(self) self.total -= removed return removed
class TestCircBuffer(unittest.TestCase): def setUp(self): self.cb = CircularBuffer(SIZE) def tearDown(self): self.cb = None def test_basic(self): """Basic test.""" self.assertTrue(self.cb.isEmpty()) self.assertFalse(self.cb.isFull()) self.assertEqual(0, len(self.cb)) def test_fill(self): """Fill up the buffer.""" for _ in range(SIZE): self.cb.add(_) self.assertFalse(self.cb.isEmpty()) self.assertTrue(self.cb.isFull()) self.assertEqual(5, len(self.cb)) def test_overFill(self): """Validate can deal with overfill.""" high = 15 for _ in range(high): self.cb.add(_) self.assertFalse(self.cb.isEmpty()) self.assertTrue(self.cb.isFull()) self.assertEqual(5, len(self.cb)) # check all are still present for _ in range(high-1, high - SIZE-1, -1): self.assertTrue(_ in self.cb) def test_fillWithRemove(self): """Validate can deal with removals.""" high = 15 for _ in range(high): self.cb.add(_) self.cb.remove() self.assertTrue(self.cb.isEmpty()) self.assertFalse(self.cb.isFull()) self.assertEqual(0, len(self.cb)) def test_randomUsage(self): """Try random behavior.""" for _ in range(1000): if random.random() < 0.75: self.cb.add(random.random()) elif not self.cb.isEmpty(): self.cb.remove()