Skip to content

Commit

Permalink
[Cellular Library] Use stack memory instead of heap memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
andysun2015 committed Sep 2, 2021
1 parent b5c3a17 commit d6ed9ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
24 changes: 7 additions & 17 deletions test/unit-test/cellular_at_core_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,14 @@ void test_Cellular_ATRemoveAllDoubleQuote_Invalid_Param( void )
void test_Cellular_ATRemoveAllDoubleQuote_Happy_Path( void )
{
CellularATError_t cellularStatus = CELLULAR_AT_SUCCESS;
char * pString = malloc( sizeof( char ) * ( strlen( CELLULAR_SAMPLE_STRING_TWO_DOUBLE_QUOTE ) + 1 ) );
char pString[ sizeof( CELLULAR_SAMPLE_STRING_TWO_DOUBLE_QUOTE ) ] = { 0 };

strcpy( pString, CELLULAR_SAMPLE_STRING_TWO_DOUBLE_QUOTE );

cellularStatus = Cellular_ATRemoveAllDoubleQuote( pString );
TEST_ASSERT_EQUAL( CELLULAR_AT_SUCCESS, cellularStatus );

TEST_ASSERT_EQUAL_STRING( CELLULAR_SAMPLE_STRING_NO_WHITE_SPACE, pString );
free( pString );
}

