File tree Expand file tree Collapse file tree 2 files changed +37
-3
lines changed
source-code/testing/Hypothesis Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Original file line number Diff line number Diff line change @@ -25,3 +25,22 @@ def fac(n: int) -> int:
25
25
for i in range (n , 1 , - 1 ):
26
26
result *= i
27
27
return result
28
+
29
+
30
+ def fac_bad (n : int ) -> int :
31
+ '''Return the factorial of n
32
+
33
+ Parameters
34
+ ----------
35
+ n : int
36
+ The number to compute the factorial of
37
+
38
+ Returns
39
+ -------
40
+ int
41
+ The factorial of n
42
+ '''
43
+ result = 1
44
+ for i in range (n , 1 , - 1 ):
45
+ result *= i
46
+ return result
Original file line number Diff line number Diff line change 1
1
from math import factorial as fac_math
2
2
from hypothesis import assume , given , strategies as st
3
- from factorial import fac
3
+ from factorial import fac , fac_bad
4
4
5
5
6
6
@given (st .integers (min_value = 0 , max_value = 100 ))
7
- def test_fac_same (n ):
7
+ def test_fac (n ):
8
8
assert fac (n ) == fac_math (n )
9
9
10
10
11
11
@given (st .integers (max_value = - 1 ))
12
12
def test_fac_neg (n ):
13
13
try :
14
- assert fac (n ) == fac_math (n )
14
+ _ = fac (n )
15
+ assert False , 'no ValueError raised'
16
+ except ValueError :
17
+ assert n < 0
18
+
19
+
20
+ @given (st .integers (min_value = 0 , max_value = 100 ))
21
+ def test_fac_bad (n ):
22
+ assert fac_bad (n ) == fac_math (n )
23
+
24
+
25
+ @given (st .integers (max_value = - 1 ))
26
+ def test_fac_bad_neg (n ):
27
+ try :
28
+ _ = fac_bad (n )
29
+ assert False , 'no ValueError raised'
15
30
except ValueError :
16
31
assert n < 0
You can’t perform that action at this time.
0 commit comments