def test_size_one(self): '''The ring buffer behaves correctly when the max is 1 ''' rb = RingBuffer(1) self.assertTrue(rb.getOldest() == None) rb.add(1) self.assertTrue(rb.getOldest() == 1) rb.add(2) self.assertTrue(rb.getOldest() == 2) rb.add(3) self.assertTrue(rb.getOldest() == 3)
def test_getOldest(self): '''You can access the oldest element in the ring buffer with getOldest When the list is empty, return None ''' rb = RingBuffer(2) self.assertTrue(rb.getOldest() == None) rb.add(1) self.assertTrue(rb.getOldest() == 1) rb.add(2) self.assertTrue(rb.getOldest() == 1) rb.add(3) self.assertTrue(rb.getOldest() == 2)
class Game: def __init__(self): # The number of equations the user is shown at the start self.difficulty = 2 # The maximum number that can appear in an equation self.max_int = 10 # Count of correct answers self.correct = 0 # Total number of equations completed self.total = 0 # Number of equations for the user to answer self.max_equations = 10 # Ring buffer of equations self.rb = RingBuffer(self.difficulty) def nextEquation(self): ''' Creates the next equation Returns a string representation of the equation for the user to see. The string includes the order number corresponding to the equation ''' # Keep track of the number of equations self.total += 1 # Create the equation eq = Equation(10) # Add it to the buffer self.rb.add(eq) # And display it return '#%s) %s' % (self.total, eq) def validateAnswer(self, answer): ''' Validates the answer and increments the count of correct answers if the answer is correct. ''' if(self.rb.getOldest().validate(answer)): self.correct +=1 return True else: return False