diff options
-rw-r--r-- | lib/mirrors.c | 11 | ||||
-rw-r--r-- | tests/test_relocation_file_replace_text.c | 40 |
2 files changed, 46 insertions, 5 deletions
diff --git a/lib/mirrors.c b/lib/mirrors.c index cad3f6b..6bc7aed 100644 --- a/lib/mirrors.c +++ b/lib/mirrors.c @@ -14,7 +14,7 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn } // Allocate buffer - if ((buffer = calloc(BUFSIZ + 1, sizeof(char))) == NULL) { + if ((buffer = calloc(BUFSIZ, sizeof(char))) == NULL) { perror("line buffer"); fprintf(SYSERROR); fclose(fp); @@ -22,7 +22,7 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn } // count number the of lines in the file - while ((fgets(buffer, BUFSIZ, fp)) != NULL) { + while ((fgets(buffer, BUFSIZ - 1, fp)) != NULL) { lines++; } @@ -51,13 +51,13 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn } // Populate results array - result = calloc(lines + 1, sizeof(char *)); + result = calloc(limit + 1, sizeof(char *)); for (size_t i = start; i < limit; i++) { if (i < start) { continue; } - if (fgets(buffer, BUFSIZ, fp) == NULL) { + if (fgets(buffer, BUFSIZ - 1, fp) == NULL) { break; } @@ -73,7 +73,8 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn break; } } - result[i - start] = strdup(buffer); + result[i] = strdup(buffer); + memset(buffer, '\0', BUFSIZ); } free(buffer); diff --git a/tests/test_relocation_file_replace_text.c b/tests/test_relocation_file_replace_text.c new file mode 100644 index 0000000..fc7688b --- /dev/null +++ b/tests/test_relocation_file_replace_text.c @@ -0,0 +1,40 @@ +#include "spm.h" +#include "framework.h" + +const char *testFmt = "case '%s': returned '%s', expected '%s'\n"; +struct TestCase testCase[] = { + {.caseValue.sptr = "This is a test... Happy friend.", .arg[0].sptr = "...", .arg[1].sptr = ".", .truthValue.sptr = "This is a test. Happy friend."}, + {.caseValue.sptr = "This is a test... Happy friend.", .arg[0].sptr = "test", .arg[1].sptr = "win", .truthValue.sptr = "This is a win... Happy friend."}, + {.caseValue.sptr = "This is a test... Happy friend.", .arg[0].sptr = "This is a test", .arg[1].sptr = "Meow cat", .truthValue.sptr = "Meow cat... Happy friend."}, + {.caseValue.sptr = "This is a test... Happy friend.", .arg[0].sptr = "T", .arg[1].sptr = "#", .truthValue.sptr = "#his is a test... Happy friend."}, + {.caseValue.sptr = "This is a test... Happy friend.", .arg[0].sptr = "", .arg[1].sptr = "#", .truthValue.sptr = "This is a test... Happy friend."}, + {.caseValue.sptr = "This is a test... Happy friend.", .arg[0].sptr = "#", .arg[1].sptr = "", .truthValue.sptr = "This is a test... Happy friend."}, + {.caseValue.sptr = "This is a test... Happy friend.", .arg[0].sptr = "", .arg[1].sptr = "", .truthValue.sptr = "This is a test... Happy friend."}, +}; +size_t numCases = sizeof(testCase) / sizeof(struct TestCase); + +int main(int argc, char *argv[]) { + for (size_t i = 0; i < numCases; i++) { + char **data = NULL; + char *caseValue = NULL; + char filename[PATH_MAX] = {0,}; + sprintf(filename, "%s.%s_%zu.mock", basename(__FILE__), __FUNCTION__, i); + + mock(filename, (void *)testCase[i].caseValue.sptr, sizeof(char), strlen(testCase[i].caseValue.sptr)); + file_replace_text(filename, testCase[i].arg[0].sptr, testCase[i].arg[1].sptr); + + data = file_readlines(filename, 0, 0, NULL); + caseValue = join(data, ""); + + myassert(strcmp(caseValue, testCase[i].truthValue.sptr) == 0, testFmt, testCase[i].caseValue.sptr, caseValue, testCase[i].truthValue.sptr); + + // clean up + for (size_t rec = 0; data[rec] != NULL; rec++) { + free(data[rec]); + } + free(data); + free(caseValue); + unlink(filename); + } + return 0; +}
\ No newline at end of file |