From 02ec735c58461a85b79aef6b0f34a37ff5f2d62c Mon Sep 17 00:00:00 2001 From: Michael Tryby Date: Tue, 16 Apr 2019 15:51:19 -0400 Subject: [PATCH] Work in progress Refactoring cstr_copy and adding test --- src/util/cstr_helper.c | 9 +++------ src/util/cstr_helper.h | 2 +- src/util/filemanager.c | 8 ++++---- tests/util/test_cstrhelper.cpp | 24 +++++++++++++++++++++++- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/util/cstr_helper.c b/src/util/cstr_helper.c index ec51594..fd91ff3 100644 --- a/src/util/cstr_helper.c +++ b/src/util/cstr_helper.c @@ -17,13 +17,10 @@ #include "cstr_helper.h" -int cstr_copy(const char *source, char **dest) -// Determines length, allocates memory, and returns a null terminated copy -// Be Aware: caller is responsible for freeing memory +int cstr_duplicate(char **dest, const char *source) +// Duplicates source string { - size_t size; - - size = 1 + strlen(source); + size_t size = 1 + strlen(source); *dest = (char *) calloc(size, sizeof(char)); if (*dest == NULL) diff --git a/src/util/cstr_helper.h b/src/util/cstr_helper.h index 6817f5d..ca3ce6d 100644 --- a/src/util/cstr_helper.h +++ b/src/util/cstr_helper.h @@ -23,7 +23,7 @@ extern "C" { #endif -int cstr_copy(const char *source, char **destination); +int cstr_duplicate(char **dest, const char *source); bool cstr_validate_id(const char *element_id); diff --git a/src/util/filemanager.c b/src/util/filemanager.c index 13c59c0..79a45d2 100644 --- a/src/util/filemanager.c +++ b/src/util/filemanager.c @@ -63,7 +63,7 @@ int get_filename(file_handle_t *file_handle, char **filename) // BE AWARE: The memory allocated here must be freed by the caller // { - return cstr_copy(file_handle->filename, filename); + return cstr_duplicate(filename, file_handle->filename); } @@ -73,7 +73,7 @@ int open_file(file_handle_t *file_handle, const char *filename, const char *file if (filename == NULL) _get_temp_filename(&(file_handle->filename)); else - cstr_copy(filename, &(file_handle->filename)); + cstr_duplicate(&(file_handle->filename), filename); if (file_mode == NULL) error = -1; @@ -195,7 +195,7 @@ int _get_temp_filename(char **tempname) return error; } else - cstr_copy(name, tempname); + cstr_duplicate(tempname, name); // --- free the pointer returned by _tempnam if (name) @@ -204,7 +204,7 @@ int _get_temp_filename(char **tempname) // --- for non-Windows systems: #else // --- use system function mkstemp() to create a temporary file name - cstr_copy("enXXXXXX", tempname); + cstr_duplicate(tempname, "enXXXXXX"); error = mkstemp(*tempname); #endif return error; diff --git a/tests/util/test_cstrhelper.cpp b/tests/util/test_cstrhelper.cpp index 1209e99..6011573 100644 --- a/tests/util/test_cstrhelper.cpp +++ b/tests/util/test_cstrhelper.cpp @@ -17,10 +17,32 @@ #include "util/cstr_helper.h" +boost::test_tools::predicate_result check_string(std::string test, std::string ref) +{ + if (ref.compare(test) == 0) + return true; + else + return false; +} + + BOOST_AUTO_TEST_SUITE(test_cstrhelper) -BOOST_AUTO_TEST_CASE(test_validate_id){ +BOOST_AUTO_TEST_CASE(test_duplicate) { + char source[] = "I will be rewarded for good behavior."; + char *dest = NULL; + + cstr_duplicate(&dest, source); + BOOST_CHECK(check_string(dest, source)); + BOOST_CHECK(cstr_isnullterm(dest) == true); + + free(dest); + free(source); +} + + +BOOST_AUTO_TEST_CASE(test_validate_id) { BOOST_CHECK(cstr_validate_id("big tank") == false); BOOST_CHECK(cstr_validate_id("big\"tank") == false);