/**
Expand Down Expand Up @@ -902,15 +901,14 @@ void test_Cellular_ATStrtoi_Invalid_Param( void )
void test_Cellular_ATStrtoi_Happy_Path( void )
{
CellularATError_t cellularStatus = CELLULAR_AT_SUCCESS;
char * pStr = malloc( sizeof( char ) * ( strlen( CELLULAR_SAMPLE_STRTOL_HAPPY_CASE_STRING ) + 1 ) );
char pStr[ sizeof( CELLULAR_SAMPLE_STRTOL_HAPPY_CASE_STRING ) ] = { 0 };
int32_t base = 10;
int32_t Result;

strcpy( pStr, CELLULAR_SAMPLE_STRTOL_HAPPY_CASE_STRING );
cellularStatus = Cellular_ATStrtoi( pStr, base, &Result );
TEST_ASSERT_EQUAL( CELLULAR_AT_SUCCESS, cellularStatus );
TEST_ASSERT_EQUAL( CELLULAR_SAMPLE_STRTOL_HAPPY_CASE_NUMBER, Result );
free( pStr );
}

/**
Expand All @@ -922,12 +920,11 @@ void test_Cellular_ATStrtoi_Error_Path( void )
int32_t base = 10;
int32_t Result;

char * pStr = malloc( sizeof( char ) * ( strlen( CELLULAR_SAMPLE_PREFIX_STRING_STAR_FIRST_INPUT ) + 1 ) );
char pStr[ sizeof( CELLULAR_SAMPLE_PREFIX_STRING_STAR_FIRST_INPUT ) ] = { 0 };

strcpy( pStr, CELLULAR_SAMPLE_PREFIX_STRING_STAR_FIRST_INPUT );
cellularStatus = Cellular_ATStrtoi( pStr, base, &Result );
TEST_ASSERT_EQUAL( CELLULAR_AT_ERROR, cellularStatus );
free( pStr );
}

/**
Expand All @@ -937,19 +934,17 @@ void test_Cellular_ATStrDup_Invalid_Param( void )
{
CellularATError_t cellularStatus = CELLULAR_AT_SUCCESS;
char * pDst;
char * pSrc;
char pSrc[ sizeof( CELLULAR_SAMPLE_PREFIX_STRING_LARGE_INPUT ) ] = { 0 };

cellularStatus = Cellular_ATStrDup( NULL, NULL );
TEST_ASSERT_EQUAL( CELLULAR_AT_BAD_PARAMETER, cellularStatus );

cellularStatus = Cellular_ATStrDup( &pDst, NULL );
TEST_ASSERT_EQUAL( CELLULAR_AT_BAD_PARAMETER, cellularStatus );

pSrc = malloc( strlen( CELLULAR_SAMPLE_PREFIX_STRING_LARGE_INPUT ) + 1 );
strcpy( pSrc, CELLULAR_SAMPLE_PREFIX_STRING_LARGE_INPUT );
cellularStatus = Cellular_ATStrDup( &pDst, pSrc );
TEST_ASSERT_EQUAL( CELLULAR_AT_BAD_PARAMETER, cellularStatus );
free( pSrc );
}

/**
Expand Down Expand Up @@ -1003,15 +998,14 @@ void test_Cellular_ATIsPrefixPresent_Invalid_Param( void )
void test_Cellular_ATIsPrefixPresent_Happy_Path( void )
{
CellularATError_t cellularStatus = CELLULAR_AT_SUCCESS;
char * pStr = malloc( sizeof( char ) * ( strlen( CELLULAR_SAMPLE_PREFIX_STRING_INPUT ) + 1 ) );
char pStr[ sizeof( CELLULAR_SAMPLE_PREFIX_STRING_INPUT ) ] = { 0 };
bool Result;

strcpy( pStr, CELLULAR_SAMPLE_PREFIX_STRING_INPUT );

cellularStatus = Cellular_ATIsPrefixPresent( pStr, &Result );
TEST_ASSERT_EQUAL( CELLULAR_AT_SUCCESS, cellularStatus );
TEST_ASSERT_EQUAL( true, Result );
free( pStr );
}

/**
Expand All @@ -1033,15 +1027,13 @@ void test_Cellular_ATIsPrefixPresent_Empty_String( void )
void test_Cellular_ATIsPrefixPresent_No_Delimiter( void )
{
CellularATError_t cellularStatus = CELLULAR_AT_SUCCESS;
char * pString;
char pString[ sizeof( CELLULAR_SAMPLE_PREFIX_STRING_WRONG_INPUT ) ] = { 0 };
bool Result;

pString = malloc( sizeof( char ) * ( strlen( CELLULAR_SAMPLE_PREFIX_STRING_WRONG_INPUT ) + 1 ) );
strcpy( pString, CELLULAR_SAMPLE_PREFIX_STRING_WRONG_INPUT );
cellularStatus = Cellular_ATIsPrefixPresent( pString, &Result );
TEST_ASSERT_EQUAL( CELLULAR_AT_SUCCESS, cellularStatus );
TEST_ASSERT_EQUAL( false, Result );
free( pString );
}

/**
Expand All @@ -1050,13 +1042,11 @@ void test_Cellular_ATIsPrefixPresent_No_Delimiter( void )
void test_Cellular_ATIsPrefixPresent_Wrong_Prefix( void )
{
CellularATError_t cellularStatus = CELLULAR_AT_SUCCESS;
char * pString;
char pString[ sizeof( CELLULAR_SAMPLE_PREFIX_STRING_STAR_FIRST_INPUT ) ] = { 0 };
bool Result;

pString = malloc( sizeof( char ) * ( strlen( CELLULAR_SAMPLE_PREFIX_STRING_STAR_FIRST_INPUT ) + 1 ) );
strcpy( pString, CELLULAR_SAMPLE_PREFIX_STRING_STAR_FIRST_INPUT );
cellularStatus = Cellular_ATIsPrefixPresent( pString, &Result );
TEST_ASSERT_EQUAL( CELLULAR_AT_SUCCESS, cellularStatus );
TEST_ASSERT_EQUAL( false, Result );
free( pString );
}
23 changes: 10 additions & 13 deletions test/unit-test/cellular_pktio_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ void test__Cellular_PktioSendAtCmd_Null_AtCmd( void )
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;
CellularCommInterfaceHandle_t commInterfaceHandle = ( CellularCommInterfaceHandle_t ) malloc( sizeof( CellularCommInterfaceHandle_t ) );
struct _cellularCommContext commInterfaceHandle = { 0 };
CellularAtReq_t atReqSetRatPriority =
{
"AT+QCFG=\"nwscanseq\"",
Expand All @@ -1661,7 +1661,7 @@ void test__Cellular_PktioSendAtCmd_Null_AtCmd( void )
memset( &context, 0, sizeof( CellularContext_t ) );

context.pCommIntf = pCommIntf;
context.hPktioCommIntf = commInterfaceHandle;
context.hPktioCommIntf = ( CellularCommInterfaceHandle_t ) &commInterfaceHandle;
pktStatus = _Cellular_PktioSendAtCmd( &context, NULL,
atReqSetRatPriority.atCmdType,
atReqSetRatPriority.pAtRspPrefix );
Expand All @@ -1676,7 +1676,7 @@ void test__Cellular_PktioSendAtCmd_Invalid_String( void )
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;
CellularCommInterfaceHandle_t commInterfaceHandle = ( CellularCommInterfaceHandle_t ) malloc( sizeof( CellularCommInterfaceHandle_t ) );
struct _cellularCommContext commInterfaceHandle = { 0 };
CellularAtReq_t atReqSetRatPriority =
{
"AT+QCFG=\"Story for Littel Red Riding Hood: Once upon a time there was a dear little girl who was loved by every one who looked at her, but most of all by her grandmother, and there was nothing that she would not have given to the child. Once she gave her a little cap of red velvet, which suited her so well that she would never wear anything else. So she was always called Little Red Riding Hood.\"",
Expand All @@ -1689,7 +1689,7 @@ void test__Cellular_PktioSendAtCmd_Invalid_String( void )

memset( &context, 0, sizeof( CellularContext_t ) );
context.pCommIntf = pCommIntf;
context.hPktioCommIntf = commInterfaceHandle;
context.hPktioCommIntf = ( CellularCommInterfaceHandle_t ) &commInterfaceHandle;
pktStatus = _Cellular_PktioSendAtCmd( &context, atReqSetRatPriority.pAtCmd,
atReqSetRatPriority.atCmdType,
atReqSetRatPriority.pAtRspPrefix );
Expand All @@ -1704,7 +1704,7 @@ void test__Cellular_PktioSendAtCmd_Happy_Path( void )
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;
CellularCommInterfaceHandle_t commInterfaceHandle = ( CellularCommInterfaceHandle_t ) malloc( sizeof( CellularCommInterfaceHandle_t ) );
struct _cellularCommContext commInterfaceHandle = { 0 };
CellularAtReq_t atReqSetRatPriority =
{
"AT+QCFG=\"nwscanseq\"",
Expand All @@ -1717,7 +1717,7 @@ void test__Cellular_PktioSendAtCmd_Happy_Path( void )

memset( &context, 0, sizeof( CellularContext_t ) );
context.pCommIntf = pCommIntf;
context.hPktioCommIntf = commInterfaceHandle;
context.hPktioCommIntf = ( CellularCommInterfaceHandle_t ) &commInterfaceHandle;
pktStatus = _Cellular_PktioSendAtCmd( &context, atReqSetRatPriority.pAtCmd,
atReqSetRatPriority.atCmdType,
atReqSetRatPriority.pAtRspPrefix );
Expand All @@ -1732,7 +1732,7 @@ void test__Cellular_PktioSendData_Invalid_Param( void )
uint32_t sentLen = 0;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;
CellularCommInterfaceHandle_t commInterfaceHandle = ( CellularCommInterfaceHandle_t ) malloc( sizeof( CellularCommInterfaceHandle_t ) );
struct _cellularCommContext commInterfaceHandle = { 0 };
char pString[] = CELLULAR_AT_CMD_MULTI_WO_PREFIX;

memset( &context, 0, sizeof( CellularContext_t ) );
Expand All @@ -1752,13 +1752,11 @@ void test__Cellular_PktioSendData_Invalid_Param( void )
0 );
TEST_ASSERT_EQUAL( 0, sentLen );

context.hPktioCommIntf = commInterfaceHandle;
context.hPktioCommIntf = ( CellularCommInterfaceHandle_t ) &commInterfaceHandle;
sentLen = _Cellular_PktioSendData( &context,
NULL,
0 );
TEST_ASSERT_EQUAL( 0, sentLen );

free( commInterfaceHandle );
}

/**
Expand All @@ -1769,18 +1767,17 @@ void test__Cellular_PktioSendData_Happy_Path( void )
uint32_t sentLen = 0;
CellularContext_t context;
CellularCommInterface_t * pCommIntf = &CellularCommInterface;
CellularCommInterfaceHandle_t commInterfaceHandle = ( CellularCommInterfaceHandle_t ) malloc( sizeof( CellularCommInterfaceHandle_t ) );
struct _cellularCommContext commInterfaceHandle = { 0 };
char pString[] = CELLULAR_AT_CMD_MULTI_WO_PREFIX;

memset( &context, 0, sizeof( CellularContext_t ) );
context.pCommIntf = pCommIntf;
context.hPktioCommIntf = commInterfaceHandle;
context.hPktioCommIntf = ( CellularCommInterfaceHandle_t ) &commInterfaceHandle;

sentLen = _Cellular_PktioSendData( &context,
( uint8_t * ) pString,
strlen( pString ) + 1 );
TEST_ASSERT_EQUAL( strlen( pString ) + 1, sentLen );
free( commInterfaceHandle );
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/unit-test/cellular_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ typedef struct PlatformMutex
*/
typedef TickType_t EventBits_t;

struct _cellularCommContext
{
int test1;
int test2;
int test3;
};

/*-----------------------------------------------------------*/

/**
Expand Down

0 comments on commit d6ed9ba

Please sign in to comment.