Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you help to write a unit testing for this program in C, Thanks Main functi

ID: 3860311 • Letter: C

Question

Could you help to write a unit testing for this program in C, Thanks

Main function and program:

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

#include "mystringfunctions.h"

// Creates a copy of the first n chars from string src
char* deepCopyStr(char* src, int n) {
// Error if a negative integer is passed
if (n < 0) {
    return NULL;
}
// Error if no src string passed
if (src == NULL) {
    return NULL;
}
char* result = (char*) malloc (sizeof(char) * (n + 1));
for (int i = 0; i < n; i++) {
    // Error if string has less than n characters
    if (src[i] == '') {
      free(result);
      return NULL;
    }
    result[i] = src[i];
}
result[n] = '';
return result;
}

// Returns true if first n characters are lowercase letters or digits
bool isLowerOrDigitStr(char* src, unsigned int n) {
for (int i = 0; i < n; i++) {
    if (('a' <= src[i] && src[i] <= 'z') ||
        ('0' <= src[i] && src[i] <= '9')) {
      continue;
    } else {
      return false;
    }
}
return true;
}

// Creates a copy of the first n chars from str1 concated with str2
char* concatStrs(char* str1, char* str2, int n) {
// Error if a negative integer is passed
if (n < 0) {
    return NULL;
}
int j = 0;
char* result = (char*) malloc (sizeof(char) * (n + 1));
char* srcStr = str1;
for (int i = 0; i < n; i++, j++) {
    // Swap to str2 if at end of str1
    if (srcStr == str1 && srcStr[j] == '') {
      srcStr = str2;
      j = 0;
    }
    // Error if strings have less than n characters
    if (srcStr == str2 && srcStr[j] == '') {
      free(result);
      return NULL;
    }
    result[i] = srcStr[j];
}
result[n] = '';
return result;
}

-------------------------------------

Unit testing : TODO is the section for the answers

// Test n = 2 and src="test string" returns "te"

void deepCopyStrTestA() {

// TODO

}

// Test n = 0 the returns ""

void deepCopyStrTestB() {

// TODO:

// ...

}

// Test negative n returns NULL"

void deepCopyStrTestC() {

// TODO:

//

}

// Test n = 4, src="testString" returns true

void isLowerOrDigitStrTestA() {

// TODO:

// ...

}

// Test n = 5, src="testString" returns false

void isLowerOrDigitStrTestB() {

// TODO:

// ...

}

// Test n = 0, src="" returns true

void isLowerOrDigitStrTestC() {

// TODO:

// ...

}

// Test n = 5, str1 = "test", str2 = "string" returns "teststring"

void concatStrsTestA() {

// TODO:

// ...

}

// n = 5, str1 = "", str2 = "test string" returns "test "

void concatStrsTestB() {

// TODO:

// ...

}

// n = 5, str1 = "test", str2 = "" returns NULL

void concatStrsTestC() {

// TODO:

// ...

}

Explanation / Answer

assert() function will be used for every unit test.

assert() assures that the condition we write in its block is satisfied otherwise it prints on error stream. And then stops the program execution.

so assert() does nothing if the expression inside it is evaluated to true. So, after assert(), we'll print the success message. If the control comes to that printf() after asset() then it means the test was successful as assert() did not cause stopping the execution.

You'll require to include <assert.h> for using assert() function.

So the methods one by one along with description are as follows:

Tests:

// Test n = 2 and src="test string" returns "te"

void deepCopyStrTestA() {

// TODO

char *resultstr = deepCopyStr("test string", 2);

assert(resultstr && resultstr[0]=='t' && resultstr[1]=='e' && resultstr[2]=='');

printf("test deepCopyStrTestA is successful");

}

// Test n = 0 the returns ""

void deepCopyStrTestB() {

// TODO:

// ...

char *resultstr = deepCopyStr("test string", 0);

assert(resultstr && result[0]=='');

printf("test deepCopyStrTestB is successful");

}

// Test negative n returns NULL"

void deepCopyStrTestC() {

// TODO:

char *resultstr = deepCopyStr("test string", -2);

assert(resultstr==NULL);

printf("test deepCopyStrTestC is successful");

}

