Ejemplo n.º 1
0
 def setUp(self):
     self.cmp = [2, 1, 3, 5, 1, 1]
     try:
         self.x = IntList()
     except NameError:
         return 1
     self.x[:] = self.cmp
Ejemplo n.º 2
0
class TestIntList(unittest.TestCase):
    def setUp(self):
        self.cmp = [2, 1, 3, 5, 1, 1]
        try:
            self.x = IntList()
        except NameError:
            return 1
        self.x[:] = self.cmp

    def test_init(self):
        x = IntList()

    def test_append(self):
        self.x.append(3)

    def test_contains(self):
        assert 2 in self.x

    def test_count(self):
        self.assertEqual(self.x.count(1), 3)

    def test_delitem(self):
        del (self.x[0])
        del (self.cmp[0])
        self.assertSequenceEqual(self.x, self.cmp)

    def test_extend(self):
        self.x.extend([3, 4, 5])
        self.cmp.extend([3, 4, 5])
        self.assertSequenceEqual(self.x, self.cmp)

    def test_getitem(self):
        self.assertEqual(self.x[0], 2)

    def test_index(self):
        ind = self.x.index(3)
        self.assertEqual(ind, 2)

    def test_insert(self):
        self.x.insert(0, 0)
        self.assertEqual(self.x[0], 0)

    def test_iter(self):
        i = 0
        for x_item in self.x:
            self.assertEqual(x_item, self.cmp[i])
            i += 1

    def test_len(self):
        self.assertEqual(len(self.x), len(self.cmp))

    def test_pop(self):
        self.assertEqual(self.x.pop(), 1)  # pop the last item
        self.assertEqual(self.x.pop(2), 3)  # pop the second item

    def test_remove(self):
        self.x.remove(2)
        self.assertEqual(self.x[0], 1)

    def test_reverse(self):
        self.cmp.reverse()
        self.x.reverse()
        self.assertSequenceEqual(self.x, self.cmp)

    def test_setitem(self):
        self.x[1] = 2048
        self.assertEqual(self.x[1], 2048)

    def test_slice(self):
        x = IntList()
        x[:] = [1, 2, 3]

    def test_sort(self):
        self.cmp.sort()
        self.x.sort()
        self.assertSequenceEqual(self.x, self.cmp)