已知检查括号匹配及注释、字符串处理的C源程序如下: #include<stdio.h> int brace,brack,paren; void in_quote(int c); void in_comment(void); void search(int c); /*rudimentary syntax checKer for C program*/ int main() { int c; extern int brace,brack,paren; while((c=getchar())!=EOF){ if(c==’/’){ if((c=getchar())==’*’) in_comment(); /*inside comment*/ else search(C) ; }else if(c==’\"||c=="") in_quote(c); /*inside quote*/ else search(c); if(brace<0){ /*output errors*/ printf("Unbalanced braces\n"); brace=0; }else if(brack<0){ printf("Unbalanced brackets\n"); brack=0; }else if(paren<0){ printf("Unbalanced parentheses\n"); paren=0; } } if(brace>0) /*output errors*/ printf("Unbalanced braces\n"); if(brack>0) printf("Unbalanced brackets\n"); if(paren>0) printf("Unbalanced parentheses\n"); return 0; } /*search:search for rudimentary syntax errors*/ void search(int c) { extern int brace,brack,paren; if(c==’{’) ++brace; else if(c==’}’) --brace; else if(c==’[’) ++brack; else if(c==’]’) --brack; else if(c==’(’) ++paren; else if(c==’)’) --paren; } /*in_comment:inside of a valid comment*/ void in_comment(void) { int c,d; c=getchar(); d=getchar(); /*curr character*/ while(c!=’*’||d!=’/’){ /*search for end*/ c=d; d=getchar(); } } /*in_quote:inside quote*/ void in_quote(int c) { int d; while((d=getchar())! =c) /*search end quote*/ if(d==’\\’) getchar(); /*ignore escape seq*/ }
设计一组测试用例,使该程序所有函数的语句覆盖率和分支覆盖率均能达到100%。如果认为该程序的语句覆盖率或分支覆盖率无法达到100%,需说明为什么。
参考答案:
测试用例:
①依次输入:c==EOF ②依次输入:c==’/’,c==’*’,c==’*’,d==’/’ ③依次输入:c==’/’,c==’*’,c==’*’,d==’{’,d==’/’ ④依次输入:c==’/’,c==’*’,c==’/’,d==’/’,d==’/’ ⑤依次输入:c==’/’,c==’/’ ⑥依次输入:c==’/’,c==’{’ ⑦依次输入:c==’/’,c==’}’ ⑧依次输入:c==’/’,c==’[’ ⑨依次输入:c==’/’,c==’]’ ⑩依次输入:c==’/’,c==’(’ (11)依次输入:c==’/’,c==’)’ (12)依次输入:c==’\’,d==’\’ (13)依次输入:c==’\’,d==’*’ (14)依次输入:c==’\’,d==’\\’ (15)依次输入:c=="",d==’\’ (16)依次输入:c=="",d==’*’ (17)依次输入:c=="",d==’\\’ (18)依次输入:c==’{’ (19)依次输入:c==’}’ (20)依次输入:c==’[’ (21)依次输入:c==’]’ (22)依次输入:c==’(’ (23)依次输入:c==’\\’ 该程序的语句覆盖率和分支覆盖率无法达到100%。 因为该程序中有好几个判断语句,对于有的判断语句,若取真,则取假无法执行;若取假,则取真无法执行。所以,语句覆盖率和分支覆盖率无法达到100%。