Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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])
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
    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])
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
 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)