mockcpp icon indicating copy to clipboard operation
mockcpp copied to clipboard

mockcpp在c++20 无法生效

Open ClaudiusJXK opened this issue 1 year ago • 5 comments

工程是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)

ClaudiusJXK avatar Jun 07 '24 03:06 ClaudiusJXK

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

izxl007 avatar Jun 25 '24 05:06 izxl007

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

fish2bird avatar Aug 22 '24 02:08 fish2bird

您好,我使用的是mockcpp项目README中提供的工程:https://gitee.com/sinojelly/gtest-with-mockcpp ,但我是macOS环境,所以重新make install了mockcpp,拷贝替换到工程中,除此之外没有改动,我确认没有开启编译器优化,但mockcpp还是没有生效。以下是我的环境信息: -- OS: Darwin, ARCHITECTURE: arm64 -- C Compiler: AppleClang version: 15.0.0.15000100 -- C++ Compiler: AppleClang version: 15.0.0.15000100 -- C Compiler Flags: -O0 -- C++ Compiler Flags: -O0 -- C Standard: -- C++ Standard: -- Build Type: Debug 请问能否帮忙看下是什么原因? 补充:使用调试器确认函数没有内联展开,但还是没有执行到桩,请问mockcpp不支持macOS吗?

yi1fun avatar Oct 09 '24 02:10 yi1fun

您好,我使用的是mockcpp项目README中提供的工程:https://gitee.com/sinojelly/gtest-with-mockcpp ,但我是macOS环境,所以重新make install了mockcpp,拷贝替换到工程中,除此之外没有改动,我确认没有开启编译器优化,但mockcpp还是没有生效。以下是我的环境信息: -- OS: Darwin, ARCHITECTURE: arm64 -- C Compiler: AppleClang version: 15.0.0.15000100 -- C++ Compiler: AppleClang version: 15.0.0.15000100 -- C Compiler Flags: -O0 -- C++ Compiler Flags: -O0 -- C Standard: -- C++ Standard: -- Build Type: Debug 请问能否帮忙看下是什么原因? 补充:使用调试器确认函数没有内联展开,但还是没有执行到桩,请问mockcpp不支持macOS吗?

arm64 的补丁代码目前 mockcpp 中尚未支持,其实是比较简单的,需要 16 字节,两条指令加长跳地址

// ldr x9, +8 
// br x9 
// addr 

另外 arm 有个缓存残影问题,需要调用 __builtin___clear_cache

fish2bird avatar Oct 09 '24 07:10 fish2bird

好的,非常感谢,请问mockcpp有支持arm64的计划吗

yi1fun avatar Oct 10 '24 09:10 yi1fun