sinojelly / mockcpp

Two C/C++ testing tools, mockcpp and testngpp.
Apache License 2.0
66 stars 39 forks source link

mockcpp在c++20 无法生效 #58

Open ClaudiusJXK opened 3 months ago

ClaudiusJXK commented 3 months ago

工程是gtest+mockcpp linux版本: ubuntu 22.04 Linux openbmc 6.5.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

gcc版本: gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0 c++标准: c++20

代码:

#include "gtest/gtest.h"
#include "mockcpp/mockcpp.hpp"

int add(int value1, int value2) {
        return value1 + value2;
}

TEST(MOCK, mock_function_test)
{
    MOCKER(add)
        .stubs()
        .will(returnValue(100));
    int value = add(1, 2);

    ASSERT_EQ(100, value);
}

执行结果: [----------] 1 test from MOCK [ RUN ] MOCK.mock_function_test ../test/src/mock/MockTest.cpp:16: Failure Expected equality of these values: 100 value Which is: 3 [ FAILED ] MOCK.mock_function_test (0 ms) [----------] 1 test from MOCK (0 ms total)

izxl007 commented 3 months ago

请问解决了吗?我也碰到了MOCKER失效执行原函数的问题

fish2bird commented 1 month ago

您应该是开了优化导致, 与 C++20 无关,
用你的代码实测证明, 开启 -O1 以及以上就会出现内联展开,从而桩未执行到的问题