Draw the CFG for this code using the conventions from class.
List the unique program paths.
Add assert statements to the following test case that exercise all unique program paths.
deftest_is_prime():# Your code here.
Solution
CFG for is_prime()unique program paths
The unique edges in the path are highlighted. You do not need to highlight the unique edges on the quiz.
(1, 2, 3)
(1, 2, 4, 7)
(1, 2, 4, 5, 6)
(1, 2, 4, 5, 4, 7) or (1, 2, 4, 5, 4, 5, 6)
test case
deftest_is_prime():# Some paths can be exercised with multiple input values. # The goal is to exercise all program paths.assertis_prime(1)==True# tests path (1, 2, 3)assertis_prime(2)==True# path (1, 2, 4, 7)assertis_prime(4)==False# path (1, 2, 4, 5, 6)assertis_prime(5)==True# path (1, 2, 4, 5, 4, 7)
Sample 2
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
defgenerate_fibonacci(n):ifn<=0:return"Error: Number of terms must be a positive integer"fibonacci_sequence=[1]a=1b=2count=1whilecount<n:fibonacci_sequence.append(a)# Update values of a and b to the next numbers in the sequencea,b=b,a+bcount+=1returnfibonacci_sequence
Draw the CFG for this code using the conventions from class.
List the unique program paths.
Add assert statements to the following test case that exercise all unique program paths.
deftest_generate_fibonacci():# Your code here.
Solution
CFG for generate_fibonacci()unique program paths
The unique edges in the path are highlighted. You do not need to highlight the unique edges on the quiz.
(14, 15, 16)
(14, 15, 18-21, 23, 29)
(14, 15, 18-21, 23, 24-27, 23, 29)
test case
deftest_generate_fibonnaci():# Some paths can be exercised with multiple input values. # The goal is to exercise all program paths.assertgenerate_fibonnaci(0)=="Error: Number of terms must be a positive integer"# tests path (14, 15, 16)assertgenerate_fibonnaci(1)==[1]# path (14, 15, 18-21, 23, 29)assertgenerate_fibonnaci(6)==[1,1,2,3,5,8]# path (14, 15, 18-21, 23, 24-27, 23, 29)
Sample 3
36
37
38
39
40
41
42
43
44
45
deffactorial(n):# Input validationifnotisinstance(n,int)orn<0:return"Error: Input must be a non-negative integer"# Use iterative approachresult=1foriinrange(1,n+1):result*=ireturnresult
Draw the CFG for this code using the conventions from class.
List the unique program paths.
Add assert statements to the following test case that exercise all unique program paths.
deftest_factorial():# Your code here.
Solution
CFG for factorial()unique program paths
The unique edges in the path are highlighted. You do not need to highlight the unique edges on the quiz.
(36, 38, 39)
(36, 38, 42, 43, 45)
(36, 38, 42, 43, 44, 43, 45)
test case
deftest_factorial():# Some paths can be exercised with multiple input values. # The goal is to exercise all program paths.assertfactorial("Alice")=="Error: Number of terms must be a positive integer"# tests path(36, 38, 39)assertfactorial(-1)=="Error: Number of terms must be a positive integer"# also tests path(36, 38, 39)assertfactorial(0)==1# tests path(36, 38, 42, 43, 45)assertfactorial(5)==120# tests path(36, 38, 42, 43, 44, 43, 45)