Skip to content

Commit

Permalink
Version: prepare 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
HajuSchulz committed Feb 12, 2024
1 parent 0f11797 commit 0882443
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
14 changes: 7 additions & 7 deletions src/c_fsm/c_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
*******************************************************************************/

/**
* @brief CFSM Header file
*
* This file contains the implementation for the cfsm
* @brief CFSM Header file
*
* This file contains the implementation for the cfsm
* pattern for finite state machines in C-language.
*
*
* Repository: https://github.com/nhjschulz/cfsm
*
*
*/

/******************************************************************************
Expand Down Expand Up @@ -74,11 +74,11 @@ void cfsm_transition(struct cfsm_Fsm * fsm, cfsm_TransitionFunction enterFunc)
fsm->onLeave(fsm);
}

/* Set enter function pointer and clear all other handler. They
/* Set enter function pointer and clear all other handler. They
* get set by the enter function if needed.
*/
*fsm = (cfsm_Fsm) { enterFunc, 0, 0, 0 };

/* Call enter function NULL checked. It might be NULL to "disable"
* all FSM operations.
*/
Expand Down
54 changes: 31 additions & 23 deletions src/c_fsm/c_fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
DESCRIPTION
*******************************************************************************/
/**
* @brief CFSM Header file
*
* This file defines the types and functions used to implement the cfsm
* @brief CFSM Header file
*
* This file defines the types and functions used to implement the cfsm
* pattern for finite state machines in C-language.
*
*
* Repository: https://github.com/nhjschulz/cfsm
*
*
* @addtogroup CFSM
*
* @{
*/

#ifndef C_FSM_H_INCLUDED
#define C_FSM_H_INCLUDED
#ifndef SRC_C_FSM_C_FSM_H_
#define SRC_C_FSM_C_FSM_H_

