1import warnings 2warnings.filterwarnings("ignore", "strop functions are obsolete;", 3 DeprecationWarning, 4 r'test.test_strop|unittest') 5import strop 6import unittest 7import sys 8from test import test_support 9 10 11class StropFunctionTestCase(unittest.TestCase): 12 13 def test_atoi(self): 14 self.assertTrue(strop.atoi(" 1 ") == 1) 15 self.assertRaises(ValueError, strop.atoi, " 1x") 16 self.assertRaises(ValueError, strop.atoi, " x1 ") 17 18 def test_atol(self): 19 self.assertTrue(strop.atol(" 1 ") == 1L) 20 self.assertRaises(ValueError, strop.atol, " 1x") 21 self.assertRaises(ValueError, strop.atol, " x1 ") 22 23 def test_atof(self): 24 self.assertTrue(strop.atof(" 1 ") == 1.0) 25 self.assertRaises(ValueError, strop.atof, " 1x") 26 self.assertRaises(ValueError, strop.atof, " x1 ") 27 28 def test_capitalize(self): 29 self.assertTrue(strop.capitalize(" hello ") == " hello ") 30 self.assertTrue(strop.capitalize("hello ") == "Hello ") 31 32 def test_find(self): 33 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) 36 37 def test_rfind(self): 38 self.assertTrue(strop.rfind("abcdefghiabc", "abc") == 9) 39 40 def test_lower(self): 41 self.assertTrue(strop.lower("HeLLo") == "hello") 42 43 def test_upper(self): 44 self.assertTrue(strop.upper("HeLLo") == "HELLO") 45 46 def test_swapcase(self): 47 self.assertTrue(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS") 48 49 def test_strip(self): 50 self.assertTrue(strop.strip(" \t\n hello \t\n ") == "hello") 51 52 def test_lstrip(self): 53 self.assertTrue(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ") 54 55 def test_rstrip(self): 56 self.assertTrue(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello") 57 58 def test_replace(self): 59 replace = strop.replace 60 self.assertTrue(replace("one!two!three!", '!', '@', 1) 61 == "one@two!three!") 62 self.assertTrue(replace("one!two!three!", '!', '@', 2) 63 == "one@two@three!") 64 self.assertTrue(replace("one!two!three!", '!', '@', 3) 65 == "one@two@three@") 66 self.assertTrue(replace("one!two!three!", '!', '@', 4) 67 == "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 73 self.assertTrue(replace("one!two!three!", '!', '@', 0) 74 == "one@two@three@") 75 self.assertTrue(replace("one!two!three!", '!', '@') 76 == "one@two@three@") 77 self.assertTrue(replace("one!two!three!", 'x', '@') 78 == "one!two!three!") 79 self.assertTrue(replace("one!two!three!", 'x', '@', 2) 80 == "one!two!three!") 81 82 def test_split(self): 83 split = strop.split 84 self.assertTrue(split("this is the split function") 85 == ['this', 'is', 'the', 'split', 'function']) 86 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']) 94 95 def test_join(self): 96 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') 99 100 # try a few long ones 101 self.assertTrue(strop.join(['x' * 100] * 100, ':') 102 == (('x' * 100) + ":") * 99 + "x" * 100) 103 self.assertTrue(strop.join(('x' * 100,) * 100, ':') 104 == (('x' * 100) + ":") * 99 + "x" * 100) 105 106 def test_maketrans(self): 107 self.assertTrue(strop.maketrans("abc", "xyz") == transtable) 108 self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq") 109 110 def test_translate(self): 111 self.assertTrue(strop.translate("xyzabcdef", transtable, "def") 112 == "xyzxyz") 113 114 def test_data_attributes(self): 115 strop.lowercase 116 strop.uppercase 117 strop.whitespace 118 119 @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 124 @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: 132 self.assertEqual(len(r), len(a) * 3) 133 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: 142 self.assertEqual(len(r), len(a) * 3) 143 144 @unittest.skipUnless(sys.maxsize == 2147483647, "only for 32-bit") 145 def test_stropreplace_overflow(self): 146 a = "A" * 0x10000 147 self.assertRaises(MemoryError, strop.replace, a, "A", a) 148 149transtable = '\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' 150 151 152# join() now works with any sequence type. 153class Sequence: 154 def __init__(self): self.seq = 'wxyz' 155 def __len__(self): return len(self.seq) 156 def __getitem__(self, i): return self.seq[i] 157 158 159def test_main(): 160 test_support.run_unittest(StropFunctionTestCase) 161 162 163if __name__ == "__main__": 164 test_main() 165