Пример #1
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)
Пример #2
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)
Пример #3
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)
Пример #4
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)
Пример #5
0
 def test_offer_head_left(self):
     """
     Test offer to a CircularArray when head is less than the length of a
         Data flow:  a Used, head Used
         Boundary:   head < len(a)
     """
     a = CircularArray(3)
     self.assertEqual(a.a, [None, None, None])
     self.assertEqual(a.head, 0)
     a.offer(0)
     self.assertEqual(a.a, [0, None, None])
     self.assertEqual(a.head, 1)
Пример #6
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])
Пример #7
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])
Пример #8
0
 def test_offer_head_right(self):
     """
     Test offer to a CircularArray when head is equal to the length of a
         Data flow:  a Used, head Used
         Boundary:   head >= len(a)
     """
     a = CircularArray(3)
     a.offer(0)
     a.offer(1)
     a.offer(2)
     self.assertEqual(a.a, [0, 1, 2])
     self.assertEqual(a.head, 3)
     a.offer(3)
     self.assertEqual(a.a, [3, 1, 2])
     self.assertEqual(a.head, 4)
Пример #9
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)