Skip to content

Commit

Permalink
Issue #37, #41, #24, #20, #5 - Improve unit tests.
Browse files Browse the repository at this point in the history
Fix ctest script.
Make test names easier to filter out in runtime.
  • Loading branch information
vldtecno committed May 12, 2024
1 parent cb3d342 commit cbe1ef9
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 157 deletions.
43 changes: 11 additions & 32 deletions Tests/WhiteBoxTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,17 @@ target_link_libraries(WhiteBoxTest PUBLIC

set(WhiteBoxTestsExecutable "WhiteBoxTest${CMAKE_EXECUTABLE_SUFFIX}")

add_test(NAME InputPlace
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*InputPlace)
add_test(NAME EnterPlaceDefault
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*EnterPlaceDefault)
add_test(NAME EnterPlace
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*EnterPlace)
add_test(NAME ExitPlace
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*ExitPlace)
add_test(NAME Overflow
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*Overflow)
add_test(NAME NullTokens
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*NullTokens)
add_test(NAME NotEnoughTokens
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*NotEnoughTokens)
add_test(NAME T1
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T1)
add_test(NAME T2
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T2)
add_test(NAME T3
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T3)
add_test(NAME T_Weights
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T_Weights)
add_test(NAME T_ZeroValueWeightException
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T_ZeroValueWeightException)
add_test(NAME T_ActivationWeightDimensionException
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T_ActivationWeightDimensionException)
add_test(NAME T_DestinationWeightDimensionException
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T_DestinationWeightDimensionException)
add_test(NAME T_ActivationPlaceRepetitionException
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T_ActivationPlaceRepetitionException)
add_test(NAME T_DestinationPlaceRepetitionException
COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*T_DestinationPlaceRepetitionException)
add_test(NAME WhiteBoxTests COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=*)

#add_test(NAME EventLoop COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=EventLoop_*)
#add_test(NAME JobQueue COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=JobQueue_*)
#add_test(NAME ManagedContainer COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=ManagedContainer_*)
#add_test(NAME Place COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=Place_*)
#add_test(NAME PlacesManager COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=PlacesManager_*)
#add_test(NAME PTN_Engine COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=PTN_Engine_*)
#add_test(NAME PTN_EngineImp COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=PTN_EngineImp_*)
#add_test(NAME Transition COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=Transition_*)
#add_test(NAME TransitionsManager COMMAND ${WhiteBoxTestsExecutable} --gtest_filter=TransitionsManager_*)

set_target_properties(WhiteBoxTest PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})

Expand Down
16 changes: 8 additions & 8 deletions Tests/WhiteBoxTests/Tests/TestEventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@
using namespace ptne;
using namespace std;

class PTNEnginImpObj : public testing::Test
class EventLoop_PTNEnginImpObj : public testing::Test
{
public:
PTN_EngineImp ptnEngineImp = PTN_EngineImp(PTN_Engine::ACTIONS_THREAD_OPTION::EVENT_LOOP);
EventLoop eventLoop = EventLoop(ptnEngineImp);
};

TEST_F(PTNEnginImpObj, isRunning_returns_if_event_loop_is_running)
TEST_F(EventLoop_PTNEnginImpObj, isRunning_returns_if_event_loop_is_running)
{
EXPECT_FALSE(eventLoop.isRunning());
}

TEST_F(PTNEnginImpObj, stop_stops_the_event_loop)
TEST_F(EventLoop_PTNEnginImpObj, stop_stops_the_event_loop)
{
EXPECT_FALSE(eventLoop.isRunning());
eventLoop.stop();
EXPECT_FALSE(eventLoop.isRunning());
}

TEST_F(PTNEnginImpObj, start_starts_the_event_loop)
TEST_F(EventLoop_PTNEnginImpObj, start_starts_the_event_loop)
{
EXPECT_FALSE(eventLoop.isRunning());
eventLoop.start(false, std::cout);
EXPECT_TRUE(eventLoop.isRunning());
eventLoop.stop();
}

