/Lib/test/test_list.py

http://unladen-swallow.googlecode.com/ · Python · 78 lines · 50 code · 13 blank · 15 comment · 6 complexity · 7b2382d850fcfc1c8c0b5a1de4cb4917 MD5 · raw file

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