Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_descr.OperatorsTest Class Reference

List of all members.

Public Member Functions

def __init__
def unop_test
def binop_test
def sliceop_test
def setop_test
def set2op_test
def setsliceop_test
def test_lists
def test_dicts
def number_operators
def test_ints
def test_floats
def test_complexes
def test_explicit_reverse_methods
def test_spam_lists
def test_spam_dicts

Public Attributes

 binops
 unops
 imag
 real

Detailed Description

Definition at line 11 of file test_descr.py.


Constructor & Destructor Documentation

def test.test_descr.OperatorsTest.__init__ (   self,
  args,
  kwargs 
)

Definition at line 13 of file test_descr.py.

00013 
00014     def __init__(self, *args, **kwargs):
00015         unittest.TestCase.__init__(self, *args, **kwargs)
00016         self.binops = {
00017             'add': '+',
00018             'sub': '-',
00019             'mul': '*',
00020             'div': '/',
00021             'divmod': 'divmod',
00022             'pow': '**',
00023             'lshift': '<<',
00024             'rshift': '>>',
00025             'and': '&',
00026             'xor': '^',
00027             'or': '|',
00028             'cmp': 'cmp',
00029             'lt': '<',
00030             'le': '<=',
00031             'eq': '==',
00032             'ne': '!=',
00033             'gt': '>',
00034             'ge': '>=',
00035         }
00036 
00037         for name, expr in list(self.binops.items()):
00038             if expr.islower():
00039                 expr = expr + "(a, b)"
00040             else:
00041                 expr = 'a %s b' % expr
00042             self.binops[name] = expr
00043 
00044         self.unops = {
00045             'pos': '+',
00046             'neg': '-',
00047             'abs': 'abs',
00048             'invert': '~',
00049             'int': 'int',
00050             'float': 'float',
00051             'oct': 'oct',
00052             'hex': 'hex',
00053         }
00054 
00055         for name, expr in list(self.unops.items()):
00056             if expr.islower():
00057                 expr = expr + "(a)"
00058             else:
00059                 expr = '%s a' % expr
00060             self.unops[name] = expr

Here is the caller graph for this function:


Member Function Documentation

def test.test_descr.OperatorsTest.binop_test (   self,
  a,
  b,
  res,
  expr = "a+b",
  meth = "__add__" 
)

Definition at line 77 of file test_descr.py.

00077 
00078     def binop_test(self, a, b, res, expr="a+b", meth="__add__"):
00079         d = {'a': a, 'b': b}
00080 
00081         # XXX Hack so this passes before 2.3 when -Qnew is specified.
00082         if meth == "__div__" and 1/2 == 0.5:
00083             meth = "__truediv__"
00084 
00085         if meth == '__divmod__': pass
00086 
00087         self.assertEqual(eval(expr, d), res)
00088         t = type(a)
00089         m = getattr(t, meth)
00090         while meth not in t.__dict__:
00091             t = t.__bases__[0]
00092         # in some implementations (e.g. PyPy), 'm' can be a regular unbound
00093         # method object; the getattr() below obtains its underlying function.
00094         self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
00095         self.assertEqual(m(a, b), res)
00096         bm = getattr(a, meth)
00097         self.assertEqual(bm(b), res)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_descr.OperatorsTest.number_operators (   self,
  a,
  b,
  skip = [] 
)

Definition at line 216 of file test_descr.py.

00216 
00217     def number_operators(self, a, b, skip=[]):
00218         dict = {'a': a, 'b': b}
00219 
00220         for name, expr in list(self.binops.items()):
00221             if name not in skip:
00222                 name = "__%s__" % name
00223                 if hasattr(a, name):
00224                     res = eval(expr, dict)
00225                     self.binop_test(a, b, res, expr, name)
00226 
00227         for name, expr in list(self.unops.items()):
00228             if name not in skip:
00229                 name = "__%s__" % name
00230                 if hasattr(a, name):
00231                     res = eval(expr, dict)
00232                     self.unop_test(a, res, expr, name)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_descr.OperatorsTest.set2op_test (   self,
  a,
  b,
  c,
  res,
  stmt = "a[b]=c",
  meth = "__setitem__" 
)

Definition at line 131 of file test_descr.py.

