Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix colliding mocking context ids (rebased) #336

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions include/fakeit/Mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,63 +71,63 @@ namespace fakeit {
}

// const
template<int id, typename R, typename T, typename ... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename ... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R (T::*vMethod)(arglist...) const) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R> (T::*)(arglist...)>(vMethod);
return impl.template stubMethod<id>(methodWithoutConstVolatile);
}

// volatile
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...) volatile) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);
return impl.template stubMethod<id>(methodWithoutConstVolatile);
}

// const volatile
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...) const volatile) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);
return impl.template stubMethod<id>(methodWithoutConstVolatile);
}

// no qualifier
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...)) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);
return impl.template stubMethod<id>(methodWithoutConstVolatile);
}

// ref
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...) &) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);
return impl.template stubMethod<id>(methodWithoutConstVolatile);
}

// const ref
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...) const&) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);
return impl.template stubMethod<id>(methodWithoutConstVolatile);
}

// rval ref
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...) &&) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);
return impl.template stubMethod<id>(methodWithoutConstVolatile);
}

// const rval ref
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
std::is_base_of<T, C>::value>::type>
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...) const&&) {
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);
Expand Down
6 changes: 3 additions & 3 deletions include/fakeit/MockImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace fakeit {
return DataMemberStubbingRoot<T, DataType>();
}

