1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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")
|