Back to index

python3.2  3.2.2
Public Member Functions
test.test_binop.RatTestCase Class Reference

List of all members.

Public Member Functions

def test_gcd
def test_constructor
def test_add
def test_sub
def test_mul
def test_div
def test_floordiv
def test_eq
def test_true_div

Detailed Description

Unit tests for Rat class and its support utilities.

Definition at line 201 of file test_binop.py.


Member Function Documentation

Definition at line 255 of file test_binop.py.

00255 
00256     def test_add(self):
00257         self.assertEqual(Rat(2, 3) + Rat(1, 3), 1)
00258         self.assertEqual(Rat(2, 3) + 1, Rat(5, 3))
00259         self.assertEqual(1 + Rat(2, 3), Rat(5, 3))
00260         self.assertEqual(1.0 + Rat(1, 2), 1.5)
00261         self.assertEqual(Rat(1, 2) + 1.0, 1.5)

Here is the call graph for this function:

Definition at line 219 of file test_binop.py.

00219 
00220     def test_constructor(self):
00221         a = Rat(10, 15)
00222         self.assertEqual(a.num, 2)
00223         self.assertEqual(a.den, 3)
00224         a = Rat(10, -15)
00225         self.assertEqual(a.num, -2)
00226         self.assertEqual(a.den, 3)
00227         a = Rat(-10, 15)
00228         self.assertEqual(a.num, -2)
00229         self.assertEqual(a.den, 3)
00230         a = Rat(-10, -15)
00231         self.assertEqual(a.num, 2)
00232         self.assertEqual(a.den, 3)
00233         a = Rat(7)
00234         self.assertEqual(a.num, 7)
00235         self.assertEqual(a.den, 1)
00236         try:
00237             a = Rat(1, 0)
00238         except ZeroDivisionError:
00239             pass
00240         else:
00241             self.fail("Rat(1, 0) didn't raise ZeroDivisionError")
00242         for bad in "0", 0.0, 0j, (), [], {}, None, Rat, unittest:
00243             try:
00244                 a = Rat(bad)
00245             except TypeError:
00246                 pass
00247             else:
00248                 self.fail("Rat(%r) didn't raise TypeError" % bad)
00249             try:
00250                 a = Rat(1, bad)
00251             except TypeError:
00252                 pass
00253             else:
00254                 self.fail("Rat(1, %r) didn't raise TypeError" % bad)

Here is the call graph for this function:

Definition at line 276 of file test_binop.py.

00276 
00277     def test_div(self):
00278         self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
00279         self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
00280         self.assertEqual(2 / Rat(5), Rat(2, 5))
00281         self.assertEqual(3.0 * Rat(1, 2), 1.5)
00282         self.assertEqual(Rat(1, 2) * 3.0, 1.5)

Here is the call graph for this function:

Definition at line 289 of file test_binop.py.

00289 
00290     def test_eq(self):
00291         self.assertEqual(Rat(10), Rat(20, 2))
00292         self.assertEqual(Rat(10), 10)
00293         self.assertEqual(10, Rat(10))
00294         self.assertEqual(Rat(10), 10.0)
00295         self.assertEqual(10.0, Rat(10))

Here is the call graph for this function:

Definition at line 283 of file test_binop.py.

00283 
00284     def test_floordiv(self):
00285         self.assertEqual(Rat(10) // Rat(4), 2)
00286         self.assertEqual(Rat(10, 3) // Rat(4, 3), 2)
00287         self.assertEqual(Rat(10) // 4, 2)
00288         self.assertEqual(10 // Rat(4), 2)

Here is the call graph for this function:

Definition at line 204 of file test_binop.py.

00204 
00205     def test_gcd(self):
00206         self.assertEqual(gcd(10, 12), 2)
00207         self.assertEqual(gcd(10, 15), 5)
00208         self.assertEqual(gcd(10, 11), 1)
00209         self.assertEqual(gcd(100, 15), 5)
00210         self.assertEqual(gcd(-10, 2), -2)
00211         self.assertEqual(gcd(10, -2), 2)
00212         self.assertEqual(gcd(-10, -2), -2)
00213         for i in range(1, 20):
00214             for j in range(1, 20):
00215                 self.assertTrue(gcd(i, j) > 0)
00216                 self.assertTrue(gcd(-i, j) < 0)
00217                 self.assertTrue(gcd(i, -j) > 0)
00218                 self.assertTrue(gcd(-i, -j) < 0)

Here is the call graph for this function:

Definition at line 269 of file test_binop.py.

00269 
00270     def test_mul(self):
00271         self.assertEqual(Rat(2, 3) * Rat(5, 7), Rat(10, 21))
00272         self.assertEqual(Rat(10, 3) * 3, 10)
00273         self.assertEqual(3 * Rat(10, 3), 10)
00274         self.assertEqual(Rat(10, 5) * 0.5, 1.0)
00275         self.assertEqual(0.5 * Rat(10, 5), 1.0)

Here is the call graph for this function:

Definition at line 262 of file test_binop.py.

00262 
00263     def test_sub(self):
00264         self.assertEqual(Rat(7, 2) - Rat(7, 5), Rat(21, 10))
00265         self.assertEqual(Rat(7, 5) - 1, Rat(2, 5))
00266         self.assertEqual(1 - Rat(3, 5), Rat(2, 5))
00267         self.assertEqual(Rat(3, 2) - 1.0, 0.5)
00268         self.assertEqual(1.0 - Rat(1, 2), 0.5)

Here is the call graph for this function:

Definition at line 296 of file test_binop.py.

00296 
00297     def test_true_div(self):
00298         self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3))
00299         self.assertEqual(Rat(10, 3) / 3, Rat(10, 9))
00300         self.assertEqual(2 / Rat(5), Rat(2, 5))
00301         self.assertEqual(3.0 * Rat(1, 2), 1.5)
00302         self.assertEqual(Rat(1, 2) * 3.0, 1.5)
00303         self.assertEqual(eval('1/2'), 0.5)

Here is the call graph for this function:


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