Debian Codegraph @

pypy/module/operator/test/test_operator.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 class AppTestOperator:
4 def test_getters_are_not_regular_functions(self):
5 import _operator as operator
6 class A(object):
7 getx = operator.attrgetter('x')
8 get3 = operator.itemgetter(3)
9 callx = operator.methodcaller("append", "x")
10 a = A()
11 a.x = 5
12 assert a.getx(a) == 5
13 assert a.get3("foobar") == "b"
14 assert a.getx(*(a,)) == 5
15 l = []
16 a.callx(l)
17 assert l == ["x"]
18
19 def test_getter_multiple_gest(self):
20 import _operator as operator
21
22 class A(object):
23 pass
24
25 a = A()
26 a.x = 'X'
27 a.y = 'Y'
28 a.z = 'Z'
29
30 assert operator.attrgetter('x','z','y')(a) == ('X', 'Z', 'Y')
31 e = raises(TypeError, operator.attrgetter, ('x', (), 'y'))
32 assert str(e.value).startswith("attribute name must be a string")
33
34 data = list(map(str, range(20)))
35 assert operator.itemgetter(2,10,5)(data) == ('2', '10', '5')
36 raises(TypeError, operator.itemgetter(2, 'x', 5), data)
37
38 def test_attrgetter(self):
39 import _operator as operator
40 raises(TypeError, operator.attrgetter, 2)
41
42 def test_dotted_attrgetter(self):
43 from _operator import attrgetter
44 class A:
45 pass
46 a = A()
47 a.name = "hello"
48 a.child = A()
49 a.child.name = "world"
50 a.child.foo = "bar"
51 assert attrgetter("child.name")(a) == "world"
52 assert attrgetter("child.name", "child.foo")(a) == ("world", "bar")
53
54 def test_attrgetter_type(self):
55 from _operator import attrgetter
56 assert type(attrgetter("child.name")) is attrgetter
57
58 def test_concat(self):
59 class Seq1:
60 def __init__(self, lst):
61 self.lst = lst
62 def __len__(self):
63 return len(self.lst)
64 def __getitem__(self, i):
65 return self.lst[i]
66 def __add__(self, other):
67 return self.lst + other.lst
68 def __mul__(self, other):
69 return self.lst * other
70 def __rmul__(self, other):
71 return other * self.lst
72
73 class Seq2(object):
74 def __init__(self, lst):
75 self.lst = lst
76 def __len__(self):
77 return len(self.lst)
78 def __getitem__(self, i):
79 return self.lst[i]
80 def __add__(self, other):
81 return self.lst + other.lst
82 def __mul__(self, other):
83 return self.lst * other
84 def __rmul__(self, other):
85 return other * self.lst
86
87 import _operator as operator
88
89 raises(TypeError, operator.concat)
90 raises(TypeError, operator.concat, None, None)
91 assert operator.concat('py', 'thon') == 'python'
92 assert operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4]
93 assert operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7]
94 assert operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7]
95 raises(TypeError, operator.concat, 13, 29)
96
97 def test_mul(self):
98 class Seq1:
99 def __init__(self, lst):
100 self.lst = lst
101 def __len__(self):
102 return len(self.lst)
103 def __getitem__(self, i):
104 return self.lst[i]
105 def __add__(self, other):
106 return self.lst + other.lst
107 def __mul__(self, other):
108 return self.lst * other
109 def __rmul__(self, other):
110 return other * self.lst
111
112 class Seq2(object):
113 def __init__(self, lst):
114 self.lst = lst
115 def __len__(self):
116 return len(self.lst)
117 def __getitem__(self, i):
118 return self.lst[i]
119 def __add__(self, other):
120 return self.lst + other.lst
121 def __mul__(self, other):
122 return self.lst * other
123 def __rmul__(self, other):
124 return other * self.lst
125
126 import _operator as operator
127
128 a = list(range(3))
129 raises(TypeError, operator.mul)
130 raises(TypeError, operator.mul, a, None)
131 assert operator.mul(a, 2) == a+a
132 assert operator.mul(a, 1) == a
133 assert operator.mul(a, 0) == []
134 a = (1, 2, 3)
135 assert operator.mul(a, 2) == a+a
136 assert operator.mul(a, 1) == a
137 assert operator.mul(a, 0) == ()
138 a = '123'
139 assert operator.mul(a, 2) == a+a
140 assert operator.mul(a, 1) == a
141 assert operator.mul(a, 0) == ''
142 a = Seq1([4, 5, 6])
143 assert operator.mul(a, 2) == [4, 5, 6, 4, 5, 6]
144 assert operator.mul(a, 1) == [4, 5, 6]
145 assert operator.mul(a, 0) == []
146 a = Seq2([4, 5, 6])
147 assert operator.mul(a, 2) == [4, 5, 6, 4, 5, 6]
148 assert operator.mul(a, 1) == [4, 5, 6]
149 assert operator.mul(a, 0) == []
150
151 def test_iadd(self):
152 import _operator as operator
153
154 list = []
155 assert operator.iadd(list, [1, 2]) is list
156 assert list == [1, 2]
157
158 def test_imul(self):
159 import _operator as operator
160
161 class X(object):
162 def __index__(self):
163 return 5
164
165 a = list(range(3))
166 raises(TypeError, operator.imul)
167 raises(TypeError, operator.imul, a, None)
168 raises(TypeError, operator.imul, a, [])
169 assert operator.imul(a, 2) is a
170 assert a == [0, 1, 2, 0, 1, 2]
171 assert operator.imul(a, 1) is a
172 assert a == [0, 1, 2, 0, 1, 2]
173
174 def test_methodcaller(self):
175 from _operator import methodcaller
176 class X(object):
177 def method(self, arg1=2, arg2=3):
178 return arg1, arg2
179 x = X()
180 assert methodcaller("method")(x) == (2, 3)
181 assert methodcaller("method", 4)(x) == (4, 3)
182 assert methodcaller("method", 4, 5)(x) == (4, 5)
183 assert methodcaller("method", 4, arg2=42)(x) == (4, 42)
184
185 def test_methodcaller_self(self):
186 from operator import methodcaller
187 class X:
188 def method(myself, self):
189 return self * 6
190 assert methodcaller("method", self=7)(X()) == 42
191
192 def test_methodcaller_not_string(self):
193 import _operator as operator
194 e = raises(TypeError, operator.methodcaller, 42)
195 assert str(e.value) == "method name must be a string"
196
197 def test_index(self):
198 import _operator as operator
199 assert operator.index(42) == 42
200 raises(TypeError, operator.index, "abc")
201 exc = raises(TypeError, operator.index, "abc")
202 assert str(exc.value) == "'str' object cannot be interpreted as an integer"
203
204 def test_indexOf(self):
205 import _operator as operator
206 raises(TypeError, operator.indexOf)
207 raises(TypeError, operator.indexOf, None, None)
208 assert operator.indexOf([4, 3, 2, 1], 3) == 1
209 raises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
210
211 def test_index_int_subclass(self):
212 import operator
213 class myint(int):
214 def __index__(self):
215 return 13289
216 assert operator.index(myint(7)) == 7
217
218 def test_index_int_subclass(self):
219 import operator
220 class myint(int):
221 def __index__(self):
222 return 13289
223 assert operator.index(myint(7)) == 7
224
225 def test_compare_digest(self):
226 import _operator as operator
227
228 # Testing input type exception handling
229 a, b = 100, 200
230 raises(TypeError, operator._compare_digest, a, b)
231 a, b = 100, b"foobar"
232 raises(TypeError, operator._compare_digest, a, b)
233 a, b = b"foobar", 200
234 raises(TypeError, operator._compare_digest, a, b)
235 a, b = u"foobar", b"foobar"
236 raises(TypeError, operator._compare_digest, a, b)
237 a, b = b"foobar", u"foobar"
238 raises(TypeError, operator._compare_digest, a, b)
239
240 # Testing bytes of different lengths
241 a, b = b"foobar", b"foo"
242 assert not operator._compare_digest(a, b)
243 a, b = b"\xde\xad\xbe\xef", b"\xde\xad"
244 assert not operator._compare_digest(a, b)
245
246 # Testing bytes of same lengths, different values
247 a, b = b"foobar", b"foobaz"
248 assert not operator._compare_digest(a, b)
249 a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea"
250 assert not operator._compare_digest(a, b)
251
252 # Testing bytes of same lengths, same values
253 a, b = b"foobar", b"foobar"
254 assert operator._compare_digest(a, b)
255 a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef"
256 assert operator._compare_digest(a, b)
257
258 # Testing bytearrays of same lengths, same values
259 a, b = bytearray(b"foobar"), bytearray(b"foobar")
260 assert operator._compare_digest(a, b)
261
262 # Testing bytearrays of diffeent lengths
263 a, b = bytearray(b"foobar"), bytearray(b"foo")
264 assert not operator._compare_digest(a, b)
265
266 # Testing bytearrays of same lengths, different values
267 a, b = bytearray(b"foobar"), bytearray(b"foobaz")
268 assert not operator._compare_digest(a, b)
269
270 # Testing byte and bytearray of same lengths, same values
271 a, b = bytearray(b"foobar"), b"foobar"
272 assert operator._compare_digest(a, b)
273 assert operator._compare_digest(b, a)
274
275 # Testing byte bytearray of diffeent lengths
276 a, b = bytearray(b"foobar"), b"foo"
277 assert not operator._compare_digest(a, b)
278 assert not operator._compare_digest(b, a)
279
280 # Testing byte and bytearray of same lengths, different values
281 a, b = bytearray(b"foobar"), b"foobaz"
282 assert not operator._compare_digest(a, b)
283 assert not operator._compare_digest(b, a)
284
285 # Testing str of same lengths
286 a, b = "foobar", "foobar"
287 assert operator._compare_digest(a, b)
288
289 # Testing str of diffeent lengths
290 a, b = "foo", "foobar"
291 assert not operator._compare_digest(a, b)
292
293 # Testing bytes of same lengths, different values
294 a, b = "foobar", "foobaz"
295 assert not operator._compare_digest(a, b)
296
297 # Testing error cases
298 a, b = u"foobar", b"foobar"
299 raises(TypeError, operator._compare_digest, a, b)
300 a, b = b"foobar", u"foobar"
301 raises(TypeError, operator._compare_digest, a, b)
302 a, b = b"foobar", 1
303 raises(TypeError, operator._compare_digest, a, b)
304 a, b = 100, 200
305 raises(TypeError, operator._compare_digest, a, b)
306 a, b = "fooä", "fooä"
307 raises(TypeError, operator._compare_digest, a, b)
308
309 # subclasses are supported by ignore __eq__
310 class mystr(str):
311 def __eq__(self, other):
312 return False
313
314 a, b = mystr("foobar"), mystr("foobar")
315 assert operator._compare_digest(a, b)
316 a, b = mystr("foobar"), "foobar"
317 assert operator._compare_digest(a, b)
318 a, b = mystr("foobar"), mystr("foobaz")
319 assert not operator._compare_digest(a, b)
320
321 class mybytes(bytes):
322 def __eq__(self, other):
323 return False
324
325 a, b = mybytes(b"foobar"), mybytes(b"foobar")
326 assert operator._compare_digest(a, b)
327 a, b = mybytes(b"foobar"), b"foobar"
328 assert operator._compare_digest(a, b)
329 a, b = mybytes(b"foobar"), mybytes(b"foobaz")
330 assert not operator._compare_digest(a, b)
331
332 def test_compare_digest_unicode(self):
333 import _operator as operator
334 assert operator._compare_digest(u'asd', u'asd')
335 assert not operator._compare_digest(u'asd', u'qwe')
336 raises(TypeError, operator._compare_digest, u'asd', b'qwe')
337
338 def test_length_hint(self):
339 import _operator as operator
340 assert operator.length_hint([1, 2]) == 2
341 with raises(TypeError) as exc:
342 operator.length_hint("abc", "abc")
343 print(exc.value)
344 assert "cannot be interpreted as an integer" in str(exc.value)
345
346 def test_repr_attrgetter(self):
347 import _operator as operator
348 assert repr(operator.attrgetter("foo")) == "operator.attrgetter('foo')"
349 assert repr(operator.attrgetter("foo", 'bar')) == (
350 "operator.attrgetter('foo', 'bar')")
351 assert repr(operator.attrgetter("foo.bar")) == (
352 "operator.attrgetter('foo.bar')")
353 assert repr(operator.attrgetter("foo", 'bar.baz')) == (
354 "operator.attrgetter('foo', 'bar.baz')")
355
356 def test_repr_itemgetter(self):
357 import _operator as operator
358 assert repr(operator.itemgetter(2)) == "operator.itemgetter(2)"
359 assert repr(operator.itemgetter(2, 3)) == "operator.itemgetter(2, 3)"
360
361 def test_repr_methodcaller(self):
362 import _operator as operator
363 assert repr(operator.methodcaller("foo", "bar", baz=42)) == (
364 "operator.methodcaller('foo', 'bar', baz=42)")
365
366 def test_countOf(self):
367 from _operator import countOf
368 assert countOf([1, 2, 1, 1, 4], 1) == 3
369 nan = float('nan')
370 assert countOf([nan, nan, 4, nan], nan) == 3
371
372 def test_iconcat(self):
373 from _operator import iconcat
374 with raises(TypeError) as exc:
375 iconcat(1, 0.5)
376 print(exc.value)
377 assert str(exc.value) == "'int' object can't be concatenated"
378
379 def test_attrgetter_pickle(self):
380 # attrgetter stores a bound method in __dict__; __reduce__ must
381 # serialize only the attribute strings to avoid pickling it.
382 import pickle
383 import operator
384
385 class A:
386 pass
387 a = A()
388 a.x = 'X'
389 a.y = 'Y'
390 a.t = A()
391 a.t.u = 'U'
392
393 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
394 # single attribute
395 f = operator.attrgetter('x')
396 f2 = pickle.loads(pickle.dumps(f, proto))
397 assert f2(a) == f(a)
398 assert repr(f2) == repr(f)
399 # multiple attributes
400 f = operator.attrgetter('x', 'y')
401 f2 = pickle.loads(pickle.dumps(f, proto))
402 assert f2(a) == f(a)
403 assert repr(f2) == repr(f)
404 # dotted attribute
405 f = operator.attrgetter('t.u')
406 f2 = pickle.loads(pickle.dumps(f, proto))
407 assert f2(a) == f(a)
408 assert repr(f2) == repr(f)