Beispiel #1
0
def doMath(op, op1, op2):
    if op == "*":
        return Mathlibrary.mul(op1, op2)
    elif op == "/":
        return Mathlibrary.div(op1, op2)
    elif op == "+":
        return Mathlibrary.add(op1, op2)
    elif op == "√":
        return Mathlibrary.root(op2, op1)
    elif op == "^":
        return Mathlibrary.pow(op1, op2)

    else:
        return Mathlibrary.sub(op1, op2)
Beispiel #2
0
class MathLibraryTestRoot(unittest.TestCase):
    def setUp(self):
        self.mathlib = Mathlibrary()

    def testRootNegativeExponent(self):
        with self.assertRaises(ValueError):
            self.mathlib.root(5, -2)
        with self.assertRaises(ValueError):
            self.mathlib.root(42, -421)

    def testRootFloatExponent(self):
        with self.assertRaises(ValueError):
            self.mathlib.root(2, 0.00001)
        with self.assertRaises(ValueError):
            self.mathlib.root(4194, 12.4201412)
        with self.assertRaises(ValueError):
            self.mathlib.root(935295, 75392.000001)

    def testRootEvenNegativeBase(self):
        with self.assertRaises(ValueError):
            self.mathlib.root(-3, 6)
        with self.assertRaises(ValueError):
            self.mathlib.root(-7318, 4917912)

    def testRootFractionNegativeBase(self):
        with self.assertRaises(ValueError):
            self.mathlib.root(-0.5, 6)
        with self.assertRaises(ValueError):
            self.mathlib.root(-1.2, 4917912)

    def testRootZeroExponent(self):
        with self.assertRaises(ValueError):
            self.mathlib.root(2, 0)

    def testRootNaturalExponent(self):
        self.assertEqual(self.mathlib.root(4, 2), 2)
        self.assertEqual(self.mathlib.root(-27, 3), -3)
        self.assertEqual(self.mathlib.root(907784931546351634835748413459499319296, 24), 42)
        self.assertEqual(self.mathlib.root(-62748517, 7), -13)
        self.assertEqual(self.mathlib.root(109418989131512359209, 42), 3)