Added Software Engineering II code

This commit is contained in:
2017-11-29 12:18:13 -08:00
parent c036c6e53f
commit 4566d98b5f
54 changed files with 9288 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
CFLAGS = -Wall -fpic -coverage -lm -std=c99 -ftest-coverage -fprofile-arcs
all:
gcc -o testme -g testme.c $(CFLAGS)
clean:
rm -f testme testmeresults.out

View File

@@ -0,0 +1,20 @@
/*
* My approach to writing the tests such that it found the error randomly, while
* still completing in the shortest possible of time. To do this, I made a char
* array that contained only the chars that were tested for to maximize
* potential for creating valid strings, and for moving through the states.
* To come up with a char, I simply used mod against a call to rand such that it
* gave me the index for a character from the array I made.
*
* For creating valid strings, I allocated memory for the five character string
* that would hopefully eventually become the word "reset" and added and extra
* spot for the null character. I then called my completed inputChar to get a
* random char and place it in each of the five spots in the array. Finally, I
* add in the null terminating character.
*
* In practice, running this does exactly what I want. Due to traversal of
* states being a simple matter of getting the right characters, the program's
* state moves to nine incredibly fast. Then, all that's left is to iterate over
* the random possibilities for strings until it comes up with the word "reset",
* at which point the program prints "error" and exits.
*/

View File

@@ -0,0 +1,84 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define NUM_VALID_CHARS 13
#define STRING_LENGTH 5
char valid_chars[NUM_VALID_CHARS] = {
'(',
'[',
'{',
')',
']',
'}',
' ',
'a',
'x',
'r',
'e',
's',
't'
};
char inputChar()
{
return valid_chars[rand() % NUM_VALID_CHARS];
}
char *inputString()
{
char *temp_string = malloc((STRING_LENGTH + 1) * sizeof(char));
for(int i = 0 ; i < STRING_LENGTH ; i++){
temp_string[i] = inputChar();
}
temp_string[STRING_LENGTH] = '\0';
return temp_string;
}
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\n", 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] == '\0'
&& state == 9)
{
printf("error ");
free(s);
exit(200);
}
free(s);
}
}
int main(int argc, char *argv[])
{
srand(time(NULL));
testme();
return 0;
}