[ATL]
[reactos.git] / rostests / apitests / atl / CComHeapPtr.cpp
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for CComHeapPtr
5 * PROGRAMMER: Mark Jansen
6 */
7
8 #include <apitest.h>
9 #include <atlbase.h>
10 #include <atlcom.h>
11
12 static PDWORD test_Alloc(DWORD value)
13 {
14 PDWORD ptr = (PDWORD)::CoTaskMemAlloc(sizeof(DWORD));
15 *ptr = value;
16 return ptr;
17 }
18
19
20 static LONG g_OpenAllocations = 0;
21 static LONG g_Reallocations = 0;
22
23 struct CMallocSpy : public IMallocSpy
24 {
25 STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject)
26 {
27 if (IsEqualGUID(riid, IID_IMallocSpy))
28 {
29 *ppvObject = this;
30 }
31 return S_OK;
32 }
33
34 virtual ULONG STDMETHODCALLTYPE AddRef() { return 1; }
35 virtual ULONG STDMETHODCALLTYPE Release() { return 1; }
36 virtual ULONG STDMETHODCALLTYPE PreAlloc(ULONG cbRequest) { return cbRequest; }
37 virtual LPVOID STDMETHODCALLTYPE PostAlloc(LPVOID pActual)
38 {
39 InterlockedIncrement(&g_OpenAllocations);
40 return pActual;
41 }
42 virtual LPVOID STDMETHODCALLTYPE PreFree(LPVOID pRequest, BOOL) { return pRequest; }
43 virtual void STDMETHODCALLTYPE PostFree(BOOL fSpyed)
44 {
45 if (fSpyed)
46 InterlockedDecrement(&g_OpenAllocations);
47 }
48 virtual ULONG STDMETHODCALLTYPE PreRealloc(LPVOID pRequest, ULONG cbRequest, LPVOID *ppNewRequest, BOOL)
49 {
50 *ppNewRequest = pRequest;
51 return cbRequest;
52 }
53 virtual LPVOID STDMETHODCALLTYPE PostRealloc(LPVOID pActual, BOOL fSpyed)
54 {
55 if (fSpyed)
56 InterlockedIncrement(&g_Reallocations);
57 return pActual;
58 }
59 virtual LPVOID STDMETHODCALLTYPE PreGetSize(LPVOID pRequest, BOOL) { return pRequest; }
60 virtual ULONG STDMETHODCALLTYPE PostGetSize(ULONG cbActual, BOOL) { return cbActual; }
61 virtual LPVOID STDMETHODCALLTYPE PreDidAlloc(LPVOID pRequest, BOOL) { return pRequest; }
62 virtual int STDMETHODCALLTYPE PostDidAlloc(LPVOID, BOOL, int fActual) { return fActual; }
63 virtual void STDMETHODCALLTYPE PreHeapMinimize() {}
64 virtual void STDMETHODCALLTYPE PostHeapMinimize() {}
65 };
66
67 static CMallocSpy g_Spy;
68
69
70 START_TEST(CComHeapPtr)
71 {
72 HRESULT hr = CoRegisterMallocSpy(&g_Spy);
73 ok(SUCCEEDED(hr), "Expected CoRegisterMallocSpy to succeed, but it failed: 0x%lx\n", hr);
74
75 {
76 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
77 CComHeapPtr<DWORD> heapPtr1;
78 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
79 CComHeapPtr<DWORD> heapPtr2(test_Alloc(0x11111111));
80 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
81
82 ok((PDWORD)heapPtr1 == NULL, "Expected heapPtr1 to be NULL, was: 0x%p\n", (PDWORD)heapPtr1);
83 ok((PDWORD)heapPtr2 != NULL, "Expected heapPtr2 to not be NULL\n");
84 ok(*heapPtr2 == 0x11111111, "Expected *heapPtr2 to be 0x11111111, but was: 0x%lx\n", *heapPtr2);
85
86 {
87 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
88 CComHeapPtr<DWORD> heapPtrSteal1(heapPtr1);
89 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
90 ok((PDWORD)heapPtr1 == NULL, "Expected heapPtr1 to be NULL, was: 0x%p\n", (PDWORD)heapPtr1);
91 ok((PDWORD)heapPtrSteal1 == NULL, "Expected heapPtrSteal1 to be NULL, was: 0x%p\n", (PDWORD)heapPtrSteal1);
92 CComHeapPtr<DWORD> heapPtrSteal2(heapPtr2);
93 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
94 ok((PDWORD)heapPtr2 == NULL, "Expected heapPtr2 to be NULL, was: 0x%p\n", (PDWORD)heapPtr2);
95 ok((PDWORD)heapPtrSteal2 != NULL, "Expected heapPtrSteal2 to not be NULL\n");
96 ok(*heapPtrSteal2 == 0x11111111, "Expected *heapPtrSteal2 to be 0x11111111, but was: 0x%lx\n", *heapPtrSteal2);
97 }
98 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
99
100 ok(heapPtr1.Allocate(1), "Expected Allocate to succeed\n");
101 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
102 ok(g_Reallocations == 0, "Expected there to be 0 reallocations, was: %ld\n", g_Reallocations);
103
104 *heapPtr1 = 0x22222222;
105 ok(*heapPtr1 == 0x22222222, "Expected *heapPtr1 to be 0x22222222, but was: 0x%lx\n", *heapPtr1);
106
107 ok(heapPtr1.Reallocate(2), "Expected Reallocate to succeed\n");
108 heapPtr1[1] = 0x33333333;
109 ok(*heapPtr1 == 0x22222222, "Expected *heapPtr1 to be 0x22222222, but was: 0x%lx\n", *heapPtr1);
110 ok(g_Reallocations == 1, "Expected there to be 1 reallocations, was: %ld\n", g_Reallocations);
111 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
112
113 heapPtr2 = heapPtr1;
114 ok(g_OpenAllocations == 1, "Expected there to be 1 allocations, was: %ld\n", g_OpenAllocations);
115 ok(*heapPtr2 == 0x22222222, "Expected *heapPtr2 to be 0x33333333, but was: 0x%lx\n", *heapPtr2);
116 ok(heapPtr2[1] == 0x33333333, "Expected heapPtr2[1] to be 0x33333333, but was: 0x%lx\n", heapPtr2[1]);
117 ok((PDWORD)heapPtr1 == NULL, "Expected heapPtr1 to be NULL, was: 0x%p\n", (PDWORD)heapPtr1);
118 }
119 ok(g_OpenAllocations == 0, "Expected there to be 0 allocations, was: %ld\n", g_OpenAllocations);
120
121 hr = CoRevokeMallocSpy();
122 ok(SUCCEEDED(hr), "Expected CoRevokeMallocSpy to succeed, but it failed: 0x%lx\n", hr);
123 }