ReactOS 0.4.15-dev-7907-g95bf896
CComHeapPtr.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS api tests
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Test for CComHeapPtr
5 * COPYRIGHT: Copyright 2015-2020 Mark Jansen (mark.jansen@reactos.org)
6 */
7
8#ifdef HAVE_APITEST
9#include <apitest.h>
10#else
11#include "atltest.h"
12#endif
13#include <atlbase.h>
14
15static PDWORD
17{
19 *ptr = value;
20 return ptr;
21}
22
23
26
27struct CMallocSpy : public IMallocSpy
28{
30 {
31 if (IsEqualGUID(riid, IID_IMallocSpy))
32 {
33 *ppvObject = this;
34 }
35 return S_OK;
36 }
37
38 virtual ULONG STDMETHODCALLTYPE AddRef() { return 1; }
39 virtual ULONG STDMETHODCALLTYPE Release() { return 1; }
40 virtual SIZE_T STDMETHODCALLTYPE PreAlloc(SIZE_T cbRequest) { return cbRequest; }
42 {
44 return pActual;
45 }
47 virtual void STDMETHODCALLTYPE PostFree(BOOL fSpyed)
48 {
49 if (fSpyed)
51 }
52 virtual SIZE_T STDMETHODCALLTYPE PreRealloc(LPVOID pRequest, SIZE_T cbRequest, LPVOID *ppNewRequest, BOOL)
53 {
54 *ppNewRequest = pRequest;
55 return cbRequest;
56 }
58 {
59 if (fSpyed)
61 return pActual;
62 }
64 virtual SIZE_T STDMETHODCALLTYPE PostGetSize(SIZE_T cbActual, BOOL) { return cbActual; }
66 virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual) { return fActual; }
69};
70
72
73
74START_TEST(CComHeapPtr)
75{
77 ok(SUCCEEDED(hr), "Expected CoRegisterMallocSpy to succeed, but it failed: 0x%lx\n", hr);
78
79 {
80 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
81 CComHeapPtr<DWORD> heapPtr1;
82 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
83 CComHeapPtr<DWORD> heapPtr2(test_Alloc(0x11111111));
84 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
85
86 ok((PDWORD)heapPtr1 == NULL, "Expected heapPtr1 to be NULL, was: 0x%p\n", (PDWORD)heapPtr1);
87 ok((PDWORD)heapPtr2 != NULL, "Expected heapPtr2 to not be NULL\n");
88 ok(*heapPtr2 == 0x11111111, "Expected *heapPtr2 to be 0x11111111, but was: 0x%lx\n", *heapPtr2);
89
90 {
91 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
92 CComHeapPtr<DWORD> heapPtrSteal1(heapPtr1);
93 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
94 ok((PDWORD)heapPtr1 == NULL, "Expected heapPtr1 to be NULL, was: 0x%p\n", (PDWORD)heapPtr1);
95 ok((PDWORD)heapPtrSteal1 == NULL, "Expected heapPtrSteal1 to be NULL, was: 0x%p\n", (PDWORD)heapPtrSteal1);
96 CComHeapPtr<DWORD> heapPtrSteal2(heapPtr2);
97 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
98 ok((PDWORD)heapPtr2 == NULL, "Expected heapPtr2 to be NULL, was: 0x%p\n", (PDWORD)heapPtr2);
99 ok((PDWORD)heapPtrSteal2 != NULL, "Expected heapPtrSteal2 to not be NULL\n");
100 ok(*heapPtrSteal2 == 0x11111111, "Expected *heapPtrSteal2 to be 0x11111111, but was: 0x%lx\n", *heapPtrSteal2);
101 }
102 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
103
104 ok(heapPtr1.Allocate(1), "Expected Allocate to succeed\n");
105 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
106 ok(g_Reallocations == 0, "Expected there to be 0 reallocations, was: %ld\n", g_Reallocations);
107
108 *heapPtr1 = 0x22222222;
109 ok(*heapPtr1 == 0x22222222, "Expected *heapPtr1 to be 0x22222222, but was: 0x%lx\n", *heapPtr1);
110
111 ok(heapPtr1.Reallocate(2), "Expected Reallocate to succeed\n");
112 heapPtr1[1] = 0x33333333;
113 ok(*heapPtr1 == 0x22222222, "Expected *heapPtr1 to be 0x22222222, but was: 0x%lx\n", *heapPtr1);
114 ok(g_Reallocations == 1, "Expected there to be 1 reallocations, was: %ld\n", g_Reallocations);
115 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
116
117 heapPtr2 = heapPtr1;
118 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
119 ok(*heapPtr2 == 0x22222222, "Expected *heapPtr2 to be 0x33333333, but was: 0x%lx\n", *heapPtr2);
120 ok(heapPtr2[1] == 0x33333333, "Expected heapPtr2[1] to be 0x33333333, but was: 0x%lx\n", heapPtr2[1]);
121 ok((PDWORD)heapPtr1 == NULL, "Expected heapPtr1 to be NULL, was: 0x%p\n", (PDWORD)heapPtr1);
122 }
123 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
124
126 ok(SUCCEEDED(hr), "Expected CoRevokeMallocSpy to succeed, but it failed: 0x%lx\n", hr);
127}
static CMallocSpy g_Spy
Definition: CComHeapPtr.cpp:71
static LONG g_Reallocations
Definition: CComHeapPtr.cpp:25
static LONG g_OpenAllocations
Definition: CComHeapPtr.cpp:24
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define ok(value,...)
Definition: atltest.h:57
#define START_TEST(x)
Definition: atltest.h:75
#define STDMETHODIMP
Definition: basetyps.h:43
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
#define NULL
Definition: types.h:112
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxRequest * pRequest
HRESULT WINAPI CoRevokeMallocSpy(void)
Definition: ifs.c:522
HRESULT WINAPI CoRegisterMallocSpy(LPMALLOCSPY pMallocSpy)
Definition: ifs.c:482
LPVOID WINAPI CoTaskMemAlloc(SIZE_T size)
Definition: ifs.c:426
REFIID riid
Definition: atlbase.h:39
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
static PVOID ptr
Definition: dispmode.c:27
static void test_Alloc(void)
Definition: misc.c:148
DWORD * PDWORD
Definition: pedump.c:68
long LONG
Definition: pedump.c:60
#define IsEqualGUID(rguid1, rguid2)
Definition: guiddef.h:147
#define REFIID
Definition: guiddef.h:118
HRESULT hr
Definition: shlfolder.c:183
virtual LPVOID STDMETHODCALLTYPE PostAlloc(LPVOID pActual)
Definition: CComHeapPtr.cpp:41
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject)
Definition: CComHeapPtr.cpp:29
virtual LPVOID STDMETHODCALLTYPE PreGetSize(LPVOID pRequest, BOOL)
Definition: CComHeapPtr.cpp:63
virtual SIZE_T STDMETHODCALLTYPE PreAlloc(SIZE_T cbRequest)
Definition: CComHeapPtr.cpp:40
virtual void STDMETHODCALLTYPE PostHeapMinimize()
Definition: CComHeapPtr.cpp:68
virtual SIZE_T STDMETHODCALLTYPE PreRealloc(LPVOID pRequest, SIZE_T cbRequest, LPVOID *ppNewRequest, BOOL)
Definition: CComHeapPtr.cpp:52
virtual void STDMETHODCALLTYPE PostFree(BOOL fSpyed)
Definition: CComHeapPtr.cpp:47
virtual LPVOID STDMETHODCALLTYPE PreFree(LPVOID pRequest, BOOL)
Definition: CComHeapPtr.cpp:46
virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual)
Definition: CComHeapPtr.cpp:66
virtual LPVOID STDMETHODCALLTYPE PostRealloc(LPVOID pActual, BOOL fSpyed)
Definition: CComHeapPtr.cpp:57
virtual ULONG STDMETHODCALLTYPE AddRef()
Definition: CComHeapPtr.cpp:38
virtual ULONG STDMETHODCALLTYPE Release()
Definition: CComHeapPtr.cpp:39
virtual void STDMETHODCALLTYPE PreHeapMinimize()
Definition: CComHeapPtr.cpp:67
virtual SIZE_T STDMETHODCALLTYPE PostGetSize(SIZE_T cbActual, BOOL)
Definition: CComHeapPtr.cpp:64
virtual LPVOID STDMETHODCALLTYPE PreDidAlloc(LPVOID pRequest, BOOL)
Definition: CComHeapPtr.cpp:65
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG
Definition: typedefs.h:59
Definition: pdh_main.c:94
_In_ void _In_ PCCERT_CONTEXT _In_opt_ LPFILETIME _In_ DWORD _In_ DWORD _Outptr_opt_ void ** ppvObject
Definition: wincrypt.h:6082