In: Computer Science
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystringfunctions, and have two files: a source file mystringfunctions.c and a header file mystringfunctions.h. In this problem, you will write automated tests known as unit tests. This program has been partially written for you in testmystringfunctions.c. Write the body of functions that are marked with a comment that begins with
// Homework TODO: ...
Do not modify other parts of the code.
the source and header files are here for reference:
#include
#include
#include
#include "mystringfunctions.h"
// Creates a new string from the first n chars of src
//
// Example:
// The result of deepCopyStr("great googly moogly", 6) is "great "
char* deepCopyStr(char* src, int n) {
return NULL;
// 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] == '\0') {
free(result);
return NULL;
}
result[i] = src[i];
}
result[n] = '\0';
result[0] = 'a';
return result;
}
// Checks that the first n chars of src are lowercase letters or digits
//
// Example:
// The result of isLowerOrDigitStr("100timeS!", 7) is true
// The result of isLowerOrDigitStr("100timeS!", 8) is false
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 new string from the first n chars of the
// concatenation of str1 and str2
//
// Example:
// The result of deepCopyStr("great", " googly moogly", 10) is "great goog"
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] == '\0') {
srcStr = str2;
j = 0;
}
// Error if strings have less than n characters
if (srcStr == str2 && srcStr[j] == '\0') {
free(result);
return NULL;
}
result[i] = srcStr[j];
}
result[n] = '\0';
return result;
}
#ifndef MYSTRINGFUNCTIONS
#define MYSTRINGFUNCTIONS
#include
// Creates a new string from the first n chars of src
//
// Example:
// The result of deepCopyStr("great googly moogly", 6) is "great "
char* deepCopyStr(char* src, int n);
// Checks that the first n chars of src are lowercase letters or digits
//
// Example:
// The result of isLowerOrDigitStr("100timeS!", 7) is true
// The result of isLowerOrDigitStr("100timeS!", 8) is false
bool isLowerOrDigitStr(char* src, unsigned int n);
// Creates a new string from the first n chars of the
// concatenation of str1 and str2
//
// Example:
// The result of deepCopyStr("great", " googly moogly", 10) is "great goog"
char* concatStrs(char* str1, char* str2, int n);
#endif
<< testmystringfunctions.c >>
#include
#include
#include
#include
// Homework TODO: Include mystringfunctions.h here
// Homework TODO: Add function prototypes here
// Asks user to pick a unit test to run.
// In practice, we write unit tests using a unit testing framework
// and all unit tests are run when the code is compiled.
// For grading purposes, we ask the user to pick the test.
// DO NOT modify this function for your homework.
int main() {
// User menu
printf("Which unit test would you like to run?\n");
printf("1) deepCopyStr\n");
printf("\ta) n = 2, src = \"test string\"\n");
printf("\tb) n = 0 returns \"\\0\"\n");
printf("\tc) negative n returns NULL\n");
printf("2) isLowerOrDigitStr\n");
printf("\ta) n = 4, src = \"testString\"\n");
printf("\tb) n = 5, src = \"testString\"\n");
printf("\tc) n = 0\n");
printf("3) concatStrs\n");
printf("\ta) n = 5, str1 = \"test\", str2 = \"string\"\n");
printf("\tb) n = 5, str1 = \"\", str2 = \"test string\" returns \"test \"\n");
printf("\tc) n = 5, str1 = \"test\", str2 = \"\" returns NULL\n");
while (!getAndRunTest()) {}
}
// Prompt user once for the test to run. 1a is deepCopyStrTestA,
// 1b is deepCopyStrTestB, 2c is isLowerOrDigitStrTestC, and so on.
// If the user input is valid, run the test and return true.
// If the user input is invalid, print the error according to the homework
// prompt and return false.
bool getAndRunTest() {
// Homework TODO: complete the code for this function.
}
// Test n = 2 and src = "test string" returns "te"
void deepCopyStrTestA() {
char* result = deepCopyStr("test string", 2);
// if (result) checks to see something is returned (even if the string is empty).
// We will see later in the course this is checking if the result is a NULL pointer
assert(result && result[0] == 't' && result[1] == 'e' && result[2] == '\0');
printf("Test successful.\n");
}
// Test n = 0 the returns "\0"
void deepCopyStrTestB() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// Test negative n returns NULL"
void deepCopyStrTestC() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// Test n = 4, src = "testString" returns true
void isLowerOrDigitStrTestA() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// Test n = 5, src = "testString" returns false
void isLowerOrDigitStrTestB() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// Test n = 0, src = "" returns true
void isLowerOrDigitStrTestC() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// Test n = 10, str1 = "test", str2 = "string" returns "teststring"
void concatStrsTestA() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// n = 5, str1 = "", str2 = "test string" returns "test "
void concatStrsTestB() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// n = 5, str1 = "test", str2 = "" returns NULL
void concatStrsTestC() {
// Homework TODO: write code for this test case according to the
// specifications in the comment above the function.
}
// Flush stdin buffer
void flushStdin() {
// Homework TODO: see 1/30/17 lecture notes to understand what this
// function will do and how it should be written. (do not worry about this part, the answer to this has been provided);
char c;
//skip all characters until end-of-file marker
// or new line/carriage return
while ( ( c = getchar( ) ) != EOF && c != '\n' && c != '\r' ) {};
}
attached are also example outputs the first one is if mystringfunctions.c is correct. the second is when it is incorrect.
correct:
Which unit test would you like to run? 1) deepCopyStr a) n = 2, src = "test string"
b) n = 0 returns "\0" c) negative n returns NULL 2) isLowerOrDigitStr a) n = 4, src = "testString" b) n = 5, src = "testString" c) n = 0 3) concatStrs a) n = 5, str1 = "test", str2 = "string" b) n = 5, str1 = "", str2 = "test string" returns "test " c) n = 5, str1 = "test", str2 = "" returns NULL pf Enter 1, 2, or 3 for the function to test. p f Enter 1, 2, or 3 for the function to test. 1
4
l Enter a, b, or c for the test case. 1a Test successful.
incorrect:
Which unit test would you like to run? 1) deepCopyStr a) n = 2, src = "test string"
b) n = 0 returns "\0" c) negative n returns NULL 2) isLowerOrDigitStr a) n = 4, src = "testString" b) n = 5, src = "testString" c) n = 0 3) concatStrs a) n = 5, str1 = "test", str2 = "string" b) n = 5, str1 = "", str2 = "test string" returns "test " c) n = 5, str1 = "test", str2 = "" returns NULL 1a Assertion failed: (result && result[0] == ’t’ && result[1] == ’e’ && result[2] == ’\0’), failed Abort trap: 6
//mustringfunction.c
//-------------------------------------------------------------------------------------------
#include <stdio.h>
//#include"mystringfunctions.h"
#include<malloc.h>
#include<stdbool.h>
#include<assert.h>
char* deepCopyStr(char* src, int n)
{
if (n < 0) {
return NULL;
}
// Error if no src string passed
if (src == NULL) {
return NULL;
}
char* result = (char*) malloc (sizeof(char) * (n + 1));
int i;
for (i = 0; i < n; i++) {
// Error if string has less than n characters
if (src[i] == '\0') {
free(result);
return NULL;
}
result[i] = src[i];
}
result[n] = '\0';
//result[0] = 'a';
return result;
}
bool isLowerOrDigitStr(char* src, unsigned int n)
{
int i;
for (i = 0; i < n; i++) {
if (('a' <= src[i] && src[i] <= 'z') || ('0' <=
src[i] && src[i] <= '9')) {
continue;
}
else {
return false;
}
}
return true;
}
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;
int i;
for (i = 0; i < n; i++, j++) {
// Swap to str2 if at end of str1
if (srcStr == str1 && srcStr[j] == '\0') {
srcStr = str2;
j = 0;
}
// Error if strings have less than n characters
if (srcStr == str2 && srcStr[j] == '\0') {
free(result);
return NULL;
}
result[i] = srcStr[j];
}
result[n] = '\0';
return result;
}
// Test n = 2 and src = "test string" returns "te"
void deepCopyStrTestA() {
char* result = deepCopyStr("test string", 2);
// if (result) checks to see something is returned (even if the
string is empty).
// We will see later in the course this is checking if the result
is a NULL pointer
assert(result && result[0] == 't' && result[1] ==
'e' && result[2] == '\0');
printf("Test successful.\n");
}
// Test n = 0 the returns "\0"
void deepCopyStrTestB() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
char* result = deepCopyStr("test string", 0);
assert(!result);
printf("Test successful.\n");
}
// Test negative n returns NULL"
void deepCopyStrTestC() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
char* result = deepCopyStr("test string", -2);
assert(!result);
printf("Test successful.\n");
}
// Test n = 4, src = "testString" returns true
void isLowerOrDigitStrTestA() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
bool b = isLowerOrDigitStr("test string",4);
assert(b);
printf("Test successful.\n");
}
// Test n = 5, src = "testString" returns false
void isLowerOrDigitStrTestB() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
bool b = isLowerOrDigitStr("test string",5);
assert(b);
printf("Test successful.\n");
}
// Test n = 0, src = "" returns true
void isLowerOrDigitStrTestC() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
bool b = isLowerOrDigitStr("",0);
assert(b);
printf("Test successful.\n");
}
// Test n = 10, str1 = "test", str2 = "string" returns
"teststring"
void concatStrsTestA() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
char *result = concatStrs("test","string",10);
assert(result && result[0] == 't' && result[1] ==
'e' && result[2] == 's' && result[3] == 't'
&& result[4] == 's' && result[5] == 't' &&
result[6] == 'r' && result[7] == 'i' && result[8]
== 'n' && result[9] == 'g' && result[10] ==
'\0');
printf("Test successful.\n");
}
// n = 5, str1 = "", str2 = "test string" returns "test "
void concatStrsTestB() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
char *result = concatStrs("","test string",5);
assert(result && result[0] == 't' && result[1] ==
'e' && result[2] == 's' && result[3] == 't'
&& result[4]==' ' && result[5]=='\0');
printf("Test successful.\n");
}
// n = 5, str1 = "test", str2 = "" returns NULL
void concatStrsTestC() {
// Homework TODO: write code for this test case according to
the
// specifications in the comment above the function.
char *result = concatStrs("test","",5);
assert(!result);
printf("Test successful.\n");
}
// Flush stdin buffer
void flushStdin() {
// Homework TODO: see 1/30/17 lecture notes to understand what
this
// function will do and how it should be written. (do not worry
about this part, the answer to this has been provided);
char c;
//skip all characters until end-of-file marker
// or new line/carriage return
while ( ( c = getchar( ) ) != EOF && c != '\n' && c
!= '\r' ) {};
}
// Prompt user once for the test to run. 1a is
deepCopyStrTestA,
// 1b is deepCopyStrTestB, 2c is isLowerOrDigitStrTestC, and so
on.
// If the user input is valid, run the test and return true.
// If the user input is invalid, print the error according to the
homework
// prompt and return false.
bool getAndRunTest() {
// Homework TODO: complete the code for this function.
char ch1,ch2;
printf("Enter 1, 2, or 3 for the function to test.\n");
scanf("%c",&ch1);
if(ch1 == '1')
{
printf("Enter a, b, or c for the test case.\n");
scanf(" %c",&ch2);
if(ch2 == 'a')
{printf("%c%c\n",ch1,ch2); deepCopyStrTestA();}
else if(ch2 == 'b')
{printf("%c%c\n",ch1,ch2); deepCopyStrTestB();}
else if(ch2 =='c')
{printf("%c%c\n",ch1,ch2); deepCopyStrTestC();}
else
return false;
}
else if(ch1 == '2')
{
printf("Enter a, b, or c for the test case.\n");
scanf(" %c,",&ch2);
if(ch2 == 'a')
{printf("%c%c\n",ch1,ch2); isLowerOrDigitStrTestA();}
else if(ch2 == 'b')
{printf("%c%c\n",ch1,ch2); isLowerOrDigitStrTestB();}
else if(ch2 =='c')
{printf("%c%c\n",ch1,ch2); isLowerOrDigitStrTestC();}
else
return false;
}
else if(ch1 == '3')
{
printf("Enter a, b, or c for the test case.\n");
scanf(" %c,",&ch2);
if(ch2 == 'a')
{printf("%c%c\n",ch1,ch2); concatStrsTestA();}
else if(ch2 == 'b')
{printf("%c%c\n",ch1,ch2); concatStrsTestB();}
else if(ch2 =='c')
{printf("%c%c\n",ch1,ch2); concatStrsTestC();}
else
return false;
}
else
{
return false;
}
return true;
}
//---------------------------------------------------------------------------------------------
//mystringfunction.h
#ifndef MYSTRINGFUNCTIONS_H
#define MYSTRINGFUNCTIONS_H
#include<stdio.h>
#include<stdbool.h>
// Creates a new string from the first n chars of src
//
// Example:
// The result of deepCopyStr("great googly moogly", 6) is "great
"
char* deepCopyStr(char* src, int n);
// Checks that the first n chars of src are lowercase letters or
digits
// Example:
// The result of isLowerOrDigitStr("100timeS!", 7) is true
// The result of isLowerOrDigitStr("100timeS!", 8) is false
bool isLowerOrDigitStr(char* src, unsigned int n);
// Creates a new string from the first n chars of the
// concatenation of str1 and str2
//
// Example:
// The result of deepCopyStr("great", " googly moogly", 10) is
"great goog"
char* concatStrs(char* str1, char* str2, int n);
#endif
//-------------------------------------------------------------------------------
//testmystringfunction.c
#include "mystringfunctions.h"
#include <stdio.h>
#include<stdbool.h>
// Homework TODO: Include mystringfunctions.h here
// Homework TODO: Add function prototypes here
// Asks user to pick a unit test to run.
// In practice, we write unit tests using a unit testing
framework
// and all unit tests are run when the code is compiled.
// For grading purposes, we ask the user to pick the test.
// DO NOT modify this function for your homework.
char* deepCopyStr(char* src, int n);
bool isLowerOrDigitStr(char* src, unsigned int n);
char* concatStrs(char* str1, char* str2, int n);
int main() {
// User menu
printf("Which unit test would you like to run?\n");
printf("1) deepCopyStr\n");
printf("\ta) n = 2, src = \"test string\"\n");
printf("\tb) n = 0 returns \"\\0\"\n");
printf("\tc) negative n returns NULL\n");
printf("2) isLowerOrDigitStr\n");
printf("\ta) n = 4, src = \"testString\"\n");
printf("\tb) n = 5, src = \"testString\"\n");
printf("\tc) n = 0\n");
printf("3) concatStrs\n");
printf("\ta) n = 5, str1 = \"test\", str2 = \"string\"\n");
printf("\tb) n = 5, str1 = \"\", str2 = \"test string\" returns
\"test \"\n");
printf("\tc) n = 5, str1 = \"test\", str2 = \"\" returns
NULL\n");
while (!getAndRunTest() ) {
}
}
//-----------------------------------------------------------------------------------------------------
Output: