Skip to content

Commit

Permalink
WIP - unit test now multiple executables to cover all code, full cove…
Browse files Browse the repository at this point in the history
…rage on msg map implementations
  • Loading branch information
skliper committed Oct 20, 2020
1 parent fafac8e commit eed8c3a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 172 deletions.
18 changes: 13 additions & 5 deletions modules/sbr/unit-test-coverage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,34 @@
#
##################################################################

# Set tests once so name changes are in one location
set(SBR_TEST_MAP_DIRECT "sbr_map_direct")
set(SBR_TEST_MAP_HASH "sbr_map_hash")
set(SBR_TEST_ROUTE_UNSORTED "sbr_route_unsorted")

# All coverage tests always built
set(SBR_TEST_SET "map_direct" "map_hash" "route_unsorted")
set(SBR_TEST_SET ${SBR_TEST_MAP_DIRECT} ${SBR_TEST_MAP_HASH} ${SBR_TEST_ROUTE_UNSORTED})

# Add configured map implementation to routing test source
if (MISSION_MSGMAP_IMPLEMENTATION STREQUAL "DIRECT")
set(route_unsorted_SRC test_cfe_sbr_map_direct.c)
set(${SBR_TEST_ROUTE_UNSORTED}_SRC ../src/cfe_sbr_map_direct.c)
elseif (MISSION_MSGMAP_IMPLEMENTATION STREQUAL "HASH")
set(route_unsorted_SRC test_cfe_sbr_map_hash.c)
set(${SBR_TEST_ROUTE_UNSORTED}_SRC ../src/cfe_sbr_map_hash.c)
endif()

# Add route implementation to map hash
set(${SBR_TEST_MAP_HASH}_SRC ../src/cfe_sbr_route_unsorted.c)

foreach(SBR_TEST ${SBR_TEST_SET})

# Unit test object library sources, options, and includes
add_library(ut_${SBR_TEST}_objs OBJECT ${${SBR_TEST}_SRC} cfe_sbr_${SBR_TEST}.c)
add_library(ut_${SBR_TEST}_objs OBJECT ${${SBR_TEST}_SRC} ../src/cfe_${SBR_TEST}.c)
target_compile_options(ut_${SBR_TEST}_objs PRIVATE ${UT_COVERAGE_COMPILE_FLAGS})
target_include_directories(ut_${SBR_TEST}_objs PRIVATE
$<TARGET_PROPERTY:${DEP},INCLUDE_DIRECTORIES>)

set (ut_${SBR_TEST}_tests
test_cfe_sbr_${SBR_TEST}.c
test_cfe_${SBR_TEST}.c
$<TARGET_OBJECTS:ut_${SBR_TEST}_objs>)

# Add executable
Expand Down
51 changes: 0 additions & 51 deletions modules/sbr/unit-test-coverage/sbr_UT.c

This file was deleted.

33 changes: 0 additions & 33 deletions modules/sbr/unit-test-coverage/test_cfe_sbr_map.h

This file was deleted.

37 changes: 25 additions & 12 deletions modules/sbr/unit-test-coverage/test_cfe_sbr_map_direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@
#include "ut_support.h"
#include "private/cfe_sbr.h"
#include "cfe_sbr_priv.h"
#include <stdlib.h>

void Test_SBR_Map_Direct(void)
{

CFE_SB_MsgId_Atom_t idx;
CFE_SB_MsgId_Atom_t msgidx;
CFE_SBR_RouteId_t routeid;
CFE_SB_MsgId_t msgid;
uint32 invalidcnt;
uint32 count;
uint32 i;

UtPrintf("Invalid msg checks");
ASSERT_EQ(CFE_SBR_SetRouteId(CFE_SB_ValueToMsgId(0), CFE_SBR_ValueToRouteId(0)), 0);
Expand All @@ -49,15 +51,15 @@ void Test_SBR_Map_Direct(void)
UT_SetForceFail(UT_KEY(CFE_SB_IsValidMsgId), true);

UtPrintf("Check that all entries are set invalid");
invalidcnt = 0;
for (idx = 0; idx <= CFE_PLATFORM_SB_HIGHEST_VALID_MSGID; idx++)
count = 0;
for (msgidx = 0; msgidx <= CFE_PLATFORM_SB_HIGHEST_VALID_MSGID; msgidx++)
{
if (!CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(idx))))
if (!CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(msgidx))))
{
invalidcnt++;
count++;
}
}
ASSERT_EQ(invalidcnt, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID + 1);
ASSERT_EQ(count, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID + 1);

