blob: 45c90a6539790f4cf17e0158b88dffb9d4cb7663 [file] [log] [blame]
Guido van Rossum2e0a6542001-05-15 02:14:44 +00001import warnings
Tim Petersd3925062002-04-16 01:27:44 +00002warnings.filterwarnings("ignore", "strop functions are obsolete;",
3 DeprecationWarning,
Barry Warsaw408b6d32002-07-30 23:27:12 +00004 r'test.test_strop|unittest')
Fred Draked992c2c2001-05-22 16:44:33 +00005import strop
Fred Draked992c2c2001-05-22 16:44:33 +00006import unittest
Benjamin Petersond42f60e2014-03-30 20:09:44 -04007import sys
Barry Warsaw04f357c2002-07-23 19:04:11 +00008from test import test_support
Guido van Rossumedaf1c91996-10-08 14:07:56 +00009
Guido van Rossumedaf1c91996-10-08 14:07:56 +000010
Fred Draked992c2c2001-05-22 16:44:33 +000011class StropFunctionTestCase(unittest.TestCase):
Barry Warsawad522fa1996-12-09 21:49:10 +000012
Fred Draked992c2c2001-05-22 16:44:33 +000013 def test_atoi(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000014 self.assertTrue(strop.atoi(" 1 ") == 1)
Fred Draked992c2c2001-05-22 16:44:33 +000015 self.assertRaises(ValueError, strop.atoi, " 1x")
16 self.assertRaises(ValueError, strop.atoi, " x1 ")
17
18 def test_atol(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000019 self.assertTrue(strop.atol(" 1 ") == 1L)
Fred Draked992c2c2001-05-22 16:44:33 +000020 self.assertRaises(ValueError, strop.atol, " 1x")
21 self.assertRaises(ValueError, strop.atol, " x1 ")
22
23 def test_atof(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000024 self.assertTrue(strop.atof(" 1 ") == 1.0)
Fred Draked992c2c2001-05-22 16:44:33 +000025 self.assertRaises(ValueError, strop.atof, " 1x")
26 self.assertRaises(ValueError, strop.atof, " x1 ")
27
28 def test_capitalize(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000029 self.assertTrue(strop.capitalize(" hello ") == " hello ")
30 self.assertTrue(strop.capitalize("hello ") == "Hello ")
Fred Draked992c2c2001-05-22 16:44:33 +000031
32 def test_find(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000033 self.assertTrue(strop.find("abcdefghiabc", "abc") == 0)
34 self.assertTrue(strop.find("abcdefghiabc", "abc", 1) == 9)
35 self.assertTrue(strop.find("abcdefghiabc", "def", 4) == -1)
Fred Draked992c2c2001-05-22 16:44:33 +000036
37 def test_rfind(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000038 self.assertTrue(strop.rfind("abcdefghiabc", "abc") == 9)
Fred Draked992c2c2001-05-22 16:44:33 +000039
40 def test_lower(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000041 self.assertTrue(strop.lower("HeLLo") == "hello")
Fred Draked992c2c2001-05-22 16:44:33 +000042
43 def test_upper(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000044 self.assertTrue(strop.upper("HeLLo") == "HELLO")
Fred Draked992c2c2001-05-22 16:44:33 +000045
46 def test_swapcase(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000047 self.assertTrue(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS")
Fred Draked992c2c2001-05-22 16:44:33 +000048
49 def test_strip(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000050 self.assertTrue(strop.strip(" \t\n hello \t\n ") == "hello")
Fred Draked992c2c2001-05-22 16:44:33 +000051
52 def test_lstrip(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000053 self.assertTrue(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ")
Fred Draked992c2c2001-05-22 16:44:33 +000054
55 def test_rstrip(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000056 self.assertTrue(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello")
Fred Draked992c2c2001-05-22 16:44:33 +000057
58 def test_replace(self):
59 replace = strop.replace
Benjamin Peterson5c8da862009-06-30 22:57:08 +000060 self.assertTrue(replace("one!two!three!", '!', '@', 1)
Fred Draked992c2c2001-05-22 16:44:33 +000061 == "one@two!three!")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000062 self.assertTrue(replace("one!two!three!", '!', '@', 2)
Fred Draked992c2c2001-05-22 16:44:33 +000063 == "one@two@three!")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000064 self.assertTrue(replace("one!two!three!", '!', '@', 3)
Fred Draked992c2c2001-05-22 16:44:33 +000065 == "one@two@three@")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000066 self.assertTrue(replace("one!two!three!", '!', '@', 4)
Fred Draked992c2c2001-05-22 16:44:33 +000067 == "one@two@three@")
68
69 # CAUTION: a replace count of 0 means infinity only to strop,
70 # not to the string .replace() method or to the
71 # string.replace() function.
72
Benjamin Peterson5c8da862009-06-30 22:57:08 +000073 self.assertTrue(replace("one!two!three!", '!', '@', 0)
Fred Draked992c2c2001-05-22 16:44:33 +000074 == "one@two@three@")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000075 self.assertTrue(replace("one!two!three!", '!', '@')
Fred Draked992c2c2001-05-22 16:44:33 +000076 == "one@two@three@")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000077 self.assertTrue(replace("one!two!three!", 'x', '@')
Fred Draked992c2c2001-05-22 16:44:33 +000078 == "one!two!three!")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000079 self.assertTrue(replace("one!two!three!", 'x', '@', 2)
Fred Draked992c2c2001-05-22 16:44:33 +000080 == "one!two!three!")
81
82 def test_split(self):
83 split = strop.split
Benjamin Peterson5c8da862009-06-30 22:57:08 +000084 self.assertTrue(split("this is the split function")
Fred Draked992c2c2001-05-22 16:44:33 +000085 == ['this', 'is', 'the', 'split', 'function'])
Benjamin Peterson5c8da862009-06-30 22:57:08 +000086 self.assertTrue(split("a|b|c|d", '|') == ['a', 'b', 'c', 'd'])
87 self.assertTrue(split("a|b|c|d", '|', 2) == ['a', 'b', 'c|d'])
88 self.assertTrue(split("a b c d", None, 1) == ['a', 'b c d'])
89 self.assertTrue(split("a b c d", None, 2) == ['a', 'b', 'c d'])
90 self.assertTrue(split("a b c d", None, 3) == ['a', 'b', 'c', 'd'])
91 self.assertTrue(split("a b c d", None, 4) == ['a', 'b', 'c', 'd'])
92 self.assertTrue(split("a b c d", None, 0) == ['a', 'b', 'c', 'd'])
93 self.assertTrue(split("a b c d", None, 2) == ['a', 'b', 'c d'])
Fred Draked992c2c2001-05-22 16:44:33 +000094
95 def test_join(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000096 self.assertTrue(strop.join(['a', 'b', 'c', 'd']) == 'a b c d')
97 self.assertTrue(strop.join(('a', 'b', 'c', 'd'), '') == 'abcd')
98 self.assertTrue(strop.join(Sequence()) == 'w x y z')
Fred Draked992c2c2001-05-22 16:44:33 +000099
100 # try a few long ones
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000101 self.assertTrue(strop.join(['x' * 100] * 100, ':')
Fred Draked992c2c2001-05-22 16:44:33 +0000102 == (('x' * 100) + ":") * 99 + "x" * 100)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000103 self.assertTrue(strop.join(('x' * 100,) * 100, ':')
Fred Draked992c2c2001-05-22 16:44:33 +0000104 == (('x' * 100) + ":") * 99 + "x" * 100)
105
106 def test_maketrans(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000107 self.assertTrue(strop.maketrans("abc", "xyz") == transtable)
Fred Draked992c2c2001-05-22 16:44:33 +0000108 self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq")
109
110 def test_translate(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000111 self.assertTrue(strop.translate("xyzabcdef", transtable, "def")
Fred Draked992c2c2001-05-22 16:44:33 +0000112 == "xyzxyz")
113
114 def test_data_attributes(self):
115 strop.lowercase
116 strop.uppercase
117 strop.whitespace
118
Benjamin Petersond42f60e2014-03-30 20:09:44 -0400119 @unittest.skipUnless(sys.maxsize == 2147483647, "only for 32-bit")
120 def test_expandtabs_overflow(self):
121 s = '\t\n' * 0x10000 + 'A' * 0x1000000
122 self.assertRaises(OverflowError, strop.expandtabs, s, 0x10001)
123
Neal Norwitze7d8be82008-07-31 17:17:14 +0000124 @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=5)
125 def test_stropjoin_huge_list(self, size):
126 a = "A" * size
127 try:
128 r = strop.join([a, a], a)
129 except OverflowError:
130 pass
131 else:
Ezio Melotti2623a372010-11-21 13:34:58 +0000132 self.assertEqual(len(r), len(a) * 3)
Neal Norwitze7d8be82008-07-31 17:17:14 +0000133
134 @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=1)
135 def test_stropjoin_huge_tup(self, size):
136 a = "A" * size
137 try:
138 r = strop.join((a, a), a)
139 except OverflowError:
140 pass # acceptable on 32-bit
141 else:
Ezio Melotti2623a372010-11-21 13:34:58 +0000142 self.assertEqual(len(r), len(a) * 3)
Barry Warsawad522fa1996-12-09 21:49:10 +0000143
144transtable = '\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'
145
Barry Warsawad522fa1996-12-09 21:49:10 +0000146
Fred Draked992c2c2001-05-22 16:44:33 +0000147# join() now works with any sequence type.
Barry Warsawda0e5201997-01-06 22:46:07 +0000148class Sequence:
149 def __init__(self): self.seq = 'wxyz'
150 def __len__(self): return len(self.seq)
151 def __getitem__(self, i): return self.seq[i]
152
Barry Warsawda0e5201997-01-06 22:46:07 +0000153
Fred Drake2e2be372001-09-20 21:33:42 +0000154def test_main():
155 test_support.run_unittest(StropFunctionTestCase)
156
157
158if __name__ == "__main__":
159 test_main()