Run-time mocking in C?

后端 未结 4 1978
旧巷少年郎
旧巷少年郎 2021-02-08 14:20

This has been pending for a long time in my list now. In brief - I need to run mocked_dummy() in the place of dummy() ON RUN-TIME, wit

4条回答
  •  悲哀的现实
    2021-02-08 14:51

    test-dept is a relatively recent C unit testing framework that allows you to do runtime stubbing of functions. I found it very easy to use - here's an example from their docs:

    void test_stringify_cannot_malloc_returns_sane_result() {
      replace_function(&malloc, &always_failing_malloc);
      char *h = stringify('h');
      assert_string_equals("cannot_stringify", h);
    }
    

    Although the downloads section is a little out of date, it seems fairly actively developed - the author fixed an issue I had very promptly. You can get the latest version (which I've been using without issues) with:

    svn checkout http://test-dept.googlecode.com/svn/trunk/ test-dept-read-only
    

    the version there was last updated in Oct 2011.

    However, since the stubbing is achieved using assembler, it may need some effort to get it to support ARM.

提交回复
热议问题