UtPrintf("Set/Get a range of ids ");
routeid = CFE_SBR_ValueToRouteId(CFE_PLATFORM_SB_MAX_MSG_IDS + 1);
Expand All @@ -71,21 +73,32 @@ void Test_SBR_Map_Direct(void)
ASSERT_EQ(CFE_SBR_GetRouteId(msgid).RouteId, routeid.RouteId);

UtPrintf("Check there is now 1 valid entry in map");
invalidcnt = 0;
for (idx = 0; idx <= CFE_PLATFORM_SB_HIGHEST_VALID_MSGID; idx++)
count = 0;
for (msgidx = 0; msgidx <= CFE_PLATFORM_SB_HIGHEST_VALID_MSGID; msgidx++)
{
if (!CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(idx))))
if (!CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(msgidx))))
{
invalidcnt++;
count++;
}
}
ASSERT_EQ(invalidcnt, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID);
ASSERT_EQ(count, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID);

UtPrintf("Set back to invalid and check again");
routeid = CFE_SBR_INVALID_ROUTE_ID;
ASSERT_EQ(CFE_SBR_SetRouteId(msgid, routeid), 0);
ASSERT_EQ(CFE_SBR_GetRouteId(msgid).RouteId, routeid.RouteId);
ASSERT_EQ(CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(msgid)), false);

/* Performance check, 0xFFFFFF on 3.2GHz linux box is around 8-9 seconds */
count = 0;
for (i = 0; i <= 0xFFFF; i++)
{
msgidx = rand() % CFE_PLATFORM_SB_HIGHEST_VALID_MSGID;
if (CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(msgidx))))
{
count++;
}
}
}

/* Main unit test routine */
Expand Down
80 changes: 44 additions & 36 deletions modules/sbr/unit-test-coverage/test_cfe_sbr_map_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "ut_support.h"
#include "private/cfe_sbr.h"
#include "cfe_sbr_priv.h"
#include "test_cfe_sbr_map.h"

/*
* Defines
Expand All @@ -50,60 +49,69 @@ CFE_SB_MsgId_t Test_SBR_Unhash(CFE_SB_MsgId_Atom_t Hash) {
return CFE_SB_ValueToMsgId(Hash);
}

void Test_SBR_Map(void)
void Test_SBR_Map_Hash(void)
{

CFE_SB_MsgId_Atom_t idx;
CFE_SBR_RouteId_t routeid;
CFE_SB_MsgId_t msgid;
uint32 invalidcnt;
CFE_SB_MsgId_Atom_t msgidx;
CFE_SBR_RouteId_t routeid[3];
CFE_SB_MsgId_t msgid[3];
uint32 count;
uint32 collisions;

UtPrintf("Invalid msg checks");
ASSERT_EQ(CFE_SBR_SetRouteId(CFE_SB_ValueToMsgId(0), CFE_SBR_ValueToRouteId(0)), 0);
ASSERT_EQ(CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(0))), false);

UtPrintf("Initialize map");
CFE_SBR_Init_Map();
UtPrintf("Initialize routing and map");
CFE_SBR_Init();

/* Force valid msgid responses */
UT_SetForceFail(UT_KEY(CFE_SB_IsValidMsgId), true);

UtPrintf("Check that all entries are set invalid");
invalidcnt = 0;
for (idx = 0; idx <= CFE_PLATFORM_SB_HIGHEST_VALID_MSGID; idx++)
count = 0;
for (msgidx = 0; msgidx <= CFE_PLATFORM_SB_HIGHEST_VALID_MSGID; msgidx++)
{
if (!CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(idx))))
if (!CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(msgidx))))
{
invalidcnt++;
count++;
}
}
ASSERT_EQ(invalidcnt, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID + 1);

UtPrintf("Set/Get a range of ids ");
routeid = CFE_SBR_ValueToRouteId(CFE_PLATFORM_SB_MAX_MSG_IDS + 1);
msgid = CFE_SB_ValueToMsgId(0);
ASSERT_EQ(CFE_SBR_SetRouteId(msgid, routeid), 0);
ASSERT_EQ(CFE_SBR_GetRouteId(msgid).RouteId, routeid.RouteId);

