def testLongExpression(self): data1 = [ "-", ["+", ["x", [], []], ["y", [], []]], ["/", ["*", ["z", [], []], ["a", [], []]], ["b", [], []]] ] #x=30, y=20, z=100, a=10, b=5; x+y-z*a/b data2 = [["x", 30], ["y", 20], ["z", 100], ["a", 10], ["b", 5]] result = evalTree(data1, data2) self.assertEqual(-150, result)
def testDivisionByZero(self): data1 = ["/", ["x", [], []], ["0", [], []]] #x=1; x/0 data2 = [["x", 1]] result = evalTree(data1, data2) self.assertEqual(None, result)
def testBasicExpression(self): data1 = ["+", ["x", [], []], ["y", [], []]] #x=1, y=2; x+y data2 = [["x", 1], ["y", 2]] result = evalTree(data1, data2) self.assertEqual(3, result)
def testAddition(self): result = evalTree(["+", ["a", [], []], ["b", [], []]], [["a", 10], ["b", 20]]) self.assertEqual(result, 30)
def testEnvironment(self): data1 = ["y", [], []] #y=30, y data2 = [["a", 10], ["y", 30]] result = evalTree(data1, data2) self.assertEqual(30, result)
def test_3(self): test3Tree=["+",["a",[],[]],["*",["b",[],[]],["c",[],[]]]] test3Environment=[["a",10],["b",20],["c",30]] self.assertEqual(evalTree(test3Tree,test3Environment),610)
def testOne(self): result = evalTree(["1", [], []], []) self.assertEqual(result, 1)
def testChildMulti(self): result = evalTree([ "+", ["a", [], []], ["*", ["+", ["a", [], []], ["a", [], []]], ["c", [], []]] ], [["a", 10], ["b", 20], ["c", 30]]) self.assertEqual(result, 610)
def testEmpty(self): result = evalTree([[], [], []], []) self.assertEqual(result, None)
def testMultiplication(self): result = evalTree(["*", ["a", [], []], ["b", [], []]], [["a", 10], ["b", 20]]) self.assertEqual(result, 200)
def testDivision(self): result = evalTree(["/", ["a", [], []], ["b", [], []]], [["a", 10], ["b", 20]]) self.assertEqual(result, 0.5)
def testSubtraction(self): result = evalTree(["-", ["b", [], []], ["a", [], []]], [["a", 10], ["b", 20]]) self.assertEqual(result, 10)
def test_1(self): test1Tree=["10",[],[]] test1Environment=[["a",10],["b",20],["c",30]] self.assertEqual(evalTree(test1Tree,test1Environment),'10')
def test_4(self): test4Tree=["*",["b",[],[]],["c",[],[]]] test4Environment=[["a",10],["b",20],["c",30]] self.assertEqual(evalTree(test4Tree,test4Environment),600)
def testMissingVariable(self): data1 = ["+", ["x", [], []], ["y", [], []]] #x=1; x/y data2 = [["x", 1]] result = evalTree(data1, data2) self.assertEqual(None, result)
def testZeroDivision(self): result = evalTree(["/", ["a", [], []], ["0", [], []]], [["a", 10]]) self.assertEqual(result, None)
def testRoot(self): data1 = ["30", [], []] #x=125,y=250,z=300; 30 data2 = [["x", 125], ["y", 250], ["z", 300]] result = evalTree(data1, data2) self.assertEqual(30, result)
def test_2(self): test2Tree=["x1",[],[]] test2Environment=[["a",10],["x1",30]] self.assertEqual(evalTree(test2Tree,test2Environment),30)