blob: 331771f67295e0e03663ebe5a6607e4151e166e4 [file] [log] [blame]
Neil Schemenauerfd288c72001-01-02 16:30:31 +00001import copy
2import sys
Tim Petersf022a4d2002-04-15 23:52:04 +00003import warnings
Neil Schemenauerfd288c72001-01-02 16:30:31 +00004
5# Fake a number that implements numeric methods through __coerce__
6class CoerceNumber:
7 def __init__(self, arg):
8 self.arg = arg
9
10 def __repr__(self):
11 return '<CoerceNumber %s>' % repr(self.arg)
12
13 def __coerce__(self, other):
14 if isinstance(other, CoerceNumber):
15 return self.arg, other.arg
16 else:
17 return (self.arg, other)
18
19
20# Fake a number that implements numeric ops through methods.
21class MethodNumber:
22
23 def __init__(self,arg):
24 self.arg = arg
25
26 def __repr__(self):
27 return '<MethodNumber %s>' % repr(self.arg)
28
29 def __add__(self,other):
30 return self.arg + other
31
32 def __radd__(self,other):
33 return other + self.arg
34
35 def __sub__(self,other):
36 return self.arg - other
37
38 def __rsub__(self,other):
39 return other - self.arg
40
41 def __mul__(self,other):
42 return self.arg * other
43
44 def __rmul__(self,other):
45 return other * self.arg
46
47 def __div__(self,other):
48 return self.arg / other
49
50 def __rdiv__(self,other):
51 return other / self.arg
52
53 def __pow__(self,other):
54 return self.arg ** other
55
56 def __rpow__(self,other):
57 return other ** self.arg
58
59 def __mod__(self,other):
60 return self.arg % other
61
62 def __rmod__(self,other):
63 return other % self.arg
64
65 def __cmp__(self, other):
66 return cmp(self.arg, other)
67
68
Neil Schemenauer38796d02001-01-03 01:52:11 +000069candidates = [ 2, 4.0, 2L, 2+0j, [1], (2,), None,
Neil Schemenauer6cbba502004-03-10 17:30:03 +000070 MethodNumber(2), CoerceNumber(2)]
Neil Schemenauerfd288c72001-01-02 16:30:31 +000071
72infix_binops = [ '+', '-', '*', '/', '**', '%' ]
73prefix_binops = [ 'divmod' ]
74
Neil Schemenauer6cbba502004-03-10 17:30:03 +000075def format_float(value):
76 if abs(value) < 0.01:
77 return '0.0'
78 else:
79 return '%.1f' % value
80
81# avoid testing platform fp quirks
82def format_result(value):
83 if isinstance(value, complex):
84 return '(%s + %sj)' % (format_float(value.real),
85 format_float(value.imag))
86 elif isinstance(value, float):
87 return format_float(value)
88 return str(value)
89
Neil Schemenauerfd288c72001-01-02 16:30:31 +000090def do_infix_binops():
91 for a in candidates:
92 for b in candidates:
93 for op in infix_binops:
94 print '%s %s %s' % (a, op, b),
95 try:
96 x = eval('a %s b' % op)
97 except:
98 error = sys.exc_info()[:2]
99 print '... %s' % error[0]
100 else:
Neil Schemenauer6cbba502004-03-10 17:30:03 +0000101 print '=', format_result(x)
Neil Schemenauerfd288c72001-01-02 16:30:31 +0000102 try:
103 z = copy.copy(a)
104 except copy.Error:
105 z = a # assume it has no inplace ops
106 print '%s %s= %s' % (a, op, b),
107 try:
108 exec('z %s= b' % op)
109 except:
110 error = sys.exc_info()[:2]
111 print '... %s' % error[0]
112 else:
Neil Schemenauer6cbba502004-03-10 17:30:03 +0000113 print '=>', format_result(z)
Neil Schemenauerfd288c72001-01-02 16:30:31 +0000114
115def do_prefix_binops():
116 for a in candidates:
117 for b in candidates:
118 for op in prefix_binops:
119 print '%s(%s, %s)' % (op, a, b),
120 try:
121 x = eval('%s(a, b)' % op)
122 except:
123 error = sys.exc_info()[:2]
124 print '... %s' % error[0]
125 else:
Neil Schemenauer6cbba502004-03-10 17:30:03 +0000126 print '=', format_result(x)
Neil Schemenauerfd288c72001-01-02 16:30:31 +0000127
Tim Petersf022a4d2002-04-15 23:52:04 +0000128warnings.filterwarnings("ignore",
129 r'complex divmod\(\), // and % are deprecated',
Tim Petersd3925062002-04-16 01:27:44 +0000130 DeprecationWarning,
Barry Warsaw408b6d32002-07-30 23:27:12 +0000131 r'test.test_coercion$')
Tim Peters7d799482002-04-16 00:01:09 +0000132do_infix_binops()
133do_prefix_binops()