// Test n = 4, src="testString" returns true

void isLowerOrDigitStrTestA() {

// TODO:

// ...

bool result = isLowerOrDigitStr("testString", 4);

assert(result);

printf("Test isLowerOrDigitStrTestA is successful");

}

// Test n = 5, src="testString" returns false

void isLowerOrDigitStrTestB() {

// TODO:

// ...

bool result = isLowerOrDigitStr("testString", 5);

assert(!result);

printf("Test isLowerOrDigitStrTestB is successful");

}

// Test n = 0, src="" returns true

void isLowerOrDigitStrTestC() {

// TODO:

// ...

bool result = isLowerOrDigitStr("", 0);

assert(result);

printf("Test isLowerOrDigitStrTestC is successful");

}

/* For next tests...

To use strcmp(), you will need to include<string.h> in test file. If you use it, it would be great as otherwise to check 5 characters inside assert() as deepCopyStrTestA() test is not feasible. */

/* This is for concatStrsTestA().

Here's 1 modification, n=5 in below will return "tests" but n=10 will return "teststring" so either take n=10 or result compare with "tests". I've compared with n=5 so result string is "tests". */

// Test n = 5, str1 = "test", str2 = "string" returns "tests"

void concatStrsTestA() {

// TODO:

// ...

char* resultstr = concatStrs("test","string",5);

assert(strcmp(resultstr,"tests")==0);

printf("Test concatStrsTestA is successful");

}

// n = 5, str1 = "", str2 = "test string" returns "test "

void concatStrsTestB() {

// TODO:

// ...

char* resultstr = concatStrs("","test string",5);

assert(strcmp(resultstr,"test ")==0);

printf("Test concatStrsTestB is successful");

}

// n = 5, str1 = "test", str2 = "" returns NULL

void concatStrsTestC() {

// TODO:

// ...

char* resultstr = concatStrs("test","",5);

assert(resultstr==NULL);

printf("Test concatStrsTestC is successful");

}

Description:

deepCopyStrTestA() : In this function, I am taking the result of deep copy in resultstr which is a char pointer, the return type of deepCopyStrTestA(). The parameters are set as per the instructions.

So in assert() we're first checking that resultstr is not null that means we get some result. If this returns false then no further things need to check so it is good to write it there.

Then we are checking the 2 characters of resultstr whether they are desired one or not. If yes, the assert() will do nothing so printf() prints the test is successful.

deepCopyStrTestB() : We are passing 0 as n here and it will return which does not mean null here because null is returned in another case as per the function definition. So here also we'll check whether we got NULL or resultstr got some value. Then the value is checked to be and is decided whether the test is successful.

If any test is not successful, no message will be printed from it. So you can identify if the method is success or not by looking the output.

deepCopyStrTestC(): Similar to above 2 methods, here we're checking for n=-ve returning NULL.

isLowerOrDigitStrTestA(): This method's return type is boolean so I am checking for the 4th character and saving the result in boolean variable. So, in assert() we only need to write 'result' and assert() will check if result=TRUE or not. So, if 4thcharacter is satisfying the result, it will print the success message.

isLowerOrDigitStrTestB(): Here the result should be false. This is the thing to be checked. So, inside assert(), we will check whether the invert of 'result' is true or not. assert(!result) is true when result = FALSE which satisfies our condition. So we'll get the result first, then we'll check whether it's false by checking its invert being TRUE.

isLowerOrDigitStrTestC(): This function is working as above 2 functions. We need to check that it returns true so we will check assert(result).

concatStrsTestA(): I am using strcmp() to check result from the function is similar to expected output or not. If both the strings are same, strcmp() returns 0 so we'll check this condition in assert() that strcmp() of resultstr from function and expected result gives 0.

concatStrsTestB() works the same way as above.

concatStrsTestC(): This function is passing less characters than 'n' so as per the function, NULL is returned. So to check NULL is returned, we'll write in assert() : resultstr==NULL

Please see some comments I wrote above the function concatStrsTestA() also. That will help in understanding the better way.

Go through the methods and do comment if there is any query.

Thank you.:)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote