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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
1
00:00:00,320 --> 00:00:03,450
Let's look at the code for PrintSum in a slightly different way
2
00:00:03,450 --> 00:00:06,140
by making something explicit. If we go through the code, we can
3
00:00:06,140 --> 00:00:08,578
see that the, the code does something in that case, if the
4
00:00:08,578 --> 00:00:11,720
result greater then zero, does something else if the result is not
5
00:00:11,720 --> 00:00:14,890
greater than zero but is less than zero, and otherwise in the
6
00:00:14,890 --> 00:00:17,800
case in which neither of these two conditions is true. Nothing really
7
00:00:17,800 --> 00:00:19,090
happens. So we're going to make that
8
00:00:19,090 --> 00:00:21,290
explicit, we're going to say here, otherwise
9
00:00:21,290 --> 00:00:25,430
do nothing, which is exactly our problem, the code does nothing, in this
10
00:00:25,430 --> 00:00:28,380
case where it should do something. So now, let's look again in
11
00:00:28,380 --> 00:00:31,470
our test cases, let's consider the first one, and I'm going to go a
12
00:00:31,470 --> 00:00:34,410
little faster in this case, because we already saw what happens If
13
00:00:34,410 --> 00:00:37,890
we execute the first test case, we get to this point, we execute
14
00:00:37,890 --> 00:00:40,710
this statement, and then we just jump to the end, as we
15
00:00:40,710 --> 00:00:44,020
saw. Now we, if we execute the second test case, we do the
16
00:00:44,020 --> 00:00:46,740
same, we get to the else statement, the condition for the if is
17
00:00:46,740 --> 00:00:50,770
true, and therefore we execute this statement. And we never reached this point
18
00:00:50,770 --> 00:00:53,320
for either of the test cases. So how can we express
19
00:00:53,320 --> 00:00:56,470
this? In order to do that, I'm going to introduce a very useful
20
00:00:56,470 --> 00:01:00,450
concept. The concept of control flow graphs. The control flow graphs
21
00:01:00,450 --> 00:01:03,310
is just a representation for the code that is very convenient when
22
00:01:03,310 --> 00:01:06,030
we run our reason about the code and its structure. And
23
00:01:06,030 --> 00:01:09,910
it's a fairly simple one that represents statement with notes and the
24
00:01:09,910 --> 00:01:12,900
flow of control within the code with edges. So here's an
25
00:01:12,900 --> 00:01:16,180
example of control flow graph for this code. There is the entry
26
00:01:16,180 --> 00:01:19,790
point of the code right here, then our statement in which we
27
00:01:19,790 --> 00:01:23,320
assign the result of A plus B to variable result. Our if
28
00:01:23,320 --> 00:01:25,720
statement and as you can see the if statement it's got two
29
00:01:25,720 --> 00:01:28,930
branches coming out of it, because based on the outcome of this
30
00:01:28,930 --> 00:01:32,376
predicate we will go one way or the other. In fact normally
31
00:01:32,376 --> 00:01:36,000
what we do, we will label this edges accordingly. So for example,
32
00:01:36,000 --> 00:01:38,580
here we will say that this is the label to be executed
33
00:01:38,580 --> 00:01:41,470
when the predicate is true. And this is the label that is executed
34
00:01:41,470 --> 00:01:44,120
when the predicate is false. Now, at this point, similar
35
00:01:44,120 --> 00:01:47,080
thing, statement five which corresponds with this one, we have another
36
00:01:47,080 --> 00:01:49,650
if statement and if that statement is true, then we get
37
00:01:49,650 --> 00:01:51,830
to this point and if it's false, we get to this
38
00:01:51,830 --> 00:01:54,370
point. So as you can see, this graph represents my
39
00:01:54,370 --> 00:01:57,040
code, in a much more intuitive way, because I can see
40
00:01:57,040 --> 00:02:00,650
right away where the control flows, while I execute the code.
41
00:02:00,650 --> 00:02:01,730
So we're going to use this
42
00:02:01,730 --> 00:02:04,430
representation to introduce further coverage criteria.
|