def test_rpn_complex(self): istr4 = '0.2 10.24 pi * 180 / * 10.24 pi * 180 / sin /' rpnins4 = Rpn(istr4) self.assertAlmostEqual(rpnins4.solve(), 0.2 * (10.24 * math.pi) / 180 / math.sin(10.24 * math.pi / 180), places=NPRC)
def test_rpn_const_pi(self): self.assertAlmostEqual(Rpn('1 pi *').solve(), 3.14159265359, places=NPRC) self.assertAlmostEqual(Rpn('1 PI *').solve(), 3.14159265359, places=NPRC)
def test_rpn_sqrt(self): self.assertAlmostEqual(Rpn('4.0 sqrt').solve(), math.sqrt(4.0), places=NPRC) self.assertAlmostEqual(Rpn('2.0 sqrt').solve(), math.sqrt(2.0), places=NPRC) self.assertIsNone(Rpn('-1.0 sqrt').solve())
def test(): istr1 = '1 2 + 3 * sin' rpnins1 = Rpn(istr1) print(rpnins1) print(rpnins1.solve()) istr2 = '1 2 + 3 * cos' rpnins2 = Rpn(istr2) print(rpnins2) print(rpnins2.solve()) istr3 = '10,3,1,2,/,*,-,tan' rpnins3 = Rpn(istr3, ',') print(rpnins3) print(rpnins3.solve()) istr4 = '0.2 10.24 pi * 180 / * 10.24 pi * 180 / sin /' rpnins4 = Rpn(istr4) print(rpnins4.solve()) istr5 = '0.2 10.24 pi * 180 / * 10.24 pi * 180 / sin /' print(Rpn.solve_rpn(istr5))
def test_rpn_sto_2(self): """test sto operation: variables""" a = Rpn('1 2 + sto three') self.assertEqual(a.variables, {'pi': math.pi}) a.solve() self.assertEqual(a.variables, {'pi': math.pi, 'three': 3.0})
def test_rpn_sto_1(self): """test sto operation: solve""" self.assertAlmostEqual(Rpn('1 2 + sto three').solve(), 3, places=NPRC) self.assertAlmostEqual(Rpn('1 2 + sto three pop three sin').solve(), 0.141120008059867, places=NPRC)
def test_rpn_solve_str(self): self.assertAlmostEqual(Rpn.solve_rpn('2 cos'), math.cos(2), places=NPRC)
def test_rpn_func2(self): istr3 = '10 3 1 2 / * - tan' rpnins3 = Rpn(istr3) self.assertAlmostEqual(rpnins3.solve(), math.tan(10 - 3 * (1.0 / 2.0)), places=NPRC)
def test_rpn_sep(self): istr3 = '10,3,1,2,/,*,-,tan' rpnins3 = Rpn(istr3, ',') self.assertAlmostEqual(rpnins3.solve(), math.tan(10 - 3 * (1.0 / 2.0)), places=NPRC)
def test_rpn_func1(self): istr1 = '1 2 + 3 * sin' rpnins1 = Rpn(istr1) self.assertAlmostEqual(rpnins1.solve(), math.sin(3 * 3), places=NPRC)
def test_rpn_simple2(self): self.assertAlmostEqual(1.5, Rpn('0.5 1 2 + *').solve(), places=NPRC)
def test_rpn_simple1(self): self.assertAlmostEqual(2, Rpn('1 1 +').solve(), places=NPRC)
def test_repr(self): istr1 = '1 2 + 3 * sin' rpnins1 = Rpn(istr1) self.assertEqual(istr1, str(rpnins1))
def test(): istr1 = '1 2 + 3 * sin' rpnins1 = Rpn(istr1) print rpnins1 print rpnins1.solve() istr2 = '1 2 + 3 * cos' rpnins2 = Rpn(istr2) print rpnins2 print rpnins2.solve() istr3 = '10,3,1,2,/,*,-,tan' rpnins3 = Rpn(istr3, ',') print rpnins3 print rpnins3.solve() istr4 = '0.2 10.24 pi * 180 / * 10.24 pi * 180 / sin /' rpnins4 = Rpn(istr4) print rpnins4.solve() istr5 = '0.2 10.24 pi * 180 / * 10.24 pi * 180 / sin /' print Rpn.solve_rpn(istr5)