예제 #1
0
    def test_pop_halves_array_capacity_if_less_than_quarter_of_capacity_used(
            self):
        da = DynamicArray()
        da._capacity = 64
        da._n = 17
        da._A = da._make_array(da._capacity)
        da._A[0:17] = [None] * 17

        #  first pop() should not shrink the array, since with 16 elements
        #  we have not passed the N/4 threshold.
        da.pop()

        self.assertEqual(da._capacity, 64)

        #  second pop() should trigger halving the array capacity:
        da.pop()

        self.assertEqual(da._capacity, 32)
예제 #2
0
    def test_pop_removes_and_returns_last_element_of_array(self):
        da = DynamicArray()
        da._n = 3
        da._capacity = 3
        da._A = da._make_array(da._capacity)
        da._A[0:3] = ['foo', 'bar', 'baz']

        result = da.pop()

        self.assertEqual(result, 'baz')
        self.assertEqual(da._A[0:2], ['foo', 'bar'])
        self.assertEqual(da._n, 2)