Adding unit test and refactoring

Added unit test for errormanager and refactored to simplify use of error_check() method.
This commit is contained in:
Michael Tryby
2019-03-20 15:43:27 -04:00
parent 01eb164fdc
commit 7eadbc25ce
5 changed files with 141 additions and 36 deletions

View File

@@ -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.
//

View File

@@ -12,16 +12,27 @@
#define ERR_MAXMSG 256
#if defined(__cplusplus)
extern "C" {
#endif
typedef struct error_s {
int error_status;
int error_status;
void (*p_msg_lookup)(int, char*, int);
} error_handle_t;
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_ */