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

I am in need of some help with this. 1) Implement a random tester for the functi

ID: 3881919 • Letter: I

Question

I am in need of some help with this.

1) Implement a random tester for the function testme() in testme.c (see below) that is capable of printing the error message. You should implement inputChar() and inputString() to produce random values.
2) Write up the development of your random tester and how it finds/prints the error message as randomstring.pdf. The randomstring.pdf is a text file that outlines how you developed your
solution and how it works.
3) Create a Makefile and add a rule in the Makefile that will compile and execute the testme.c file.

Helpful hints:
• You can choose and design your input pool (such as, string size (random or fixed), include every characters in the ASCII code, include every lowercase letter in the alphabet, or include
only the letters used in the target statement, etc.).
• Your random test generator should not take more than five minutes to achieve the coverage goal.

test.c

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<time.h>

char inputChar()

{

// TODO: rewrite this function

   return ' ';

}

char *inputString()

{

// TODO: rewrite this function

   return "";

}

void testme()

{

   int tcCount = 0;

   char *s;

   char c;

   int state = 0;

   while (1)

   {

tcCount++;

c = inputChar();

s = inputString();

printf("Iteration %d: c = %c, s = %s, state = %d ", tcCount, c, s, state);

if (c == '[' && state == 0) state = 1;

if (c == '(' && state == 1) state = 2;

if (c == '{' && state == 2) state = 3;

if (c == ' '&& state == 3) state = 4;

if (c == 'a' && state == 4) state = 5;

if (c == 'x' && state == 5) state = 6;

if (c == '}' && state == 6) state = 7;

if (c == ')' && state == 7) state = 8;

if (c == ']' && state == 8) state = 9;

if (s[0] == 'r' && s[1] == 'e' && s[2] == 's' && s[3] == 'e' && s[4] == 't' && s[5] == '' && state == 9)

{

printf("error ");

exit(200);

}

   }

}

int main(int argc, char *argv[])

{

   srand(time(NULL));

   testme();

   return 0;

}

Explanation / Answer

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
char inputChar()
{
   char randomchar; /* Define a temporary char variable */
   randomchar = (char)rand()%256; /* Generate a random character using rand() function */
   return randomchar;
}

char *inputString()
{
   int iter = 0; /* Define an iterator */
   static char randomstr[6]; /* Define a static char array where you will store the random string */
   char tempstr[] = "[({ax})]reset"; /* Define a temporary string having all the collection of characters used */
   memset(randomstr, '', sizeof(randomstr)); /* Initialize the random string to NULL before populating random string */
   while(iter<5)
   {
       randomstr[iter++] = tempstr[rand()%13]; /* Populate the random string using generated random character */
   }
   return randomstr;
}

void testme()
{
   int tcCount = 0;
   char *s;
   char c;
   int state = 0;
   while (1)
   {
       tcCount++;
       c = inputChar();
       s = inputString();
       printf("Iteration %d: c = %c, s = %s, state = %d ", tcCount, c, s, state);
       if (c == '[' && state == 0) state = 1;
       if (c == '(' && state == 1) state = 2;
       if (c == '{' && state == 2) state = 3;
       if (c == ' '&& state == 3) state = 4;
       if (c == 'a' && state == 4) state = 5;
       if (c == 'x' && state == 5) state = 6;
       if (c == '}' && state == 6) state = 7;
       if (c == ')' && state == 7) state = 8;
       if (c == ']' && state == 8) state = 9;
       if (s[0] == 'r' && s[1] == 'e' && s[2] == 's' && s[3] == 'e' && s[4] == 't' && s[5] == '' && state == 9)
       {
           printf("error ");
           exit(200);
       }
   }
}

int main(int argc, char *argv[])
{
   srand(time(NULL));
   testme();
   return 0;
}

Explanation randomstring.pdf

Generating Random character
============================
randomchar = (char)rand()%256;

rand() function generates a random value each time it is called.
We can do randomValue%256. The result will be in the range 0-255(which is the ascii char range).
All the ascii character set we will use to generate a random character each time it is called.

Generating Random String
==========================

static char randomstr[6];
char tempstr[] = "[({ax})]reset";
while(iter<5)
                randomstr[iter++] = tempstr[rand()%13];

First define a static char array where you will store the random string.
The size of the defined static array is same as the string which is being compared in the base function.
Then Define a temporary string having the set of all the characters used in the base function.
As the no. of characters present in the character set is 13,
We can generate a random number within the range 0-12 by rand()%12.
And Populate the random string using generated random character present at the index.