00131 
00132     def set2op_test(self, a, b, c, res, stmt="a[b]=c", meth="__setitem__"):
00133         d = {'a': deepcopy(a), 'b': b, 'c': c}
00134         exec(stmt, d)
00135         self.assertEqual(d['a'], res)
00136         t = type(a)
00137         m = getattr(t, meth)
00138         while meth not in t.__dict__:
00139             t = t.__bases__[0]
00140         # in some implementations (e.g. PyPy), 'm' can be a regular unbound
00141         # method object; the getattr() below obtains its underlying function.
00142         self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
00143         d['a'] = deepcopy(a)
00144         m(d['a'], b, c)
00145         self.assertEqual(d['a'], res)
00146         d['a'] = deepcopy(a)
00147         bm = getattr(d['a'], meth)
00148         bm(b, c)
00149         self.assertEqual(d['a'], res)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_descr.OperatorsTest.setop_test (   self,
  a,
  b,
  res,
  stmt = "a+=b",
  meth = "__iadd__" 
)

Definition at line 112 of file test_descr.py.

00112 
00113     def setop_test(self, a, b, res, stmt="a+=b", meth="__iadd__"):
00114         d = {'a': deepcopy(a), 'b': b}
00115         exec(stmt, d)
00116         self.assertEqual(d['a'], res)
00117         t = type(a)
00118         m = getattr(t, meth)
00119         while meth not in t.__dict__:
00120             t = t.__bases__[0]
00121         # in some implementations (e.g. PyPy), 'm' can be a regular unbound
00122         # method object; the getattr() below obtains its underlying function.
00123         self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
00124         d['a'] = deepcopy(a)
00125         m(d['a'], b)
00126         self.assertEqual(d['a'], res)
00127         d['a'] = deepcopy(a)
00128         bm = getattr(d['a'], meth)
00129         bm(b)
00130         self.assertEqual(d['a'], res)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_descr.OperatorsTest.setsliceop_test (   self,
  a,
  b,
  c,
  d,
  res,
  stmt = "a[b:c]=d",
  meth = "__setitem__" 
)

Definition at line 150 of file test_descr.py.

00150 
00151     def setsliceop_test(self, a, b, c, d, res, stmt="a[b:c]=d", meth="__setitem__"):
00152         dictionary = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d}
00153         exec(stmt, dictionary)
00154         self.assertEqual(dictionary['a'], res)
00155         t = type(a)
00156         while meth not in t.__dict__:
00157             t = t.__bases__[0]
00158         m = getattr(t, meth)
00159         # in some implementations (e.g. PyPy), 'm' can be a regular unbound
00160         # method object; the getattr() below obtains its underlying function.
00161         self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
00162         dictionary['a'] = deepcopy(a)
00163         m(dictionary['a'], slice(b, c), d)
00164         self.assertEqual(dictionary['a'], res)
00165         dictionary['a'] = deepcopy(a)
00166         bm = getattr(dictionary['a'], meth)
00167         bm(slice(b, c), d)
00168         self.assertEqual(dictionary['a'], res)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_descr.OperatorsTest.sliceop_test (   self,
  a,
  b,
  c,
  res,
  expr = "a[b:c]",
  meth = "__getitem__" 
)

Definition at line 98 of file test_descr.py.

00098 
00099     def sliceop_test(self, a, b, c, res, expr="a[b:c]", meth="__getitem__"):
00100         d = {'a': a, 'b': b, 'c': c}
00101         self.assertEqual(eval(expr, d), res)
00102         t = type(a)
00103         m = getattr(t, meth)
00104         while meth not in t.__dict__:
00105             t = t.__bases__[0]
00106         # in some implementations (e.g. PyPy), 'm' can be a regular unbound
00107         # method object; the getattr() below obtains its underlying function.
00108         self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
00109         self.assertEqual(m(a, slice(b, c)), res)
00110         bm = getattr(a, meth)
00111         self.assertEqual(bm(slice(b, c)), res)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 255 of file test_descr.py.

