2010-03-24 82 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 2010-03-25 02:17:53

1

要最大化代碼覆蓋率,您需要達到100%的覆蓋率。

首先,100%的目標通常是浪費時間。通過一切手段使用代碼覆蓋工具來幫助你,但不要被數字所迷惑。更好地測試代碼的特定區域並故意忽略其他部分,而不是將其工作分散在整個代碼庫中,而不考慮其重要性。

話雖如此,爲了得到這個特定情況下的覆蓋率,您將不得不嘲笑讀取方法,以便您可以控制它的功能。你需要使用依賴注入來實現這一點。另一種技術可能是分配只有一個字節的緩衝區,強制該分支被採用。