Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_functions.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/test_functions.py')
-rw-r--r--tests/unit/test_functions.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/unit/test_functions.py b/tests/unit/test_functions.py
new file mode 100644
index 0000000..4aae5ac
--- /dev/null
+++ b/tests/unit/test_functions.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+import unittest
+import math
+from decimal import Decimal
+from rational import Rational
+import functions
+
+
+class FunctionsTest(unittest.TestCase):
+
+ def setUp(self):
+ pass
+
+ def test_b10bin(self):
+ self.assertEqual(functions.b10bin(10111), 23)
+
+ with self.assertRaises(ValueError):
+ functions.b10bin(2)
+
+ def test_gcd(self):
+ self.assertEqual(functions.gcd(20, 10), 10)
+ with self.assertRaises(ValueError):
+ functions.gcd("20", 10)
+
+ def test_factorial(self):
+ self.assertEqual(functions.factorial(10), 3628800)
+
+ # 0! = 1 (see http://www.youtube.com/watch?v=Mfk_L4Nx2ZI for info)
+ self.assertEqual(functions.factorial(0), 1)
+
+ with self.assertRaises(ValueError):
+ functions.factorial("10")
+
+ def test_factorize(self):
+ self.assertEqual(functions.factorize(100), "2 * 2 * 5 * 5")
+
+ # 0 should be returned if argument doesn't pass is_int.
+ # It's raising errors now, is_int is wrong.
+ # self.assertEqual(functions.factorize("10"), 0)
+
+ def test_is_int(self):
+ self.assertTrue(functions.is_int(10))
+
+ # This exposes the mistake in is_int. This should not throw error, but
+ # does.
+ # self.assertFalse(functions.is_int("10"))
+
+ def test_div(self):
+ with self.assertRaises(ValueError):
+ functions.div(10, 0)
+
+ self.assertEqual(str(functions.div(10, 5)), str(2))
+ self.assertEqual(str(functions.div(50, 8)), "25/4")
+
+ def test_log10(self):
+ self.assertEqual(functions.log10(1000), 3)
+ with self.assertRaises(ValueError):
+ functions.log10(-1)
+
+ def test_ln(self):
+ self.assertEqual(functions.ln(5), math.log(5))
+
+ with self.assertRaises(ValueError):
+ functions.ln(-1)
+
+ def test_mod(self):
+ self.assertEqual(functions.mod(10, 3), 1)
+
+ # is_int problem again
+ # with self.assertRaises(ValueError):
+ # functions.mod(10, "1")