From 3ff0e986bee05be4abb3c49d2fddec0fa8ccfc59 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 8 Apr 2020 10:23:35 -0400 Subject: Add test_relocation_file_replace_text (#14) * Add test_relocation_file_replace_text * Fix bad BUFSIZ allocation and interaction * Use the line limit not the number of lines * Zero line buffer before next read --- tests/test_relocation_file_replace_text.c | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/test_relocation_file_replace_text.c (limited to 'tests/test_relocation_file_replace_text.c') 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 -- cgit