1
您好我是GoogleMock的新手,但並不是新的嘲笑(我有Python經驗)。編譯錯誤:「創建負數大小的數組('-0x00000000000000001')」,「分配只讀位置」
對於基於C代碼的界面,我們希望使用Googlemock。編譯一切順利。沒有問題與限定基於C的代碼: 模擬頭文件
#include "gmock/gmock.h"
extern "C"
{
#include "interface/interfacetype.h"
}
struct HELPER
{
virtual ~HELPER() {}
virtual int interface_func(ID_ENUM_TYPE id, MY_STRUCT_TYPE *params) = 0;
};
struct INTERFACE_MOCK : public HELPER
{
MOCK_METHOD2(interface_func, int(ID_ENUM_TYPE id, MY_STRUCT_TYPE *params));
};
extern INTERFACE_MOCK *mock_create_mock();
extern void mock_delete_mock();
模擬實現:
#include mock.h
extern "C"
{
#include "interface/interfacetype.h"
}
INTERFACE_MOCK *interface_2_mock = NULL;
extern "C"
{
int interface_func(ID_ENUM_TYPE id, MY_STRUCT_TYPE *params)
{
return interface_2_mock->interface_func(id, params);
}
}
INTERFACE_MOCK *mock_create_mock()
{
if (interface_2_mock == NULL)
{
interface_2_mock = new(INTERFACE_MOCK);
}
return interface_2_mock;
}
void mock_delete_mock()
{
delete interface_2_mock;
}
MY_STRUCT_TYPE如下:
typedef struct my_struct_tag
{
float value[196]
} MY_STRUCT_TYPE
單元測試碼是如下:
INTERFACE_MOCK *interface_2_mock;
class fixture : public ::testing::Test
{
protected:
virtual void SetUp()
{
interface_2_mock = mock_create_mock();
}
virtual void TearDown()
{
mock_delete_mock();
}
};
TEST_F(fixture, test_case)
{
MY_STRUCT_TYPE params;
int result = 0;
for (int i=0; i<196; i++)
{
params.value[i] = 1.23;
}
// I'm only interested in checking that function is called,
// mocking the return-value and mocking 'params' which is an output-param
EXPECT_CALL(*interface_2_mock, interface_func(_, _))
.Times(2)
.WillRepeatedly(DoAll(SetArgReferee<1>(¶ms), return 0)));
// Call function_under_test which calls interface_func
result = function_under_test();
ASSERT_EQ(0, result) << "Return-value " << result << " not as expected"
}
編譯這一切都很順利,直到編譯EXPECT_CALL行。在那裏,我們有以下錯誤,我們不明白:
Rebuilding "<target>.oppsparc" on host "<host>"
======== Finished "<target>.oppsparc" on host "<host>" ========
Sun native compile : test_my_test.cpp to test_my_test.oppsparc
In file included from ./gmock/gmock.h:65,
from mock/interface_2_mock.hpp:33,
from test_my_test.cpp:23:
./gmock/gmock-more-actions.h: In member function 'typename testing::internal::Function<F>::Result testing::SetArgRefereeActionP<k, value_type>::gmock_Impl<F>::gmock_PerformImpl(const typename testing::internal::Function<F>::ArgumentTuple&, arg0_type, arg1_type, arg2_type, arg3_type, arg4_type, arg5_type, arg6_type, arg7_type, arg8_type, arg9_type) const [with arg0_type = ID_ENUM_TYPE , arg1_type = MY_STRUCT_TYPE*, arg2_type = testing::internal::ExcessiveArg, arg3_type = testing::internal::ExcessiveArg, arg4_type = testing::internal::ExcessiveArg, arg5_type = testing::internal::ExcessiveArg, arg6_type = testing::internal::ExcessiveArg, arg7_type = testing::internal::ExcessiveArg, arg8_type = testing::internal::ExcessiveArg, arg9_type = testing::internal::ExcessiveArg, F = void(ID_ENUM_TYPE , MY_STRUCT_TYPE*), int k = 1, value_type = MY_STRUCT_TYPE*]':
./gmock/gmock-generated-actions.h:664: instantiated from 'static Result testing::internal::ActionHelper<Result, Impl>::Perform(Impl*, const std::tr1::tuple<_U1, _U2>&) [with A0 = ID_ENUM_TYPE , A1 =MY_STRUCT_TYPE*, Result = void, Impl = testing::SetArgRefereeActionP<1, MY_STRUCT_TYPE*>::gmock_Impl<void(ID_ENUM_TYPE ,MY_STRUCT_TYPE*)>]'
./gmock/gmock-more-actions.h:170: instantiated from 'typename testing::internal::Function<F>::Result testing::SetArgRefereeActionP<k, value_type>::gmock_Impl<F>::Perform(const typename testing::internal::Function<F>::ArgumentTuple&) [with F = void(ID_ENUM_TYPE , MY_STRUCT_TYPE*), int k = 1, value_type = MY_STRUCT_TYPE*]'
test_my_test.cpp:251: instantiated from here
./gmock/gmock-more-actions.h:175: error: creating array with negative size ('-0x00000000000000001')
./gmock/gmock-more-actions.h:177: error: assignment of read-only location 'std::tr1::get [with int __i = 1, _Elements = ID_ENUM_TYPE, MY_STRUCT_TYPE*](((const std::tr1::tuple<ID_ENUM_TYPE, MY_STRUCT_TYPE*>&)((const std::tr1::tuple<ID_ENUM_TYPE, MY_STRUCT_TYPE*>*)args)))'
*** Error code 1
========================================================
Aborting...
你能幫助我們嗎?
/*編輯* /我看到我遺漏了夾具
最有可能的「大小爲負創造陣」源於「窮人的靜態斷言」,即在某處程序員編寫了一個編譯時斷言的程序,以確保某些值是正確的,當它失敗時,會得到上述錯誤。 – Lundin 2014-10-29 14:49:26
@Lundin:感謝您的回覆,但事實證明,傳遞引用和傳遞指針引用不應該混淆。正如我在下面解釋的,我應該使用SetArgPointee i.s.o. SetArgReferee。 – Nemelis 2014-10-30 08:35:43