mapi32: Fix many test failures on Win9x.
[wine] / dlls / mapi32 / tests / imalloc.c
1 /*
2  * Unit test suite for MAPI IMalloc functions
3  *
4  * Copyright 2004 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #include "wine/test.h"
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winerror.h"
27 #include "winnt.h"
28 #include "mapiutil.h"
29
30 static HMODULE hMapi32 = 0;
31
32 static SCODE    (WINAPI *pScInitMapiUtil)(ULONG);
33 static LPMALLOC (WINAPI *pMAPIGetDefaultMalloc)(void);
34
35 static void test_IMalloc(void)
36 {
37     LPVOID lpMem;
38     ULONG ulRef;
39     int iRet;
40     HRESULT hRet;
41     LPMALLOC lpMalloc;
42     LPVOID lpVoid;
43
44     pMAPIGetDefaultMalloc = (void*)GetProcAddress(hMapi32,
45                                                   "MAPIGetDefaultMalloc@0");
46     if (!pMAPIGetDefaultMalloc)
47         return;
48
49     lpMalloc = pMAPIGetDefaultMalloc();
50     if (!lpMalloc)
51         return;
52
53     lpVoid = NULL;
54     hRet = IMalloc_QueryInterface(lpMalloc, &IID_IUnknown, &lpVoid);
55     ok (hRet == S_OK && lpVoid != NULL,
56         "IID_IUnknown: expected S_OK, non-null, got 0x%08x, %p\n",
57         hRet, lpVoid);
58
59     lpVoid = NULL;
60     hRet = IMalloc_QueryInterface(lpMalloc, &IID_IMalloc, &lpVoid);
61     ok (hRet == S_OK && lpVoid != NULL,
62         "IID_IIMalloc: expected S_OK, non-null, got 0x%08x, %p\n",
63         hRet, lpVoid);
64
65     /* Prove that native mapi uses LocalAlloc/LocalFree */
66     lpMem = IMalloc_Alloc(lpMalloc, 61);
67     ok (lpMem && IMalloc_GetSize(lpMalloc, lpMem) == LocalSize(lpMem),
68         "Expected non-null, same size, got %p, %s size\n", lpMem,
69         lpMem ? "different" : "same");
70
71     iRet = IMalloc_DidAlloc(lpMalloc, lpMem);
72     ok (iRet == -1, "DidAlloc, expected -1. got %d\n", iRet);
73
74     IMalloc_HeapMinimize(lpMalloc);
75  
76     LocalFree(lpMem);
77  
78     ulRef = IMalloc_AddRef(lpMalloc);
79     ok (ulRef == 1u, "AddRef expected 1, returned %d\n", ulRef); 
80     
81     ulRef = IMalloc_Release(lpMalloc);
82     ok (ulRef == 1u, "AddRef expected 1, returned %d\n", ulRef);
83
84     IMalloc_Release(lpMalloc);
85 }
86
87 START_TEST(imalloc)
88 {
89     SCODE ret;
90
91     hMapi32 = LoadLibraryA("mapi32.dll");
92
93     pScInitMapiUtil = (void*)GetProcAddress(hMapi32, "ScInitMapiUtil@4");
94     if (!pScInitMapiUtil)
95     {
96         skip("ScInitMapiUtil is not available\n");
97         FreeLibrary(hMapi32);
98         return;
99     }
100
101     SetLastError(0xdeadbeef);
102     ret = pScInitMapiUtil(0);
103     if ((ret != S_OK) && (GetLastError() == ERROR_PROC_NOT_FOUND))
104     {
105         skip("ScInitMapiUtil is not implemented\n");
106         FreeLibrary(hMapi32);
107         return;
108     }
109
110     test_IMalloc();
111
112     FreeLibrary(hMapi32);
113 }