Debian Codegraph @

cpython27/Lib/test/test_strop.py

raw symbols SCIP
1 import warnings
2 warnings.filterwarnings("ignore", "strop functions are obsolete;",
3 DeprecationWarning,
4 r'test.test_strop|unittest')
5 import unittest
6 import sys
7 from test import test_support
8
9 strop = test_support.import_module("strop")
10
11
12 class StropFunctionTestCase(unittest.TestCase):
13
14 def test_atoi(self):
15 self.assertTrue(strop.atoi(" 1 ") == 1)
16 self.assertRaises(ValueError, strop.atoi, " 1x")
17 self.assertRaises(ValueError, strop.atoi, " x1 ")
18
19 def test_atol(self):
20 self.assertTrue(strop.atol(" 1 ") == 1L)
21 self.assertRaises(ValueError, strop.atol, " 1x")
22 self.assertRaises(ValueError, strop.atol, " x1 ")
23
24 def test_atof(self):
25 self.assertTrue(strop.atof(" 1 ") == 1.0)
26 self.assertRaises(ValueError, strop.atof, " 1x")
27 self.assertRaises(ValueError, strop.atof, " x1 ")
28
29 def test_capitalize(self):
30 self.assertTrue(strop.capitalize(" hello ") == " hello ")
31 self.assertTrue(strop.capitalize("hello ") == "Hello ")
32
33 def test_find(self):
34 self.assertTrue(strop.find("abcdefghiabc", "abc") == 0)
35 self.assertTrue(strop.find("abcdefghiabc", "abc", 1) == 9)
36 self.assertTrue(strop.find("abcdefghiabc", "def", 4) == -1)
37
38 def test_rfind(self):
39 self.assertTrue(strop.rfind("abcdefghiabc", "abc") == 9)
40
41 def test_lower(self):
42 self.assertTrue(strop.lower("HeLLo") == "hello")
43
44 def test_upper(self):
45 self.assertTrue(strop.upper("HeLLo") == "HELLO")
46
47 def test_swapcase(self):
48 self.assertTrue(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS")
49
50 def test_strip(self):
51 self.assertTrue(strop.strip(" \t\n hello \t\n ") == "hello")
52
53 def test_lstrip(self):
54 self.assertTrue(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ")
55
56 def test_rstrip(self):
57 self.assertTrue(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello")
58
59 def test_replace(self):
60 replace = strop.replace
61 self.assertTrue(replace("one!two!three!", '!', '@', 1)
62 == "one@two!three!")
63 self.assertTrue(replace("one!two!three!", '!', '@', 2)
64 == "one@two@three!")
65 self.assertTrue(replace("one!two!three!", '!', '@', 3)
66 == "one@two@three@")
67 self.assertTrue(replace("one!two!three!", '!', '@', 4)
68 == "one@two@three@")
69
70 # CAUTION: a replace count of 0 means infinity only to strop,
71 # not to the string .replace() method or to the
72 # string.replace() function.
73
74 self.assertTrue(replace("one!two!three!", '!', '@', 0)
75 == "one@two@three@")
76 self.assertTrue(replace("one!two!three!", '!', '@')
77 == "one@two@three@")
78 self.assertTrue(replace("one!two!three!", 'x', '@')
79 == "one!two!three!")
80 self.assertTrue(replace("one!two!three!", 'x', '@', 2)
81 == "one!two!three!")
82
83 def test_split(self):
84 split = strop.split
85 self.assertTrue(split("this is the split function")
86 == ['this', 'is', 'the', 'split', 'function'])
87 self.assertTrue(split("a|b|c|d", '|') == ['a', 'b', 'c', 'd'])
88 self.assertTrue(split("a|b|c|d", '|', 2) == ['a', 'b', 'c|d'])
89 self.assertTrue(split("a b c d", None, 1) == ['a', 'b c d'])
90 self.assertTrue(split("a b c d", None, 2) == ['a', 'b', 'c d'])
91 self.assertTrue(split("a b c d", None, 3) == ['a', 'b', 'c', 'd'])
92 self.assertTrue(split("a b c d", None, 4) == ['a', 'b', 'c', 'd'])
93 self.assertTrue(split("a b c d", None, 0) == ['a', 'b', 'c', 'd'])
94 self.assertTrue(split("a b c d", None, 2) == ['a', 'b', 'c d'])
95
96 def test_join(self):
97 self.assertTrue(strop.join(['a', 'b', 'c', 'd']) == 'a b c d')
98 self.assertTrue(strop.join(('a', 'b', 'c', 'd'), '') == 'abcd')
99 self.assertTrue(strop.join(Sequence()) == 'w x y z')
100
101 # try a few long ones
102 self.assertTrue(strop.join(['x' * 100] * 100, ':')
103 == (('x' * 100) + ":") * 99 + "x" * 100)
104 self.assertTrue(strop.join(('x' * 100,) * 100, ':')
105 == (('x' * 100) + ":") * 99 + "x" * 100)
106
107 def test_maketrans(self):
108 self.assertTrue(strop.maketrans("abc", "xyz") == transtable)
109 self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq")
110
111 def test_translate(self):
112 self.assertTrue(strop.translate("xyzabcdef", transtable, "def")
113 == "xyzxyz")
114
115 def test_data_attributes(self):
116 strop.lowercase
117 strop.uppercase
118 strop.whitespace
119
120 @unittest.skipUnless(sys.maxsize == 2147483647, "only for 32-bit")
121 def test_expandtabs_overflow(self):
122 s = '\t\n' * 0x10000 + 'A' * 0x1000000
123 self.assertRaises(OverflowError, strop.expandtabs, s, 0x10001)
124
125 @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=5)
126 def test_stropjoin_huge_list(self, size):
127 a = "A" * size
128 try:
129 r = strop.join([a, a], a)
130 except OverflowError:
131 pass
132 else:
133 self.assertEqual(len(r), len(a) * 3)
134
135 @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=1)
136 def test_stropjoin_huge_tup(self, size):
137 a = "A" * size
138 try:
139 r = strop.join((a, a), a)
140 except OverflowError:
141 pass # acceptable on 32-bit
142 else:
143 self.assertEqual(len(r), len(a) * 3)
144
145 @unittest.skipUnless(sys.maxsize == 2147483647, "only for 32-bit")
146 def test_stropreplace_overflow(self):
147 a = "A" * 0x10000
148 self.assertRaises(MemoryError, strop.replace, a, "A", a)
149
150 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'
151
152
153 # join() now works with any sequence type.
154 class Sequence:
155 def __init__(self): self.seq = 'wxyz'
156 def __len__(self): return len(self.seq)
157 def __getitem__(self, i): return self.seq[i]
158
159
160 def test_main():
161 test_support.run_unittest(StropFunctionTestCase)
162
163
164 if __name__ == "__main__":
165 test_main()