TEST(EventLoop, start_single_threaded_stops_immediately_after_start)
TEST(EventLoop_, start_single_threaded_stops_immediately_after_start)
{
PTN_EngineImp ptnEngineImp(PTN_Engine::ACTIONS_THREAD_OPTION::SINGLE_THREAD);
EventLoop eventLoop(ptnEngineImp);
Expand All @@ -59,7 +59,7 @@ TEST(EventLoop, start_single_threaded_stops_immediately_after_start)
EXPECT_FALSE(eventLoop.isRunning());
}

TEST_F(PTNEnginImpObj, start_again_throws)
TEST_F(EventLoop_PTNEnginImpObj, start_again_throws)
{
EXPECT_FALSE(eventLoop.isRunning());
eventLoop.start(false, std::cout);
Expand All @@ -68,14 +68,14 @@ TEST_F(PTNEnginImpObj, start_again_throws)
}

// void notifyNewEvent();
TEST_F(PTNEnginImpObj, notifyNewEvent)
TEST_F(EventLoop_PTNEnginImpObj, notifyNewEvent)
{
ASSERT_FALSE(eventLoop.isRunning());
// TO DO
// fake the ptn_engine
}

TEST_F(PTNEnginImpObj, setSleepDuration)
TEST_F(EventLoop_PTNEnginImpObj, setSleepDuration)
{
EXPECT_EQ(100ms, eventLoop.getSleepDuration());
eventLoop.setSleepDuration(10ms);
Expand Down
8 changes: 4 additions & 4 deletions Tests/WhiteBoxTests/Tests/TestJobQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
using namespace ptne;
using namespace std;

class JobQueueObj : public testing::Test
class JobQueue_Obj : public testing::Test
{
public:
JobQueue jobQueue;
};

TEST_F(JobQueueObj, activate_activates_the_job_queue)
TEST_F(JobQueue_Obj, activate_activates_the_job_queue)
{
EXPECT_TRUE(jobQueue.isActive());
jobQueue.deactivate();
Expand All @@ -37,7 +37,7 @@ TEST_F(JobQueueObj, activate_activates_the_job_queue)
EXPECT_TRUE(jobQueue.isActive());
}

TEST_F(JobQueueObj, add_job_when_deactivated_runs_job_when_again_active)
TEST_F(JobQueue_Obj, add_job_when_deactivated_runs_job_when_again_active)
{
bool executed = false;
auto f = [&executed]() { executed = true; };
Expand All @@ -52,7 +52,7 @@ TEST_F(JobQueueObj, add_job_when_deactivated_runs_job_when_again_active)
EXPECT_TRUE(executed);
}

TEST_F(JobQueueObj, run_add_job_runs_job_immediately_if_active)
TEST_F(JobQueue_Obj, run_add_job_runs_job_immediately_if_active)
{
bool executed = false;
auto f = [&executed]() { executed = true; };
Expand Down
18 changes: 9 additions & 9 deletions Tests/WhiteBoxTests/Tests/TestManagedContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,43 @@ template class ptne::ManagedContainer<int>;
using namespace std;
using namespace ptne;

class ManagedContainerInt : public testing::Test
class ManagedContainer_Int : public testing::Test
{
public:
ManagedContainer<int> managedContainer;
};

TEST_F(ManagedContainerInt, addItem_adds_item)
TEST_F(ManagedContainer_Int, addItem_adds_item)
{
managedContainer.addItem("a", 1);
managedContainer.addItem("b", 2);
EXPECT_EQ(1, managedContainer.getItem("a"));
EXPECT_EQ(2, managedContainer.getItem("b"));
}

TEST_F(ManagedContainerInt, addItem_add_repeated_item_name_throws)
TEST_F(ManagedContainer_Int, addItem_add_repeated_item_name_throws)
{
managedContainer.addItem("a", 1);
EXPECT_EQ(1, managedContainer.getItem("a"));
ASSERT_THROW(managedContainer.addItem("a", 2), RepeatedFunctionException);
}

TEST_F(ManagedContainerInt, addItem_add_item_without_name_throws)
TEST_F(ManagedContainer_Int, addItem_add_item_without_name_throws)
{
ASSERT_THROW(managedContainer.addItem("", 1), InvalidFunctionNameException);
}

TEST_F(ManagedContainerInt, getItem_get_item_with_empty_name_throws)
TEST_F(ManagedContainer_Int, getItem_get_item_with_empty_name_throws)
{
ASSERT_THROW(managedContainer.getItem(""), InvalidFunctionNameException);
}

TEST_F(ManagedContainerInt, getItem_get_item_with_non_existing_name_throws)
TEST_F(ManagedContainer_Int, getItem_get_item_with_non_existing_name_throws)
{
ASSERT_THROW(managedContainer.getItem("a"), InvalidFunctionNameException);
}

TEST_F(ManagedContainerInt, getItems_returns_items_and_in_the_correct_order)
TEST_F(ManagedContainer_Int, getItems_returns_items_and_in_the_correct_order)
{
managedContainer.addItem("a", 1);
managedContainer.addItem("b", 2);
Expand All @@ -83,7 +83,7 @@ TEST_F(ManagedContainerInt, getItems_returns_items_and_in_the_correct_order)
}
}

TEST_F(ManagedContainerInt, getItems_with_invalid_name_throws)
TEST_F(ManagedContainer_Int, getItems_with_invalid_name_throws)
{
managedContainer.addItem("a", 1);
managedContainer.addItem("b", 2);
Expand All @@ -92,7 +92,7 @@ TEST_F(ManagedContainerInt, getItems_with_invalid_name_throws)
ASSERT_THROW(managedContainer.getItems(names), InvalidFunctionNameException);
}

TEST_F(ManagedContainerInt, getItems_with_empty_name_throws)
TEST_F(ManagedContainer_Int, getItems_with_empty_name_throws)
{
managedContainer.addItem("a", 1);
managedContainer.addItem("b", 2);
Expand Down
42 changes: 21 additions & 21 deletions Tests/WhiteBoxTests/Tests/TestPTN_Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@
using namespace std;
using namespace ptne;

class EventLoopPTNEngine : public testing::Test
class PTN_Engine_EventLoop : public testing::Test
{
public:
PTN_Engine ptnEngine = PTN_Engine(PTN_Engine::ACTIONS_THREAD_OPTION::EVENT_LOOP);
};

class JobQueuePTNEngine : public testing::Test
class PTN_Engine_JobQueue : public testing::Test
{
public:
PTN_Engine ptnEngine = PTN_Engine(PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE);
};

TEST(PTN_Engine, constructors_do_not_throw)
TEST(PTN_Engine_, constructors_do_not_throw)
{
ASSERT_NO_THROW(PTN_Engine{ PTN_Engine::ACTIONS_THREAD_OPTION::DETACHED });
ASSERT_NO_THROW(PTN_Engine{ PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE });
ASSERT_NO_THROW(PTN_Engine{ PTN_Engine::ACTIONS_THREAD_OPTION::SINGLE_THREAD });
ASSERT_NO_THROW(PTN_Engine{ PTN_Engine::ACTIONS_THREAD_OPTION::EVENT_LOOP });
}

TEST(PTN_Engine, getActionsThreadOption_returns_the_actions_thread_option)
TEST(PTN_Engine_, getActionsThreadOption_returns_the_actions_thread_option)
{
PTN_Engine ptnEngineJobQueue(PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE);
EXPECT_EQ(PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE, ptnEngineJobQueue.getActionsThreadOption());
Expand All @@ -58,7 +58,7 @@ TEST(PTN_Engine, getActionsThreadOption_returns_the_actions_thread_option)
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, addArc_adds_an_arc_between_a_place_and_a_transition)
TEST_F(PTN_Engine_JobQueue, addArc_adds_an_arc_between_a_place_and_a_transition)
{
ptnEngine.createPlace(PlaceProperties{ .name = "P1", .input = true });

Expand Down Expand Up @@ -101,7 +101,7 @@ TEST_F(JobQueuePTNEngine, addArc_adds_an_arc_between_a_place_and_a_transition)
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, clearNet_clears_the_petri_net_from_all_places_and_transitons)
TEST_F(PTN_Engine_JobQueue, clearNet_clears_the_petri_net_from_all_places_and_transitons)
{
EXPECT_TRUE(ptnEngine.getTransitionsProperties().empty());
EXPECT_TRUE(ptnEngine.getPlacesProperties().empty());
Expand All @@ -124,7 +124,7 @@ TEST_F(JobQueuePTNEngine, clearNet_clears_the_petri_net_from_all_places_and_tran
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, createPlace_creates_a_place_in_the_petri_net)
TEST_F(PTN_Engine_JobQueue, createPlace_creates_a_place_in_the_petri_net)
{
EXPECT_TRUE(ptnEngine.getPlacesProperties().empty());
PlaceProperties placeProperties{
Expand Down Expand Up @@ -154,7 +154,7 @@ TEST_F(JobQueuePTNEngine, createPlace_creates_a_place_in_the_petri_net)
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, createTransition_creates_a_transition_in_the_petri_net)
TEST_F(PTN_Engine_JobQueue, createTransition_creates_a_transition_in_the_petri_net)
{
EXPECT_TRUE(ptnEngine.getTransitionsProperties().empty());
TransitionProperties transitionProperties{
Expand Down Expand Up @@ -184,7 +184,7 @@ TEST_F(JobQueuePTNEngine, createTransition_creates_a_transition_in_the_petri_net
// TO DO test invoking while in execution
}

TEST(PTN_Engine, execute_starts_the_execution_of_the_petri_net)
TEST(PTN_Engine_, execute_starts_the_execution_of_the_petri_net)
{
PTN_Engine ptnEngine(PTN_Engine::ACTIONS_THREAD_OPTION::SINGLE_THREAD);
TransitionProperties transitionProperties{
Expand Down Expand Up @@ -228,7 +228,7 @@ TEST(PTN_Engine, execute_starts_the_execution_of_the_petri_net)
}

TEST_F(
EventLoopPTNEngine,
PTN_Engine_EventLoop,
event_loop_sleep_time_duration_determines_the_time_the_event_loop_waits_until_it_checks_if_a_transition_can_be_fired)
{
EXPECT_EQ(100ms, ptnEngine.getEventLoopSleepDuration());
Expand Down Expand Up @@ -300,19 +300,19 @@ event_loop_sleep_time_duration_determines_the_time_the_event_loop_waits_until_it
EXPECT_GT(duration_ms, 950.0);
}

TEST_F(JobQueuePTNEngine, getNumberOfTokens_throws_if_an_invalid_place_name_is_given)
TEST_F(PTN_Engine_JobQueue, getNumberOfTokens_throws_if_an_invalid_place_name_is_given)
{
ASSERT_THROW(ptnEngine.getNumberOfTokens(""), PTN_Exception);
}

TEST_F(JobQueuePTNEngine, incrementInputPlace_thows_if_an_invalid_place_name_is_given)
TEST_F(PTN_Engine_JobQueue, incrementInputPlace_thows_if_an_invalid_place_name_is_given)
{
ASSERT_THROW(ptnEngine.incrementInputPlace(""), PTN_Exception);

// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, isEventLoopRunning_returns_if_the_event_loop_is_running_for_JOB_QUEUE_mode)
TEST_F(PTN_Engine_JobQueue, isEventLoopRunning_returns_if_the_event_loop_is_running_for_JOB_QUEUE_mode)
{
EXPECT_FALSE(ptnEngine.isEventLoopRunning());
ptnEngine.execute();
Expand All @@ -322,7 +322,7 @@ TEST_F(JobQueuePTNEngine, isEventLoopRunning_returns_if_the_event_loop_is_runnin
// TO DO test invoking while in execution
}

TEST_F(EventLoopPTNEngine, isEventLoopRunning_returns_if_the_event_loop_is_running_for_EVENT_LOOP_mode)
TEST_F(PTN_Engine_EventLoop, isEventLoopRunning_returns_if_the_event_loop_is_running_for_EVENT_LOOP_mode)
{
EXPECT_FALSE(ptnEngine.isEventLoopRunning());
ptnEngine.execute();
Expand All @@ -332,7 +332,7 @@ TEST_F(EventLoopPTNEngine, isEventLoopRunning_returns_if_the_event_loop_is_runni
// TO DO test invoking while in execution
}

TEST(PTN_Engine, isEventLoopRunning_returns_if_the_event_loop_is_running_for_DETACHED_mode)
TEST(PTN_Engine_, isEventLoopRunning_returns_if_the_event_loop_is_running_for_DETACHED_mode)
{
PTN_Engine ptnEngine(PTN_Engine::ACTIONS_THREAD_OPTION::DETACHED);
EXPECT_FALSE(ptnEngine.isEventLoopRunning());
Expand All @@ -343,7 +343,7 @@ TEST(PTN_Engine, isEventLoopRunning_returns_if_the_event_loop_is_running_for_DET
// TO DO test invoking while in execution
}

TEST(PTN_Engine, isEventLoopRunning_returns_false_for_SINGLE_THREAD_mode)
TEST(PTN_Engine_, isEventLoopRunning_returns_false_for_SINGLE_THREAD_mode)
{
PTN_Engine ptnEngine(PTN_Engine::ACTIONS_THREAD_OPTION::SINGLE_THREAD);
EXPECT_FALSE(ptnEngine.isEventLoopRunning());
Expand All @@ -354,7 +354,7 @@ TEST(PTN_Engine, isEventLoopRunning_returns_false_for_SINGLE_THREAD_mode)
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, registerAction_registers_an_action_in_the_petri_net)
TEST_F(PTN_Engine_JobQueue, registerAction_registers_an_action_in_the_petri_net)
{
ActionFunction actionFunction = []() {};
ASSERT_THROW(ptnEngine.registerAction("", actionFunction), InvalidFunctionNameException);
Expand All @@ -370,7 +370,7 @@ TEST_F(JobQueuePTNEngine, registerAction_registers_an_action_in_the_petri_net)
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, registerCondition_registers_a_condition_function_in_the_petri_net)
TEST_F(PTN_Engine_JobQueue, registerCondition_registers_a_condition_function_in_the_petri_net)
{
ConditionFunction conditionFunction = []() { return false; };
ASSERT_THROW(ptnEngine.registerCondition("", conditionFunction), InvalidFunctionNameException);
Expand All @@ -388,7 +388,7 @@ TEST_F(JobQueuePTNEngine, registerCondition_registers_a_condition_function_in_th
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, removeArc_removes_an_arc_between_a_place_and_a_transition)
TEST_F(PTN_Engine_JobQueue, removeArc_removes_an_arc_between_a_place_and_a_transition)
{
ArcProperties arcProperties;
ASSERT_THROW(ptnEngine.removeArc(arcProperties), PTN_Exception);
Expand All @@ -404,7 +404,7 @@ TEST_F(JobQueuePTNEngine, removeArc_removes_an_arc_between_a_place_and_a_transit
// TO DO test invoking while in execution
}

TEST_F(JobQueuePTNEngine, setActionsThreadOption_sets_the_runtime_mode_in_the_petri_net)
TEST_F(PTN_Engine_JobQueue, setActionsThreadOption_sets_the_runtime_mode_in_the_petri_net)
{
ASSERT_NO_THROW(ptnEngine.setActionsThreadOption(PTN_Engine::ACTIONS_THREAD_OPTION::SINGLE_THREAD));
ASSERT_NO_THROW(ptnEngine.setActionsThreadOption(PTN_Engine::ACTIONS_THREAD_OPTION::DETACHED));
Expand All @@ -417,7 +417,7 @@ TEST_F(JobQueuePTNEngine, setActionsThreadOption_sets_the_runtime_mode_in_the_pe
ASSERT_THROW(ptnEngine.setActionsThreadOption(PTN_Engine::ACTIONS_THREAD_OPTION::JOB_QUEUE), PTN_Exception);
}

TEST_F(JobQueuePTNEngine, stops_the_petri_net_execution_and_never_throws)
TEST_F(PTN_Engine_JobQueue, stops_the_petri_net_execution_and_never_throws)
{
ASSERT_NO_THROW(ptnEngine.stop());
ASSERT_NO_THROW(ptnEngine.stop());
Expand Down
Loading

0 comments on commit cbe1ef9

Please sign in to comment.