1import sys
2from test import test_support, list_tests
3
4class ListTest(list_tests.CommonTest):
5    type2test = list
6
7    def test_basic(self):
8        self.assertEqual(list([]), [])
9        l0_3 = [0, 1, 2, 3]
10        l0_3_bis = list(l0_3)
11        self.assertEqual(l0_3, l0_3_bis)
12        self.assertTrue(l0_3 is not l0_3_bis)
13        self.assertEqual(list(()), [])
14        self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
15        self.assertEqual(list(''), [])
16        self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
17
18        if sys.maxsize == 0x7fffffff:
19            # This test can currently only work on 32-bit machines.
20            # XXX If/when PySequence_Length() returns a ssize_t, it should be
21            # XXX re-enabled.
22            # Verify clearing of bug #556025.
23            # This assumes that the max data size (sys.maxint) == max
24            # address size this also assumes that the address size is at
25            # least 4 bytes with 8 byte addresses, the bug is not well
26            # tested
27            #
28            # Note: This test is expected to SEGV under Cygwin 1.3.12 or
29            # earlier due to a newlib bug.  See the following mailing list
30            # thread for the details:
31
32            #     http://sources.redhat.com/ml/newlib/2002/msg00369.html
33            self.assertRaises(MemoryError, list, xrange(sys.maxint // 2))
34
35        # This code used to segfault in Py2.4a3
36        x = []
37        x.extend(-y for y in x)
38        self.assertEqual(x, [])
39
40    def test_truth(self):
41        super(ListTest, self).test_truth()
42        self.assertTrue(not [])
43        self.assertTrue([42])
44
45    def test_identity(self):
46        self.assertTrue([] is not [])
47
48    def test_len(self):
49        super(ListTest, self).test_len()
50        self.assertEqual(len([]), 0)
51        self.assertEqual(len([0]), 1)
52        self.assertEqual(len([0, 1, 2]), 3)
53
54    def test_overflow(self):
55        lst = [4, 5, 6, 7]
56        n = int((sys.maxint*2+2) // len(lst))
57        def mul(a, b): return a * b
58        def imul(a, b): a *= b
59        self.assertRaises((MemoryError, OverflowError), mul, lst, n)
60        self.assertRaises((MemoryError, OverflowError), imul, lst, n)
61
62def test_main(verbose=None):
63    test_support.run_unittest(ListTest)
64
65    # verify reference counting
66    import sys
67    if verbose and hasattr(sys, "gettotalrefcount"):
68        import gc
69        counts = [None] * 5
70        for i in xrange(len(counts)):
71            test_support.run_unittest(ListTest)
72            gc.collect()
73            counts[i] = sys.gettotalrefcount()
74        print counts
75
76
77if __name__ == "__main__":
78    test_main(verbose=True)
79