template<int id, typename R, typename T, typename ... arglist, class = typename std::enable_if<std::is_base_of<T, C>::value>::type>
template<size_t id, typename R, typename T, typename ... arglist, class = typename std::enable_if<std::is_base_of<T, C>::value>::type>
MockingContext<R, arglist...> stubMethod(R(T::*vMethod)(arglist...)) {
return MockingContext<R, arglist...>(new UniqueMethodMockingContextImpl < id, R, arglist... >
(*this, vMethod));
Expand Down Expand Up @@ -226,7 +226,7 @@ namespace fakeit {
};


template<int id, typename R, typename ... arglist>
template<size_t id, typename R, typename ... arglist>
class UniqueMethodMockingContextImpl : public MethodMockingContextImpl<R, arglist...> {
protected:

Expand Down Expand Up @@ -323,7 +323,7 @@ namespace fakeit {
return origMethodPtr;
}

template<unsigned int id, typename R, typename ... arglist>
template<size_t id, typename R, typename ... arglist>
RecordedMethodBody<R, arglist...> &stubMethodIfNotStubbed(DynamicProxy<C, baseclasses...> &proxy,
R (C::*vMethod)(arglist...)) {
if (!proxy.isMethodStubbed(vMethod)) {
Expand Down
24 changes: 17 additions & 7 deletions include/fakeit/api_macros.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
#pragma once

#include "mockutils/constexpr_hash.hpp"

#ifdef _MSC_VER
#define __func__ __FUNCTION__
#endif

#define COUNTER_STRINGIFY( counter ) #counter

#define STUB_ID_STR( counter ) \
__FILE__ COUNTER_STRINGIFY(counter)

#define STUB_ID(counter) \
fakeit::constExprHash(STUB_ID_STR(counter))

#define MOCK_TYPE(mock) \
std::remove_reference<decltype((mock).get())>::type

Expand All @@ -29,25 +39,25 @@
(mock).dtor().setMethodDetails(#mock,"destructor")

#define Method(mock, method) \
(mock).template stub<__COUNTER__>(&MOCK_TYPE(mock)::method).setMethodDetails(#mock,#method)
(mock).template stub<STUB_ID(__COUNTER__)>(&MOCK_TYPE(mock)::method).setMethodDetails(#mock,#method)

#define OverloadedMethod(mock, method, prototype) \
(mock).template stub<__COUNTER__>(OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
(mock).template stub<STUB_ID(__COUNTER__)>(OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)

#define ConstOverloadedMethod(mock, method, prototype) \
(mock).template stub<__COUNTER__>(CONST_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
(mock).template stub<STUB_ID(__COUNTER__)>(CONST_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)

#define RefOverloadedMethod(mock, method, prototype) \
(mock).template stub<__COUNTER__>(REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
(mock).template stub<STUB_ID(__COUNTER__)>(REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)

#define ConstRefOverloadedMethod(mock, method, prototype) \
(mock).template stub<__COUNTER__>(CONST_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
(mock).template stub<STUB_ID(__COUNTER__)>(CONST_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)

#define RValRefOverloadedMethod(mock, method, prototype) \
(mock).template stub<__COUNTER__>(R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
(mock).template stub<STUB_ID(__COUNTER__)>(R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)

#define ConstRValRefOverloadedMethod(mock, method, prototype) \
(mock).template stub<__COUNTER__>(CONST_R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
(mock).template stub<STUB_ID(__COUNTER__)>(CONST_R_VAL_REF_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)

#define Verify(...) \
Verify( __VA_ARGS__ ).setFileInfo(__FILE__, __LINE__, __func__)
Expand Down
25 changes: 12 additions & 13 deletions include/mockutils/DynamicProxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
#include <vector>
#include <array>
#include <new>
#include <limits>

#include "mockutils/VirtualTable.hpp"
#include "mockutils/union_cast.hpp"
#include "mockutils/MethodInvocationHandler.hpp"
#include "mockutils/VTUtils.hpp"
#include "mockutils/FakeObject.hpp"
Expand All @@ -28,25 +26,26 @@ namespace fakeit {

class InvocationHandlers : public InvocationHandlerCollection {
std::vector<std::shared_ptr<Destructible>> &_methodMocks;
std::vector<unsigned int> &_offsets;
std::vector<size_t> &_offsets;

unsigned int getOffset(unsigned int id) const
unsigned int getOffset(size_t id) const
{
unsigned int offset = 0;
for (; offset < _offsets.size(); offset++) {
if (_offsets[offset] == id) {
// Only check _offsets for methods that were mocked.
if (_methodMocks[offset] != nullptr && _offsets[offset] == id) {
break;
}
}
return offset;
}

public:
InvocationHandlers(std::vector<std::shared_ptr<Destructible>> &methodMocks, std::vector<unsigned int> &offsets)
InvocationHandlers(std::vector<std::shared_ptr<Destructible>> &methodMocks, std::vector<size_t> &offsets)
: _methodMocks(methodMocks), _offsets(offsets) {
}

Destructible *getInvocatoinHandlerPtrById(unsigned int id) override {
Destructible *getInvocatoinHandlerPtrById(size_t id) override {
unsigned int offset = getOffset(id);
std::shared_ptr<Destructible> ptr = _methodMocks[offset];
return ptr.get();
Expand All @@ -61,8 +60,8 @@ namespace fakeit {

DynamicProxy(C &inst) :
_instancePtr(&inst),
_methodMocks(VTUtils::getVTSize<C>()),
_offsets(VTUtils::getVTSize<C>(), std::numeric_limits<int>::max()),
_methodMocks(VTUtils::getVTSize<C>(), nullptr),
_offsets(VTUtils::getVTSize<C>(), 0),
_invocationHandlers(_methodMocks, _offsets) {
_originalVt.copyFrom(VirtualTable<C, baseclasses...>::getVTable(*_instancePtr));
_originalVt.setCookie(InvocationHandlerCollection::VtCookieIndex, &_invocationHandlers);
Expand Down Expand Up @@ -97,18 +96,18 @@ namespace fakeit {

void Reset() {
_methodMocks = {};
_methodMocks.resize(VTUtils::getVTSize<C>());
_methodMocks.resize(VTUtils::getVTSize<C>(), nullptr);
_members = {};
_offsets = {};
_offsets.resize(VTUtils::getVTSize<C>());
_offsets.resize(VTUtils::getVTSize<C>(), 0);
VirtualTable<C, baseclasses...>::getVTable(*_instancePtr).copyFrom(_originalVt);
}

void Clear()
{
}

template<int id, typename R, typename ... arglist>
template<size_t id, typename R, typename ... arglist>
void stubMethod(R(C::*vMethod)(arglist...), MethodInvocationHandler<R, arglist...> *methodInvocationHandler) {
auto offset = VTUtils::getOffset(vMethod);
MethodProxyCreator<R, arglist...> creator;
Expand Down Expand Up @@ -217,7 +216,7 @@ namespace fakeit {
//
std::vector<std::shared_ptr<Destructible>> _methodMocks;
std::vector<std::shared_ptr<Destructible>> _members;
std::vector<unsigned int> _offsets;
std::vector<size_t> _offsets;
InvocationHandlers _invocationHandlers;

FakeObject<C, baseclasses...> &getFake() {
Expand Down
6 changes: 3 additions & 3 deletions include/mockutils/MethodProxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace fakeit {

struct MethodProxy {

MethodProxy(unsigned int id, unsigned int offset, void *vMethod) :
MethodProxy(size_t id, unsigned int offset, void *vMethod) :
_id(id),
_offset(offset),
_vMethod(vMethod) {
Expand All @@ -16,7 +16,7 @@ namespace fakeit {
return _offset;
}

unsigned int getId() const {
size_t getId() const {
return _id;
}

Expand All @@ -25,7 +25,7 @@ namespace fakeit {
}

private:
unsigned int _id;
size_t _id;
unsigned int _offset;
void *_vMethod;
};
Expand Down
14 changes: 7 additions & 7 deletions include/mockutils/MethodProxyCreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace fakeit {
struct InvocationHandlerCollection {
static const unsigned int VtCookieIndex = 0;

virtual Destructible *getInvocatoinHandlerPtrById(unsigned int index) = 0;
virtual Destructible *getInvocatoinHandlerPtrById(size_t index) = 0;

static InvocationHandlerCollection *getInvocationHandlerCollection(void *instance) {
VirtualTableBase &vt = VirtualTableBase::getVTable(instance);
Expand All @@ -29,19 +29,19 @@ namespace fakeit {

public:

template<unsigned int id>
template<size_t id>
MethodProxy createMethodProxy(unsigned int offset) {
return MethodProxy(id, offset, union_cast<void *>(&MethodProxyCreator::methodProxyX < id > ));
}

template<unsigned int id>
template<size_t id>
MethodProxy createMethodProxyStatic(unsigned int offset) {
return MethodProxy(id, offset, union_cast<void *>(&MethodProxyCreator::methodProxyXStatic < id > ));
}

protected:

R methodProxy(unsigned int id, const typename fakeit::production_arg<arglist>::type... args) {
R methodProxy(size_t id, const typename fakeit::production_arg<arglist>::type... args) {
InvocationHandlerCollection *invocationHandlerCollection = InvocationHandlerCollection::getInvocationHandlerCollection(
this);
MethodInvocationHandler<R, arglist...> *invocationHandler =
Expand All @@ -50,12 +50,12 @@ namespace fakeit {
return invocationHandler->handleMethodInvocation(std::forward<const typename fakeit::production_arg<arglist>::type>(args)...);
}

template<int id>
template<size_t id>
R methodProxyX(arglist ... args) {
return methodProxy(id, std::forward<const typename fakeit::production_arg<arglist>::type>(args)...);
}

static R methodProxyStatic(void* instance, unsigned int id, const typename fakeit::production_arg<arglist>::type... args) {
static R methodProxyStatic(void* instance, size_t id, const typename fakeit::production_arg<arglist>::type... args) {
InvocationHandlerCollection *invocationHandlerCollection = InvocationHandlerCollection::getInvocationHandlerCollection(
instance);
MethodInvocationHandler<R, arglist...> *invocationHandler =
Expand All @@ -64,7 +64,7 @@ namespace fakeit {
return invocationHandler->handleMethodInvocation(std::forward<const typename fakeit::production_arg<arglist>::type>(args)...);
}

template<int id>
template<size_t id>
static R methodProxyXStatic(void* instance, arglist ... args) {
return methodProxyStatic(instance, id, std::forward<const typename fakeit::production_arg<arglist>::type>(args)...);
}
Expand Down
17 changes: 17 additions & 0 deletions include/mockutils/constexpr_hash.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace fakeit {

constexpr size_t _FNV_prime = sizeof(size_t) == 4 ? 16777619ULL : 1099511628211ULL;
constexpr size_t _FNV_offset_basis = sizeof(size_t) == 4 ? 2166136261ULL : 14695981039346656037ULL;

constexpr size_t _constExprHashImpl(const char* str, size_t count) {
return count ? (_constExprHashImpl(str, count - 1) ^ str[count - 1]) * _FNV_prime : _FNV_offset_basis;
}

template<size_t N>
constexpr size_t constExprHash(const char(&str)[N]) {
return _constExprHashImpl(str, N);
}

}
2 changes: 1 addition & 1 deletion include/mockutils/gcc/VirtualTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace fakeit {

private:
static void **buildVTArray() {
int size = VTUtils::getVTSize<C>();
unsigned int size = VTUtils::getVTSize<C>();
auto array = new void *[size + 2 + numOfCookies]{};
array += numOfCookies; // skip cookies
array++; // skip top_offset
Expand Down
4 changes: 2 additions & 2 deletions include/mockutils/mscpp/VirtualTable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ namespace fakeit {
}

void initAll(void *value) {
auto size = getSize();
unsigned int size = getSize();
for (unsigned int i = 0; i < size; i++) {
setMethod(i, value);
}
Expand All @@ -256,7 +256,7 @@ namespace fakeit {
static const unsigned int dtorCookieIndex = numOfCookies - 1; // use the last cookie

static void **buildVTArray() {
int vtSize = VTUtils::getVTSize<C>();
unsigned int vtSize = VTUtils::getVTSize<C>();
auto array = new void *[vtSize + numOfCookies + 1]{};
RTTICompleteObjectLocator<C, baseclasses...> *objectLocator = new RTTICompleteObjectLocator<C, baseclasses...>(
typeid(C));
Expand Down
5 changes: 4 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ add_executable(FakeIt_tests
functional.cpp
gcc_stubbing_multiple_values_tests.cpp
gcc_type_info_tests.cpp
inherited_funcs_tests.cpp
miscellaneous_tests.cpp
move_only_return_tests.cpp
moving_mocks_around.cpp
moving_mocks_around_tests.cpp
msc_stubbing_multiple_values_tests.cpp
msc_type_info_tests.cpp
multiple_translation_units_stub.cpp
multiple_translation_units_stub_test.cpp
overloadded_methods_tests.cpp
referece_types_tests.cpp
remove_const_volatile_tests.cpp
Expand Down
Loading
Loading