如何最大化代码覆盖率?

时间:2010-03-24 23:28:54

标签: c unix

嘿所有,以下是从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);
}

2 个答案:

答案 0 :(得分:1)

要最大限度地提高代码覆盖率,您需要达到100%的覆盖率。

首先,100%的目标通常是浪费时间。一定要使用代码覆盖工具来帮助你,但不要被数字所困扰。更重要的是要很好地测试代码的特定区域,并故意错过其他部分,而不是在整个代码库上分散您的工作,无论其重要性如何。

话虽如此,为了获得此特定情况的覆盖范围,您将不得不模拟read方法,以便您可以控制它的作用。您需要使用依赖注入来实现此目的。另一种技术可能是只分配一个字节的缓冲区,强制采用该分支。

答案 1 :(得分:1)

根据代码,听起来您的输入小于4096(1 << SWALLOW_REALLOC_LOG)个字节。给它一个更大的输入(并确保你提供这个更大的输入而不是普通文件,但通过管道)你应该点击该代码。