2010-03-24 4 views
0

안녕하세요, 다음은 unix ptx 유틸리티에서 가져온 코드 스 니펫입니다. 이 유틸리티에서 코드 적용 범위를 최대화하려고 시도하고 있지만 표시된 부분에 도달 할 수 없습니다. 물론 필자는 이전처럼 C 스킬이 강하지 않다. 코드 부분은 주석으로 표시되어 있지만 블록의 맨 아래를 향하고 있습니다.코드 적용 범위를 극대화하려면 어떻게해야합니까?

if (used_length == allocated_length) 
{ 
    allocated_length += (1 << SWALLOW_REALLOC_LOG); 
    block->start 
    = (char *) xrealloc (block->start, allocated_length); 
} 

해당 블록을 처리하기 위해 표시된 부분을 해석하면 도움이됩니다.

/* Reallocation step when swallowing non regular files. The value is not 
    the actual reallocation step, but its base two logarithm. */ 
#define SWALLOW_REALLOC_LOG 12 

static void swallow_file_in_memory (const char *file_name, BLOCK *block) 
{ 
    int file_handle;  /* file descriptor number */ 
    struct stat stat_block; /* stat block for file */ 
    size_t allocated_length; /* allocated length of memory buffer */ 
    size_t used_length;  /* used length in memory buffer */ 
    int read_length;  /* number of character gotten on last read */ 

    /* As special cases, a file name which is NULL or "-" indicates standard 
    input, which is already opened. In all other cases, open the file from 
    its name. */ 
    bool using_stdin = !file_name || !*file_name || strcmp (file_name, "-") == 0; 
    if (using_stdin) 
    file_handle = STDIN_FILENO; 
    else 
    if ((file_handle = open (file_name, O_RDONLY)) < 0) 
     error (EXIT_FAILURE, errno, "%s", file_name); 

    /* If the file is a plain, regular file, allocate the memory buffer all at 
    once and swallow the file in one blow. In other cases, read the file 
    repeatedly in smaller chunks until we have it all, reallocating memory 
    once in a while, as we go. */ 

    if (fstat (file_handle, &stat_block) < 0) 
    error (EXIT_FAILURE, errno, "%s", file_name); 

    if (S_ISREG (stat_block.st_mode)) 
    { 
     size_t in_memory_size; 

     block->start = (char *) xmalloc ((size_t) stat_block.st_size); 

     if ((in_memory_size = read (file_handle, 
        block->start, (size_t) stat_block.st_size)) 
     != stat_block.st_size) 
    { 
     error (EXIT_FAILURE, errno, "%s", file_name); 
    } 
     block->end = block->start + in_memory_size; 
    } 
    else 
    { 
     block->start = (char *) xmalloc ((size_t) 1 << SWALLOW_REALLOC_LOG); 
     used_length = 0; 
     allocated_length = (1 << SWALLOW_REALLOC_LOG); 

     while (read_length = read (file_handle, 
       block->start + used_length, 
       allocated_length - used_length), 
     read_length > 0) 
    { 
     used_length += read_length; 
     /* Cannot cover from this point...*/ 
     if (used_length == allocated_length) 
     { 
      allocated_length += (1 << SWALLOW_REALLOC_LOG); 
      block->start 
     = (char *) xrealloc (block->start, allocated_length); 
     } 
     /* ...to this point. */ 
    } 

     if (read_length < 0) 
    error (EXIT_FAILURE, errno, "%s", file_name); 

     block->end = block->start + used_length; 
    } 

    /* Close the file, but only if it was not the standard input. */ 

    if (! using_stdin && close (file_handle) != 0) 
    error (EXIT_FAILURE, errno, "%s", file_name); 
} 

답변

1

입력 한 코드가 4096 (1 << SWALLOW_REALLOC_LOG) 바이트보다 작은 것 같습니다. 더 큰 입력을주고 (그리고 더 큰 입력을 일반 파일이 아닌 파이프를 통해 제공하는지 확인하십시오.) 코드를 입력해야합니다.

+0

고맙습니다. 그것이 문제였습니다. 감사합니다. – naivedeveloper

1

코드 범위를 최대화하려면 100 % 적용 범위를 초과해야합니다.

첫째, 100 % 목표는 일반적으로 시간 낭비입니다. 꼭 당신을 돕기 위해 코드 커버리지 도구를 사용하십시오. 그러나 숫자에 사로 잡히지 마십시오. 코드의 특정 영역을 테스트하고 코드베이스의 중요성에 관계없이 전체 코드베이스에 노력을 분산시키는 대신 의도적으로 다른 부분을 놓치는 것이 더 중요합니다.

이 특정 케이스에서 적용 범위를 확보하려면 읽기 방법을 조롱해야합니다. 이를 수행하려면 종속성 삽입을 사용해야합니다. 또 다른 기법은 단지 1 바이트의 버퍼를 할당하여 그 분기를 강제로 수행하는 것입니다.

관련 문제