/Lib/test/test_strop.py

http://unladen-swallow.googlecode.com/ · Python · 153 lines · 116 code · 32 blank · 5 comment · 7 complexity · abc45c817dce5cd9c0ca25dac9fdafd1 MD5 · raw file

  1. import warnings
  2. warnings.filterwarnings("ignore", "strop functions are obsolete;",
  3. DeprecationWarning,
  4. r'test.test_strop|unittest')
  5. import strop
  6. import unittest
  7. from test import test_support
  8. class StropFunctionTestCase(unittest.TestCase):
  9. def test_atoi(self):
  10. self.assert_(strop.atoi(" 1 ") == 1)
  11. self.assertRaises(ValueError, strop.atoi, " 1x")
  12. self.assertRaises(ValueError, strop.atoi, " x1 ")
  13. def test_atol(self):
  14. self.assert_(strop.atol(" 1 ") == 1L)
  15. self.assertRaises(ValueError, strop.atol, " 1x")
  16. self.assertRaises(ValueError, strop.atol, " x1 ")
  17. def test_atof(self):
  18. self.assert_(strop.atof(" 1 ") == 1.0)
  19. self.assertRaises(ValueError, strop.atof, " 1x")
  20. self.assertRaises(ValueError, strop.atof, " x1 ")
  21. def test_capitalize(self):
  22. self.assert_(strop.capitalize(" hello ") == " hello ")
  23. self.assert_(strop.capitalize("hello ") == "Hello ")
  24. def test_find(self):
  25. self.assert_(strop.find("abcdefghiabc", "abc") == 0)
  26. self.assert_(strop.find("abcdefghiabc", "abc", 1) == 9)
  27. self.assert_(strop.find("abcdefghiabc", "def", 4) == -1)
  28. def test_rfind(self):
  29. self.assert_(strop.rfind("abcdefghiabc", "abc") == 9)
  30. def test_lower(self):
  31. self.assert_(strop.lower("HeLLo") == "hello")
  32. def test_upper(self):
  33. self.assert_(strop.upper("HeLLo") == "HELLO")
  34. def test_swapcase(self):
  35. self.assert_(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS")
  36. def test_strip(self):
  37. self.assert_(strop.strip(" \t\n hello \t\n ") == "hello")
  38. def test_lstrip(self):
  39. self.assert_(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ")
  40. def test_rstrip(self):
  41. self.assert_(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello")
  42. def test_replace(self):
  43. replace = strop.replace
  44. self.assert_(replace("one!two!three!", '!', '@', 1)
  45. == "one@two!three!")
  46. self.assert_(replace("one!two!three!", '!', '@', 2)
  47. == "one@two@three!")
  48. self.assert_(replace("one!two!three!", '!', '@', 3)
  49. == "one@two@three@")
  50. self.assert_(replace("one!two!three!", '!', '@', 4)
  51. == "one@two@three@")
  52. # CAUTION: a replace count of 0 means infinity only to strop,
  53. # not to the string .replace() method or to the
  54. # string.replace() function.
  55. self.assert_(replace("one!two!three!", '!', '@', 0)
  56. == "one@two@three@")
  57. self.assert_(replace("one!two!three!", '!', '@')
  58. == "one@two@three@")
  59. self.assert_(replace("one!two!three!", 'x', '@')
  60. == "one!two!three!")
  61. self.assert_(replace("one!two!three!", 'x', '@', 2)
  62. == "one!two!three!")
  63. def test_split(self):
  64. split = strop.split
  65. self.assert_(split("this is the split function")
  66. == ['this', 'is', 'the', 'split', 'function'])
  67. self.assert_(split("a|b|c|d", '|') == ['a', 'b', 'c', 'd'])
  68. self.assert_(split("a|b|c|d", '|', 2) == ['a', 'b', 'c|d'])
  69. self.assert_(split("a b c d", None, 1) == ['a', 'b c d'])
  70. self.assert_(split("a b c d", None, 2) == ['a', 'b', 'c d'])
  71. self.assert_(split("a b c d", None, 3) == ['a', 'b', 'c', 'd'])
  72. self.assert_(split("a b c d", None, 4) == ['a', 'b', 'c', 'd'])
  73. self.assert_(split("a b c d", None, 0) == ['a', 'b', 'c', 'd'])
  74. self.assert_(split("a b c d", None, 2) == ['a', 'b', 'c d'])
  75. def test_join(self):
  76. self.assert_(strop.join(['a', 'b', 'c', 'd']) == 'a b c d')
  77. self.assert_(strop.join(('a', 'b', 'c', 'd'), '') == 'abcd')
  78. self.assert_(strop.join(Sequence()) == 'w x y z')
  79. # try a few long ones
  80. self.assert_(strop.join(['x' * 100] * 100, ':')
  81. == (('x' * 100) + ":") * 99 + "x" * 100)
  82. self.assert_(strop.join(('x' * 100,) * 100, ':')
  83. == (('x' * 100) + ":") * 99 + "x" * 100)
  84. def test_maketrans(self):
  85. self.assert_(strop.maketrans("abc", "xyz") == transtable)
  86. self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq")
  87. def test_translate(self):
  88. self.assert_(strop.translate("xyzabcdef", transtable, "def")
  89. == "xyzxyz")
  90. def test_data_attributes(self):
  91. strop.lowercase
  92. strop.uppercase
  93. strop.whitespace
  94. @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=5)
  95. def test_stropjoin_huge_list(self, size):
  96. a = "A" * size
  97. try:
  98. r = strop.join([a, a], a)
  99. except OverflowError:
  100. pass
  101. else:
  102. self.assertEquals(len(r), len(a) * 3)
  103. @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=1)
  104. def test_stropjoin_huge_tup(self, size):
  105. a = "A" * size
  106. try:
  107. r = strop.join((a, a), a)
  108. except OverflowError:
  109. pass # acceptable on 32-bit
  110. else:
  111. self.assertEquals(len(r), len(a) * 3)
  112. transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377'
  113. # join() now works with any sequence type.
  114. class Sequence:
  115. def __init__(self): self.seq = 'wxyz'
  116. def __len__(self): return len(self.seq)
  117. def __getitem__(self, i): return self.seq[i]
  118. def test_main():
  119. test_support.run_unittest(StropFunctionTestCase)
  120. if __name__ == "__main__":
  121. test_main()