Document Actions
Send this page to somebody Print this page
Parte 9

9.1. Doctest

Factorial

"""
This is the "factorial" module.

The example module supplies one function, factorial(). For example,

"""

def factorial(n):
"""Return the factorial of n, an exact integer >= 0.
If the result is small enough to fit in an int, return an int.
Else return a long.
"""

import math
if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(n) != n:
raise ValueError("n must be exact integer")
if n+1 == n: # catch a value like 1e300
raise OverflowError("n too large")
result = 1
factor = 2
while factor <= n:
result *= factor
factor += 1
return result
>>> from factorial import factorial
>>> factorial(1)
1
>>> factorial(2)
2
>>> factorial(3)
6
>>> factorial(4)
24
>>> factorial(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "factorial.py", line 46, in factorial
raise ValueError("n must be >= 0")
ValueError: n must be >= 0
>>> factorial(-2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "factorial.py", line 46, in factorial
raise ValueError("n must be >= 0")
ValueError: n must be >= 0
>>> from sys import maxint
>>> factorial(maxint*maxint)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "factorial.py", line 48, in factorial
raise ValueError("n must be exact integer")
ValueError: n must be exact integer
>>> maxint*maxint
4611686014132420609L
Editamos factorial.py
"""
This is the "example" module.

The example module supplies one function, factorial(). For example,

>>> factorial(5)
120
"""

def factorial(n):
"""Return the factorial of n, an exact integer >= 0.

If the result is small enough to fit in an int, return an int.
Else return a long.

>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> [factorial(long(n)) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(30)
265252859812191058636308480000000L
>>> factorial(30L)
265252859812191058636308480000000L
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0

Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000L

It must also not be ridiculously large:
>>> factorial(1e100)
Traceback (most recent call last):
...
OverflowError: n too large
"""

import math
if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(n) != n:
raise ValueError("n must be exact integer")
if n+1 == n: # catch a value like 1e300
raise OverflowError("n too large")
result = 1
factor = 2
while factor <= n:
result *= factor
factor += 1
return result

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()
$ python factorial.py 
$

Editando:

    while factor <= n:
result *= factor

por

    while factor <= n:
result += factor

resulta

$ python factorial.py 
**********************************************************************
File "factorial.py", line 6, in __main__
Failed example:
factorial(5)
Expected:
120
Got:
15
**********************************************************************
File "factorial.py", line 16, in __main__.factorial
Failed example:
[factorial(n) for n in range(6)]
Expected:
[1, 1, 2, 6, 24, 120]
Got:
[1, 1, 3, 6, 10, 15]
**********************************************************************
File "factorial.py", line 18, in __main__.factorial
Failed example:
[factorial(long(n)) for n in range(6)]
Expected:
[1, 1, 2, 6, 24, 120]
Got:
[1, 1, 3, 6, 10, 15]
**********************************************************************
File "factorial.py", line 20, in __main__.factorial
Failed example:
factorial(30)
Expected:
265252859812191058636308480000000L
Got:
465
**********************************************************************
File "factorial.py", line 22, in __main__.factorial
Failed example:
factorial(30L)
Expected:
265252859812191058636308480000000L
Got:
465
**********************************************************************
File "factorial.py", line 34, in __main__.factorial
Failed example:
factorial(30.0)
Expected:
265252859812191058636308480000000L
Got:
465
**********************************************************************
2 items had failures:
1 of 1 in __main__
5 of 8 in __main__.factorial
***Test Failed*** 6 failures.

Unit testing

"""
Este modulo imprime cuantos dias fatlan para un evento que ocurre el 12/08/2008

El modulo provee una sola funcion, cuantosDias(). For example,

"""

from datetime import *

def cuantosDias():
"""Devuelve cuantos dias faltan para las jornadas

>>> type( cuantosDias() )
<type 'int'>
"""
today = date.today()
evento = date(2008, 8, 12)
dias = evento - today

return(dias.days)


def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()
"""
Este modulo prueba jrsl con unit test
"""
from jrsl import *
import unittest

class Test(unittest.TestCase):
def test_dias(self):
self.assertEquals( cuantosDias(), 89 )

def test_tipo(self):
self.assertEquals( type(cuantosDias()) , int)

if __name__ == '__main__':
unittest.main()

Ejercicio

1. Hacer el caso del factorial con unit test. Hacer casos de test para los errores (ayuda usar assertRaises)

Copyright (C) 2004-2007 Menttes - All Rights Reserved