ReactOS  0.4.15-dev-1177-g6cb3b62
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 
15 static PDWORD
17 {
18  PDWORD ptr = (PDWORD)::CoTaskMemAlloc(sizeof(DWORD));
19  *ptr = value;
20  return ptr;
21 }
22 
23 
25 static LONG g_Reallocations = 0;
26 
27 struct 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  }
46  virtual LPVOID STDMETHODCALLTYPE PreFree(LPVOID pRequest, BOOL) { return pRequest; }
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  }
57  virtual LPVOID STDMETHODCALLTYPE PostRealloc(LPVOID pActual, BOOL fSpyed)
58  {
59  if (fSpyed)
61  return pActual;
62  }
63  virtual LPVOID STDMETHODCALLTYPE PreGetSize(LPVOID pRequest, BOOL) { return pRequest; }
64  virtual SIZE_T STDMETHODCALLTYPE PostGetSize(SIZE_T cbActual, BOOL) { return cbActual; }
65  virtual LPVOID STDMETHODCALLTYPE PreDidAlloc(LPVOID pRequest, BOOL) { return pRequest; }
66  virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual) { return fActual; }
69 };
70 
72 
73 
74 START_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 
125  hr = CoRevokeMallocSpy();
126  ok(SUCCEEDED(hr), "Expected CoRevokeMallocSpy to succeed, but it failed: 0x%lx\n", hr);
127 }
#define REFIID
Definition: guiddef.h:118
static LONG g_OpenAllocations
Definition: CComHeapPtr.cpp:24
HRESULT hr
Definition: shlfolder.c:183
REFIID riid
Definition: precomp.h:44
virtual SIZE_T STDMETHODCALLTYPE PreAlloc(SIZE_T cbRequest)
Definition: CComHeapPtr.cpp:40
START_TEST(CComHeapPtr)
Definition: CComHeapPtr.cpp:74
virtual void STDMETHODCALLTYPE PreHeapMinimize()
Definition: CComHeapPtr.cpp:67
virtual void STDMETHODCALLTYPE PostHeapMinimize()
Definition: CComHeapPtr.cpp:68
virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual)
Definition: CComHeapPtr.cpp:66
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
HRESULT WINAPI CoRevokeMallocSpy(void)
Definition: ifs.c:522
virtual LPVOID STDMETHODCALLTYPE PreGetSize(LPVOID pRequest, BOOL)
Definition: CComHeapPtr.cpp:63
static PVOID ptr
Definition: dispmode.c:27
#define STDMETHODIMP
Definition: basetyps.h:43
smooth NULL
Definition: ftsmooth.c:416
static CMallocSpy g_Spy
Definition: CComHeapPtr.cpp:71
virtual ULONG STDMETHODCALLTYPE AddRef()
Definition: CComHeapPtr.cpp:38
virtual LPVOID STDMETHODCALLTYPE PostRealloc(LPVOID pActual, BOOL fSpyed)
Definition: CComHeapPtr.cpp:57
virtual SIZE_T STDMETHODCALLTYPE PostGetSize(SIZE_T cbActual, BOOL)
Definition: CComHeapPtr.cpp:64
static LONG g_Reallocations
Definition: CComHeapPtr.cpp:25
LONG HRESULT
Definition: typedefs.h:79
static PDWORD test_Alloc(DWORD value)
Definition: CComHeapPtr.cpp:16
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
unsigned long DWORD
Definition: ntddk_ex.h:95
virtual LPVOID STDMETHODCALLTYPE PostAlloc(LPVOID pActual)
Definition: CComHeapPtr.cpp:41
virtual LPVOID STDMETHODCALLTYPE PreFree(LPVOID pRequest, BOOL)
Definition: CComHeapPtr.cpp:46
virtual void STDMETHODCALLTYPE PostFree(BOOL fSpyed)
Definition: CComHeapPtr.cpp:47
#define InterlockedDecrement
Definition: armddk.h:52
virtual ULONG STDMETHODCALLTYPE Release()
Definition: CComHeapPtr.cpp:39
REFIID LPVOID * ppvObject
Definition: precomp.h:44
virtual LPVOID STDMETHODCALLTYPE PreDidAlloc(LPVOID pRequest, BOOL)
Definition: CComHeapPtr.cpp:65
GLsizei const GLfloat * value
Definition: glext.h:6069
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define S_OK
Definition: intsafe.h:51
#define InterlockedIncrement
Definition: armddk.h:53
#define ok(value,...)
Definition: atltest.h:57
HRESULT WINAPI CoRegisterMallocSpy(LPMALLOCSPY pMallocSpy)
Definition: ifs.c:482
BOOL WINAPI IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
Definition: compobj.c:4112
DWORD * PDWORD
Definition: pedump.c:68
unsigned int ULONG
Definition: retypes.h:1
virtual SIZE_T STDMETHODCALLTYPE PreRealloc(LPVOID pRequest, SIZE_T cbRequest, LPVOID *ppNewRequest, BOOL)
Definition: CComHeapPtr.cpp:52
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject)
Definition: CComHeapPtr.cpp:29
LPVOID WINAPI CoTaskMemAlloc(SIZE_T size)
Definition: ifs.c:426
#define SUCCEEDED(hr)
Definition: intsafe.h:49