def test_k_not_met(self): """ Test that maximum() compares only necessary indices """ a = CircularArray(100000000) # one hundred million a.offer(1) start = time.clock() a.maximum() end = time.clock() self.assertTrue(end - start < 1e-04)
def test_maximum_middle(self): """ Test calling maximum on a partially filled array Structured basis: initial condition met Boundary: len(a) > head > 0 """ a = CircularArray(3) a.offer(1) self.assertEqual(a.maximum(), 1) a.offer(3) self.assertEqual(a.maximum(), 3) a.offer(2) self.assertEqual(a.maximum(), 3) self.assertEqual(a.a, [1, 3, 2])
def test_k_not_met(self): """ Test that maximum only considers elements within head """ a = CircularArray(3) a.offer(5) a.a[1] = 10 # direct assignment self.assertEqual(a.a, [5, 10, None]) self.assertEqual(a.maximum(), 5)
def test_wrapped(self): """ Test the circular property of the array """ a = CircularArray(3) for x in xrange(1, 7): a.offer(x) self.assertEqual(a.a, [4, 5, 6]) self.assertEqual(a.maximum(), 6)
def test_full(self): """ Test a call to maximum() """ a = CircularArray(3) for x in xrange(1, 4): a.offer(x) self.assertEqual(a.a, [1, 2, 3]) self.assertEqual(a.maximum(), 3)
def test_maximum_left(self): """ Test calling maximum on an empty array Structured basis: initial condition not met Boundary: head <= 0 """ a = CircularArray(3) self.assertEqual(a.a, [None, None, None]) self.assertEqual(a.maximum(), None)
def test_maximum_right(self): """ Test calling maximum on an overfilled array Structured basis: initial condition met Boundary: head >= len(a) > 0 """ # pre-wrap a = CircularArray(3) a.offer(1) self.assertEqual(a.maximum(), 1) a.offer(3) self.assertEqual(a.maximum(), 3) a.offer(2) self.assertEqual(a.maximum(), 3) self.assertEqual(a.a, [1, 3, 2]) # post-wrap a.offer(0) self.assertEqual(a.maximum(), 3) a.offer(0) self.assertEqual(a.maximum(), 2) self.assertEqual(a.a, [0, 0, 2])
def test_stress(self): """ Stress test entire class Warning: this test has a long runtime """ BIG_NUMBER = 1000000 # one million a = CircularArray(BIG_NUMBER) # populate first half with random integers for _ in xrange(BIG_NUMBER / 2): a.offer(randint(0, 9)) # offer the maximum a.offer(10) # populate second half with random integers for _ in xrange(BIG_NUMBER / 2, BIG_NUMBER): a.offer(randint(0, 9)) self.assertEqual(a.maximum(), 10)
def test_empty(self): """ Test a call to maximum() on an empty array """ a = CircularArray(3) self.assertEqual(a.a, [None, None, None]) self.assertEqual(a.maximum(), None)