annotate Lib/test/test_binop.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Tests for binary operators on subtypes of built-in types."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
4 from test import test_support
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 def gcd(a, b):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 """Greatest common divisor using Euclid's algorithm."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 while a:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 a, b = b%a, a
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 return b
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 def isint(x):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 """Test whether an object is an instance of int or long."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 return isinstance(x, int) or isinstance(x, long)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 def isnum(x):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 """Test whether an object is an instance of a built-in numeric type."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 for T in int, long, float, complex:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 if isinstance(x, T):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 return 1
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 return 0
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 def isRat(x):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 """Test wheter an object is an instance of the Rat class."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 return isinstance(x, Rat)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 class Rat(object):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 """Rational number implemented as a normalized pair of longs."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 __slots__ = ['_Rat__num', '_Rat__den']
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def __init__(self, num=0L, den=1L):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 """Constructor: Rat([num[, den]]).
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 The arguments must be ints or longs, and default to (0, 1)."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 if not isint(num):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 raise TypeError, "Rat numerator must be int or long (%r)" % num
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 if not isint(den):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 raise TypeError, "Rat denominator must be int or long (%r)" % den
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 # But the zero is always on
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 if den == 0:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 raise ZeroDivisionError, "zero denominator"
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 g = gcd(den, num)
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19482
diff changeset
45 self.__num = long(num//g)
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19482
diff changeset
46 self.__den = long(den//g)
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def _get_num(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 """Accessor function for read-only 'num' attribute of Rat."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 return self.__num
19786
4abfc176685c Rename 'getset' to 'property'.
Guido van Rossum <guido@python.org>
parents: 19694
diff changeset
51 num = property(_get_num, None)
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 def _get_den(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 """Accessor function for read-only 'den' attribute of Rat."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return self.__den
19786
4abfc176685c Rename 'getset' to 'property'.
Guido van Rossum <guido@python.org>
parents: 19694
diff changeset
56 den = property(_get_den, None)
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def __repr__(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 """Convert a Rat to an string resembling a Rat constructor call."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 return "Rat(%d, %d)" % (self.__num, self.__den)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 def __str__(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 """Convert a Rat to a string resembling a decimal numeric value."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 return str(float(self))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 def __float__(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 """Convert a Rat to a float."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 return self.__num*1.0/self.__den
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 def __int__(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 """Convert a Rat to an int; self.den must be 1."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 if self.__den == 1:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 try:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 return int(self.__num)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 except OverflowError:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 raise OverflowError, ("%s too large to convert to int" %
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 repr(self))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 raise ValueError, "can't convert %s to int" % repr(self)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 def __long__(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 """Convert a Rat to an long; self.den must be 1."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 if self.__den == 1:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 return long(self.__num)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 raise ValueError, "can't convert %s to long" % repr(self)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def __add__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 """Add two Rats, or a Rat and a number."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 other = Rat(other)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 return Rat(self.__num*other.__den + other.__num*self.__den,
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 self.__den*other.__den)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if isnum(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 return float(self) + other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 __radd__ = __add__
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 def __sub__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 """Subtract two Rats, or a Rat and a number."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 other = Rat(other)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 if isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return Rat(self.__num*other.__den - other.__num*self.__den,
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.__den*other.__den)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 if isnum(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return float(self) - other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 def __rsub__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 """Subtract two Rats, or a Rat and a number (reversed args)."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 other = Rat(other)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 if isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return Rat(other.__num*self.__den - self.__num*other.__den,
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 self.__den*other.__den)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 if isnum(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 return other - float(self)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def __mul__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 """Multiply two Rats, or a Rat and a number."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 if isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 return Rat(self.__num*other.__num, self.__den*other.__den)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return Rat(self.__num*other, self.__den)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if isnum(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 return float(self)*other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 __rmul__ = __mul__
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 def __truediv__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 """Divide two Rats, or a Rat and a number."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 if isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 return Rat(self.__num*other.__den, self.__den*other.__num)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 return Rat(self.__num, self.__den*other)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if isnum(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return float(self) / other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 __div__ = __truediv__
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 def __rtruediv__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 """Divide two Rats, or a Rat and a number (reversed args)."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 if isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return Rat(other.__num*self.__den, other.__den*self.__num)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 return Rat(other*self.__den, self.__num)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 if isnum(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 return other / float(self)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 __rdiv__ = __rtruediv__
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 def __floordiv__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 """Divide two Rats, returning the floored result."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 other = Rat(other)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 elif not isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 x = self/other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 return x.__num // x.__den
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 def __rfloordiv__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 """Divide two Rats, returning the floored result (reversed args)."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 x = other/self
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 return x.__num // x.__den
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 def __divmod__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 """Divide two Rats, returning quotient and remainder."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 other = Rat(other)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 elif not isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 x = self//other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 return (x, self - other * x)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 def __rdivmod__(self, other):
34941
475f8adcb032 Fix a docstring with mismatched opening and closing quotes.
Brett Cannon <bcannon@gmail.com>
parents: 24364
diff changeset
181 """Divide two Rats, returning quotient and remainder (reversed args)."""
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 other = Rat(other)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 elif not isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 return divmod(other, self)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 def __mod__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 """Take one Rat modulo another."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 return divmod(self, other)[1]
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 def __rmod__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 """Take one Rat modulo another (reversed args)."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 return divmod(other, self)[1]
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 def __eq__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 """Compare two Rats for equality."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 if isint(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 return self.__den == 1 and self.__num == other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 if isRat(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 return self.__num == other.__num and self.__den == other.__den
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 if isnum(other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 return float(self) == other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 return NotImplemented
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 def __ne__(self, other):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 """Compare two Rats for inequality."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 return not self == other
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 34941
diff changeset
210 # Silence Py3k warning
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 34941
diff changeset
211 __hash__ = None
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 34941
diff changeset
212
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 class RatTestCase(unittest.TestCase):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 """Unit tests for Rat class and its support utilities."""
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 def test_gcd(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 self.assertEqual(gcd(10, 12), 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 self.assertEqual(gcd(10, 15), 5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 self.assertEqual(gcd(10, 11), 1)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 self.assertEqual(gcd(100, 15), 5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 self.assertEqual(gcd(-10, 2), -2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 self.assertEqual(gcd(10, -2), 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 self.assertEqual(gcd(-10, -2), -2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 for i in range(1, 20):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 for j in range(1, 20):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 34941
diff changeset
226 self.assertTrue(gcd(i, j) > 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 34941
diff changeset
227 self.assertTrue(gcd(-i, j) < 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 34941
diff changeset
228 self.assertTrue(gcd(i, -j) > 0)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 34941
diff changeset
229 self.assertTrue(gcd(-i, -j) < 0)
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 def test_constructor(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 a = Rat(10, 15)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 self.assertEqual(a.num, 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 self.assertEqual(a.den, 3)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 a = Rat(10L, 15L)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 self.assertEqual(a.num, 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 self.assertEqual(a.den, 3)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 a = Rat(10, -15)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 self.assertEqual(a.num, -2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 self.assertEqual(a.den, 3)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 a = Rat(-10, 15)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 self.assertEqual(a.num, -2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 self.assertEqual(a.den, 3)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 a = Rat(-10, -15)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 self.assertEqual(a.num, 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 self.assertEqual(a.den, 3)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 a = Rat(7)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 self.assertEqual(a.num, 7)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 self.assertEqual(a.den, 1)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 try:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 a = Rat(1, 0)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 except ZeroDivisionError:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 pass
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 else:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 self.fail("Rat(1, 0) didn't raise ZeroDivisionError")
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 for bad in "0", 0.0, 0j, (), [], {}, None, Rat, unittest:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 try:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 a = Rat(bad)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 except TypeError:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 pass
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 else:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 self.fail("Rat(%r) didn't raise TypeError" % bad)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 try:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 a = Rat(1, bad)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 except TypeError:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 pass
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 else:
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 self.fail("Rat(1, %r) didn't raise TypeError" % bad)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 def test_add(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 self.assertEqual(Rat(2, 3) + Rat(1, 3), 1)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 self.assertEqual(Rat(2, 3) + 1, Rat(5, 3))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 self.assertEqual(1 + Rat(2, 3), Rat(5, 3))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 self.assertEqual(1.0 + Rat(1, 2), 1.5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 self.assertEqual(Rat(1, 2) + 1.0, 1.5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 def test_sub(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 self.assertEqual(Rat(7, 2) - Rat(7, 5), Rat(21, 10))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 self.assertEqual(Rat(7, 5) - 1, Rat(2, 5))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 self.assertEqual(1 - Rat(3, 5), Rat(2, 5))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 self.assertEqual(Rat(3, 2) - 1.0, 0.5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 self.assertEqual(1.0 - Rat(1, 2), 0.5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
283
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 def test_mul(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 self.assertEqual(Rat(2, 3) * Rat(5, 7), Rat(10, 21))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 self.assertEqual(Rat(10, 3) * 3, 10)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 self.assertEqual(3 * Rat(10, 3), 10)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 self.assertEqual(Rat(10, 5) * 0.5, 1.0)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 self.assertEqual(0.5 * Rat(10, 5), 1.0)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 def test_div(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 self.assertEqual(2 / Rat(5), Rat(2, 5))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 self.assertEqual(3.0 * Rat(1, 2), 1.5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 self.assertEqual(Rat(1, 2) * 3.0, 1.5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
297
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 def test_floordiv(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 self.assertEqual(Rat(10) // Rat(4), 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 self.assertEqual(Rat(10, 3) // Rat(4, 3), 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 self.assertEqual(Rat(10) // 4, 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 self.assertEqual(10 // Rat(4), 2)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 def test_eq(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 self.assertEqual(Rat(10), Rat(20, 2))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 self.assertEqual(Rat(10), 10)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 self.assertEqual(10, Rat(10))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 self.assertEqual(Rat(10), 10.0)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 self.assertEqual(10.0, Rat(10))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 def test_future_div(self):
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 exec future_test
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
313
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 # XXX Ran out of steam; TO DO: divmod, div, future division
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 future_test = """
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 from __future__ import division
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 self.assertEqual(2 / Rat(5), Rat(2, 5))
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 self.assertEqual(3.0 * Rat(1, 2), 1.5)
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 self.assertEqual(Rat(1, 2) * 3.0, 1.5)
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19083
diff changeset
323 self.assertEqual(eval('1/2'), 0.5)
19083
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 """
5c27f0e4f50f A test suite for binary operators, disguised as a rational number
Guido van Rossum <guido@python.org>
parents:
diff changeset
325
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19786
diff changeset
326 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19786
diff changeset
327 test_support.run_unittest(RatTestCase)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19786
diff changeset
328
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19786
diff changeset
329
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19786
diff changeset
330 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19786
diff changeset
331 test_main()