Work in progress
Refactoring cstr_copy and adding test
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user