aboutsummaryrefslogtreecommitdiffstats
path: root/test/mock/memory_mock.hpp
blob: a1f14b6e107fa99c0af005208d3dcc829c1c964f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * SPDX-License-Identifier: Apache-2.0
 */
#include <gmock/gmock.h>
#include <functional>

#include <stdlib.h>

/*
void *malloc(size_t size);
void free(void *ptr);
*/
static std::function<void*(size_t size)> _malloc;
static std::function<void(void *ptr)> _free;

class MemoryMocker {
public:
	MemoryMocker() {
		_malloc = [this](size_t size) {
			return malloc(size);
		};
		_free = [this](void *ptr) {
			return free(ptr);
		};
	}

	~MemoryMocker() {
		_malloc = {};
		_free = {};
	}

	MOCK_CONST_METHOD1(malloc, void*(size_t));
	MOCK_CONST_METHOD1(free, void(void *));
};

class MemoryMockBase {
protected:
	MemoryMocker memorym;
};

#ifdef __cplusplus
extern "C" {
#endif
static void* unittest_malloc(size_t size)
{
	return _malloc(size);
}
/*
static void free(void *ptr)
{
	_free(ptr);
}
*/
#ifdef __cplusplus
}
#endif