routeid = CFE_SBR_ValueToRouteId(0);
msgid = CFE_SB_ValueToMsgId(CFE_PLATFORM_SB_HIGHEST_VALID_MSGID);
ASSERT_EQ(CFE_SBR_SetRouteId(msgid, routeid), 0);
ASSERT_EQ(CFE_SBR_GetRouteId(msgid).RouteId, routeid.RouteId);

UtPrintf("Check there is now 1 valid entry in map");
invalidcnt = 0;
for (idx = 0; idx <= CFE_PLATFORM_SB_HIGHEST_VALID_MSGID; idx++)
ASSERT_EQ(count, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID + 1);

/* Note AddRoute required for hash logic to work since it depends on MsgId in routing table */
UtPrintf("Add routes and check with a rollover and a skip");
msgid[0] = CFE_SB_ValueToMsgId(0);
msgid[1] = Test_SBR_Unhash(0xFFFFFFFF);
msgid[2] = Test_SBR_Unhash(0x7FFFFFFF);
routeid[0] = CFE_SBR_AddRoute(msgid[0], &collisions);
ASSERT_EQ(collisions, 0);
routeid[1] = CFE_SBR_AddRoute(msgid[1], &collisions);
ASSERT_EQ(collisions, 0);
routeid[2] = CFE_SBR_AddRoute(msgid[2], &collisions);
ASSERT_EQ(collisions, 2);

ASSERT_EQ(CFE_SBR_RouteIdToValue(CFE_SBR_GetRouteId(msgid[0])), CFE_SBR_RouteIdToValue(routeid[0]));
ASSERT_EQ(CFE_SBR_RouteIdToValue(CFE_SBR_GetRouteId(msgid[1])), CFE_SBR_RouteIdToValue(routeid[1]));
ASSERT_EQ(CFE_SBR_RouteIdToValue(CFE_SBR_GetRouteId(msgid[2])), CFE_SBR_RouteIdToValue(routeid[2]));

/* Performance check, 0xFFFFFF on 3.2GHz linux box is around 8-9 seconds */
count = 0;
for (msgidx = 0; msgidx <= 0xFFFF; msgidx++)
{
if (!CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(idx))))
if (CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(CFE_SB_ValueToMsgId(msgidx))))
{
invalidcnt++;
count++;
}
}
ASSERT_EQ(invalidcnt, CFE_PLATFORM_SB_HIGHEST_VALID_MSGID);
}

/* Main unit test routine */
void UtTest_Setup(void)
{
UT_Init("map_hash");
UtPrintf("Software Bus Routing hash map coverage test...");

UtPrintf("Set back to invalid and check again");
routeid = CFE_SBR_INVALID_ROUTE_ID;
ASSERT_EQ(CFE_SBR_SetRouteId(msgid, routeid), 0);
ASSERT_EQ(CFE_SBR_GetRouteId(msgid).RouteId, routeid.RouteId);
ASSERT_EQ(CFE_SBR_IsValidRouteId(CFE_SBR_GetRouteId(msgid)), false);
UT_ADD_TEST(Test_SBR_Map_Hash);
}

33 changes: 0 additions & 33 deletions modules/sbr/unit-test-coverage/test_cfe_sbr_route.h

This file was deleted.

13 changes: 11 additions & 2 deletions modules/sbr/unit-test-coverage/test_cfe_sbr_route_unsorted.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
#include "ut_support.h"
#include "private/cfe_sbr.h"
#include "cfe_sbr_priv.h"
#include "test_cfe_sbr_route.h"

void Test_SBR_Route(void)
void Test_SBR_Route_Unsort(void)
{

/* TODO
Expand Down Expand Up @@ -102,3 +101,13 @@ void CFE_SBR_ForEachRouteId(CFE_SBR_CallbackPtr_t CallbackPtr, void *ArgPtr, CFE

#endif
}

/* Main unit test routine */
void UtTest_Setup(void)
{
UT_Init("route_unsort");
UtPrintf("Software Bus Routing unsorted coverage test...");

UT_ADD_TEST(Test_SBR_Route_Unsort);
}

0 comments on commit eed8c3a

Please sign in to comment.