如何在eclipse-cdt ubuntu 14.0中使用C语言在Socket-Client程序中运行单元测试用例?

时间:2015-03-03 04:58:59

标签: c++ c eclipse-cdt ubuntu-14.04 stream-socket-client

我在eclipse-cdt(ubuntu 14.0)中制作了简单的StringReverse C项目并制作成.so(共享库)。 我在eclipse-cdt中为StringReverse C项目创建了测试用例。 测试成功运行。

但是,这些C项目和测试用例我想在eclipse-cdt中的Socket-Client编程中运行。 假设,我想在客户端传递测试用例,测试用例在服务器端找到C函数。我该怎么做...?

Server-Client Project的参考:

http://www.cs.cf.ac.uk/Dave/C/node28.html#SECTION002800000000000000000

我的StringReverse C代码:

包括:

#define LENGTH_OF_STRING 20

#include <stdio.h>
#include <string.h>
#include "error.h"

int StringReverse (char *ptrToDestination, char *ptrToSource);

来源:

#include "StringReverse.h"
#include "error.h"

int StringReverse(char *ptrToDestination,char *ptrToSource)
    {

    int i,j;

    if(NULL == (void *)ptrToSource) {
        return ERR_NULL_POINTER;
    }

    if (LENGTH_OF_STRING <= strlen(ptrToSource)) {
        return ERR_STRING_LENGTH;
    }

    if(NULL == (void *)ptrToDestination) {
        return ERR_NULL_POINTER;
    }

    for(i=0; ptrToSource[i]; i++) {
    }

    for(i=i-1,j=0; i>=0; i--,j++) {
            ptrToDestination[j] = ptrToSource[i];
    }
    ptrToDestination[j]  ='\0';

    if(strlen(ptrToSource) != strlen(ptrToDestination)) {
        return ERR_STRING_LENGTH;
    }
    return SUCCESS;
}

我的测试用例:

AllTests.cpp:

#include "CommandLineTestRunner.h"

int main(int ac, char** av)
{
    int result = RUN_ALL_TESTS(ac, av);

    return result;
}

StringReverse_Test.cpp:

extern "C"
{
#include "StringReverse.h"
#include "error.h"
}

#define COUNT_FOR_NULL 1

#include "TestHarness.h"
#include "TestHarness_c.h"
#include <string.h>

int StatusOfStringReverseFunction;
TEST_GROUP(StringReverse)
{
    void setup()
    {
        /**
          * This method is used to initialize the test. It is runs before each test.
          *
          */
        printf("Tests start here...\n");
    }
    void teardown()
    {
        /**
         * This method is used to finalized the test. It is runs after each test.
         */
        printf("Tests end here...\n");
    }
};

/**
 * \brief
 * I require a test which can check the StringReverse function is capable to return the reverse a string,
 * if I assigned string to pointer.
 *
 * \details
 * I require two character pointers for the test.
 * char *ptrToSource contains input as string.
 * char *ptrToDestination contains the result of StringReverse function.
 * I extend StringReverse function to reverse the string assigned by pointer.
 * I check the StringReverse function is capable to return the reverse a string or not, using LONGS_EQUAL macro.
 */
TEST(StringReverse, tst_StringReverse_PointerToString)
{
    char *ptrToSource = "abcdefghijkl";
    char *ptrToDestination = NULL;

    CHECK(NULL != ptrToSource);

    ptrToDestination = (char *)    malloc((strlen(ptrToSource)+COUNT_FOR_NULL)*sizeof(char));

    CHECK(NULL != ptrToDestination);

    StatusOfStringReverseFunction = StringReverse(ptrToDestination, ptrToSource);

    CHECK(strlen(ptrToDestination) == strlen(ptrToSource));
    LONGS_EQUAL(SUCCESS, StatusOfStringReverseFunction);

    free ((void *)ptrToDestination);
}

/**
 * \brief
 * I require a test by which can I check that function StringReverse is capable to make the null string is reverse or not.
 *
 * \details
 * I require two character arrays.
 * char source[LENGTH_OF_STRING] contains input as a null string.
 * char destination[LENGTH_OF_STRING] contains the result of StringReverse function.
 * I extend StringReverse function to check the null string is reverse or not.
 * I check the StringReverse function is capable to return null string or not, using macro LONGS_EQUAL.
 */
TEST(StringReverse, tst_StringReverse_Null)
{
    char source[LENGTH_OF_STRING] = "";
    char destination[LENGTH_OF_STRING];

    StatusOfStringReverseFunction = StringReverse (destination, source);

    LONGS_EQUAL(SUCCESS, StatusOfStringReverseFunction);
}

解决方案是什么?

0 个答案:

没有答案