def test_int_large_strings(self): x = "1" for i in range(100): x += x i += 1 frac = Rational(x, 3) self.assertEqual(frac.__str__(), x+"/3")
def bottomnumberchangeonly(self): frac1 = Rational(1, 10) frac2 = Rational(1, 5) quotient = frac2.__sub__(frac1) self.assertEqual(1, quotient.n) self.assertEqual(10, quotient.d)
def testGreaterDividend(self): frac1 = Rational(4, 5) frac2 = Rational(1, 6) quotient = frac1.__div__(frac2) self.assertEqual(24, quotient.n) self.assertEqual(5, quotient.d)
def testBigOverSmall(self): frac1 = Rational(6542, 5456) frac2 = Rational(1, 7) quotient = frac1.__div__(frac2) self.assertEqual(22897, quotient.n) self.assertEqual(2728, quotient.d)
def testWholeNumberDivisor(self): frac1 = Rational(3, 4) frac2 = Rational(6, 6) quotient = frac1.__div__(frac2) self.assertEqual(3, quotient.n) self.assertEqual(4, quotient.d)
def testWholeNumberDividend(self): frac1 = Rational(4, 4) frac2 = Rational(1, 3) quotient = frac1.__div__(frac2) self.assertEqual(3, quotient.n) self.assertEqual(1, quotient.d)
def testWholeNumberBoth(self): frac1 = Rational(5, 5) frac2 = Rational(2, 2) quotient = frac1.__div__(frac2) self.assertEqual(1, quotient.n) self.assertEqual(1, quotient.d)
def testZeroDividend(self): frac1 = Rational(0, 0) frac2 = Rational(1, 5) quotient = frac1.__div__(frac2) self.assertEqual(0, quotient.n) self.assertEqual(0, quotient.d)
def testEqual(self): frac1 = Rational(3, 5) frac2 = Rational(3, 5) quotient = frac1.__div__(frac2) self.assertEqual(1, quotient.n) self.assertEqual(1, quotient.d)
def testGreaterDivisor(self): frac1 = Rational(2, 5) frac2 = Rational(6, 7) quotient = frac1.__div__(frac2) self.assertEqual(7, quotient.n) self.assertEqual(15, quotient.d)
def testsubtractingnegative(self): frac1 = Rational(2, 5) frac2 = Rational(-6, 10) quotient = frac1.__sub__(frac2) self.assertEqual(0, quotient.n) self.assertEqual(1, quotient.d)
def equaldivision(self): frac1 = Rational(5, 5) frac2 = Rational(6, 6) quotient = frac1.__sub__(frac2) self.assertEqual(0, quotient.n) self.assertEqual(1, quotient.d)
def subtractingfrombigvalues(self): frac1 = Rational(100000000, 1) frac2 = Rational(1, 1) quotient = frac1.__sub__(frac2) self.assertEqual(99999999, quotient.n) self.assertEqual(1, quotient.d)
def subtractingbigvalues(self): frac1 = Rational(5, 2) frac2 = Rational(10000000, 2) quotient = frac1.__sub__(frac2) self.assertEqual(-9999995, quotient.n) self.assertEqual(2, quotient.d)
def subtractingzerovalue(self): frac1 = Rational(5, 2) frac2 = Rational(0, 100) quotient = frac1.__sub__(frac2) self.assertEqual(5, quotient.n) self.assertEqual(2, quotient.d)
def testSmallOverBig(self): frac1 = Rational(3, 4) frac2 = Rational(6234, 4345) quotient = frac1.__div__(frac2) self.assertEqual(4345, quotient.n) self.assertEqual(8312, quotient.d)
def testtopnumberchangeonly(self): frac1 = Rational(3, 5) frac2 = Rational(2, 5) quotient = frac1.__sub__(frac2) self.assertEqual(0, quotient.n) self.assertEqual(1, quotient.d)
def testNegativeDenominator(self): fraction1 = Rational(-6, 1) fraction2 = Rational(1, 1) product = fraction1.__mul__(fraction2) #Makes sure that the negative is in the numerator, as agreed upon on Feb. 17th, 2019, at 6:30pm self.assertNotEqual(6, product.n) self.assertNotEqual(-1, product.d) self.assertEqual(-6, product.n) self.assertEqual(1, product.d)
def testFractionNegative(self): fraction1 = Rational(-6, 6) fraction2 = Rational(9, 12) product = fraction1.__mul__(fraction2) #I am not sure if product.n or product.d holds the negative value of the resulting fraction #to combat this, I check if the resulting fraction is negative, then check the absolute value of the numbers realProduct = product.n / product.d self.assertEqual(realProduct < 0, true) self.assertEqual(abs(3), product.n) self.assertEqual(abs(4), product.d)
def testFloat(self): with self.assertRaises(TypeError): Rational(4.5, 1) with self.assertRaises(TypeError): Rational(1, 3.445) with self.assertRaises(TypeError): Rational(12.213, 123.4366)
def testBoolean(self): with self.assertRaises(TypeError): Rational(True, 1) with self.assertRaises(TypeError): Rational(1, False) with self.assertRaises(TypeError): Rational(False, True)
def testDictionary(self): d = {} with self.assertRaises(TypeError): Rational(d, 1) with self.assertRaises(TypeError): Rational(1, d) with self.assertRaises(TypeError): Rational(d, d)
def testRandom(self): #Might cause problems if you don't know what the random numbers are... for x in range(0, 20): top1 = random.randint(-1000000, 1000000) bot1 = random.randint(1, 1000000) top2 = random.randint(-1000000, 1000000) bot2 = random.randint(1, 1000000) fraction1 = Rational(top1, bot1) fraction2 = Rational(top2, bot2) product = fraction1.__mul__(fraction2) self.assertEqual(top1 * top2, product.n) self.assertEqual(bot1 * bot2, product.d)
def testTruple(self): truple = ('123', 2, True) with self.assertRaises(TypeError): Rational(truple, 1) with self.assertRaises(TypeError): Rational(1, truple) with self.assertRaises(TypeError): Rational(truple, truple)
def testList(self): list = [1, 2, 3] with self.assertRaises(TypeError): Rational(list, 1) with self.assertRaises(TypeError): Rational(1, list) with self.assertRaises(TypeError): Rational(list, list)
def testNormalizeNegatives(self): frac1 = Rational(-1, 3) self.assertEqual(-1, frac1.n) self.assertEqual(3, frac1.d) frac2 = Rational(1, -4) self.assertEqual(-1, frac1.n) self.assertEqual(4, frac1.d) frac3 = Rational(-2, -5) self.assertEqual(2, frac1.n) self.assertEqual(5, frac1.d)
def testObject(self): class Ob: def __init__(self): data = None ob = Ob() with self.assertRaises(TypeError): Rational(ob, 1) with self.assertRaises(TypeError): Rational(1, ob) with self.assertRaises(TypeError): Rational(ob, ob)
class Test__str__(unittest.TestCase): frac = Rational() def test_int_normal(self): frac = Rational(3, 4) self.assertEqual(frac.__str__(), "3/4") def test_int_normal_2(self): frac = Rational(1, 1) self.assertEqual(frac.__str__(), "1/1") def test_int_large_reduction(self): frac = Rational(-999, 999) self.assertEqual(frac.__str__(), "-1/1") def test_int_small_incretion(self): frac = Rational(.01/.0001) self.assertEqual(frac.__str__(), "100/1") def test_int_large_strings(self): x = "1" for i in range(100): x += x i += 1 frac = Rational(x, 3) self.assertEqual(frac.__str__(), x+"/3") def test_str_no_inproper_strings(self): frac = Rational("yes", "no") with self.assertRaises(Exception, msg="No, bad program, get off the couch"): frac.__str__() def test_str_strings_to_ints(self): frac = Rational("34", "22") self.assertEqual(frac.__str__(), "17/11") def test_str_no_objects(self): frac = Rational(object, object) with self.assertRaises(Exception, msg="No, bad program, get off the couch"): frac.__str__()
def testString(self): with self.assertRaises(TypeError): Rational("hello", 1) with self.assertRaises(TypeError): Rational(1, "hello") with self.assertRaises(TypeError): Rational("hello", "hello") with self.assertRaises(TypeError): Rational("123", 1) with self.assertRaises(TypeError): Rational(1, "23") with self.assertRaises(TypeError): Rational("122", "1565")
def test_int_normal_2(self): frac = Rational(1, 1) self.assertEqual(frac.__str__(), "1/1")