英文:
Are closing brackets counted as statements in Statement Coverage? What about if they come after a return statement?
问题
I'm doing a simple assignment where I have to go over test case coverage (statement coverage, path coverage, etc.) of a function.
我正在做一个简单的任务,需要检查函数的测试用例覆盖率(语句覆盖、路径覆盖等)。
I've tried endlessly to add code here and StackOverflow won't accept it no matter how I format it, so I'll just explain a very simple example.
我尝试不断地在这里添加代码,但无论我如何格式化,StackOverflow都不接受它,所以我将只解释一个非常简单的例子。
Let's say you get to an if statement that has a return statement inside it. In the line below the return line is the if's closing bracket '}'.
假设你遇到一个包含 return 语句的 if 语句。在 return 行下面的一行是 if 的闭合括号 '}'。
My professor and our textbook have been pretty vague about what a statement is, but my understanding is that for a line of code to be a statement it has to perform some type of function such as assigning a value to a variable or being a conditional statement like an if or while loop.
我的教授和我们的教材对于语句的定义相当模糊,但我的理解是,要使一行代码成为语句,它必须执行某种功能,如为变量赋值或是条件语句,如 if 或 while 循环。
So my questions are:
- Does the closing bracket count as a statement? Or do they only count as a line?
- When the computer is reading the code and hits the return statement, does it jump to the correct number of closing brackets before leaving the function and returning a value?
因此,我的问题是:
- 闭合括号是否算作一个语句?还是它们只算作一行?
- 当计算机读取代码并遇到 return 语句时,它是否会跳转到正确数量的闭合括号,然后离开函数并返回一个值?
英文:
I'm doing a simple assignment where I have to go over test case coverage(statement coverage, path coverage, etc) of a function.
I've tried endlessly to add code here and StackOverflow won't accept it no matter how I format it, so I'll just explain a very simple example.
Let's say you get to an if statement that has a return statement inside it. In the line below the return line is the if's closing bracket '}'
My professor and our textbook have been pretty vague about what a statement is, but my understanding is that for a line of code to be a statement it has to perform some type of function such as assigning a value to a variable or being a conditional statement like an if or while loop.
So my questions are:
- Does the closing bracket count as a statement? Or do they only count as a line?
- When the computer is reading the code and hits the return statement, does it jump to the correct number of closing brackets before leaving the function and returning a value?
答案1
得分: 1
以下是您要翻译的部分:
Closing brackets are not traditionally counted as statements.
Even if they follow a return
(or any other kind of unconditional control transfer, e.g. goto
to raise
exception.).
A more interesting case is:
if (...) {
return;
x=1;
}
The x=1;
statement can't get control. Test coverage should tell you it is uncovered because it never gets executed.
This example is exactly the same code, slightly reformatted:
if (...) {
return; x=1; }
Many test coverage tools will show you covered lines rather than covered code, so would mark the return line as covered. That's wrong; x=1;
is still uncovered and the coverage display tool should make that clear.
This is especially important if you have a long source line which exits (e.g., by exception) in the middle:
x=foo().bar().baz();
If bar()
throws an exception, then foo().
is covered, x=...
is not, and baz()
is not. This shows why line coverage, while common, can be very misleading.
Now consider this case:
if (a) {
...
if (b)
return;
}
c;
If a and b
are true, the return
statement executes. Control doesn't flow past the if (b)
statement. If a
is true and b
is false... the return isn't executed and control flows past the if (b)
to statement c;
. In this case you can argue the }
is "covered" even though it isn't a statement. A good coverage tool should show } c;
as covered.
英文:
Closing brackets are not traditionally counted as statements.
Even if they follow a return
(or any other kind of unconditional control transfer, e.g. goto
to raise
exception.).
A more interesting case is:
if (...) {
return;
x=1;
}
The x=1;
statement can't get control. Test coverage should tell you it is uncovered because it never gets executed.
This example is exactly the same code, slightly reformatted:
if (...) {
return; x=1; }
Many test coverage tools will show you covered lines rather than covered code, so would mark the return line as covered. That's wrong; x=1;
is still uncovered and the coverage display tool should make that clear.
This is especially important if you have a long source line which exits (e.g., by exception) in the middle:
x=foo().bar().baz();
If bar()
throws an exception, then foo().
is covered, x=...
is not, and baz()
is not. This shows why line coverage, while common, can be very misleading.
Now consider this case:
if (a) {
...
if (b)
return;
}
c;
If a and b
are true, the return
statement executes. Control doesn't flow past the if (b)
statement. If a
is true and b
is false... the return isn't executed and control flows past the if (b)
to statement c;
. In this case you can argue the }
is "covered" even though it isn't a statement. A good coverage tool should show } c;
as covered.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论