函数未通过某些测试用例

时间:2018-11-09 22:49:13

标签: c string

我对代码战中的挑战解决方案的一项测试有疑问。我必须编写一个返回输入字符串中字符的字母位置的函数。我的解决方案如下。我通过了所有测试,也通过了代码战的测试,但是都失败了(我没有实现此测试代码,这是代码战实现的测试代码的补充):

Test(number_tests, should_pass) {
    srand(time(NULL));
    char in[11] = {0};
    char *ptr;
    for (int i = 0; i < 15; i++) {
      for (int j = 0; j < 10; j++) {
        char c = rand() % 10;
        in[j] = c + '0';
      }
      ptr = alphabet_position(in);
      cr_assert_eq(strcmp(ptr, ""), 0);
      free(ptr); 
    }
}

我收到的错误如下:表达式(strcmp(ptr, "")) == (0) is false.感谢您的帮助! ps另外我还注意到我正在泄漏内存(我不知道如何解决这个问题,所以我想我将使用数组来跟踪字符串,而不使用malloc)->我想这不是我要解决的问题只是在主函数中是free(ptr)。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *alphabet_position(char *text);

// test 
int main()
{
    if (!strcmp("1 2 3", alphabet_position("abc")))
    {
        printf("success...\n");
    }
    else
    {
        printf("fail...\n");
    }

    if (!strcmp("", alphabet_position("..")))
    {
        printf("success...\n");
    }
    else
    {
        printf("fail...\n");
    }
    if (!strcmp("20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11", alphabet_position("The sunset sets at twelve o' clock.")))
    {
        printf("success...\n");
    }
    else
    {
        printf("fail...\n");
    }


}

char *alphabet_position(char *text) 
{

  // signature: string -> string
  // purpose: extact alphabet position of letters in input string and
  // return string of alphabet positions

  // return "123"; // stub

  // track numerical value of each letter according to it's alphabet position 
  char *alph = "abcdefghijklmnopqrstuvwxyz";
  // allocate maximum possible space for return string
  // each char maps to two digit number + trailing space after number
  char *s = malloc(sizeof(char) * (3 * strlen(text) + 1));
  // keep track of the begining of return string
  char *head = s;

  int index = 0;
  int flag = 0;

  while(*text != '\0')
  {
      if ( ((*text > 64) && (*text < 91)) || ((*text > 96) && (*text < 123)))
      {
          flag = 1;
          index = (int)(strchr(alph, tolower(*text)) - alph) + 1;
          if (index > 9)
          {
            int n = index / 10;
            int m = index % 10;
            *s = n + '0';
            s++;
            *s = m + '0';
            s++;
            *s = ' ';
            s++;

          }
          else
          {
            *s = index + '0';
            s++;
            *s = ' ';
            s++;
          }
      }
    text++;
  }
  if (flag != 0)  // if string contains at least one letter
  {
  *(s -1) = '\0'; // remove the trailing space and insert string termination
  }
  return head;
}

1 个答案:

答案 0 :(得分:1)

这是我想发生的事情:

如果输入字符串中的 none 个字符都是字母字符,则不会使用s,因此malloc()分配的内存可以是任何东西。 malloc()不会清除内存/将内存清零。

您输入的".."大小写通过只是一个巧合。代码战测试用例连续执行许多此类非字母测试,每个测试都导致malloc(),并且如果其中任何一个失败,则整个操作都会失败。

我尝试重新创建这种情况,但是(正如我所说)这是不可预测的。要对此进行测试,请添加一条调试行以在s仍为flag时输出0的值:

if (flag != 0) { // if string contains at least one letter
    *(s -1) = '\0'; // remove the trailing space and insert string termination
}
else {
    printf("flag is still 0 : %s\n", s);
}

我敢打赌,有时您会得到一个不是""的垃圾/随机字符串。