Adding unit test and refactoring
Added unit test for errormanager and refactored to simplify use of error_check() method.
This commit is contained in:
@@ -93,7 +93,7 @@ int EXPORT_PY_API proj_gettitle(Handle ph, char *line1, char *line2, char *line3
|
||||
int EXPORT_PY_API proj_settitle(Handle ph, const char *line1, const char *line2, const char *line3)
|
||||
{
|
||||
handle_t *pr = (handle_t *)ph;
|
||||
return error_set(pr->error, EN_settitle(pr->project, line1, line2, line3));
|
||||
return error_set(pr->error, EN_settitle(pr->project, (char *)line1, (char *)line2, (char *)line3));
|
||||
}
|
||||
|
||||
int EXPORT_PY_API proj_getcount(Handle ph, EN_CountType code, int *count)
|
||||
@@ -761,28 +761,13 @@ void EXPORT_PY_API err_clear(Handle ph)
|
||||
|
||||
int EXPORT_PY_API err_check(Handle ph, char** msg_buffer)
|
||||
//
|
||||
// Purpose: Returns the error message or NULL.
|
||||
// Purpose: Returns the error code and message or 0 and NULL respectively.
|
||||
//
|
||||
// Note: Caller must free memory allocated by EN_check_error
|
||||
//
|
||||
{
|
||||
int errorcode = 0;
|
||||
char *temp = NULL;
|
||||
|
||||
handle_t *pr = (handle_t *)ph;
|
||||
|
||||
|
||||
if (pr == NULL) return -1;
|
||||
else
|
||||
{
|
||||
errorcode = pr->error->error_status;
|
||||
if (errorcode)
|
||||
temp = error_check(pr->error);
|
||||
|
||||
*msg_buffer = temp;
|
||||
}
|
||||
|
||||
return errorcode;
|
||||
return error_check(pr->error, msg_buffer);
|
||||
}
|
||||
|
||||
int EXPORT_PY_API toolkit_getversion(int *version)
|
||||
|
||||
@@ -9,16 +9,25 @@
|
||||
// Author: Michael E. Tryby
|
||||
// US EPA - ORD/NRMRL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//#ifdef _WIN32
|
||||
//#define _CRTDBG_MAP_ALLOC
|
||||
//#include <stdlib.h>
|
||||
//#include <crtdbg.h>
|
||||
//#else
|
||||
#include <stdlib.h>
|
||||
//#endif
|
||||
#include <string.h>
|
||||
|
||||
#include "errormanager.h"
|
||||
|
||||
error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int))
|
||||
|
||||
error_handle_t *error_new_manager(void (*p_error_message)(int, char*, int))
|
||||
//
|
||||
// Purpose: Constructs a new error handle.
|
||||
//
|
||||
{
|
||||
error_handle_t* error_handle;
|
||||
error_handle_t *error_handle;
|
||||
error_handle = (error_handle_t*)calloc(1, sizeof(error_handle_t));
|
||||
|
||||
error_handle->p_msg_lookup = p_error_message;
|
||||
@@ -26,7 +35,7 @@ error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int))
|
||||
return error_handle;
|
||||
}
|
||||
|
||||
void error_dst_manager(error_handle_t* error_handle)
|
||||
void error_dst_manager(error_handle_t *error_handle)
|
||||
//
|
||||
// Purpose: Destroys the error handle.
|
||||
//
|
||||
@@ -34,38 +43,39 @@ void error_dst_manager(error_handle_t* error_handle)
|
||||
free(error_handle);
|
||||
}
|
||||
|
||||
int error_set(error_handle_t* error_handle, int errorcode)
|
||||
int error_set(error_handle_t *error_handle, int error_code)
|
||||
//
|
||||
// Purpose: Sets an error code in the handle.
|
||||
//
|
||||
{
|
||||
// If the error code is 0 no action is taken and 0 is returned.
|
||||
// This is a feature not a bug.
|
||||
if (errorcode)
|
||||
error_handle->error_status = errorcode;
|
||||
if (error_code)
|
||||
error_handle->error_status = error_code;
|
||||
|
||||
return errorcode;
|
||||
return error_code;
|
||||
}
|
||||
|
||||
char* error_check(error_handle_t* error_handle)
|
||||
int error_check(error_handle_t *error_handle, char **error_message)
|
||||
//
|
||||
// Purpose: Returns the error message or NULL.
|
||||
//
|
||||
// Note: Caller must free memory allocated by check_error
|
||||
//
|
||||
{
|
||||
char* temp = NULL;
|
||||
{ int error_code = error_handle->error_status;
|
||||
char *temp = NULL;
|
||||
|
||||
if (error_handle->error_status != 0) {
|
||||
temp = (char*) calloc(ERR_MAXMSG, sizeof(char));
|
||||
if (error_code != 0) {
|
||||
temp = (char*) calloc(ERR_MAXMSG + 1, sizeof(char));
|
||||
|
||||
if (temp)
|
||||
error_handle->p_msg_lookup(error_handle->error_status, temp, ERR_MAXMSG);
|
||||
error_handle->p_msg_lookup(error_code, temp, ERR_MAXMSG);
|
||||
}
|
||||
return temp;
|
||||
*error_message = temp;
|
||||
return error_code;
|
||||
}
|
||||
|
||||
void error_clear(error_handle_t* error_handle)
|
||||
void error_clear(error_handle_t *error_handle)
|
||||
//
|
||||
// Purpose: Clears the error from the handle.
|
||||
//
|
||||
|
||||
@@ -12,6 +12,12 @@
|
||||
|
||||
#define ERR_MAXMSG 256
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct error_s {
|
||||
int error_status;
|
||||
void (*p_msg_lookup)(int, char*, int);
|
||||
@@ -20,8 +26,13 @@ typedef struct error_s {
|
||||
error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int));
|
||||
void error_dst_manager(error_handle_t* error_handle);
|
||||
|
||||
int error_set(error_handle_t* error_handle, int errorcode);
|
||||
char* error_check(error_handle_t* error_handle);
|
||||
int error_set(error_handle_t* error_handle, int error_code);
|
||||
int error_check(error_handle_t* error_handle, char **error_message);
|
||||
void error_clear(error_handle_t* error_handle);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ERRORMANAGER_H_ */
|
||||
|
||||
18
tests/util/CMakeLists.txt
Normal file
18
tests/util/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
|
||||
enable_testing()
|
||||
|
||||
set (Boost_USE_STATIC_LIBS OFF)
|
||||
find_package(Boost COMPONENTS unit_test_framework)
|
||||
include_directories (${Boost_INCLUDE_DIRS} ../../src/)
|
||||
|
||||
set (test_source
|
||||
./test_errormanager.cpp
|
||||
../../src/util/errormanager.c
|
||||
)
|
||||
|
||||
add_executable(test_errormanager ${test_source})
|
||||
|
||||
target_link_libraries(test_errormanager ${Boost_LIBRARIES})
|
||||
81
tests/util/test_errormanager.cpp
Normal file
81
tests/util/test_errormanager.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
|
||||
|
||||
#define BOOST_TEST_MODULE errormanager
|
||||
#define BOOST_TEST_DYN_LINK
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "util/errormanager.h"
|
||||
|
||||
|
||||
#define MESSAGE_STRING "This is unit testing!"
|
||||
|
||||
|
||||
void mock_lookup(int errcode, char *errmsg, int len)
|
||||
{
|
||||
char *msg = NULL;
|
||||
|
||||
if (errcode == 100) {
|
||||
msg = MESSAGE_STRING;
|
||||
}
|
||||
else {
|
||||
msg = "";
|
||||
}
|
||||
strncpy(errmsg, msg, len);
|
||||
}
|
||||
|
||||
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_errormanager)
|
||||
|
||||
BOOST_AUTO_TEST_CASE (test_create_destroy)
|
||||
{
|
||||
error_handle_t *error_handle = NULL;
|
||||
error_handle = error_new_manager(&mock_lookup);
|
||||
|
||||
error_dst_manager(error_handle);
|
||||
}
|
||||
|
||||
|
||||
struct Fixture{
|
||||
Fixture() {
|
||||
error_message = NULL;
|
||||
error_handle = error_new_manager(&mock_lookup);
|
||||
}
|
||||
~Fixture() {
|
||||
error_dst_manager(error_handle);
|
||||
free(error_message);
|
||||
}
|
||||
int error;
|
||||
error_handle_t *error_handle;
|
||||
char *error_message;
|
||||
};
|
||||
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE (test_set_clear, Fixture)
|
||||
{
|
||||
error = error_set(error_handle, 100);
|
||||
BOOST_CHECK(error == 100);
|
||||
|
||||
error_clear(error_handle);
|
||||
error = error_check(error_handle, &error_message);
|
||||
BOOST_CHECK(error == 0);
|
||||
BOOST_CHECK(error_message == NULL);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(test_set_check, Fixture)
|
||||
{
|
||||
error = error_set(error_handle, 100);
|
||||
BOOST_CHECK(error == 100);
|
||||
|
||||
error = error_check(error_handle, &error_message);
|
||||
BOOST_CHECK(check_string(error_message, MESSAGE_STRING));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
Reference in New Issue
Block a user