本文介绍了如何重置状态机时,单元测试C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TI处理器,需要进行单元测试嵌入式C的本身。结果
对于目标编译IAR使用,但我使用的MinGW GCC运行Win7的机器上测试。

I have a se of embedded C for a TI processor that need to be unit tested.
For target compilation IAR is used but i am running the tests on a Win7 machine using MinGW GCC.

在C code有含有状态机的功能,有时需要测试之间被复位。这些状态机经常保持自己的状态变量局部静态的,使这项工作很难,如果不是不可能的。

In the C code there are functions containing state machines that sometimes need to be reset between tests. These state machines often keep their state variable locally static, making that task difficult if not impossible.

我不是很C ++类精明,但我对进口的C函数到一个包裹C ++类memberfunctions从而有可能只需要创建一个新的对象,无论复位我需要一个想法。在code以下的非功能性,但它说明了我的想法。

I'm not very C++ class savvy but i had an idea about "importing" the C functions into a wrapping C++ class as memberfunctions making it possible to just create a new object whenever a reset i needed. The code below is non functional but it illustrates my idea.

在main.cpp中:

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;
    }

在statemachine.h:

in statemachine.h:

    void stateMachine(void);

在statemachine.c:

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不鼓励,因为它可以被认为是遗留。结果,
任何其他的解决办法当然是欢迎的!

Alterations to the statemachine.c/.h is not encouraged since it can be considered "legacy".
Any other solutions is of course welcome as well!

推荐答案

@unwind送我望着动态code加载!结果
阅读这些:
功能和的给了我足够炮制以下解决方案。

@unwind sent me looking at dynamic code loading!
Reading these:Dynamically load a function from a DLL and http://www.transmissionzero.co.uk/computing/building-dlls-with-mingw/ gave me enough to concoct the following solution.

在statemachine.h:

in statemachine.h:

    void stateMachine(void);

在statemachine.c:

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;
      }
      }
    }

在statemachinelib.c:

in 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;
    }

在此codeI省略了很多安全语句,例如,检查如果DLL可以加载函数是否发现,我们是否想太多DLLEXPORT或dllimport的等等只是为了使其更易于把握什么正在进行。如果你要实现这个任何真正的项目,你至少应该阅读这两个我上面提到的资源。

In this code i have omitted a lot of safety statements such as checking if the DLL could be loaded, if the function is found, whether we want too dllexport or dllimport and so on only to make it easier to grasp what is going on. If you are going to implement this in any real project you should at least read both of the resources i mentioned above.

使用MinGW的DLL的编译:

compilation of the DLL using MinGW:

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

可执行文件的编制,也MinGW的:

compilation of the executable, also MinGW:

>gcc -o main.exe main.c

执行率:

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

我就离开这个在这里几天,如果没有人反对,我将记住这是我接受的答案!

I'll just leave this here for a few days and if no one objects i will mark this as my accepted answer!

编辑:我已经阐述了一下,这里有一个从我一个(解决)问题只是一个轻微的好办法Exporting一个功能,通过一个DLL 铸造成一个指针

I have elaborated a bit and there's another (solved) question from me here with just a slight tweak Exporting a function, cast into a pointer through a DLL

这篇关于如何重置状态机时,单元测试C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 14:00