Skip to content

Commit

Permalink
Fix #8: Add logic to sample_lib for UT
Browse files Browse the repository at this point in the history
In order to make a useful library unit test example, the
sample_lib itself needs to perform some more interesting work.

In particular it needs at least one external function call
on which a code path decision is made, such that the example UT
code will have more than one code path to demonstrate.

Additionally, the library should have some internal "global"
structure (like real libs would) that contains its state, that
the UT can also test/query.

Both objectives can be accomplished with a small string buffer
that gets populated with a strncpy() call during init, and
printed with the "process" command.
  • Loading branch information
jphickey committed Nov 21, 2019
1 parent b6c4696 commit 2264f97
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
21 changes: 21 additions & 0 deletions fsw/public_inc/sample_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@
/*************************************************************************
** Exported Functions
*************************************************************************/

/************************************************************************/
/** \brief Library Initialization Function
**
** \par Description
** This function is required by CFE to initialize the library
** It should be specified in the cfe_es_startup.scr file as part
** of loading this library. It is not directly invoked by
** applications.
**
** \par Assumptions, External Events, and Notes:
** None
**
** \returns
** \retstmt Returns #CFE_SUCCESS if successful \endcode
** \endreturns
**
*************************************************************************/
int32 SAMPLE_LibInit(void);


/************************************************************************/
/** \brief Sample Lib Function
**
Expand Down
32 changes: 28 additions & 4 deletions fsw/src/sample_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@
#include "sample_lib.h"
#include "sample_lib_version.h"

/* for "strncpy()" */
#include <string.h>

/*************************************************************************
** Macro Definitions
*************************************************************************/

#define SAMPLE_LIB_BUFFER_SIZE 16


/*************************************************************************
** Private Function Prototypes
** Private Data Structures
*************************************************************************/
int32 SAMPLE_LibInit(void);
static char SAMPLE_Buffer[SAMPLE_LIB_BUFFER_SIZE];

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
Expand All @@ -49,8 +54,27 @@ int32 SAMPLE_LibInit(void);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 SAMPLE_LibInit(void)
{
/*
* Call a C library function, like strcpy(), and test its result.
*
* This is primary for a unit test example, to have more than
* one code path to exercise.
*
* The specification for strncpy() indicates that it should return
* the pointer to the destination buffer, so it should be impossible
* for this to ever fail when linked with a compliant C library.
*/
if (strncpy(SAMPLE_Buffer, "SAMPLE DATA", sizeof(SAMPLE_Buffer)-1) !=
SAMPLE_Buffer)
{
return CFE_STATUS_NOT_IMPLEMENTED;
}

OS_printf ("SAMPLE Lib Initialized. Version %d.%d.%d.%d",
/* ensure termination */
SAMPLE_Buffer[sizeof(SAMPLE_Buffer)-1] = 0;


OS_printf ("SAMPLE Lib Initialized. Version %d.%d.%d.%d\n",
SAMPLE_LIB_MAJOR_VERSION,
SAMPLE_LIB_MINOR_VERSION,
SAMPLE_LIB_REVISION,
Expand All @@ -67,7 +91,7 @@ int32 SAMPLE_LibInit(void)
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 SAMPLE_Function( void )
{
OS_printf ("SAMPLE_Function called\n");
OS_printf ("SAMPLE_Function called, buffer=\'%s\'\n", SAMPLE_Buffer);

return(CFE_SUCCESS);

Expand Down

0 comments on commit 2264f97

Please sign in to comment.