1.Draw CFG(Control flow graph) diagram for the given code segment. [5 marks]
2.Calculate Cyclometric Complexity. [1 marks]
3.State Basic Path Sets. [1 marks]
Generate test cases for each of the paths. (just mention the x and n values) [3 marks]
def checkSign(k):
if k > 0:
print(“Positive”)
elif k == 0:
print(“Zero”)
else:
print(“Negative”)
return
k = -k
print(“Negative Complement”)
x=5
n=2
while x>n:
x = x-1
checkSign(x)
print(“THE END”)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
(Entry)
|
v
(1) if k > 0:
|
|
|
|
(2) print("Positive")
|
|
|
v
(3) elif k == 0:
|
|
|
|
(4) print("Zero")
|
v
(5) else: print("Negative")
|
v
(6) return
(7) k = -k
(8) print("Negative Complement")
(9) x=5
(10) n=2
(11) while x>n:
|
|
|
v
(12) x = x-1
(13) checkSign(x)
(14) print("THE END")
|
|
|
v
(Exit)
Cyclomatic Complexity:
The Cyclomatic Complexity is calculated by counting the number of decision points (e.g. if/elif/else) and adding 1 to the number of basic blocks. In this case, there are 4 decision points (1, 3, 10, 11) and 15 basic blocks (1-6, 7, 8, 9, 10, 11-14). Cyclomatic Complexity = 4 + 15 - 1 = 15.
Basic Path Sets:
There are a total of 5 basic paths in this code segment:
(1) -> (2)
(1) -> (3) -> (4)
(1) -> (5)
(7) -> (8)
(11) -> (12) -> (13) -> (14) -> (Exit)
Generating Test Cases:
Test Case 1: k = 5 (positive integer) -> path 1, x, and n values are not relevant for this path.
Test Case 2: k = 0 (zero) -> path 2, x, and n values are not relevant for this path.
Test Case 3: k < 0 (negative integer) -> path 3, x, and n values are not relevant for this path.
Test Case 4: k = 1 (complement of -1) -> path 4, x, and n values are not relevant for this path.
Test Case 5: x > n (while loop execution) -> path 5. In this case, x can be set as 6 or any value greater than n (2) and n should be a smaller value.
Keep in mind that the "checkSign" function is called recursively within the while loop, so multiple calls for testing this function are implied in Test Case 5.