示例#1
0
 def test_equal(self):
     bool1 = Rational(3, 4) == Rational(3, 4)
     bool2 = Rational(3, 4) != Rational(7, 4)
     bool3 = Rational(3, 4) == Rational(7, 4)
     bool4 = Rational(3, 4) != Rational(3, 4)
     self.assertEqual(bool1, True, "Error in equal")
     self.assertEqual(bool2, True, "Error in equal")
     self.assertEqual(bool3, False, "Error in equal")
     self.assertEqual(bool4, False, "Error in equal")
示例#2
0
 def test_BR4(self):
     x = Rational(-3, 4)
     y = Rational(3, -4)
     self.assertEqual(x, y, "Error in BR4")
示例#3
0
 def test_BR3(self):
     x = Rational(3, 6)
     y = Rational(1, 2)
     self.assertEqual(x, y, "Error in B3")
示例#4
0
 def test_BR2(self):
     x = Rational(2, 4)
     self.assertEqual(x.__str__(), "1/2",
                      "Rational(2, 4) should output 1/2")
示例#5
0
 def test_BR1(self):
     with self.assertRaises(ValueError):
         Rational(5, 0)
示例#6
0
 def test_div_neg(self):
     result = Rational(2, 3) / Rational(-5, 7)
     self.assertEqual(result, Rational(-14, 15),
                      "Error in dividing Negative fractions")
示例#7
0
 def test_mult_neg(self):
     result = Rational(2, 5) * Rational(-2, 5)
     self.assertEqual(result, Rational(-4, 25),
                      "Error in multiplying Negative fractions")
示例#8
0
 def test_div_pos(self):
     result = Rational(2, 3) / Rational(5, 7)
     self.assertEqual(result, Rational(14, 15),
                      "Error in dividing Positive fractions")
示例#9
0
 def test_sub_neg(self):
     result = Rational(2, 5) - Rational(-2, 5)
     self.assertEqual(result, Rational(4, 5),
                      "Error in Substracting Negative Fractions")
示例#10
0
 def test_mult_pos(self):
     result = Rational(2, 5) * Rational(2, 5)
     self.assertEqual(result, Rational(4, 25),
                      "Error multiplying Positive fractions")
示例#11
0
 def test_sub_pos(self):
     result = Rational(4, 5) - Rational(2, 5)
     self.assertEqual(result, Rational(2, 5),
                      "Error in Substracting Positive Fractions")
示例#12
0
 def test_add_neg(self):
     result = Rational(4, 5) - Rational(3, 5)
     self.assertEqual(result, Rational(1, 5),
                      "Error in Adding Negative Fractions")
示例#13
0
 def test_add_pos(self):
     result = Rational(2, 5) + Rational(2, 5)
     self.assertEqual(result, Rational(4, 5),
                      "Error in Adding Positive Fractions")
# Driver for class Rational.

from RationalNumber import Rational

# create objects of class Rational
rational1 = Rational()  # 1/1
rational2 = Rational(10, 30)  # 10/30 (reduces to 1/3)
rational3 = Rational(-7, 14)  # -7/14 (reduces to -1/2)

# print objects of class Rational
print("rational1:", rational1)
print("rational2:", rational2)
print("rational3:", rational3)
print()

# test mathematical operators
print(rational1, "/", rational2, "=", rational1 / rational2)
print(rational3, "-", rational2, "=", rational3 - rational2)
print(rational2, "*", rational3, "-", rational1, "=", \
   rational2 * rational3 - rational1)

# overloading + implicitly overloads +=
rational1 += rational2 * rational3
print("\nrational1 after adding rational2 * rational3:", rational1)
print()

# test comparison operators
print(rational1, "<=", rational2, ":", rational1 <= rational2)
print(rational1, ">", rational3, ":", rational1 > rational3)
print()
# Fig. 8.10: relationalNumber_example.py
# Driver for class Rational.

from RationalNumber import Rational

# create objects of class Rational
rational1 = Rational()  # 1/1
rational2 = Rational(10, 30)  # 10/30 (reduces to 1/3)
rational3 = Rational(-7, 14)  # -7/14 (reduces to -1/2)

# print objects of class Rational
print "rational1:", rational1
print "rational2:", rational2
print "rational3:", rational3
print

# test mathematical operators
print rational1, "/", rational2, "=", rational1 / rational2
print rational3, "-", rational2, "=", rational3 - rational2
print rational2, "*", rational3, "-", rational1, "=", \
   rational2 * rational3 - rational1

# overloading + implicitly overloads +=
rational1 += rational2 * rational3
print "\nrational1 after adding rational2 * rational3:", rational1
print

# test comparison operators
print rational1, "<=", rational2, ":", rational1 <= rational2
print rational1, ">", rational3, ":", rational1 > rational3
print