1

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

但是,这些 C 项目和测试用例我想在 eclipse-cdt 中的 Socket-Client Programming 中运行。假设我想在客户端通过测试用例,并且测试用例在服务器端找到 C 函数。我怎样才能做到这一点..?

服务器-客户端项目的参考资料:

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);
}

解决办法是什么 ?

4

0 回答 0