[Tutor] a FIFO with fixed capacity?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Mar 31 23:20:35 CEST 2005


> ps -- as for the need for a circular buffer vs. FIFO: I think my dsp
> background pushed me toward the CB.  My app involves data acquisition
> for extended periods of time.  I can't grow the FIFO infinitely, but it
> is no big deal if a few samples get overwritten.  Does this make sense?

Hi Marcus,

Let me make sure I understand.  Let's imagine that we have such a
CircularQueue, with methods:

    push(element)
    pop()
    isEmpty()

Would the following test code capture how you expect the buffer to behave?

###### Warning, untested code, since I don't have a circular queue
###### implementation handy yet.  *grin*  Let's do some test-driven
###### development.
######
import unittest

class TestCircularQueue(unittest.testCase):
    def testSimpleQueue(self):
        queue = CircularQueue(capacity=3)
        queue.push("a")
        queue.push("b")
        queue.push("c")
        self.assertEquals(False, queue.isEmpty())
        self.assertEquals("a", queue.pop())
        self.assertEquals("b", queue.pop())
        self.assertEquals("c", queue.pop())
	self.assertEquals(True, queue.isEmpty())

    def testOverflowWrapsQueue(self):
        queue = CircularQueue(capacity=3)
        queue.push("a")
        queue.push("b")
        queue.push("c")
        queue.push("d")
        self.assertEquals("b", queue.pop())
        self.assertEquals("c", queue.pop())
        self.assertEquals("d", queue.pop())
        self.assertEquals(True, queue.isEmpty())
######

I'm writing this as a unit test so that we can concretely see if my
understanding of the behavior is the same at the one you expect to see.
Does this match?


Best of wishes to you!



More information about the Tutor mailing list