#ifdef __cplusplus
extern "C" {
Expand All @@ -52,6 +52,10 @@ extern "C" {
* Macros
*****************************************************************************/

#define CFSM_VER_MAJOR 0 /**< semantic versioning major X.x.x */
#define CFSM_VER_MINOR 1 /**< semantic versioning minor x.X.x */
#define CFSM_VER_PATCH 0 /**< semantic versioning patch x.x.X */

/******************************************************************************
* Types and Classes
*****************************************************************************/
Expand All @@ -77,60 +81,64 @@ typedef struct cfsm_Fsm {

/**
* @brief Initialize the given fsm.
*
*
* @param fsm The fsm data structure
* @since 0.1.0
*/
void cfsm_init(struct cfsm_Fsm * fsm);

/**
* @brief Transition given fsm to a new state
*
* Perform a state transition be calling the function given by enterFunc.
*
* Perform a state transition be calling the function given by enterFunc.
* The called function is expected to update the needed state handlers in
* the state structure. Unused handlers needs not to be set.
* Passing NULL as enterfunc triggers the leave handler for the current
* state and clears all handlers.
*
*
* @param fsm The fsm data structure
* @param enterFunc The enter function for the new fsm state (may be NULL)
* @since 0.1.0
*/
void cfsm_transition(struct cfsm_Fsm * fsm, cfsm_TransitionFunction enterFunc);

/**
* @brief Execute a process cycle to the current fsm state
*
* Call the process handler of the current fsm state. This
*
* Call the process handler of the current fsm state. This
* function is expected to be called cyclicly. The function
* does nothing if the current state has no process handler
* set during enter.
*
*
* @param fsm The fsm data structure
* @since 0.1.0
*/
void cfsm_process(struct cfsm_Fsm * fsm);

/**
* @brief Signal an event to the current fsm state.
*
* Call the onEvent handler of the current fsm state. This
*
* Call the onEvent handler of the current fsm state. This
* function is expected to be called when an even shall be
* signaled to the current state. An event is just an
* application defined integer id. It has no meaning to
* signaled to the current state. An event is just an
* application defined integer id. It has no meaning to
* the fsm itself. Events provide a method to react to
* application events when they occure instead of polling
* for them during process cycles.
*
* for them during process cycles.
*
* An example for an event id could be a UI button press
* to trigger a state dependend reaction.
*
*
* @param fsm The fsm data structure
* @param eventId An application defined ID to identify the event.
* @since 0.1.0
*/
void cfsm_event(struct cfsm_Fsm * fsm, int eventId);

#ifdef __cplusplus
}
#endif

#endif /* C_FSM_H_INCLUDED */
#endif /* SRC_C_FSM_C_FSM_H_ */

/** @} */
/** @} */
29 changes: 17 additions & 12 deletions tests/test_c_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*******************************************************************************/
/**
* @brief CFSM test suite
*
*
* @addtogroup tests
*
* @{
Expand Down Expand Up @@ -85,20 +85,25 @@ static StateOperationCounter state_B;
* External functions
*****************************************************************************/

void setUp(void)
void setUp(void)
{
cfsm_init(&fsm);

memset(&state_A, 0, sizeof(state_A));
memset(&state_B, 0, sizeof(state_A));

}

void tearDown(void)
{
}

void test_cfsm_version()
{
TEST_ASSERT_EQUAL(0, CFSM_VER_MAJOR);
TEST_ASSERT_EQUAL(1, CFSM_VER_MINOR);
TEST_ASSERT_EQUAL(0, CFSM_VER_PATCH);
}

void test_cfsm_init_should_clear_handler()
{
memset(&fsm, -1, sizeof(fsm)); /* corrupt content */
Expand All @@ -117,7 +122,6 @@ void test_cfsm_init_is_safe_to_use()
/* should not crash */
cfsm_process(&fsm);
cfsm_event(&fsm, 0x12345678);

}

void test_cfsm_transition_should_set_enter_handler_only()
Expand All @@ -139,7 +143,7 @@ void test_cfs_process()
TEST_ASSERT_EQUAL_INT(state_A.eventCalls, 0);
TEST_ASSERT_EQUAL_INT(state_A.lastEventId, 0);

for (int i = 0; i < 10; ++i)
for (int i = 0; i < 10; ++i)
{
cfsm_process(&fsm);
TEST_ASSERT_EQUAL_INT(state_A.lastEventId, i);
Expand All @@ -159,7 +163,7 @@ void test_cfs_signalEvent()
TEST_ASSERT_EQUAL_INT(state_A.processCalls, 0);
TEST_ASSERT_EQUAL_INT(state_A.eventCalls, 0);

for (int i = 0; i < 10; ++i)
for (int i = 0; i < 10; ++i)
{
cfsm_event(&fsm, i);
}
Expand Down Expand Up @@ -211,7 +215,7 @@ void test_cfs_transition_A_B_A()
TEST_ASSERT_EQUAL_PTR(fsm.onEvent, State_A_onEvent);
TEST_ASSERT_EQUAL_PTR(fsm.onProcess, State_A_onProcess);
TEST_ASSERT_EQUAL_PTR(fsm.onLeave, State_A_onLeave);

TEST_ASSERT_EQUAL_INT(state_A.enterCalls, 2);
TEST_ASSERT_EQUAL_INT(state_A.leaveCalls, 1);
TEST_ASSERT_EQUAL_INT(state_A.processCalls, 0);
Expand All @@ -226,7 +230,7 @@ void test_cfs_transition_A_B_A()
int main(void)
{
UNITY_BEGIN();

RUN_TEST(test_cfsm_init_is_safe_to_use);
RUN_TEST(test_cfsm_init_should_clear_handler);
RUN_TEST(test_cfsm_transition_should_set_enter_handler_only);
Expand All @@ -248,7 +252,7 @@ static void State_A_onEnter(cfsm_Fsm * fsm)
fsm->onLeave = State_A_onLeave;
fsm->onProcess = State_A_onProcess;

state_A.enterCalls++;
state_A.enterCalls++;
}

static void State_A_onEvent(cfsm_Fsm * fsm, int eventId)
Expand All @@ -259,8 +263,8 @@ static void State_A_onEvent(cfsm_Fsm * fsm, int eventId)
static void State_A_onProcess(cfsm_Fsm * fsm)
{
state_A.processCalls++;

}

static void State_A_onLeave(cfsm_Fsm * fsm)
{
state_A.leaveCalls++;
Expand All @@ -278,11 +282,12 @@ static void State_B_onEvent(cfsm_Fsm * fsm, int eventId)
{
state_B.eventCalls++;
}

static void State_B_onProcess(cfsm_Fsm * fsm)
{
state_B.processCalls++;

}

static void State_B_onLeave(cfsm_Fsm * fsm)
{
state_B.leaveCalls++;
Expand Down

0 comments on commit 0882443

Please sign in to comment.