|
6 | 6 | from test_support import verbose, TESTFN, unlink, TestFailed |
7 | 7 |
|
8 | 8 | def main(): |
9 | | - |
10 | 9 | testtype('c', 'c') |
11 | | - |
| 10 | + testtype('u', u'\u263a') |
12 | 11 | for type in (['b', 'h', 'i', 'l', 'f', 'd']): |
13 | 12 | testtype(type, 1) |
14 | | - |
| 13 | + testunicode() |
| 14 | + testsubclassing() |
15 | 15 | unlink(TESTFN) |
16 | 16 |
|
| 17 | +def testunicode(): |
| 18 | + try: |
| 19 | + array.array('b', u'foo') |
| 20 | + except TypeError: |
| 21 | + pass |
| 22 | + else: |
| 23 | + raise TestFailed("creating a non-unicode array from " |
| 24 | + "a Unicode string should fail") |
| 25 | + |
| 26 | + x = array.array('u', u'\xa0\xc2\u1234') |
| 27 | + x.fromunicode(u' ') |
| 28 | + x.fromunicode(u'') |
| 29 | + x.fromunicode(u'') |
| 30 | + x.fromunicode(u'\x11abc\xff\u1234') |
| 31 | + s = x.tounicode() |
| 32 | + if s != u'\xa0\xc2\u1234 \x11abc\xff\u1234': |
| 33 | + raise TestFailed("fromunicode()/tounicode()") |
| 34 | + |
| 35 | + s = u'\x00="\'a\\b\x80\xff\u0000\u0001\u1234' |
| 36 | + a = array.array('u', s) |
| 37 | + if verbose: |
| 38 | + print "repr of type 'u' array:", repr(a) |
| 39 | + print " expected: array('u', %r)" % s |
| 40 | + |
| 41 | +def testsubclassing(): |
| 42 | + class EditableString(array.array): |
| 43 | + def __new__(cls, s, *args, **kwargs): |
| 44 | + return array.array.__new__(cls, 'c', s) |
| 45 | + |
| 46 | + def __init__(self, s, color='blue'): |
| 47 | + array.array.__init__(self, 'c', s) |
| 48 | + self.color = color |
| 49 | + |
| 50 | + def strip(self): |
| 51 | + self[:] = array.array('c', self.tostring().strip()) |
| 52 | + |
| 53 | + def __repr__(self): |
| 54 | + return 'EditableString(%r)' % self.tostring() |
| 55 | + |
| 56 | + s = EditableString("\ttest\r\n") |
| 57 | + s.strip() |
| 58 | + if s.tostring() != 'test': |
| 59 | + raise TestFailed, "subclassing array.array failed somewhere" |
| 60 | + if s.color != 'blue': |
| 61 | + raise TestFailed, "assigning attributes to instance of array subclass" |
| 62 | + s.color = 'red' |
| 63 | + if s.color != 'red': |
| 64 | + raise TestFailed, "assigning attributes to instance of array subclass" |
| 65 | + if s.__dict__.keys() != ['color']: |
| 66 | + raise TestFailed, "array subclass __dict__" |
| 67 | + |
| 68 | + class ExaggeratingArray(array.array): |
| 69 | + __slots__ = ['offset'] |
| 70 | + |
| 71 | + def __new__(cls, typecode, data, offset): |
| 72 | + return array.array.__new__(cls, typecode, data) |
| 73 | + |
| 74 | + def __init__(self, typecode, data, offset): |
| 75 | + self.offset = offset |
| 76 | + |
| 77 | + def __getitem__(self, i): |
| 78 | + return array.array.__getitem__(self, i) + self.offset |
| 79 | + |
| 80 | + a = ExaggeratingArray('i', [3, 6, 7, 11], 4) |
| 81 | + if a[0] != 7: |
| 82 | + raise TestFailed, "array subclass overriding __getitem__" |
| 83 | + try: |
| 84 | + a.color = 'blue' |
| 85 | + except AttributeError: |
| 86 | + pass |
| 87 | + else: |
| 88 | + raise TestFailed, "array subclass __slots__ was ignored" |
| 89 | + |
17 | 90 |
|
18 | 91 | def testoverflow(type, lowerLimit, upperLimit): |
19 | 92 | # should not overflow assigning lower limit |
@@ -54,7 +127,6 @@ def testoverflow(type, lowerLimit, upperLimit): |
54 | 127 |
|
55 | 128 |
|
56 | 129 | def testtype(type, example): |
57 | | - |
58 | 130 | a = array.array(type) |
59 | 131 | a.append(example) |
60 | 132 | if verbose: |
@@ -97,6 +169,33 @@ def testtype(type, example): |
97 | 169 | print 'array of %s converted to a string: ' \ |
98 | 170 | % a.typecode, `a.tostring()` |
99 | 171 |
|
| 172 | + # Try out inplace addition and multiplication |
| 173 | + a = array.array(type, [example]) |
| 174 | + b = a |
| 175 | + a += array.array(type, [example]*2) |
| 176 | + if a is not b: |
| 177 | + raise TestFailed, "array(%s) inplace addition" % `type` |
| 178 | + if a != array.array(type, [example] * 3): |
| 179 | + raise TestFailed, "array(%s) inplace addition" % `type` |
| 180 | + |
| 181 | + a *= 5 |
| 182 | + if a is not b: |
| 183 | + raise TestFailed, "array(%s) inplace multiplication" % `type` |
| 184 | + if a != array.array(type, [example] * 15): |
| 185 | + raise TestFailed, "array(%s) inplace multiplication" % `type` |
| 186 | + |
| 187 | + a *= 0 |
| 188 | + if a is not b: |
| 189 | + raise TestFailed, "array(%s) inplace multiplication by 0" % `type` |
| 190 | + if a != array.array(type, []): |
| 191 | + raise TestFailed, "array(%s) inplace multiplication by 0" % `type` |
| 192 | + |
| 193 | + a *= 1000 |
| 194 | + if a is not b: |
| 195 | + raise TestFailed, "empty array(%s) inplace multiplication" % `type` |
| 196 | + if a != array.array(type, []): |
| 197 | + raise TestFailed, "empty array(%s) inplace multiplication" % `type` |
| 198 | + |
100 | 199 | if type == 'c': |
101 | 200 | a = array.array(type, "abcde") |
102 | 201 | a[:-1] = a |
@@ -135,6 +234,44 @@ def testtype(type, example): |
135 | 234 | a.reverse() |
136 | 235 | if a != array.array(type, "dca"): |
137 | 236 | raise TestFailed, "array(%s) reverse-test" % `type` |
| 237 | + elif type == 'u': |
| 238 | + a = array.array(type, u"abcde") |
| 239 | + a[:-1] = a |
| 240 | + if a != array.array(type, u"abcdee"): |
| 241 | + raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
| 242 | + a = array.array(type, u"abcde") |
| 243 | + a[1:] = a |
| 244 | + if a != array.array(type, u"aabcde"): |
| 245 | + raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
| 246 | + a = array.array(type, u"abcde") |
| 247 | + a[1:-1] = a |
| 248 | + if a != array.array(type, u"aabcdee"): |
| 249 | + raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
| 250 | + if a.index(u"e") != 5: |
| 251 | + raise TestFailed, "array(%s) index-test" % `type` |
| 252 | + if a.count(u"a") != 2: |
| 253 | + raise TestFailed, "array(%s) count-test" % `type` |
| 254 | + a.remove(u"e") |
| 255 | + if a != array.array(type, u"aabcde"): |
| 256 | + raise TestFailed, "array(%s) remove-test" % `type` |
| 257 | + if a.pop(0) != u"a": |
| 258 | + raise TestFailed, "array(%s) pop-test" % `type` |
| 259 | + if a.pop(1) != u"b": |
| 260 | + raise TestFailed, "array(%s) pop-test" % `type` |
| 261 | + a.extend(array.array(type, u"xyz")) |
| 262 | + if a != array.array(type, u"acdexyz"): |
| 263 | + raise TestFailed, "array(%s) extend-test" % `type` |
| 264 | + a.pop() |
| 265 | + a.pop() |
| 266 | + a.pop() |
| 267 | + x = a.pop() |
| 268 | + if x != u'e': |
| 269 | + raise TestFailed, "array(%s) pop-test" % `type` |
| 270 | + if a != array.array(type, u"acd"): |
| 271 | + raise TestFailed, "array(%s) pop-test" % `type` |
| 272 | + a.reverse() |
| 273 | + if a != array.array(type, u"dca"): |
| 274 | + raise TestFailed, "array(%s) reverse-test" % `type` |
138 | 275 | else: |
139 | 276 | a = array.array(type, [1, 2, 3, 4, 5]) |
140 | 277 | a[:-1] = a |
|
0 commit comments