Nickson
Nickson

Reputation: 135

Mapping C code to existing Unit Test code

I have a pre-written Test Code written In C.

I am trying to write code that should compile based on that test code. Basically it is about pointers. I have tried scripting something but it keeps on returning "Failed".

Below is the snippet to the test Pre-written Code:

#define TEST_LBS    5.5
int testConvertLbsG(void)
{
double lbs = TEST_LBS;
int intA, intB, fail = 0;

printf("---------------------------\n");
printf("Function: convertLbsG\n");
printf("---------------------------\n");

// Test-1: argument and return value
intA = intB = 0;
printf("Test-1: ");
intB = convertLbsG(&lbs, &intA);
if (intA == intB && intA == TEST_LBSG)
{
    printf("<PASSED>\n");
}
else
{
    printf("<!!! FAILED !!!>\n");
    fail++;
}
// Test-2: return value only
intA = intB = 0;
printf("Test-2: ");
intA = convertLbsG(&lbs, NULL);
if (intA == TEST_LBSG)
{
    printf("<PASSED>\n");
}
else
{
    printf("<!!! FAILED !!!>\n");
    fail++;
}

// Test-3: argument only
intA = intB = 0;
printf("Test-3: ");
convertLbsG(&lbs, &intA);
if (intA == TEST_LBSG)
{
    printf("<PASSED>\n");
}
else
{
    printf("<!!! FAILED !!!>\n");
    fail++;
}

Below is the code I have been trying to write to match up and compile based on the test code above:

int convertLbsKg(double *x ,double *y) {
while (true) {
    if (x >= 0) {
        if (x != NULL) {
            double value;
            x = &value;

            y = x;
            *y = value;

            }
        return;
    }

}   
}

I would appreciate some help on where I am going wrong

Upvotes: 0

Views: 39

Answers (1)

user9706
user9706

Reputation:

Using the correct return type, descriptive variable names, const the value you don't change, correct logic (the original code makes no sense to me), and I added the asserts that I would add to catch null pointers:

int convertLbsKg(const double *weight_in_lbs, int *weight_in_kg) {
   assert(weight_in_lbs);
   if(!weight_in_kg) return 0;
   return *weight_in_kg = 0.4535924 * (*weight_in_lbs);
}

Upvotes: 1

Related Questions