/Languages/IronPython/Tests/compat/sbs_true_division.py
Python | 114 lines | 82 code | 16 blank | 16 comment | 12 complexity | 3b7e3a5d2663c2a98772183aaf0fb3f1 MD5 | raw file
1#####################################################################################
2#
3# Copyright (c) Microsoft Corporation. All rights reserved.
4#
5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6# copy of the license can be found in the License.html file at the root of this distribution. If
7# you cannot locate the Apache License, Version 2.0, please send an email to
8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9# by the terms of the Apache License, Version 2.0.
10#
11# You must not remove this notice, or any other, from this software.
12#
13#
14#####################################################################################
15
16from __future__ import division
17
18# not all are testing true division
19
20import sys
21from common import *
22import testdata
23
24collection = testdata.merge_lists(
25 [None],
26 testdata.list_int,
27 testdata.list_float,
28 testdata.list_bool,
29 testdata.list_long,
30 testdata.list_complex,
31
32 testdata.list_myint,
33 testdata.list_myfloat,
34 testdata.list_mylong,
35 testdata.list_mycomplex,
36 )
37
38class oldstyle:
39 def __init__(self, value):
40 self.value = value
41 def __truediv__(self, other):
42 return self.value / other
43 def __rtruediv__(self, other):
44 return other / self.value
45 def __itruediv__(self, other):
46 # left side is no longer an oldstyle instance after /=
47 return self.value / other
48 def __repr__(self):
49 return "oldstyle(%s)" % str(self.value)
50
51class newstyle(object):
52 def __init__(self, value):
53 self.value = value
54 def __truediv__(self, other):
55 return self.value / other
56 def __rtruediv__(self, other):
57 return other / self.value
58 def __itruediv__(self, other):
59 return self.value / other
60 def __repr__(self):
61 return "newstyle(%s)" % str(self.value)
62
63collection_oldstyle = [oldstyle(x) for x in collection]
64collection_newstyle = [newstyle(x) for x in collection]
65
66def clone_list(l):
67 l2 = []
68 for x in l:
69 if x is newstyle:
70 l2.append(newstyle(x.value))
71 elif x is oldstyle:
72 l2.append(oldstyle(x.value))
73 else :
74 l2.append(x)
75 return l2
76
77class common(object):
78 def division(self, leftc, rightc):
79 for x in leftc:
80 for y in rightc:
81 printwith("case", x, "/", y, type(x), type(y))
82 try:
83 ret = x / y
84 printwithtype(ret)
85 except:
86 printwith("same", sys.exc_type)
87 def inplace(self, leftc, rightc):
88 rc = clone_list(rightc)
89 for y in rc:
90 for x in clone_list(leftc):
91 printwith("case", x, "/=", y, type(x), type(y))
92 try:
93 x /= y
94 printwithtype(x)
95 except:
96 printwith("same", sys.exc_type)
97
98class test_division(common):
99 def test_simple(self): super(test_division, self).division(collection, collection)
100
101 def test_class_ol(self): super(test_division, self).division(collection_oldstyle, collection)
102 def test_class_or(self): super(test_division, self).division(collection, collection_oldstyle)
103 def test_class_nl(self): super(test_division, self).division(collection_newstyle, collection)
104 def test_class_nr(self): super(test_division, self).division(collection, collection_newstyle)
105
106 def test_ip_simple(self): super(test_division, self).inplace(collection, collection)
107
108 def test_ip_class_ol(self): super(test_division, self).inplace(collection_oldstyle, collection)
109 def test_ip_class_or(self): super(test_division, self).inplace(collection, collection_oldstyle)
110 def test_ip_class_nl(self): super(test_division, self).inplace(collection_newstyle, collection)
111 def test_ip_class_nr(self): super(test_division, self).inplace(collection, collection_newstyle)
112
113runtests(test_division)
114