-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_triangle_coverage.py
More file actions
129 lines (110 loc) · 3.87 KB
/
test_triangle_coverage.py
File metadata and controls
129 lines (110 loc) · 3.87 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from enum import Enum
from typing import Set
from coverage_report import (
print_test_execution,
print_coverage_summary,
print_branch_coverage_table,
print_latex_table,
run_tests,
)
class TriangleType(Enum):
NOT_A_TRIANGLE = "NOT_A_TRIANGLE"
SCALENE = "SCALENE"
EQUILATERAL = "EQUILATERAL"
ISOSCELES = "ISOSCELES"
def classify(num1: int, num2: int, num3: int, covered_stmts: Set[int], covered_branches: Set[str]) -> TriangleType:
type_result = None
# Branch 1: num1 > num2?
if num1 > num2:
covered_branches.add("1T")
covered_stmts.add(1) # temp = num1
temp = num1
covered_stmts.add(2) # num1 = num2
num1 = num2
covered_stmts.add(3) # num2 = temp
num2 = temp
else:
covered_branches.add("1F")
# Branch 2: num1 > num3?
if num1 > num3:
covered_branches.add("2T")
covered_stmts.add(4) # temp = num1
temp = num1
covered_stmts.add(5) # num1 = num3
num1 = num3
covered_stmts.add(6) # num3 = temp
num3 = temp
else:
covered_branches.add("2F")
# Branch 3: num2 > num3?
if num2 > num3:
covered_branches.add("3T")
covered_stmts.add(7) # temp = num2
temp = num2
covered_stmts.add(8) # num2 = num3
num2 = num3
covered_stmts.add(9) # num3 = temp
num3 = temp
else:
covered_branches.add("3F")
# Branch 4: num1 + num2 <= num3?
if num1 + num2 <= num3:
covered_branches.add("4T")
covered_stmts.add(10) # type_result = TriangleType.NOT_A_TRIANGLE
type_result = TriangleType.NOT_A_TRIANGLE
else:
covered_branches.add("4F")
covered_stmts.add(11) # type_result = TriangleType.SCALENE
type_result = TriangleType.SCALENE
# Branch 5: num1 == num2?
if num1 == num2:
covered_branches.add("5T")
# Branch 6: num2 == num3?
if num2 == num3:
covered_branches.add("6T")
covered_stmts.add(12) # type_result = TriangleType.EQUILATERAL
type_result = TriangleType.EQUILATERAL
else:
covered_branches.add("6F")
else:
covered_branches.add("5F")
# Branch 7: num1 == num2?
if num1 == num2:
covered_branches.add("7T")
covered_stmts.add(13) # type_result = TriangleType.ISOSCELES
type_result = TriangleType.ISOSCELES
else:
covered_branches.add("7F")
# Branch 8: num2 == num3?
if num2 == num3:
covered_branches.add("8T")
covered_stmts.add(14) # type_result = TriangleType.ISOSCELES
type_result = TriangleType.ISOSCELES
else:
covered_branches.add("8F")
covered_stmts.add(15) # return type_result
return type_result
def main():
test_cases = {
"(2,3,3)": (2, 3, 3),
"(3,2,1)": (3, 2, 1),
"(3,3,3)": (3, 3, 3),
"(2,2,3)": (2, 2, 3),
"(4,5,6)": (4, 5, 6),
}
test_results, total_covered_stmts, total_covered_branches = run_tests(test_cases, classify)
print_test_execution("Triangle Classify Coverage Test", test_cases, test_results, show_result=True)
all_statements = set(range(1, 16))
all_branches = {f"{i}{j}" for i in range(1, 9) for j in ["T", "F"]}
print_coverage_summary(total_covered_stmts, total_covered_branches, all_statements, all_branches)
print_branch_coverage_table(test_cases, test_results, max_branch_num=8)
print()
print_latex_table(
test_cases,
test_results,
max_branch_num=8,
caption="Branch coverage for Triangle.classify() by test case ($\\times$ = covered)"
)
return 0
if __name__ == "__main__":
exit(main())