어떤 이유에서인지 아래 코드 블록을 다루는 데 어려움을 겪고 있습니다. 이 코드는 UNIX uniq 명령에서 발췌 한 것입니다. 나는 모든 블록을 커버하는 테스트 케이스를 작성하는 것을 시도하고 있지만,이 블록에 도달 할 수없는 것 :직관적이지 않은 코드 블록을 어떻게 처리합니까?
if (nfiles == 2)
{
// Generic error routine
}
맥락에서 :
int main (int argc, char **argv)
{
int optc = 0;
bool posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
int nfiles = 0;
char const *file[2];
file[0] = file[1] = "-";
program_name = argv[0];
skip_chars = 0;
skip_fields = 0;
check_chars = SIZE_MAX;
for (;;)
{
/* Parse an operand with leading "+" as a file after "--" was
seen; or if pedantic and a file was seen; or if not
obsolete. */
if (optc == -1 || (posixly_correct && nfiles != 0) || ((optc = getopt_long (argc, argv, "-Dcdf:is:uw:", longopts, NULL)) == -1))
{
if (optind == argc)
break;
if (nfiles == 2)
{
// Handle errors
}
file[nfiles++] = argv[optind++];
}
else switch (optc)
{
case 1:
{
unsigned long int size;
if (optarg[0] == '+' && posix2_version() < 200112 && xstrtoul (optarg, NULL, 10, &size, "") == LONGINT_OK && size <= SIZE_MAX)
skip_chars = size;
else if (nfiles == 2)
{
// Handle error
}
else
file[nfiles++] = optarg;
}
break;
}
}
}
어떤 도움을 크게 감상 할 수있다. 감사.
두 번째 if 문을 테스트하기 위해 테스트를 생성 할 수 있지만 if 문은 다음과 같은 것으로 보입니다. if (optind == argc) break; 첫 번째 경우에 모든 것을 포착합니다. – naivedeveloper
"uniq a b c"또는 이와 비슷한 것을 시도해 보셨습니까? –
나는이 사건을 일으키기 위해'uniq a b c d'를 할 필요가 있다고 생각한다. – caf