Skip to content

Commit

Permalink
added main and parser unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrabbe committed Jan 25, 2015
1 parent dafe47a commit 4d5ea41
Show file tree
Hide file tree
Showing 6 changed files with 548 additions and 190 deletions.
9 changes: 6 additions & 3 deletions input.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@

{
DO A FROM 1 TO 1000 {
DO A FROM 1 TO 400 {
SET C := A 20 * ;
FD C
RT 213
SET D := A 3 / ;
SET D := A 3 * ;
FD D
RT 13

SET E := C D + ;
SET F := C D - ;
SET G := E F / ;
LT G
}
}
130 changes: 112 additions & 18 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by ben on 18/01/2015.
// Copyright (c) 2015 ben. All rights reserved.
//
#define sprint(s) printf(#s " = ""%s""\n",s);
//#define sprint(s) printf(#s " = ""%s""\n",s);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -14,15 +14,28 @@

char * readFile(const char * argv1);

void unitTests();

//unit test functions
void testReadFile();
void testStrDup();
void testStringsMatch();

int main(int argc, const char * argv[])
{
if(TESTING)
{
unitTests();
return 1;
}
if(argc!=2)
{
fprintf(stderr, "ERROR: expected a .txt file path as 1st argument.\nExiting.\n");
exit(0);
}
char * inputString = readFile(argv[1]);
if(inputString==NULL) exit(1);

symbolList * symList = parse(inputString);
pointArray * path = buildPath(symList);
printf("%d points in path\n",path->numberOfPoints);
Expand All @@ -36,39 +49,38 @@ int main(int argc, const char * argv[])
*/
char * readFile(const char * argv1)
{
printf("opening %s.\n",argv1);
printf("readFile opening %s.\n",argv1);
FILE * fp = NULL;
fp = fopen(argv1, "r");
if(!fp)
{
fprintf(stderr, "ERROR: could not open file. Exiting.\n");
exit(0);
printError("could not open file.", __FILE__, __FUNCTION__, __LINE__);
return NULL;
}
char * inputString = NULL;
char fileChar = getc(fp);
int length=0;
while(fileChar!=EOF)
{
++length;
char * tmp = realloc( inputString, length );
char * tmp = realloc( inputString, length);
if(!tmp)
{
fprintf(stderr, "ERROR: realloc in readFile() main.c failed. Exiting.\n");
exit(0);
printError("realloc in readFile() failed.", __FILE__, __FUNCTION__, __LINE__);
return NULL;
}
inputString = tmp;
inputString[length-1]=fileChar;
fileChar = getc(fp);
}
char * tmp = realloc( inputString, length );
char * tmp = realloc( inputString, length);
if(!tmp)
{
fprintf(stderr, "ERROR: realloc in readFile() main.c failed. Exiting.\n");
exit(0);
printError("realloc in readFile() failed.", __FILE__, __FUNCTION__, __LINE__);
return NULL;
}
inputString = tmp;
inputString[length]='\0';
sprint(inputString);
return inputString;
}

Expand All @@ -80,7 +92,7 @@ int printError(const char * errorString, const char file[], const char function[
{
char * editableErrorString = strdup(errorString);
char stringStart[MAX_ERROR_STRING_SIZE];
sprintf(stringStart,"\n******Error in %s module in function %s at line %d.\n\n", file, function, line);
sprintf(stringStart,"\n******Error in %s %s line %d.\n\n", file, function, line);
strcat(stringStart,editableErrorString);
printf("%s\n\n",stringStart);
free(editableErrorString);
Expand All @@ -95,20 +107,102 @@ char *strdup(const char * s)
{
size_t len = 1+strlen(s);//gets the size of s
char *p = malloc(len);//allocates a block big enough to hold s

return p ? memcpy(p, s, len) : NULL;//if p is non 0 ie malloc worked, then copy everything in s into p and return p. if p is NULL malloc didnt work so return NULL
if(p==NULL)
{
printError("malloc in strdup() failed.", __FILE__, __FUNCTION__, __LINE__);
exit(0);
}
return memcpy(p, s, len);//returns ptr to start of string
}

int stringsMatch(const char * string1, const char * string2)
{
if(strcmp(string1,string2)==0)
if(strcmp(string1,string2)==0) return 1;
else return 0;
}

#pragma mark Unit Tests

void unitTests()
{
printf("********************************************************************\n");
printf("\n* UNIT TESTS *\n\n");
printf("********************************************************************\n\n");

printf("\n\n\n********************************************************************\n");
printf("\n* Testing main.c *\n\n");
printf("********************************************************************\n\n");
unitTests_main();
printf("\n\n\n********************************************************************\n");
printf("\n* Testing parser.c *\n\n");
printf("********************************************************************\n\n");
unitTests_parser();


}

void unitTests_main()
{
sput_start_testing();
sput_set_output_stream(NULL);

sput_enter_suite("testReadFile()");
sput_run_test(testReadFile);
sput_leave_suite();

sput_enter_suite("testStrDup()");
sput_run_test(testStrDup);
sput_leave_suite();

sput_enter_suite("testStringsMatch()");
sput_run_test(testStringsMatch);
sput_leave_suite();

sput_finish_testing();

}

void testReadFile()
{
sput_fail_unless(readFile("thisIsNotAFile.txt")==NULL, "trying to open a file that doesnt exist should print error and return NULL");
char * returnedString = readFile("readFileTest.txt");
FILE * fp = NULL;
fp = fopen("readFileTest.txt", "r");
if(!fp)
{
return 1;
printError("could not open test file.", __FILE__, __FUNCTION__, __LINE__);
}
else
int charFromTheReturnedStringPostion=0;
int returnedStringLength = (int)strlen(returnedString);
char charFromFile = getc(fp);
char charFromTheReturnedString = returnedString[charFromTheReturnedStringPostion];
while(charFromFile!=EOF && charFromTheReturnedStringPostion<=returnedStringLength)
{
return 0;
char str[500];
sprintf(str, "Checks each char of the file with that of the returned string. Now checking %c == %c.(file == returned)",charFromFile,charFromTheReturnedString);
sput_fail_unless(charFromFile==charFromTheReturnedString,str);
charFromFile = getc(fp);
charFromTheReturnedString = returnedString[++charFromTheReturnedStringPostion];
}
}

void testStrDup()
{
char testString[15] = "testString";
char * duplicatedString = strdup(testString);
char str[500];
sprintf(str, "Checks that a duplicated string matches the original. Checking %s==%s",testString,duplicatedString);
sput_fail_unless(strcmp(testString,duplicatedString)==0, str);
}

void testStringsMatch()
{
char testString[15] = "testString";
char testString2[15] = "testString";
char notTestString[15] = "notTestString";
char str[500];
sprintf(str, "Checks that stringsMatch returns 1 for a match. Checking with %s==%s.",testString,testString2);
sput_fail_unless(stringsMatch(testString, testString2)==1, str);
sprintf(str, "Checks that stringsMatch returns 0 for no match. Checking with %s==%s.",testString,notTestString);
sput_fail_unless(stringsMatch(testString, notTestString)==0, str);
}
24 changes: 21 additions & 3 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#include "sput.h"
#include "debug.h"

#define TESTING 1
#define VERBOSE 1
#define PRINT_ERRORS 1 //turn on/off stderr error messages.
#define MAX_ERROR_STRING_SIZE 400
#define MAX_ERROR_STRING_SIZE 600
#define NUMBER_OF_DIMENSIONS 2
#define DIM_MAX 1

Expand All @@ -37,6 +38,9 @@ typedef struct symbolList {
} symbolList;

symbolList * parse(char * inputString);



/******************************************************************************/
//Path Making Functions
typedef enum dimension {
Expand All @@ -54,13 +58,27 @@ typedef struct pointArray {
} pointArray;

pointArray * buildPath( symbolList * symList);



/******************************************************************************/
//Drawing:
//Drawing Functions
void draw(pointArray * path);

//generic functions


/******************************************************************************/
//Utility Functions
int printError(const char * errorString, const char file[], const char function[], const int line);
char *strdup(const char * s);
int stringsMatch(const char * string1, const char * string2);



/******************************************************************************/
//Module Unit Tests
void unitTests_main();
void unitTests_parser();


#endif
Loading

0 comments on commit 4d5ea41

Please sign in to comment.