本文介绍了如何涉及IO单元测试的C函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的编写单元测试涉及IO操作的C函数问题。例如,下面是code我写获得从控制台的用户输入字符串。我不知道对如何使用的getchar()函数进行自动化测试用户的输入。

 的char * GetStringFromConsole()
{    字符* strToReturn = NULL;
    INT LEN = 128;    strToReturn =(字符*)malloc的(LEN);
    如果(strToReturn)
    {
        INT CH;
        字符* PTR = strToReturn;
        INT计数器= 0;
        对于(;)
        {
            CH =的getchar();
            反++;            如果(反== LEN)
            {
                strToReturn =的realloc(strToReturn,LEN * = 2);
                PTR = strToReturn +反1;
            }            如果((CH = EOF)及!&安培;!(CH ='\\ n')及及(计数器< LEN))
            {
                * ptr的+ = CH;
            }
            其他
            {
                打破;
            }        }
        * PTR ='\\ 0';
    }
    返回strToReturn;
}


解决方案

模拟的getchar


  1. 利用preprocessor ​​,例如在您的测试文件。

     的#define的getchar mock_getchar
    #包括GetStringFromConsole.h
    ...
    为const char * mock_getchar_data_ptr;
    烧焦mock_getchar()
    {
        返回* mock_getchar_data_ptr ++;
    }
    ...
    //在测试功能
    mock_getchar_data_ptr =你好!\\ n;
    YourAssertEquals(你好,GetStringFromConsole());


  2. 链接器的替补的符号(困难,在我看来),例如定义你自己的的getchar 某处源.c文件,而不是链接到STDLIB(在Windows例如,MSVCRT)


  3. 修改下测试功能以接受返回一个函数字符,最好的选择(恕我直言) - 以 STDLIB没有冲突。并设置由点传递啄像 mock_getchar 测试 1 测试code。

     的typedef CHAR(* getchartype)();
    字符* GetStringFromConsole(getchartype mygetchar)
    {
        ...
        C = mygetchar()


有关分 1 2 我会建议使用自己的函数,而不是的getchar (如 mygetchar ) - 这种方式,你可以嘲笑/替代它没有冲突性病面临包括/库

I am facing problems in writing unit tests to C functions which involve IO operation. For example, below is the code I wrote to get an input string from the user from console. I do not know as to how to automate testing user input using getchar() function.

char * GetStringFromConsole()
{

    char *strToReturn = NULL;
    int len = 128;

    strToReturn = (char*)malloc(len);
    if (strToReturn) 
    {
        int ch;
        char *ptr  = strToReturn;
        int counter = 0;
        for (; ;) 
        {
            ch = getchar();
            counter++;

            if (counter == len)
            {
                strToReturn = realloc(strToReturn, len*=2 );
                ptr = strToReturn + counter-1;
            }

            if ((ch != EOF) && (ch != '\n') && (counter < len))
            {
                *ptr++ = ch;
            }
            else 
            {
                break;
            }

        }
        *ptr = '\0';
    }
    return strToReturn;
}   
解决方案

Mock getchar:

  1. Utilizing preprocessor, e.g. in your test file.

    #define getchar mock_getchar
    #include "GetStringFromConsole.h"
    ...
    const char* mock_getchar_data_ptr;
    char mock_getchar()
    {
        return *mock_getchar_data_ptr++;
    }
    ...
    // in test function
    mock_getchar_data_ptr = "hello!\n";
    YourAssertEquals("hello", GetStringFromConsole());
    

  2. Substitute symbol for the linker(harder, in my opinion), e.g. define your own getchar somewhere in your source .c files instead of linking to a stdlib(e.g. msvcrt on windows)

  3. Modify function under test to accept a function returning char, best choice(IMHO) - no conflicts with stdlib. And setup a test by passing a thingy like mock_getchar from point 1 in test code.

    typedef char (*getchartype)();
    char * GetStringFromConsole(getchartype mygetchar)
    {
        ...
        c  = mygetchar()
    

For points 1 and 2 I'd propose to use your own function instead of getchar (e.g. mygetchar) - this way you could mock/substitute it without facing conflicts with std includes/libs.

这篇关于如何涉及IO单元测试的C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:43