class TestPrintFunctions(unittest.TestCase): def setUp(self): self.ev = Evaluator() def tearDown(self): del self.ev def test_log5(self): self.ev.ev('format "%0.5f"') self.ev.ev('2 log ') x = self.ev.pop() actual = self.ev.format_value(x) expected = "%0.5f" % (math.log10(2)) self.assertEqual(expected, actual) def test_print_hex(self): self.ev.ev('format "0x%04x"') self.ev.ev('50') x = self.ev.pop() actual = self.ev.format_value(x) expected = "0x0032" self.assertEqual(expected, actual) self.ev.ev('depth') actual = self.ev.pop() expected = 0 self.assertEqual(expected, actual)
class TestTrigonometricFunctions(unittest.TestCase): pi = 3.14159265358979 def setUp(self): self.ev = Evaluator() def tearDown(self): del self.ev def test_todegrees(self): self.ev.ev("pi toDegrees") expected = 180 actual = self.ev.pop() self.assertEqual(expected, actual) def test_toradians(self): self.ev.ev("90 toRadians") expected = self.pi / 2.0 actual = self.ev.pop() self.assertAlmostEqual(expected, actual) def test_acos(self): self.ev.ev("0.5 acos toDegrees") expected = 60 actual = self.ev.pop() self.assertAlmostEqual(expected, actual) def test_asin(self): self.ev.ev("3 sqrt 2 / asin toDegrees") expected = 60 actual = self.ev.pop() self.assertAlmostEqual(expected, actual) def test_atan(self): self.ev.ev("1 atan") actual = self.ev.pop() expected = self.pi / 4 self.assertAlmostEqual(expected, actual) def test_atan2(self): self.ev.ev("-1 -1 atan2") actual = self.ev.pop() expected = -3 * self.pi / 4 self.assertAlmostEqual(expected, actual) def test_cos(self): self.ev.ev("55 toRadians cos") actual = self.ev.pop() expected = 0.573576436351 self.assertAlmostEqual(expected, actual) def test_sin(self): self.ev.ev("14 toRadians sin") actual = self.ev.pop() expected = 0.2419218956 self.assertAlmostEqual(expected, actual)
class TestLogFunctions(unittest.TestCase): def setUp(self): self.ev = Evaluator() def tearDown(self): del self.ev def test_exp(self): self.ev.ev("2 exp") actual = self.ev.pop() expected = 7.389056098 self.assertAlmostEqual(expected, actual) def test_ln(self): self.ev.ev("2 ln") actual = self.ev.pop() expected = 0.6931471805599453 self.assertAlmostEqual(expected, actual) def test_log(self): self.ev.ev("2 log") actual = self.ev.pop() expected = 0.301029995663981 self.assertAlmostEqual(expected, actual)
class TestStackFunctions(unittest.TestCase): def setUp(self): self.ev = Evaluator() def tearDown(self): del self.ev def test_clear(self): self.ev.ev("1 2 3") self.assertEqual(len(self.ev.stack), 3) self.ev.ev("clear") self.assertEqual(len(self.ev.stack), 0) def test_depth(self): self.ev.ev("45 11 depth") expected = 2 actual = self.ev.pop() self.assertEqual(expected, actual) def test_drop(self): self.ev.ev("1 2 3 drop") self.assertListEqual(self.ev.stack, [1.0, 2.0]) def test_dup(self): self.ev.ev("5 dup") self.assertListEqual(self.ev.stack, [5, 5]) def test_over(self): self.ev.ev("1 2 over") self.assertListEqual(self.ev.stack, [1, 2, 1]) def test_rot(self): self.ev.ev("10 20 30 rot") self.assertListEqual(self.ev.stack, [20, 30, 10]) def test_swap(self): self.ev.ev("10 20 30 swap") self.assertListEqual(self.ev.stack, [10, 30, 20])
class TestArithmeticOperations(unittest.TestCase): def setUp(self): self.ev = Evaluator() def tearDown(self): del self.ev def test_add(self): ev = self.ev ev.ev('2 3') ev.do_add() expected = 5 actual = ev.pop() self.assertEqual(expected, actual) def test_sub(self): ev = self.ev ev.ev('3 5') ev.do_sub() expected = -2 actual = ev.pop() self.assertEqual(expected, actual) def test_mult(self): self.ev.ev("10 1.8 *") expected = 18 actual = self.ev.pop() self.assertEqual(expected, actual) def test_div(self): self.ev.ev("10 1.8 /") expected = 5.55555555 actual = self.ev.pop() self.assertAlmostEqual(expected, actual) def test_increment(self): self.ev.ev("2 ++") expected = 3 actual = self.ev.pop() self.assertEqual(expected, actual) self.ev.ev("4 1+") expected = 5 actual = self.ev.pop() self.assertEqual(expected, actual) def test_decrement(self): self.ev.ev("2 --") expected = 1 actual = self.ev.pop() self.assertEqual(expected, actual) self.ev.ev("4 1-") expected = 3 actual = self.ev.pop() self.assertEqual(expected, actual) def test_mod(self): self.ev.ev("7 3 mod") expected = 1 actual = self.ev.pop() self.assertEqual(expected, actual) self.ev.ev("14 5 %") expected = 4 actual = self.ev.pop() self.assertEqual(expected, actual) def test_int(self): self.ev.ev("7 3 / int") expected = 2 actual = self.ev.pop() self.assertEqual(expected, actual) def test_sqrt(self): self.ev.ev("9 sqrt") expected = 3 actual = self.ev.pop() self.assertEqual(expected, actual) def test_pow(self): self.ev.ev("2 3 **") expected = 8 actual = self.ev.pop() self.assertEqual(expected, actual) def test_empty_stack(self): with StringIO() as fp: with stdout_redirected(fp): ev = self.ev ev.ev('2') ev.do_add() output = fp.getvalue() self.assertIn("Stack empty", output)