How to reset machine state during unit test C

I have built-in C for a TI processor that needs to be tested in a block.
IAR is used for target compilation, but I run tests on a Win7 machine using MinGW GCC.

C code has functions that contain state machines, which sometimes need to be reset between tests. These state machines often keep their state variable locally static, which complicates the task, if not impossible.

I am not very versed in the C ++ class, but I had the idea of ​​"importing" C functions into the C ++ class for packaging, because it allows you to simply create a new object whenever you need to reset. The code below does not work, but it illustrates my idea.

in main.cpp:

    #include "statemachine.h"

    using namespace std;

    class stateMachineWrapper {
    public:
        extern void stateMachine(void);
    };

    int main() {
        stateMachineWrapper myObject;

        myObject.stateMachine();

        myObject.stateMachine();

        stateMachineWrapper myNewObject;

        myNewObject.stateMachine();

        myNewObject.stateMachine();

        return 0;
    }

in statemachine.h:

    void stateMachine(void);

in statemachine.c:

    #include <stdio.h>

    void stateMachine(void)
    {
      static int myState = 0;

      switch(myState)
      {
      case 0:
      {
        printf("Init State");
        myState = 1;
        break;
      }
      case 1:
      {
        printf("Second state");
        break;
      }
      default:
      {
        printf("Default");
        break;
      }
      }
    }

statemachine.c/.h , "".
, , !

+4
2

@unwind !
: DLL http://www.transmissionzero.co.uk/computing/building-dlls-with-mingw/ , .

statemachine.h:

    void stateMachine(void);

statemachine.c:

    #include <stdio.h>

    void stateMachine(void)
    {
      static int myState = 0;

      switch(myState)
      {
      case 0:
      {
        printf("Init State");
        myState = 1;
        break;
      }
      case 1:
      {
        printf("Second state");
        break;
      }
      default:
      {
        printf("Default");
        break;
      }
      }
    }

statemachinelib.c:

    #include "statemachine.h"

    __declspec(dllexport) void __cdecl statemachineWrap()
    {
      stateMachine();
    }

main.c:

    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>

    typedef int (__stdcall *f_funci)();

    int main(int argc, char **argv)
    {
      HINSTANCE hGetProcIDDLL = LoadLibrary("statemachinelib.dll");

      f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "statemachineWrap");

      funci();

      funci();

      funci();

      FreeLibrary(hGetProcIDDLL);  //Windows detects that no one is using this library anymore and unloads it from memory, giving the new LoadLibrary a fresh instance

      hGetProcIDDLL = LoadLibrary("statemachinelib.dll");

      funci = (f_funci)GetProcAddress(hGetProcIDDLL, "statemachineWrap");

      funci();

      funci();

      funci();

      return 0;
    }

, DLL, , dllexport dllimport .. , , . , .

DLL MinGW:

>gcc -c statemachine.c statemachinelib.c
>gcc -o statemachinelib.dll -s -shared statemachinelib.o statemachine.o -Wl,--subsystem,windows

, MinGW:

>gcc -o main.exe main.c

:

>main.exe
Init State
Second state
Second state
Init State
Second state
Second state

, , !

: , () , DLL

+1

. ++ static , C.

C, static.

, , , , (= ).

+2

Source: https://habr.com/ru/post/1614426/


All Articles