00255 
00256     def test_complexes(self):
00257         # Testing complex operations...
00258         self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge',
00259                                                   'int', 'float',
00260                                                   'divmod', 'mod'])
00261 
00262         class Number(complex):
00263             __slots__ = ['prec']
00264             def __new__(cls, *args, **kwds):
00265                 result = complex.__new__(cls, *args)
00266                 result.prec = kwds.get('prec', 12)
00267                 return result
00268             def __repr__(self):
00269                 prec = self.prec
00270                 if self.imag == 0.0:
00271                     return "%.*g" % (prec, self.real)
00272                 if self.real == 0.0:
00273                     return "%.*gj" % (prec, self.imag)
00274                 return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag)
00275             __str__ = __repr__
00276 
00277         a = Number(3.14, prec=6)
00278         self.assertEqual(repr(a), "3.14")
00279         self.assertEqual(a.prec, 6)
00280 
00281         a = Number(a, prec=2)
00282         self.assertEqual(repr(a), "3.1")
00283         self.assertEqual(a.prec, 2)
00284 
00285         a = Number(234.5)
00286         self.assertEqual(repr(a), "234.5")
00287         self.assertEqual(a.prec, 12)

Here is the call graph for this function:

Definition at line 186 of file test_descr.py.

00186 
00187     def test_dicts(self):
00188         # Testing dict operations...
00189         self.binop_test({1:2,3:4}, 1, 1, "b in a", "__contains__")
00190         self.binop_test({1:2,3:4}, 2, 0, "b in a", "__contains__")
00191         self.binop_test({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
00192 
00193         d = {1:2, 3:4}
00194         l1 = []
00195         for i in list(d.keys()):
00196             l1.append(i)
00197         l = []
00198         for i in iter(d):
00199             l.append(i)
00200         self.assertEqual(l, l1)
00201         l = []
00202         for i in d.__iter__():
00203             l.append(i)
00204         self.assertEqual(l, l1)
00205         l = []
00206         for i in dict.__iter__(d):
00207             l.append(i)
00208         self.assertEqual(l, l1)
00209         d = {1:2, 3:4}
00210         self.unop_test(d, 2, "len(a)", "__len__")
00211         self.assertEqual(eval(repr(d), {}), d)
00212         self.assertEqual(eval(d.__repr__(), {}), d)
00213         self.set2op_test({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c",
00214                         "__setitem__")

Here is the call graph for this function:

Definition at line 288 of file test_descr.py.

00288 
00289     def test_explicit_reverse_methods(self):
00290         # see issue 9930
00291         self.assertEqual(complex.__radd__(3j, 4.0), complex(4.0, 3.0))
00292         self.assertEqual(float.__rsub__(3.0, 1), -2.0)

Here is the call graph for this function:

Definition at line 251 of file test_descr.py.

00251 
00252     def test_floats(self):
00253         # Testing float operations...
00254         self.number_operators(100.0, 3.0)

Here is the call graph for this function:

Definition at line 233 of file test_descr.py.

00233 
00234     def test_ints(self):
00235         # Testing int operations...
00236         self.number_operators(100, 3)
00237         # The following crashes in Python 2.2
00238         self.assertEqual((1).__bool__(), 1)
00239         self.assertEqual((0).__bool__(), 0)
00240         # This returns 'NotImplemented' in Python 2.2
00241         class C(int):
00242             def __add__(self, other):
00243                 return NotImplemented
00244         self.assertEqual(C(5), 5)
00245         try:
00246             C() + ""
00247         except TypeError:
00248             pass
00249         else:
00250             self.fail("NotImplemented should have caused TypeError")

Here is the call graph for this function:

Definition at line 169 of file test_descr.py.

00169 
00170     def test_lists(self):
00171         # Testing list operations...
00172         # Asserts are within individual test methods
00173         self.binop_test([1], [2], [1,2], "a+b", "__add__")
00174         self.binop_test([1,2,3], 2, 1, "b in a", "__contains__")
00175         self.binop_test([1,2,3], 4, 0, "b in a", "__contains__")
00176         self.binop_test([1,2,3], 1, 2, "a[b]", "__getitem__")
00177         self.sliceop_test([1,2,3], 0, 2, [1,2], "a[b:c]", "__getitem__")
00178         self.setop_test([1], [2], [1,2], "a+=b", "__iadd__")
00179         self.setop_test([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__")
00180         self.unop_test([1,2,3], 3, "len(a)", "__len__")
00181         self.binop_test([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__")
00182         self.binop_test([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__")
00183         self.set2op_test([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__")
00184         self.setsliceop_test([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d",
00185                         "__setitem__")

Here is the call graph for this function:

Definition at line 338 of file test_descr.py.

00338 
00339     def test_spam_dicts(self):
00340         # Testing spamdict operations...
00341         import copy, xxsubtype as spam
00342         def spamdict(d, memo=None):
00343             import xxsubtype as spam
00344             sd = spam.spamdict()
00345             for k, v in list(d.items()):
00346                 sd[k] = v
00347             return sd
00348         # This is an ugly hack:
00349         copy._deepcopy_dispatch[spam.spamdict] = spamdict
00350 
00351         self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
00352         self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
00353         self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
00354         d = spamdict({1:2,3:4})
00355         l1 = []
00356         for i in list(d.keys()):
00357             l1.append(i)
00358         l = []
00359         for i in iter(d):
00360             l.append(i)
00361         self.assertEqual(l, l1)
00362         l = []
00363         for i in d.__iter__():
00364             l.append(i)
00365         self.assertEqual(l, l1)
00366         l = []
00367         for i in type(spamdict({})).__iter__(d):
00368             l.append(i)
00369         self.assertEqual(l, l1)
00370         straightd = {1:2, 3:4}
00371         spamd = spamdict(straightd)
00372         self.unop_test(spamd, 2, "len(a)", "__len__")
00373         self.unop_test(spamd, repr(straightd), "repr(a)", "__repr__")
00374         self.set2op_test(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}),
00375                    "a[b]=c", "__setitem__")
00376         # Test subclassing
00377         class C(spam.spamdict):
00378             def foo(self): return 1
00379         a = C()
00380         self.assertEqual(list(a.items()), [])
00381         self.assertEqual(a.foo(), 1)
00382         a['foo'] = 'bar'
00383         self.assertEqual(list(a.items()), [('foo', 'bar')])
00384         self.assertEqual(a.getstate(), 0)
00385         a.setstate(100)
00386         self.assertEqual(a.getstate(), 100)

Here is the call graph for this function:

Definition at line 294 of file test_descr.py.

00294 
00295     def test_spam_lists(self):
00296         # Testing spamlist operations...
00297         import copy, xxsubtype as spam
00298 
00299         def spamlist(l, memo=None):
00300             import xxsubtype as spam
00301             return spam.spamlist(l)
00302 
00303         # This is an ugly hack:
00304         copy._deepcopy_dispatch[spam.spamlist] = spamlist
00305 
00306         self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b",
00307                        "__add__")
00308         self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__")
00309         self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__")
00310         self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__")
00311         self.sliceop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]",
00312                           "__getitem__")
00313         self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b",
00314                         "__iadd__")
00315         self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b",
00316                         "__imul__")
00317         self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__")
00318         self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b",
00319                         "__mul__")
00320         self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a",
00321                         "__rmul__")
00322         self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c",
00323                          "__setitem__")
00324         self.setsliceop_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]),
00325                              spamlist([1,5,6,4]), "a[b:c]=d", "__setitem__")
00326         # Test subclassing
00327         class C(spam.spamlist):
00328             def foo(self): return 1
00329         a = C()
00330         self.assertEqual(a, [])
00331         self.assertEqual(a.foo(), 1)
00332         a.append(100)
00333         self.assertEqual(a, [100])
00334         self.assertEqual(a.getstate(), 0)
00335         a.setstate(42)
00336         self.assertEqual(a.getstate(), 42)

Here is the call graph for this function:

def test.test_descr.OperatorsTest.unop_test (   self,
  a,
  res,
  expr = "len(a)",
  meth = "__len__" 
)

Definition at line 61 of file test_descr.py.

00061 
00062     def unop_test(self, a, res, expr="len(a)", meth="__len__"):
00063         d = {'a': a}
00064         self.assertEqual(eval(expr, d), res)
00065         t = type(a)
00066         m = getattr(t, meth)
00067 
00068         # Find method in parent class
00069         while meth not in t.__dict__:
00070             t = t.__bases__[0]
00071         # in some implementations (e.g. PyPy), 'm' can be a regular unbound
00072         # method object; the getattr() below obtains its underlying function.
00073         self.assertEqual(getattr(m, 'im_func', m), t.__dict__[meth])
00074         self.assertEqual(m(a), res)
00075         bm = getattr(a, meth)
00076         self.assertEqual(bm(), res)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 15 of file test_descr.py.

Definition at line 269 of file test_descr.py.

Definition at line 271 of file test_descr.py.

Definition at line 43 of file test_descr.py.


The documentation for this class was generated from the following file: