Skip to content

Commit e91d6b8

Browse files
committed
Merge remote-tracking branch 'otrempe-fakeit/Fix-CollidingMockingContextIds' into otrempe-Fix-CollidingMockingContextIds
2 parents c1d74ea + 2230a37 commit e91d6b8

13 files changed

+109
-31
lines changed

include/fakeit/Mock.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,63 +71,63 @@ namespace fakeit {
7171
}
7272

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

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

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

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

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

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

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

129129
// const rval ref
130-
template<int id, typename R, typename T, typename... arglist, class = typename std::enable_if<
130+
template<size_t id, typename R, typename T, typename... arglist, class = typename std::enable_if<
131131
std::is_base_of<T, C>::value>::type>
132132
MockingContext<internal::WithCommonVoid_t<R>, arglist...> stub(R(T::*vMethod)(arglist...) const&&) {
133133
auto methodWithoutConstVolatile = reinterpret_cast<internal::WithCommonVoid_t<R>(T::*)(arglist...)>(vMethod);

include/fakeit/MockImpl.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace fakeit {
101101
return DataMemberStubbingRoot<T, DataType>();
102102
}
103103

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

228228

229-
template<int id, typename R, typename ... arglist>
229+
template<size_t id, typename R, typename ... arglist>
230230
class UniqueMethodMockingContextImpl : public MethodMockingContextImpl<R, arglist...> {
231231
protected:
232232

@@ -323,7 +323,7 @@ namespace fakeit {
323323
return origMethodPtr;
324324
}
325325

326-
template<unsigned int id, typename R, typename ... arglist>
326+
template<size_t id, typename R, typename ... arglist>
327327
RecordedMethodBody<R, arglist...> &stubMethodIfNotStubbed(DynamicProxy<C, baseclasses...> &proxy,
328328
R (C::*vMethod)(arglist...)) {
329329
if (!proxy.isMethodStubbed(vMethod)) {

include/fakeit/api_macros.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
#pragma once
22

3+
#include "mockutils/constexpr_hash.hpp"
4+
35
#ifdef _MSC_VER
46
#define __func__ __FUNCTION__
57
#endif
68

9+
#define COUNTER_STRINGIFY( counter ) #counter
10+
11+
#define STUB_ID_STR( counter ) \
12+
__FILE__ COUNTER_STRINGIFY(counter)
13+
14+
#define STUB_ID(counter) \
15+
fakeit::constExprHash(STUB_ID_STR(counter))
16+
717
#define MOCK_TYPE(mock) \
818
std::remove_reference<decltype((mock).get())>::type
919

@@ -29,13 +39,13 @@
2939
(mock).dtor().setMethodDetails(#mock,"destructor")
3040

3141
#define Method(mock, method) \
32-
(mock).template stub<__COUNTER__>(&MOCK_TYPE(mock)::method).setMethodDetails(#mock,#method)
42+
(mock).template stub<STUB_ID(__COUNTER__)>(&MOCK_TYPE(mock)::method).setMethodDetails(#mock,#method)
3343

3444
#define OverloadedMethod(mock, method, prototype) \
35-
(mock).template stub<__COUNTER__>(OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
45+
(mock).template stub<STUB_ID(__COUNTER__)>(OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
3646

3747
#define ConstOverloadedMethod(mock, method, prototype) \
38-
(mock).template stub<__COUNTER__>(CONST_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
48+
(mock).template stub<STUB_ID(__COUNTER__)>(CONST_OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
3949

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

include/mockutils/DynamicProxy.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ namespace fakeit {
2828

2929
class InvocationHandlers : public InvocationHandlerCollection {
3030
std::vector<std::shared_ptr<Destructible>> &_methodMocks;
31-
std::vector<unsigned int> &_offsets;
31+
std::vector<size_t> &_offsets;
3232

33-
unsigned int getOffset(unsigned int id) const
33+
unsigned int getOffset(size_t id) const
3434
{
3535
unsigned int offset = 0;
3636
for (; offset < _offsets.size(); offset++) {
@@ -42,11 +42,11 @@ namespace fakeit {
4242
}
4343

4444
public:
45-
InvocationHandlers(std::vector<std::shared_ptr<Destructible>> &methodMocks, std::vector<unsigned int> &offsets)
45+
InvocationHandlers(std::vector<std::shared_ptr<Destructible>> &methodMocks, std::vector<size_t> &offsets)
4646
: _methodMocks(methodMocks), _offsets(offsets) {
4747
}
4848

49-
Destructible *getInvocatoinHandlerPtrById(unsigned int id) override {
49+
Destructible *getInvocatoinHandlerPtrById(size_t id) override {
5050
unsigned int offset = getOffset(id);
5151
std::shared_ptr<Destructible> ptr = _methodMocks[offset];
5252
return ptr.get();
@@ -108,7 +108,7 @@ namespace fakeit {
108108
{
109109
}
110110

111-
template<int id, typename R, typename ... arglist>
111+
template<size_t id, typename R, typename ... arglist>
112112
void stubMethod(R(C::*vMethod)(arglist...), MethodInvocationHandler<R, arglist...> *methodInvocationHandler) {
113113
auto offset = VTUtils::getOffset(vMethod);
114114
MethodProxyCreator<R, arglist...> creator;
@@ -217,7 +217,7 @@ namespace fakeit {
217217
//
218218
std::vector<std::shared_ptr<Destructible>> _methodMocks;
219219
std::vector<std::shared_ptr<Destructible>> _members;
220-
std::vector<unsigned int> _offsets;
220+
std::vector<size_t> _offsets;
221221
InvocationHandlers _invocationHandlers;
222222

223223
FakeObject<C, baseclasses...> &getFake() {

include/mockutils/MethodProxy.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace fakeit {
66

77
struct MethodProxy {
88

9-
MethodProxy(unsigned int id, unsigned int offset, void *vMethod) :
9+
MethodProxy(size_t id, unsigned int offset, void *vMethod) :
1010
_id(id),
1111
_offset(offset),
1212
_vMethod(vMethod) {
@@ -16,7 +16,7 @@ namespace fakeit {
1616
return _offset;
1717
}
1818

19-
unsigned int getId() const {
19+
size_t getId() const {
2020
return _id;
2121
}
2222

@@ -25,7 +25,7 @@ namespace fakeit {
2525
}
2626

2727
private:
28-
unsigned int _id;
28+
size_t _id;
2929
unsigned int _offset;
3030
void *_vMethod;
3131
};

include/mockutils/MethodProxyCreator.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace fakeit {
1111
struct InvocationHandlerCollection {
1212
static const unsigned int VtCookieIndex = 0;
1313

14-
virtual Destructible *getInvocatoinHandlerPtrById(unsigned int index) = 0;
14+
virtual Destructible *getInvocatoinHandlerPtrById(size_t index) = 0;
1515

1616
static InvocationHandlerCollection *getInvocationHandlerCollection(void *instance) {
1717
VirtualTableBase &vt = VirtualTableBase::getVTable(instance);
@@ -29,7 +29,7 @@ namespace fakeit {
2929

3030
public:
3131

32-
template<unsigned int id>
32+
template<size_t id>
3333
MethodProxy createMethodProxy(unsigned int offset) {
3434
return MethodProxy(id, offset, union_cast<void *>(&MethodProxyCreator::methodProxyX < id > ));
3535
}
@@ -41,7 +41,7 @@ namespace fakeit {
4141

4242
protected:
4343

44-
R methodProxy(unsigned int id, const typename fakeit::production_arg<arglist>::type... args) {
44+
R methodProxy(size_t id, const typename fakeit::production_arg<arglist>::type... args) {
4545
InvocationHandlerCollection *invocationHandlerCollection = InvocationHandlerCollection::getInvocationHandlerCollection(
4646
this);
4747
MethodInvocationHandler<R, arglist...> *invocationHandler =
@@ -50,7 +50,7 @@ namespace fakeit {
5050
return invocationHandler->handleMethodInvocation(std::forward<const typename fakeit::production_arg<arglist>::type>(args)...);
5151
}
5252

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

include/mockutils/VTUtils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace fakeit {
6565
}
6666

6767
template<typename C>
68-
static unsigned int getVTSize() {
68+
static size_t getVTSize() {
6969
struct Derrived : public C {
7070
virtual void endOfVt() {
7171
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
namespace fakeit {
4+
5+
constexpr size_t _FNV_prime = sizeof(size_t) == 4 ? 16777619ULL : 1099511628211ULL;
6+
constexpr size_t _FNV_offset_basis = sizeof(size_t) == 4 ? 2166136261ULL : 14695981039346656037ULL;
7+
8+
constexpr size_t _constExprHashImpl(const char* str, size_t count) {
9+
return count ? (_constExprHashImpl(str, count - 1) ^ str[count - 1]) * _FNV_prime : _FNV_offset_basis;
10+
}
11+
12+
template<size_t N>
13+
constexpr size_t constExprHash(const char(&str)[N]) {
14+
return _constExprHashImpl(str, N);
15+
}
16+
17+
}

include/mockutils/mscpp/VirtualTable.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ namespace fakeit {
178178
}
179179

180180
void copyFrom(VirtualTable<C, baseclasses...> &from) {
181-
unsigned int size = VTUtils::getVTSize<C>();
181+
auto size = VTUtils::getVTSize<C>();
182182
for (unsigned int i = 0; i < size; i++) {
183183
_firstMethod[i] = from.getMethod(i);
184184
}
@@ -234,7 +234,7 @@ namespace fakeit {
234234
setCookie(dtorCookieIndex, method);
235235
}
236236

237-
unsigned int getSize() {
237+
size_t getSize() {
238238
return VTUtils::getVTSize<C>();
239239
}
240240

@@ -256,7 +256,7 @@ namespace fakeit {
256256
static const unsigned int dtorCookieIndex = numOfCookies - 1; // use the last cookie
257257

258258
static void **buildVTArray() {
259-
int vtSize = VTUtils::getVTSize<C>();
259+
auto vtSize = VTUtils::getVTSize<C>();
260260
auto array = new void *[vtSize + numOfCookies + 1]{};
261261
RTTICompleteObjectLocator<C, baseclasses...> *objectLocator = new RTTICompleteObjectLocator<C, baseclasses...>(
262262
typeid(C));

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ add_executable(FakeIt_tests
1515
moving_mocks_around.cpp
1616
msc_stubbing_multiple_values_tests.cpp
1717
msc_type_info_tests.cpp
18+
multiple_translation_units_stub.cpp
19+
multiple_translation_units_stub_test.cpp
1820
overloadded_methods_tests.cpp
1921
referece_types_tests.cpp
2022
remove_const_volatile_tests.cpp

0 commit comments

Comments
 (0)