quartz: Make dwSamplesProcessed a longlong.
[wine] / dlls / oleaut32 / tests / safearray.c
1 /*
2  * SafeArray test program
3  *
4  * Copyright 2002 Marcus Meissner
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
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <float.h>
26 #include <time.h>
27
28 #define COBJMACROS
29 #include "wine/test.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "wingdi.h"
34 #include "winnls.h"
35 #include "winsock.h"
36 #include "winerror.h"
37 #include "winnt.h"
38
39 #include "wtypes.h"
40 #include "oleauto.h"
41
42 static HMODULE hOleaut32;
43
44 static HRESULT (WINAPI *pSafeArrayAllocDescriptorEx)(VARTYPE,UINT,SAFEARRAY**);
45 static HRESULT (WINAPI *pSafeArrayCopyData)(SAFEARRAY*,SAFEARRAY*);
46 static HRESULT (WINAPI *pSafeArrayGetIID)(SAFEARRAY*,GUID*);
47 static HRESULT (WINAPI *pSafeArraySetIID)(SAFEARRAY*,REFGUID);
48 static HRESULT (WINAPI *pSafeArrayGetVartype)(SAFEARRAY*,VARTYPE*);
49 static HRESULT (WINAPI *pSafeArrayGetRecordInfo)(SAFEARRAY*,IRecordInfo**);
50 static SAFEARRAY* (WINAPI *pSafeArrayCreateEx)(VARTYPE,UINT,SAFEARRAYBOUND*,LPVOID);
51 static SAFEARRAY* (WINAPI *pSafeArrayCreateVector)(VARTYPE,LONG,ULONG);
52
53 #define GETPTR(func) p##func = (void*)GetProcAddress(hOleaut32, #func)
54
55 /* Is a given function exported from oleaut32? */
56 #define HAVE_FUNC(func) ((void*)GetProcAddress(hOleaut32, #func) != NULL)
57
58 /* Have IRecordInfo data type? */
59 #define HAVE_OLEAUT32_RECORD  HAVE_FUNC(SafeArraySetRecordInfo)
60 /* Have R8 data type? */
61 #define HAVE_OLEAUT32_R8      HAVE_FUNC(VarR8FromI1)
62 /* Have I8/UI8 data type? */
63 #define HAVE_OLEAUT32_I8      HAVE_FUNC(VarI8FromI1)
64 /* Have the decimal type? */
65 #define HAVE_OLEAUT32_DECIMAL HAVE_FUNC(VarDecAdd)
66 /* Have INT_PTR/UINT_PTR type? */
67 static BOOL HAVE_OLEAUT32_INT_PTR;
68
69 /* very old version? */
70 #define IS_ANCIENT (!HAVE_FUNC(VarI1FromI2))
71
72 #define START_REF_COUNT 1
73 #define RECORD_SIZE 64
74 #define RECORD_SIZE_FAIL 17
75 /************************************************************************
76  * Dummy IRecordInfo Implementation
77  */
78 typedef struct IRecordInfoImpl
79 {
80   const IRecordInfoVtbl *lpvtbl;
81   LONG ref;
82   DWORD sizeCalled;
83   DWORD clearCalled;
84 } IRecordInfoImpl;
85
86 static const IRecordInfoVtbl IRecordInfoImpl_VTable;
87
88 static IRecordInfoImpl *IRecordInfoImpl_Construct(void)
89 {
90   IRecordInfoImpl *rec;
91
92   rec = HeapAlloc(GetProcessHeap(), 0, sizeof(IRecordInfoImpl));
93   rec->lpvtbl = &IRecordInfoImpl_VTable;
94   rec->ref = START_REF_COUNT;
95   rec->clearCalled = 0;
96   rec->sizeCalled = 0;
97   return rec;
98 }
99
100 static ULONG CALLBACK IRecordInfoImpl_AddRef(IRecordInfo *iface)
101 {
102   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
103   return InterlockedIncrement(&This->ref);
104 }
105
106 static ULONG CALLBACK IRecordInfoImpl_Release(IRecordInfo *iface)
107 {
108   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
109   return InterlockedDecrement(&This->ref);
110 }
111
112 static BOOL fail_GetSize; /* Whether to fail the GetSize call */
113
114 static HRESULT CALLBACK IRecordInfoImpl_RecordClear(IRecordInfo *iface, PVOID pvExisting)
115 {
116   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
117   This->clearCalled++;
118   return S_OK;
119 }
120
121 static HRESULT CALLBACK IRecordInfoImpl_GetSize(IRecordInfo *iface, ULONG* size)
122 {
123   IRecordInfoImpl* This=(IRecordInfoImpl*)iface;
124   This->sizeCalled++;
125   *size = 17;
126   if (fail_GetSize)
127     return E_UNEXPECTED;
128   *size = RECORD_SIZE;
129   return S_OK;
130 }
131
132 static HRESULT CALLBACK IRecordInfoImpl_Dummy(IRecordInfo *iface)
133 {
134   trace("Called an unexpected IRecordInfo method - please report!\n");
135   /* Quit because we'll just crash anyway */
136   fflush(NULL);
137   exit(255);
138 }
139
140 static const IRecordInfoVtbl IRecordInfoImpl_VTable =
141 {
142   (PVOID)IRecordInfoImpl_Dummy,
143   IRecordInfoImpl_AddRef,
144   IRecordInfoImpl_Release,
145   (PVOID)IRecordInfoImpl_Dummy,
146   IRecordInfoImpl_RecordClear,
147   (PVOID)IRecordInfoImpl_Dummy,
148   (PVOID)IRecordInfoImpl_Dummy,
149   (PVOID)IRecordInfoImpl_Dummy,
150   (PVOID)IRecordInfoImpl_GetSize,
151   (PVOID)IRecordInfoImpl_Dummy,
152   (PVOID)IRecordInfoImpl_Dummy,
153   (PVOID)IRecordInfoImpl_Dummy,
154   (PVOID)IRecordInfoImpl_Dummy,
155   (PVOID)IRecordInfoImpl_Dummy,
156   (PVOID)IRecordInfoImpl_Dummy,
157   (PVOID)IRecordInfoImpl_Dummy,
158   (PVOID)IRecordInfoImpl_Dummy,
159   (PVOID)IRecordInfoImpl_Dummy,
160   (PVOID)IRecordInfoImpl_Dummy
161 };
162
163 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
164 {
165   switch (vt)
166   {
167     case VT_I1:
168     case VT_UI1:      return sizeof(BYTE);
169     case VT_BOOL:
170     case VT_I2:
171     case VT_UI2:      return sizeof(SHORT);
172     case VT_I4:
173     case VT_UI4:
174     case VT_R4:
175     case VT_ERROR:    return sizeof(LONG);
176     case VT_R8:
177       if (HAVE_OLEAUT32_R8)
178         return sizeof(LONG64);
179     case VT_I8:
180     case VT_UI8:
181       if (HAVE_OLEAUT32_I8)
182         return sizeof(LONG64);
183       break;
184     case VT_INT:
185     case VT_UINT:     return sizeof(INT);
186     case VT_INT_PTR:
187     case VT_UINT_PTR: 
188       if (HAVE_OLEAUT32_INT_PTR)
189         return sizeof(UINT_PTR);
190       break;
191     case VT_CY:       return sizeof(CY);
192     case VT_DATE:     return sizeof(DATE);
193     case VT_BSTR:     return sizeof(BSTR);
194     case VT_DISPATCH: return sizeof(LPDISPATCH);
195     case VT_VARIANT:  return sizeof(VARIANT);
196     case VT_UNKNOWN:  return sizeof(LPUNKNOWN);
197     case VT_DECIMAL:
198       if (HAVE_OLEAUT32_DECIMAL)
199         return sizeof(DECIMAL);
200       break;
201   }
202   return 0;
203 }
204
205 static void check_for_VT_INT_PTR(void)
206 {
207     /* Set a global flag if VT_INT_PTR is supported */
208
209     SAFEARRAY* a;
210     SAFEARRAYBOUND bound;
211     bound.cElements     = 0;
212     bound.lLbound       = 0;
213     a = SafeArrayCreate(VT_INT_PTR, 1, &bound);
214     if (a) {
215         trace("VT_INT_PTR is supported\n");
216         HAVE_OLEAUT32_INT_PTR = TRUE;
217         SafeArrayDestroy(a);
218     }
219     else {
220         trace("VT_INT_PTR is not supported\n");
221         HAVE_OLEAUT32_INT_PTR = FALSE;
222     }        
223 }
224
225 #define VARTYPE_NOT_SUPPORTED 0
226 static struct {
227         VARTYPE vt;    /* VT */
228         UINT elemsize; /* elementsize by VT */
229         UINT expflags; /* fFeatures from SafeArrayAllocDescriptorEx */
230         UINT addflags; /* additional fFeatures from SafeArrayCreate */
231 } vttypes[] = {
232 {VT_EMPTY,    VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
233 {VT_NULL,     VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
234 {VT_I2,       2,                    FADF_HAVEVARTYPE,0},
235 {VT_I4,       4,                    FADF_HAVEVARTYPE,0},
236 {VT_R4,       4,                    FADF_HAVEVARTYPE,0},
237 {VT_R8,       8,                    FADF_HAVEVARTYPE,0},
238 {VT_CY,       8,                    FADF_HAVEVARTYPE,0},
239 {VT_DATE,     8,                    FADF_HAVEVARTYPE,0},
240 {VT_BSTR,     sizeof(BSTR),         FADF_HAVEVARTYPE,FADF_BSTR},
241 {VT_DISPATCH, sizeof(LPDISPATCH),   FADF_HAVEIID,    FADF_DISPATCH},
242 {VT_ERROR,    4,                    FADF_HAVEVARTYPE,0},
243 {VT_BOOL,     2,                    FADF_HAVEVARTYPE,0},
244 {VT_VARIANT,  sizeof(VARIANT),      FADF_HAVEVARTYPE,FADF_VARIANT},
245 {VT_UNKNOWN,  sizeof(LPUNKNOWN),    FADF_HAVEIID,    FADF_UNKNOWN},
246 {VT_DECIMAL,  sizeof(DECIMAL),      FADF_HAVEVARTYPE,0},
247 {15,          VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0}, /* no VT_xxx */
248 {VT_I1,       1,                    FADF_HAVEVARTYPE,0},
249 {VT_UI1,      1,                    FADF_HAVEVARTYPE,0},
250 {VT_UI2,      2,                    FADF_HAVEVARTYPE,0},
251 {VT_UI4,      4,                    FADF_HAVEVARTYPE,0},
252 {VT_I8,       VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
253 {VT_UI8,      VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
254 {VT_INT,      sizeof(INT),          FADF_HAVEVARTYPE,0},
255 {VT_UINT,     sizeof(UINT),         FADF_HAVEVARTYPE,0},
256 {VT_VOID,     VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
257 {VT_HRESULT,  VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
258 {VT_PTR,      VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
259 {VT_SAFEARRAY,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
260 {VT_CARRAY,   VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
261 {VT_USERDEFINED,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
262 {VT_LPSTR,    VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
263 {VT_LPWSTR,   VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
264 {VT_FILETIME, VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
265 {VT_RECORD,   VARTYPE_NOT_SUPPORTED,FADF_RECORD,0},
266 {VT_BLOB,     VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
267 {VT_STREAM,   VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
268 {VT_STORAGE,  VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
269 {VT_STREAMED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
270 {VT_STORED_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
271 {VT_BLOB_OBJECT,VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
272 {VT_CF,       VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
273 {VT_CLSID,    VARTYPE_NOT_SUPPORTED,FADF_HAVEVARTYPE,0},
274 };
275
276 static void test_safearray(void)
277 {
278         SAFEARRAY       *a, b, *c;
279         unsigned int    i;
280         LONG            indices[2];
281         HRESULT         hres;
282         SAFEARRAYBOUND  bound, bounds[2];
283         VARIANT         v;
284         LPVOID          data;
285         IID             iid;
286         VARTYPE         vt;
287         LONG            l;
288         unsigned char   *ptr1, *ptr2;
289
290         hres = SafeArrayDestroy( NULL);
291         ok( hres == S_OK, "SafeArrayDestroy( NULL) returned 0x%x\n", hres);
292
293         bound.cElements = 1;
294         bound.lLbound   = 0;
295         a = SafeArrayCreate(-1, 1, &bound);
296         ok(NULL == a,"SAC(-1,1,[1,0]) not failed?\n");
297
298         bound.cElements = 0;
299         bound.lLbound   = 42;
300         a = SafeArrayCreate(VT_I4, 1, &bound);
301         ok(NULL != a,"SAC(VT_I4,1,[0,0]) failed.\n");
302
303         hres = SafeArrayGetLBound(a, 1, &l);
304         ok(hres == S_OK, "SAGLB of 0 size dimensioned array failed with %x\n",hres);
305         ok(l == 42, "SAGLB of 0 size dimensioned array failed to return 42, but returned %d\n",l);
306         hres = SafeArrayGetUBound(a, 1, &l);
307         ok(hres == S_OK, "SAGUB of 0 size dimensioned array failed with %x\n",hres);
308         ok(l == 41, "SAGUB of 0 size dimensioned array failed to return 41, but returned %d\n",l);
309         
310         hres = SafeArrayAccessData(a, &data);
311         ok(hres == S_OK, "SafeArrayAccessData of 0 size dimensioned array failed with %x\n", hres);
312         SafeArrayUnaccessData(a);
313
314         bound.cElements = 2;
315         hres = SafeArrayRedim(a, &bound);
316         ok(hres == S_OK,"SAR of a 0 elements dimension failed with hres %x\n", hres);
317         bound.cElements = 0;
318         hres = SafeArrayRedim(a, &bound);
319         ok(hres == S_OK || hres == E_OUTOFMEMORY,
320           "SAR to a 0 elements dimension failed with hres %x\n", hres);
321         hres = SafeArrayDestroy(a);
322         ok(hres == S_OK,"SAD of 0 dim array faild with hres %x\n", hres);
323
324         SafeArrayAllocDescriptor(2, &a);
325         a->rgsabound[0].cElements = 2;
326         a->rgsabound[0].lLbound = 1;
327         a->rgsabound[1].cElements = 4;
328         a->rgsabound[1].lLbound = 1;
329         a->cbElements = 2;
330         SafeArrayAllocData(a);
331
332         indices[0] = 4;
333         indices[1] = 2;
334         hres = SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
335         ok(hres == S_OK, "SAPOI failed with hres %x\n", hres);
336         SafeArrayAccessData(a, (void **)&ptr2);
337         ok(ptr1 - ptr2 == 14, "SAPOI got wrong ptr\n");
338         *(WORD *)ptr1 = 0x55aa;
339         SafeArrayUnaccessData(a);
340
341         bound.cElements = 10;
342         bound.lLbound = 1;
343         SafeArrayRedim(a, &bound);
344         ptr1 = NULL;
345         SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
346         ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
347
348         bound.cElements = 10;
349         bound.lLbound = 0;
350         SafeArrayRedim(a, &bound);
351         SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
352         ok(*(WORD *)ptr1 == 0, "Expanded area not zero-initialized\n");
353
354         indices[1] = 1;
355         SafeArrayPtrOfIndex(a, indices, (void **)&ptr1);
356         ok(*(WORD *)ptr1 == 0x55aa, "Data not preserved when resizing array\n");
357
358         hres = SafeArrayDestroy(a);
359         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
360
361         bounds[0].cElements = 0;        bounds[0].lLbound =  1;
362         bounds[1].cElements =  2;       bounds[1].lLbound = 23;
363         a = SafeArrayCreate(VT_I4,2,bounds);
364         ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
365
366         hres = SafeArrayDestroy(a);
367         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
368         bounds[0].cElements = 1;        bounds[0].lLbound =  1;
369         bounds[1].cElements = 0;        bounds[1].lLbound = 23;
370         a = SafeArrayCreate(VT_I4,2,bounds);
371         ok(a != NULL,"SAC(VT_INT32,2,...) with 0 element dim failed.\n");
372
373         hres = SafeArrayDestroy(a);
374         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
375
376         bounds[0].cElements = 42;       bounds[0].lLbound =  1;
377         bounds[1].cElements =  2;       bounds[1].lLbound = 23;
378     a = SafeArrayCreate(VT_I4,2,bounds);
379     ok(a != NULL,"SAC(VT_INT32,2,...) failed.\n");
380
381         hres = SafeArrayGetLBound (a, 0, &l);
382         ok (hres == DISP_E_BADINDEX, "SAGLB 0 failed with %x\n", hres);
383         hres = SafeArrayGetLBound (a, 1, &l);
384         ok (hres == S_OK, "SAGLB 1 failed with %x\n", hres);
385         ok (l == 1, "SAGLB 1 returned %d instead of 1\n", l);
386         hres = SafeArrayGetLBound (a, 2, &l);
387         ok (hres == S_OK, "SAGLB 2 failed with %x\n", hres);
388         ok (l == 23, "SAGLB 2 returned %d instead of 23\n", l);
389         hres = SafeArrayGetLBound (a, 3, &l);
390         ok (hres == DISP_E_BADINDEX, "SAGLB 3 failed with %x\n", hres);
391
392         hres = SafeArrayGetUBound (a, 0, &l);
393         ok (hres == DISP_E_BADINDEX, "SAGUB 0 failed with %x\n", hres);
394         hres = SafeArrayGetUBound (a, 1, &l);
395         ok (hres == S_OK, "SAGUB 1 failed with %x\n", hres);
396         ok (l == 42, "SAGUB 1 returned %d instead of 42\n", l);
397         hres = SafeArrayGetUBound (a, 2, &l);
398         ok (hres == S_OK, "SAGUB 2 failed with %x\n", hres);
399         ok (l == 24, "SAGUB 2 returned %d instead of 24\n", l);
400         hres = SafeArrayGetUBound (a, 3, &l);
401         ok (hres == DISP_E_BADINDEX, "SAGUB 3 failed with %x\n", hres);
402
403         i = SafeArrayGetDim(a);
404         ok(i == 2, "getdims of 2 din array returned %d\n",i);
405
406         indices[0] = 0;
407         indices[1] = 23;
408         hres = SafeArrayGetElement(a, indices, &i);
409         ok(DISP_E_BADINDEX == hres,"SAGE failed [0,23], hres 0x%x\n",hres);
410
411         indices[0] = 1;
412         indices[1] = 22;
413         hres = SafeArrayGetElement(a, indices, &i);
414         ok(DISP_E_BADINDEX == hres,"SAGE failed [1,22], hres 0x%x\n",hres);
415
416         indices[0] = 1;
417         indices[1] = 23;
418         hres = SafeArrayGetElement(a, indices, &i);
419         ok(S_OK == hres,"SAGE failed [1,23], hres 0x%x\n",hres);
420
421         indices[0] = 1;
422         indices[1] = 25;
423         hres = SafeArrayGetElement(a, indices, &i);
424         ok(DISP_E_BADINDEX == hres,"SAGE failed [1,24], hres 0x%x\n",hres);
425
426         indices[0] = 3;
427         indices[1] = 23;
428         hres = SafeArrayGetElement(a, indices, &i);
429         ok(S_OK == hres,"SAGE failed [42,23], hres 0x%x\n",hres);
430
431         hres = SafeArrayAccessData(a, (void**)&ptr1);
432         ok(S_OK == hres, "SAAD failed with 0x%x\n", hres);
433
434         indices[0] = 3;
435         indices[1] = 23;
436         hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
437         ok(S_OK == hres,"SAPOI failed [1,23], hres 0x%x\n",hres);
438         ok(ptr2 - ptr1 == 8,"ptr difference is not 8, but %d (%p vs %p)\n", ptr2-ptr1, ptr2, ptr1);
439
440         indices[0] = 3;
441         indices[1] = 24;
442         hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
443         ok(S_OK == hres,"SAPOI failed [5,24], hres 0x%x\n",hres);
444         ok(ptr2 - ptr1 == 176,"ptr difference is not 176, but %d (%p vs %p)\n", ptr2-ptr1, ptr2, ptr1);
445
446         indices[0] = 20;
447         indices[1] = 23;
448         hres = SafeArrayPtrOfIndex(a, indices, (void**)&ptr2);
449         ok(S_OK == hres,"SAPOI failed [20,23], hres 0x%x\n",hres);
450         ok(ptr2 - ptr1 == 76,"ptr difference is not 76, but %d (%p vs %p)\n", ptr2-ptr1, ptr2, ptr1);
451
452         hres = SafeArrayUnaccessData(a);
453         ok(S_OK == hres, "SAUAD failed with 0x%x\n", hres);
454
455         hres = SafeArrayDestroy(a);
456         ok(hres == S_OK,"SAD faild with hres %x\n", hres);
457
458         for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
459         if ((i == VT_I8 || i == VT_UI8) && HAVE_OLEAUT32_I8)
460         {
461           vttypes[i].elemsize = sizeof(LONG64);
462         }
463
464         a = SafeArrayCreate(vttypes[i].vt, 1, &bound);
465
466         ok((!a && !vttypes[i].elemsize) ||
467            (a && vttypes[i].elemsize == a->cbElements) ||
468            (IS_ANCIENT && (vttypes[i].vt == VT_DECIMAL || vttypes[i].vt == VT_I1 ||
469             vttypes[i].vt == VT_UI2 || vttypes[i].vt == VT_UI4 || vttypes[i].vt == VT_INT ||
470             vttypes[i].vt == VT_UINT)),
471            "SAC(%d,1,[1,0]), %p result %d, expected %d\n",
472            vttypes[i].vt,a,(a?a->cbElements:0),vttypes[i].elemsize);
473
474         if (a)
475         {
476           if (!HAVE_OLEAUT32_RECORD)
477             vttypes[i].expflags = 0;
478           ok(a->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),
479              "SAC of %d returned feature flags %x, expected %x\n",
480           vttypes[i].vt, a->fFeatures,
481           vttypes[i].expflags|vttypes[i].addflags);
482           ok(SafeArrayGetElemsize(a) == vttypes[i].elemsize,
483              "SAGE for vt %d returned elemsize %d instead of expected %d\n",
484              vttypes[i].vt, SafeArrayGetElemsize(a),vttypes[i].elemsize);
485         }
486
487                 if (!a) continue;
488
489         if (pSafeArrayGetVartype)
490         {
491             hres = pSafeArrayGetVartype(a, &vt);
492             ok(hres == S_OK, "SAGVT of arra y with vt %d failed with %x\n", vttypes[i].vt, hres);
493             if (vttypes[i].vt == VT_DISPATCH) {
494                         /* Special case. Checked against Windows. */
495                         ok(vt == VT_UNKNOWN, "SAGVT of a        rray with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
496             } else {
497                         ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
498             }
499         }
500
501                 hres = SafeArrayCopy(a, &c);
502                 ok(hres == S_OK, "failed to copy safearray of vt %d with hres %x\n", vttypes[i].vt, hres);
503
504                 ok(vttypes[i].elemsize == c->cbElements,"copy of SAC(%d,1,[1,0]), result %d, expected %d\n",vttypes[i].vt,(c?c->cbElements:0),vttypes[i].elemsize
505                 );
506                 ok(c->fFeatures == (vttypes[i].expflags | vttypes[i].addflags),"SAC of %d returned feature flags %x, expected %x\n", vttypes[i].vt, c->fFeatures, vttypes[i].expflags|vttypes[i].addflags);
507                 ok(SafeArrayGetElemsize(c) == vttypes[i].elemsize,"SAGE for vt %d returned elemsize %d instead of expected %d\n",vttypes[i].vt, SafeArrayGetElemsize(c),vttypes[i].elemsize);
508
509         if (pSafeArrayGetVartype) {
510             hres = pSafeArrayGetVartype(c, &vt);
511             ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
512             if (vttypes[i].vt == VT_DISPATCH) {
513                 /* Special case. Checked against Windows. */
514                 ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
515             } else {
516                 ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
517             }
518         }
519
520         if (pSafeArrayCopyData) {
521             hres = pSafeArrayCopyData(a, c);
522             ok(hres == S_OK, "failed to copy safearray data of vt %d with hres %x\n", vttypes[i].vt, hres);
523
524             hres = SafeArrayDestroyData(c);
525             ok(hres == S_OK,"SADD of copy of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
526         }
527
528                 hres = SafeArrayDestroy(c);
529                 ok(hres == S_OK,"SAD faild with hres %x\n", hres);
530
531                 hres = SafeArrayDestroy(a);
532                 ok(hres == S_OK,"SAD of array with vt %d failed with hres %x\n", vttypes[i].vt, hres);
533         }
534
535         /* Test conversion of type|VT_ARRAY <-> VT_BSTR */
536         bound.lLbound = 0;
537         bound.cElements = 10;
538         a = SafeArrayCreate(VT_UI1, 1, &bound);
539         ok(a != NULL, "SAC failed.\n");
540         ok(S_OK == SafeArrayAccessData(a, &data),"SACD failed\n");
541         memcpy(data,"Hello World\n",10);
542         ok(S_OK == SafeArrayUnaccessData(a),"SAUD failed\n");
543         V_VT(&v) = VT_ARRAY|VT_UI1;
544         V_ARRAY(&v) = a;
545         hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
546         ok(hres==S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n",hres);
547         ok(V_VT(&v) == VT_BSTR,"CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.v\n",V_VT(&v));
548         ok(V_BSTR(&v)[0] == 0x6548,"First letter are not 'He', but %x\n", V_BSTR(&v)[0]);
549         VariantClear(&v);
550
551         /* check locking functions */
552         a = SafeArrayCreate(VT_I4, 1, &bound);
553         ok(a!=NULL,"SAC should not fail\n");
554
555         hres = SafeArrayAccessData(a, &data);
556         ok(hres == S_OK,"SAAD failed with hres %x\n",hres);
557
558         hres = SafeArrayDestroy(a);
559         ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
560
561         hres = SafeArrayDestroyData(a);
562         ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy data not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
563
564         hres = SafeArrayDestroyDescriptor(a);
565         ok(hres == DISP_E_ARRAYISLOCKED,"locked safe array destroy descriptor not failed with DISP_E_ARRAYISLOCKED, but with hres %x\n", hres);
566
567         hres = SafeArrayUnaccessData(a);
568         ok(hres == S_OK,"SAUD failed after lock/destroy test\n");
569
570         hres = SafeArrayDestroy(a);
571         ok(hres == S_OK,"SAD failed after lock/destroy test\n");
572
573         /* Test if we need to destroy data before descriptor */
574         a = SafeArrayCreate(VT_I4, 1, &bound);
575         ok(a!=NULL,"SAC should not fail\n");
576         hres = SafeArrayDestroyDescriptor(a);
577         ok(hres == S_OK,"SADD with data in array failed with hres %x\n",hres);
578
579         /* IID functions */
580         /* init a small stack safearray */
581     if (pSafeArraySetIID) {
582         memset(&b, 0, sizeof(b));
583         b.cDims = 1;
584         memset(&iid, 0x42, sizeof(IID));
585         hres = pSafeArraySetIID(&b,&iid);
586         ok(hres == E_INVALIDARG,"SafeArraySetIID of non IID capable safearray did not return E_INVALIDARG, but %x\n",hres);
587
588         hres = SafeArrayAllocDescriptor(1,&a);
589         ok((a->fFeatures & FADF_HAVEIID) == 0,"newly allocated descriptor with SAAD should not have FADF_HAVEIID\n");
590         hres = pSafeArraySetIID(a,&iid);
591         ok(hres == E_INVALIDARG,"SafeArraySetIID of newly allocated descriptor with SAAD should return E_INVALIDARG, but %x\n",hres);
592
593         hres = SafeArrayDestroyDescriptor(a);
594         ok(hres == S_OK,"SADD failed with hres %x\n",hres);
595     }
596
597     if (!pSafeArrayAllocDescriptorEx)
598         return;
599
600         for (i=0;i<sizeof(vttypes)/sizeof(vttypes[0]);i++) {
601         a = NULL;
602                 hres = pSafeArrayAllocDescriptorEx(vttypes[i].vt,1,&a);
603                 ok(a->fFeatures == vttypes[i].expflags,"SAADE(%d) resulted with flags %x, expected %x\n", vttypes[i].vt, a->fFeatures, vttypes[i].expflags);
604                 if (a->fFeatures & FADF_HAVEIID) {
605                         hres = pSafeArrayGetIID(a, &iid);
606                         ok(hres == S_OK,"SAGIID failed for vt %d with hres %x\n", vttypes[i].vt,hres);
607                         switch (vttypes[i].vt) {
608                         case VT_UNKNOWN:
609                                 ok(IsEqualGUID(((GUID*)a)-1,&IID_IUnknown),"guid for VT_UNKNOWN is not IID_IUnknown\n");
610                                 ok(IsEqualGUID(&iid, &IID_IUnknown),"SAGIID returned wrong GUID for IUnknown\n");
611                                 break;
612                         case VT_DISPATCH:
613                                 ok(IsEqualGUID(((GUID*)a)-1,&IID_IDispatch),"guid for VT_UNKNOWN is not IID_IDispatch\n");
614                                 ok(IsEqualGUID(&iid, &IID_IDispatch),"SAGIID returned wrong GUID for IDispatch\n");
615                                 break;
616                         default:
617                                 ok(FALSE,"unknown vt %d with FADF_HAVEIID\n",vttypes[i].vt);
618                                 break;
619                         }
620                 } else {
621                         hres = pSafeArrayGetIID(a, &iid);
622                         ok(hres == E_INVALIDARG,"SAGIID did not fail for vt %d with hres %x\n", vttypes[i].vt,hres);
623                 }
624                 if (a->fFeatures & FADF_RECORD) {
625                         ok(vttypes[i].vt == VT_RECORD,"FADF_RECORD for non record %d\n",vttypes[i].vt);
626                 }
627                 if (a->fFeatures & FADF_HAVEVARTYPE) {
628                         ok(vttypes[i].vt == ((DWORD*)a)[-1], "FADF_HAVEVARTYPE set, but vt %d mismatch stored %d\n",vttypes[i].vt,((DWORD*)a)[-1]);
629                 }
630
631                 hres = pSafeArrayGetVartype(a, &vt);
632                 ok(hres == S_OK, "SAGVT of array with vt %d failed with %x\n", vttypes[i].vt, hres);
633
634                 if (vttypes[i].vt == VT_DISPATCH) {
635                         /* Special case. Checked against Windows. */
636                         ok(vt == VT_UNKNOWN, "SAGVT of array with VT_DISPATCH returned not VT_UNKNOWN, but %d\n", vt);
637                 } else {
638                         ok(vt == vttypes[i].vt, "SAGVT of array with vt %d returned %d\n", vttypes[i].vt, vt);
639                 }
640
641                 if (a->fFeatures & FADF_HAVEIID) {
642                         hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
643                         ok(hres == S_OK,"SASIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
644                         hres = pSafeArrayGetIID(a, &iid);
645                         ok(hres == S_OK,"SAGIID failed with FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
646                         ok(IsEqualGUID(&iid, &IID_IStorage),"returned iid is not IID_IStorage\n");
647                 } else {
648                         hres = pSafeArraySetIID(a, &IID_IStorage); /* random IID */
649                         ok(hres == E_INVALIDARG,"SASIID did not failed with !FADF_HAVEIID set for vt %d with %x\n", vttypes[i].vt, hres);
650                 }
651                 hres = SafeArrayDestroyDescriptor(a);
652                 ok(hres == S_OK,"SADD failed with hres %x\n",hres);
653         }
654 }
655
656 static void test_SafeArrayAllocDestroyDescriptor(void)
657 {
658   SAFEARRAY *sa;
659   HRESULT hres;
660   int i;
661
662   /* Failure cases */
663   hres = SafeArrayAllocDescriptor(0, &sa);
664   ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
665
666   hres = SafeArrayAllocDescriptor(65536, &sa);
667   ok(IS_ANCIENT || hres == E_INVALIDARG,
668      "65536 dimensions gave hres 0x%x\n", hres);
669
670   if (0)
671   {
672   /* Crashes on 95: XP & Wine return E_POINTER */
673   hres=SafeArrayAllocDescriptor(1, NULL);
674   ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
675   }
676
677   /* Test up to the dimension boundary case */
678   for (i = 5; i <= 65535; i += 30)
679   {
680     hres = SafeArrayAllocDescriptor(i, &sa);
681     ok(hres == S_OK, "%d dimensions failed; hres 0x%x\n", i, hres);
682
683     if (hres == S_OK)
684     {
685       ok(SafeArrayGetDim(sa) == (UINT)i, "Dimension is %d; should be %d\n",
686          SafeArrayGetDim(sa), i);
687
688       hres = SafeArrayDestroyDescriptor(sa);
689       ok(hres == S_OK, "destroy failed; hres 0x%x\n", hres);
690     }
691   }
692
693   if (!pSafeArrayAllocDescriptorEx)
694     return;
695
696   hres = pSafeArrayAllocDescriptorEx(VT_UI1, 0, &sa);
697   ok(hres == E_INVALIDARG, "0 dimensions gave hres 0x%x\n", hres);
698
699   hres = pSafeArrayAllocDescriptorEx(VT_UI1, 65536, &sa);
700   ok(hres == E_INVALIDARG, "65536 dimensions gave hres 0x%x\n", hres);
701
702   hres = pSafeArrayAllocDescriptorEx(VT_UI1, 1, NULL);
703   ok(hres == E_POINTER,"NULL parm gave hres 0x%x\n", hres);
704
705   hres = pSafeArrayAllocDescriptorEx(-1, 1, &sa);
706   ok(hres == S_OK, "VT = -1 gave hres 0x%x\n", hres);
707
708   sa->rgsabound[0].cElements = 0;
709   sa->rgsabound[0].lLbound = 1;
710
711   hres = SafeArrayAllocData(sa);
712   ok(hres == S_OK, "SafeArrayAllocData gave hres 0x%x\n", hres);
713
714   hres = SafeArrayDestroy(sa);
715   ok(hres == S_OK,"SafeArrayDestroy failed with hres %x\n",hres);
716 }
717
718 static void test_SafeArrayCreateLockDestroy(void)
719 {
720   SAFEARRAYBOUND sab[4];
721   SAFEARRAY *sa;
722   HRESULT hres;
723   VARTYPE vt;
724   int dimension;
725
726 #define NUM_DIMENSIONS (int)(sizeof(sab) / sizeof(sab[0]))
727
728   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
729   {
730     sab[dimension].lLbound = 0;
731     sab[dimension].cElements = 8;
732   }
733
734   /* Failure cases */
735 /* This test crashes very early versions with no error checking...
736   sa = SafeArrayCreate(VT_UI1, 1, NULL);
737   ok(sa == NULL, "NULL bounds didn't fail\n");
738 */
739   sa = SafeArrayCreate(VT_UI1, 65536, sab);
740   ok(IS_ANCIENT || !sa, "Max bounds didn't fail\n");
741
742   memset(sab, 0, sizeof(sab));
743
744   /* Don't test 0 sized dimensions, as Windows has a bug which allows this */
745
746   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
747     sab[dimension].cElements = 8;
748
749   /* Test all VARTYPES in 1-4 dimensions */
750   for (dimension = 1; dimension < 4; dimension++)
751   {
752     for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
753     {
754       DWORD dwLen = SAFEARRAY_GetVTSize(vt);
755
756       sa = SafeArrayCreate(vt, dimension, sab);
757
758       if (dwLen)
759         ok(sa || (IS_ANCIENT && (vt == VT_DECIMAL || vt == VT_I1 || vt == VT_UI2 ||
760            vt == VT_UI4 || vt == VT_INT || vt == VT_UINT)),
761            "VARTYPE %d (@%d dimensions) failed\n", vt, dimension);
762       else
763         ok(sa == NULL || vt == VT_R8,
764            "VARTYPE %d (@%d dimensions) succeeded!\n", vt, dimension);
765
766       if (sa)
767       {
768         ok(SafeArrayGetDim(sa) == (UINT)dimension,
769            "VARTYPE %d (@%d dimensions) cDims is %d, expected %d\n",
770            vt, dimension, SafeArrayGetDim(sa), dimension);
771         ok(SafeArrayGetElemsize(sa) == dwLen || vt == VT_R8,
772            "VARTYPE %d (@%d dimensions) cbElements is %d, expected %d\n",
773            vt, dimension, SafeArrayGetElemsize(sa), dwLen);
774
775         if (vt != VT_UNKNOWN && vt != VT_DISPATCH)
776         {
777           ok((sa->fFeatures & FADF_HAVEIID) == 0,
778              "Non interface type should not have FADF_HAVEIID\n");
779           if (pSafeArraySetIID)
780           {
781             hres = pSafeArraySetIID(sa, &IID_IUnknown);
782             ok(hres == E_INVALIDARG,
783                "Non interface type allowed SetIID(), hres %x\n", hres);
784           }
785           if (vt != VT_RECORD)
786           {
787             VARTYPE aVt;
788
789             ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEVARTYPE,
790                "Non interface type should have FADF_HAVEVARTYPE\n");
791             if (pSafeArrayGetVartype)
792             {
793               hres = pSafeArrayGetVartype(sa, &aVt);
794               ok(hres == S_OK && aVt == vt,
795                  "Non interface type %d: bad type %d, hres %x\n", vt, aVt, hres);
796             }
797           }
798         }
799         else
800         {
801           ok(IS_ANCIENT || sa->fFeatures & FADF_HAVEIID,
802              "Interface type should have FADF_HAVEIID\n");
803           if (pSafeArraySetIID)
804           {
805             hres = pSafeArraySetIID(sa, &IID_IUnknown);
806             ok(hres == S_OK,
807                "Non interface type disallowed SetIID(), hres %x\n", hres);
808           }
809           ok((sa->fFeatures & FADF_HAVEVARTYPE) == 0,
810              "Interface type %d should not have FADF_HAVEVARTYPE\n", vt);
811         }
812
813         hres = SafeArrayLock(sa);
814         ok(hres == S_OK, "Lock VARTYPE %d (@%d dimensions) failed; hres 0x%x\n",
815            vt, dimension, hres);
816
817         if (hres == S_OK)
818         {
819           hres = SafeArrayDestroy(sa);
820           ok(hres == DISP_E_ARRAYISLOCKED,"Destroy() got hres %x\n", hres);
821
822           hres = SafeArrayDestroyData(sa);
823           ok(hres == DISP_E_ARRAYISLOCKED,"DestroyData() got hres %x\n", hres);
824
825           hres = SafeArrayDestroyDescriptor(sa);
826           ok(hres == DISP_E_ARRAYISLOCKED,"DestroyDescriptor() got hres %x\n", hres);
827
828           hres = SafeArrayUnlock(sa);
829           ok(hres == S_OK, "Unlock VARTYPE %d (@%d dims) hres 0x%x\n",
830              vt, dimension, hres);
831
832           hres = SafeArrayDestroy(sa);
833           ok(hres == S_OK, "destroy VARTYPE %d (@%d dims) hres 0x%x\n",
834              vt, dimension, hres);
835         }
836       }
837     }
838   }
839 }
840
841 static void test_VectorCreateLockDestroy(void)
842 {
843   SAFEARRAY *sa;
844   HRESULT hres;
845   VARTYPE vt;
846   int element;
847
848   if (!pSafeArrayCreateVector)
849     return;
850   sa = pSafeArrayCreateVector(VT_UI1, 0, 0);
851   ok(sa != NULL, "SACV with 0 elements failed.\n");
852
853   hres = SafeArrayDestroy(sa);
854   ok(hres == S_OK, "SafeArrayDestroy failed with hres %x\n",hres);
855
856   /* Test all VARTYPES in different lengths */
857   for (element = 1; element <= 101; element += 10)
858   {
859     for (vt = VT_EMPTY; vt < VT_CLSID; vt++)
860     {
861       DWORD dwLen = SAFEARRAY_GetVTSize(vt);
862
863       sa = pSafeArrayCreateVector(vt, 0, element);
864
865       if (dwLen)
866         ok(sa != NULL, "VARTYPE %d (@%d elements) failed\n", vt, element);
867       else
868         ok(sa == NULL, "VARTYPE %d (@%d elements) succeeded!\n", vt, element);
869
870       if (sa)
871       {
872         ok(SafeArrayGetDim(sa) == 1, "VARTYPE %d (@%d elements) cDims %d, not 1\n",
873            vt, element, SafeArrayGetDim(sa));
874         ok(SafeArrayGetElemsize(sa) == dwLen,
875            "VARTYPE %d (@%d elements) cbElements is %d, expected %d\n",
876            vt, element, SafeArrayGetElemsize(sa), dwLen);
877
878         hres = SafeArrayLock(sa);
879         ok(hres == S_OK, "Lock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
880            vt, element, hres);
881
882         if (hres == S_OK)
883         {
884           hres = SafeArrayUnlock(sa);
885           ok(hres == S_OK, "Unlock VARTYPE %d (@%d elements) failed; hres 0x%x\n",
886              vt, element, hres);
887
888           hres = SafeArrayDestroy(sa);
889           ok(hres == S_OK, "destroy VARTYPE %d (@%d elements) failed; hres 0x%x\n",
890              vt, element, hres);
891         }
892       }
893     }
894   }
895 }
896
897 static void test_LockUnlock(void)
898 {
899   SAFEARRAYBOUND sab[4];
900   SAFEARRAY *sa;
901   HRESULT hres;
902   BOOL bVector = FALSE;
903   int dimension;
904
905   /* Failure cases */
906   hres = SafeArrayLock(NULL);
907   ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
908   hres = SafeArrayUnlock(NULL);
909   ok(hres == E_INVALIDARG, "Lock NULL array hres 0x%x\n", hres);
910
911   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
912   {
913     sab[dimension].lLbound = 0;
914     sab[dimension].cElements = 8;
915   }
916
917   sa = SafeArrayCreate(VT_UI1, NUM_DIMENSIONS, sab);
918
919   /* Test maximum locks */
920 test_LockUnlock_Vector:
921   if (sa)
922   {
923     int count = 0;
924
925     hres = SafeArrayUnlock(sa);
926     ok (hres == E_UNEXPECTED, "Bad %sUnlock gave hres 0x%x\n",
927         bVector ? "vector " : "\n", hres);
928
929     while ((hres = SafeArrayLock(sa)) == S_OK)
930       count++;
931     ok (count == 65535 && hres == E_UNEXPECTED, "Lock %sfailed at %d; hres 0x%x\n",
932         bVector ? "vector " : "\n", count, hres);
933
934     if (count == 65535 && hres == E_UNEXPECTED)
935     {
936       while ((hres = SafeArrayUnlock(sa)) == S_OK)
937         count--;
938       ok (count == 0 && hres == E_UNEXPECTED, "Unlock %sfailed at %d; hres 0x%x\n",
939           bVector ? "vector " : "\n", count, hres);
940     }
941
942     SafeArrayDestroy(sa);
943   }
944
945   if (bVector == FALSE && pSafeArrayCreateVector)
946   {
947     /* Test again with a vector */
948     sa = pSafeArrayCreateVector(VT_UI1, 0, 100);
949     bVector = TRUE;
950     goto test_LockUnlock_Vector;
951   }
952 }
953
954 static void test_SafeArrayGetPutElement(void)
955 {
956   SAFEARRAYBOUND sab[4];
957   LONG indices[NUM_DIMENSIONS];
958   SAFEARRAY *sa;
959   HRESULT hres;
960   int value = 0, gotvalue, dimension;
961   unsigned int x,y,z,a;
962
963   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
964   {
965     sab[dimension].lLbound = dimension * 2 + 1;
966     sab[dimension].cElements = dimension * 3 + 1;
967   }
968
969   sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
970   if (!sa)
971     return; /* Some early versions can't handle > 3 dims */
972
973   ok(sa->cbElements == sizeof(value), "int size mismatch\n");
974   if (sa->cbElements != sizeof(value))
975     return;
976
977   /* Failure cases */
978   for (x = 0; x < NUM_DIMENSIONS; x++)
979   {
980     indices[0] = sab[0].lLbound;
981     indices[1] = sab[1].lLbound;
982     indices[2] = sab[2].lLbound;
983     indices[3] = sab[3].lLbound;
984
985     indices[x] = indices[x] - 1;
986     hres = SafeArrayPutElement(sa, indices, &value);
987     ok(hres == DISP_E_BADINDEX, "Put allowed too small index in dimension %d\n", x);
988     hres = SafeArrayGetElement(sa, indices, &value);
989     ok(hres == DISP_E_BADINDEX, "Get allowed too small index in dimension %d\n", x);
990
991     indices[x] = sab[x].lLbound + sab[x].cElements;
992     hres = SafeArrayPutElement(sa, indices, &value);
993     ok(hres == DISP_E_BADINDEX, "Put allowed too big index in dimension %d\n", x);
994     hres = SafeArrayGetElement(sa, indices, &value);
995     ok(hres == DISP_E_BADINDEX, "Get allowed too big index in dimension %d\n", x);
996   }
997
998   indices[0] = sab[0].lLbound;
999   indices[1] = sab[1].lLbound;
1000   indices[2] = sab[2].lLbound;
1001   indices[3] = sab[3].lLbound;
1002
1003   hres = SafeArrayPutElement(NULL, indices, &value);
1004   ok(hres == E_INVALIDARG, "Put NULL array hres 0x%x\n", hres);
1005   hres = SafeArrayGetElement(NULL, indices, &value);
1006   ok(hres == E_INVALIDARG, "Get NULL array hres 0x%x\n", hres);
1007
1008   hres = SafeArrayPutElement(sa, NULL, &value);
1009   ok(hres == E_INVALIDARG, "Put NULL indices hres 0x%x\n", hres);
1010   hres = SafeArrayGetElement(sa, NULL, &value);
1011   ok(hres == E_INVALIDARG, "Get NULL indices hres 0x%x\n", hres);
1012
1013   if (0)
1014   {
1015   /* This is retarded. Windows checks every case of invalid parameters
1016    * except the following, which crashes. We ERR this in Wine.
1017    */
1018   hres = SafeArrayPutElement(sa, indices, NULL);
1019   ok(hres == E_INVALIDARG, "Put NULL value hres 0x%x\n", hres);
1020   }
1021
1022   hres = SafeArrayGetElement(sa, indices, NULL);
1023   ok(hres == E_INVALIDARG, "Get NULL value hres 0x%x\n", hres);
1024
1025   value = 0;
1026
1027   /* Make sure we can read and get back the correct values in 4 dimensions,
1028    * Each with a different size and lower bound.
1029    */
1030   for (x = 0; x < sab[0].cElements; x++)
1031   {
1032     indices[0] = sab[0].lLbound + x;
1033     for (y = 0; y < sab[1].cElements; y++)
1034     {
1035       indices[1] = sab[1].lLbound + y;
1036       for (z = 0; z < sab[2].cElements; z++)
1037       {
1038         indices[2] = sab[2].lLbound + z;
1039         for (a = 0; a < sab[3].cElements; a++)
1040         {
1041           indices[3] = sab[3].lLbound + a;
1042           hres = SafeArrayPutElement(sa, indices, &value);
1043           ok(hres == S_OK, "Failed to put element at (%d,%d,%d,%d) hres 0x%x\n",
1044              x, y, z, a, hres);
1045           value++;
1046         }
1047       }
1048     }
1049   }
1050
1051   value = 0;
1052
1053   for (x = 0; x < sab[0].cElements; x++)
1054   {
1055     indices[0] = sab[0].lLbound + x;
1056     for (y = 0; y < sab[1].cElements; y++)
1057     {
1058       indices[1] = sab[1].lLbound + y;
1059       for (z = 0; z < sab[2].cElements; z++)
1060       {
1061         indices[2] = sab[2].lLbound + z;
1062         for (a = 0; a < sab[3].cElements; a++)
1063         {
1064           indices[3] = sab[3].lLbound + a;
1065           gotvalue = value / 3;
1066           hres = SafeArrayGetElement(sa, indices, &gotvalue);
1067           ok(hres == S_OK, "Failed to get element at (%d,%d,%d,%d) hres 0x%x\n",
1068              x, y, z, a, hres);
1069           if (hres == S_OK)
1070             ok(value == gotvalue, "Got value %d instead of %d at (%d,%d,%d,%d)\n",
1071                gotvalue, value, x, y, z, a);
1072           value++;
1073         }
1074       }
1075     }
1076   }
1077   SafeArrayDestroy(sa);
1078 }
1079
1080 static void test_SafeArrayGetPutElement_BSTR(void)
1081 {
1082   SAFEARRAYBOUND sab;
1083   LONG indices[1];
1084   SAFEARRAY *sa;
1085   HRESULT hres;
1086   BSTR value = 0, gotvalue;
1087   const OLECHAR szTest[5] = { 'T','e','s','t','\0' };
1088
1089   sab.lLbound = 1;
1090   sab.cElements = 1;
1091
1092   sa = SafeArrayCreate(VT_BSTR, 1, &sab);
1093   ok(sa != NULL, "BSTR test couldn't create array\n");
1094   if (!sa)
1095     return;
1096
1097   ok(sa->cbElements == sizeof(BSTR), "BSTR size mismatch\n");
1098   if (sa->cbElements != sizeof(BSTR))
1099     return;
1100
1101   indices[0] = sab.lLbound;
1102   value = SysAllocString(szTest);
1103   ok (value != NULL, "Expected non-NULL\n");
1104   hres = SafeArrayPutElement(sa, indices, value);
1105   ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1106   gotvalue = NULL;
1107   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1108   ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1109   if (hres == S_OK)
1110     ok(SysStringLen(value) == SysStringLen(gotvalue), "Got len %d instead of %d\n", SysStringLen(gotvalue), SysStringLen(value));
1111   SafeArrayDestroy(sa);
1112   SysFreeString(value);
1113   SysFreeString(gotvalue);
1114 }
1115
1116 static int tunk_xref = 0;
1117 static HRESULT WINAPI tunk_QueryInterface(LPUNKNOWN punk,REFIID riid, LPVOID *x) {
1118         return E_FAIL;
1119 }
1120 static ULONG WINAPI tunk_AddRef(LPUNKNOWN punk) {
1121         return ++tunk_xref;
1122 }
1123
1124 static ULONG WINAPI tunk_Release(LPUNKNOWN punk) {
1125         return --tunk_xref;
1126 }
1127
1128 static const IUnknownVtbl xtunk_vtbl = {
1129         tunk_QueryInterface,
1130         tunk_AddRef,
1131         tunk_Release
1132 };
1133
1134 static struct xtunk_iface {
1135         const IUnknownVtbl *lpvtbl;
1136 } xtunk_iface;
1137
1138
1139 static void test_SafeArrayGetPutElement_IUnknown(void)
1140 {
1141   SAFEARRAYBOUND sab;
1142   LONG indices[1];
1143   SAFEARRAY *sa;
1144   HRESULT hres;
1145   LPUNKNOWN value = 0, gotvalue;
1146
1147   sab.lLbound = 1;
1148   sab.cElements = 1;
1149   sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1150   ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1151   if (!sa)
1152     return;
1153
1154   ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1155   if (sa->cbElements != sizeof(LPUNKNOWN))
1156     return;
1157
1158   indices[0] = sab.lLbound;
1159   xtunk_iface.lpvtbl = &xtunk_vtbl;
1160   value = (LPUNKNOWN)&xtunk_iface;
1161   tunk_xref = 1;
1162   ok (value != NULL, "Expected non-NULL\n");
1163   hres = SafeArrayPutElement(sa, indices, value);
1164   ok(hres == S_OK, "Failed to put bstr element hres 0x%x\n", hres);
1165   ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1166   gotvalue = NULL;
1167   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1168   ok(tunk_xref == 3,"Failed to increment refcount of iface.\n");
1169   ok(hres == S_OK, "Failed to get bstr element at hres 0x%x\n", hres);
1170   if (hres == S_OK)
1171     ok(value == gotvalue, "Got %p instead of %p\n", gotvalue, value);
1172   SafeArrayDestroy(sa);
1173   ok(tunk_xref == 2,"Failed to decrement refcount of iface.\n");
1174 }
1175
1176 static void test_SafeArrayRedim_IUnknown(void)
1177 {
1178   SAFEARRAYBOUND sab;
1179   LONG indices[1];
1180   SAFEARRAY *sa;
1181   HRESULT hres;
1182   LPUNKNOWN value;
1183
1184   sab.lLbound = 1;
1185   sab.cElements = 2;
1186   sa = SafeArrayCreate(VT_UNKNOWN, 1, &sab);
1187   ok(sa != NULL, "UNKNOWN test couldn't create array\n");
1188   if (!sa)
1189     return;
1190
1191   ok(sa->cbElements == sizeof(LPUNKNOWN), "LPUNKNOWN size mismatch\n");
1192   if (sa->cbElements != sizeof(LPUNKNOWN))
1193     return;
1194
1195   indices[0] = 2;
1196   xtunk_iface.lpvtbl = &xtunk_vtbl;
1197   value = (LPUNKNOWN)&xtunk_iface;
1198   tunk_xref = 1;
1199   hres = SafeArrayPutElement(sa, indices, value);
1200   ok(hres == S_OK, "Failed to put IUnknown element hres 0x%x\n", hres);
1201   ok(tunk_xref == 2,"Failed to increment refcount of iface.\n");
1202   sab.cElements = 1;
1203   hres = SafeArrayRedim(sa, &sab);
1204   ok(hres == S_OK, "Failed to shrink array hres 0x%x\n", hres);
1205   ok(tunk_xref == 1, "Failed to decrement refcount\n");
1206   SafeArrayDestroy(sa);
1207 }
1208
1209 static void test_SafeArrayGetPutElement_VARIANT(void)
1210 {
1211   SAFEARRAYBOUND sab;
1212   LONG indices[1];
1213   SAFEARRAY *sa;
1214   HRESULT hres;
1215   VARIANT value, gotvalue;
1216
1217   sab.lLbound = 1;
1218   sab.cElements = 1;
1219   sa = SafeArrayCreate(VT_VARIANT, 1, &sab);
1220   ok(sa != NULL, "VARIANT test couldn't create array\n");
1221   if (!sa)
1222     return;
1223
1224   ok(sa->cbElements == sizeof(VARIANT), "VARIANT size mismatch\n");
1225   if (sa->cbElements != sizeof(VARIANT))
1226     return;
1227
1228   indices[0] = sab.lLbound;
1229   V_VT(&value) = VT_I4;
1230   V_I4(&value) = 0x42424242;
1231   hres = SafeArrayPutElement(sa, indices, &value);
1232   ok(hres == S_OK, "Failed to put Variant I4 element hres 0x%x\n", hres);
1233
1234   V_VT(&gotvalue) = 0xdead;
1235   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1236   ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1237
1238   V_VT(&gotvalue) = VT_EMPTY;
1239   hres = SafeArrayGetElement(sa, indices, &gotvalue);
1240   ok(hres == S_OK, "Failed to get variant element at hres 0x%x\n", hres);
1241   if (hres == S_OK) {
1242     ok(V_VT(&value) == V_VT(&gotvalue), "Got type 0x%x instead of 0x%x\n", V_VT(&value), V_VT(&gotvalue));
1243     if (V_VT(&value) == V_VT(&gotvalue))
1244         ok(V_I4(&value) == V_I4(&gotvalue), "Got %d instead of %d\n", V_I4(&value), V_VT(&gotvalue));
1245   }
1246   SafeArrayDestroy(sa);
1247 }
1248
1249
1250 static void test_SafeArrayCopyData(void)
1251 {
1252   SAFEARRAYBOUND sab[4];
1253   SAFEARRAY *sa;
1254   SAFEARRAY *sacopy;
1255   HRESULT hres;
1256   int dimension,size=1;
1257
1258   if (!pSafeArrayCopyData)
1259     return;
1260
1261   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1262   {
1263     sab[dimension].lLbound = dimension * 2 + 2;
1264     sab[dimension].cElements = dimension * 3 + 1;
1265     size *= sab[dimension].cElements;
1266   }
1267
1268   sa = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1269   ok(sa != NULL, "Copy test couldn't create array\n");
1270   sacopy = SafeArrayCreate(VT_INT, NUM_DIMENSIONS, sab);
1271   ok(sacopy != NULL, "Copy test couldn't create copy array\n");
1272
1273   if (!sa || !sacopy)
1274     return;
1275
1276   ok(sa->cbElements == sizeof(int), "int size mismatch\n");
1277   if (sa->cbElements != sizeof(int))
1278     return;
1279
1280   /* Fill the source array with some data; it doesn't matter what */
1281   for (dimension = 0; dimension < size; dimension++)
1282   {
1283     int* data = (int*)sa->pvData;
1284     data[dimension] = dimension;
1285   }
1286
1287   hres = pSafeArrayCopyData(sa, sacopy);
1288   ok(hres == S_OK, "copy data failed hres 0x%x\n", hres);
1289   if (hres == S_OK)
1290   {
1291     ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1292   }
1293
1294   /* Failure cases */
1295   hres = pSafeArrayCopyData(NULL, sacopy);
1296   ok(hres == E_INVALIDARG, "Null copy source hres 0x%x\n", hres);
1297   hres = pSafeArrayCopyData(sa, NULL);
1298   ok(hres == E_INVALIDARG, "Null copy hres 0x%x\n", hres);
1299
1300   sacopy->rgsabound[0].cElements += 1;
1301   hres = pSafeArrayCopyData(sa, sacopy);
1302   ok(hres == E_INVALIDARG, "Bigger copy first dimension hres 0x%x\n", hres);
1303
1304   sacopy->rgsabound[0].cElements -= 2;
1305   hres = pSafeArrayCopyData(sa, sacopy);
1306   ok(hres == E_INVALIDARG, "Smaller copy first dimension hres 0x%x\n", hres);
1307   sacopy->rgsabound[0].cElements += 1;
1308
1309   sacopy->rgsabound[3].cElements += 1;
1310   hres = pSafeArrayCopyData(sa, sacopy);
1311   ok(hres == E_INVALIDARG, "Bigger copy last dimension hres 0x%x\n", hres);
1312
1313   sacopy->rgsabound[3].cElements -= 2;
1314   hres = pSafeArrayCopyData(sa, sacopy);
1315   ok(hres == E_INVALIDARG, "Smaller copy last dimension hres 0x%x\n", hres);
1316   sacopy->rgsabound[3].cElements += 1;
1317
1318   SafeArrayDestroy(sacopy);
1319   sacopy = NULL;
1320   hres = pSafeArrayCopyData(sa, sacopy);
1321   ok(hres == E_INVALIDARG, "->Null copy hres 0x%x\n", hres);
1322
1323   hres = SafeArrayCopy(sa, &sacopy);
1324   ok(hres == S_OK, "copy failed hres 0x%x\n", hres);
1325   if (hres == S_OK)
1326   {
1327     ok(SafeArrayGetElemsize(sa) == SafeArrayGetElemsize(sacopy),"elemsize wrong\n");
1328     ok(SafeArrayGetDim(sa) == SafeArrayGetDim(sacopy),"dimensions wrong\n");
1329     ok(!memcmp(sa->pvData, sacopy->pvData, size * sizeof(int)), "compared different\n");
1330   }
1331
1332   SafeArrayDestroy(sa);
1333 }
1334
1335 static void test_SafeArrayCreateEx(void)
1336 {
1337   IRecordInfoImpl* iRec;
1338   SAFEARRAYBOUND sab[4];
1339   SAFEARRAY *sa;
1340   HRESULT hres;
1341   int dimension;
1342
1343   if (!pSafeArrayCreateEx)
1344     return;
1345
1346   for (dimension = 0; dimension < NUM_DIMENSIONS; dimension++)
1347   {
1348     sab[dimension].lLbound = 0;
1349     sab[dimension].cElements = 8;
1350   }
1351
1352   /* Failure cases */
1353   sa = pSafeArrayCreateEx(VT_UI1, 1, NULL, NULL);
1354   ok(sa == NULL, "CreateEx NULL bounds didn't fail\n");
1355
1356   /* test IID storage & defaulting */
1357   sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, (PVOID)&IID_ITypeInfo);
1358   ok(sa != NULL, "CreateEx (ITypeInfo) failed\n");
1359
1360   if (sa)
1361   {
1362     GUID guid;
1363     if (pSafeArrayGetIID)
1364     {
1365       hres = pSafeArrayGetIID(sa, &guid);
1366       ok(hres == S_OK, "CreateEx (ITypeInfo) no IID hres 0x%x\n", hres);
1367       if (hres == S_OK)
1368       {
1369         ok(IsEqualGUID(&guid, &IID_ITypeInfo), "CreateEx (ITypeInfo) bad IID\n");
1370       }
1371     }
1372     if (pSafeArraySetIID)
1373     {
1374       hres = pSafeArraySetIID(sa, &IID_IUnknown);
1375       ok(hres == S_OK, "Failed to set IID, hres = %8x\n", hres);
1376       if (hres == S_OK && pSafeArrayGetIID)
1377       {
1378         hres = pSafeArrayGetIID(sa, &guid);
1379         ok(hres == S_OK && IsEqualGUID(&guid, &IID_IUnknown), "Set bad IID\n");
1380       }
1381     }
1382     SafeArrayDestroy(sa);
1383   }
1384
1385   sa = pSafeArrayCreateEx(VT_DISPATCH, 1, sab, NULL);
1386   ok(sa != NULL, "CreateEx (NULL) failed\n");
1387
1388   if (sa)
1389   {
1390     GUID guid;
1391     if (pSafeArrayGetIID)
1392     {
1393       hres = pSafeArrayGetIID(sa, &guid);
1394       ok(hres == S_OK, "CreateEx (NULL) no IID hres 0x%x\n", hres);
1395       if (hres == S_OK)
1396       {
1397         ok(IsEqualGUID(&guid, &IID_IDispatch), "CreateEx (NULL) bad IID\n");
1398       }
1399     }
1400     SafeArrayDestroy(sa);
1401   }
1402
1403   sa = pSafeArrayCreateEx(VT_UNKNOWN, 1, sab, NULL);
1404   ok(sa != NULL, "CreateEx (NULL-Unk) failed\n");
1405
1406   if (sa)
1407   {
1408     GUID guid;
1409     if (pSafeArrayGetIID)
1410     {
1411       hres = pSafeArrayGetIID(sa, &guid);
1412       ok(hres == S_OK, "CreateEx (NULL-Unk) no IID hres 0x%x\n", hres);
1413       if (hres == S_OK)
1414       {
1415         ok(IsEqualGUID(&guid, &IID_IUnknown), "CreateEx (NULL-Unk) bad IID\n");
1416       }
1417     }
1418     SafeArrayDestroy(sa);
1419   }
1420
1421   /* VT_RECORD failure case */
1422   sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, NULL);
1423   ok(sa == NULL, "CreateEx (NULL-Rec) succeded\n");
1424
1425   iRec = IRecordInfoImpl_Construct();
1426
1427   /* Win32 doesn't care if GetSize fails */
1428   fail_GetSize = TRUE;
1429   sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, (LPVOID)iRec);
1430   ok(sa != NULL, "CreateEx (Fail Size) failed\n");
1431   ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1432   ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1433   ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1434   if (sa)
1435   {
1436     ok(sa->cbElements == RECORD_SIZE_FAIL, "Altered size to %d\n", sa->cbElements);
1437     SafeArrayDestroy(sa);
1438     ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1439   }
1440
1441   /* Test VT_RECORD array */
1442   fail_GetSize = FALSE;
1443   iRec->ref = START_REF_COUNT;
1444   iRec->sizeCalled = 0;
1445   iRec->clearCalled = 0;
1446   sa = pSafeArrayCreateEx(VT_RECORD, 1, sab, (LPVOID)iRec);
1447   ok(sa != NULL, "CreateEx (Rec) failed\n");
1448   ok(iRec->ref == START_REF_COUNT + 1, "Wrong iRec refcount %d\n", iRec->ref);
1449   ok(iRec->sizeCalled == 1, "GetSize called %d times\n", iRec->sizeCalled);
1450   ok(iRec->clearCalled == 0, "Clear called %d times\n", iRec->clearCalled);
1451   if (sa && pSafeArrayGetRecordInfo)
1452   {
1453     IRecordInfo* saRec = NULL;
1454     hres = pSafeArrayGetRecordInfo(sa, &saRec);
1455
1456     ok(hres == S_OK,"GRI failed\n");
1457     ok(saRec == (IRecordInfo*)iRec,"Different saRec\n");
1458     ok(iRec->ref == START_REF_COUNT + 2, "Didn't AddRef %d\n", iRec->ref);
1459     if (iRec->ref == START_REF_COUNT + 2)
1460       IRecordInfo_Release(saRec);
1461
1462     ok(sa->cbElements == RECORD_SIZE,"Elemsize is %d\n", sa->cbElements);
1463
1464     SafeArrayDestroy(sa);
1465     ok(iRec->sizeCalled == 1, "Destroy->GetSize called %d times\n", iRec->sizeCalled);
1466     ok(iRec->clearCalled == sab[0].cElements, "Destroy->Clear called %d times\n", iRec->clearCalled);
1467     ok(iRec->ref == START_REF_COUNT, "Wrong iRec refcount %d\n", iRec->ref);
1468   }
1469 }
1470
1471 static void test_SafeArrayClear(void)
1472 {
1473   SAFEARRAYBOUND sab;
1474   SAFEARRAY *sa;
1475   VARIANTARG v;
1476   HRESULT hres;
1477
1478   sab.lLbound = 0;
1479   sab.cElements = 10;
1480   sa = SafeArrayCreate(VT_UI1, 1, &sab);
1481   ok(sa != NULL, "Create() failed.\n");
1482   if (!sa)
1483     return;
1484
1485   /* Test clearing non-NULL variants containing arrays */
1486   V_VT(&v) = VT_ARRAY|VT_UI1;
1487   V_ARRAY(&v) = sa;
1488   hres = VariantClear(&v);
1489   ok(hres == S_OK && V_VT(&v) == VT_EMPTY, "VariantClear: hres 0x%x, Type %d\n", hres, V_VT(&v));
1490   ok(V_ARRAY(&v) == sa, "VariantClear: Overwrote value\n");
1491
1492   sa = SafeArrayCreate(VT_UI1, 1, &sab);
1493   ok(sa != NULL, "Create() failed.\n");
1494   if (!sa)
1495     return;
1496
1497   V_VT(&v) = VT_SAFEARRAY;
1498   V_ARRAY(&v) = sa;
1499   hres = VariantClear(&v);
1500   ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1501
1502   V_VT(&v) = VT_SAFEARRAY|VT_BYREF;
1503   V_ARRAYREF(&v) = &sa;
1504   hres = VariantClear(&v);
1505   ok(hres == DISP_E_BADVARTYPE, "VariantClear: hres 0x%x\n", hres);
1506
1507   SafeArrayDestroy(sa);
1508 }
1509
1510 static void test_SafeArrayCopy(void)
1511 {
1512   SAFEARRAYBOUND sab;
1513   SAFEARRAY *sa, *sa2;
1514   VARIANTARG vSrc, vDst;
1515   HRESULT hres;
1516
1517   sab.lLbound = 0;
1518   sab.cElements = 10;
1519   sa = SafeArrayCreate(VT_UI1, 1, &sab);
1520   ok(sa != NULL, "Create() failed.\n");
1521   if (!sa)
1522     return;
1523
1524   /* Test copying non-NULL variants containing arrays */
1525   V_VT(&vSrc) = (VT_ARRAY|VT_BYREF|VT_UI1);
1526   V_ARRAYREF(&vSrc) = &sa;
1527   V_VT(&vDst) = VT_EMPTY;
1528
1529   hres = VariantCopy(&vDst, &vSrc);
1530   ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_BYREF|VT_UI1),
1531      "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1532   ok(V_ARRAYREF(&vDst) == &sa, "VariantClear: Performed deep copy\n");
1533
1534   V_VT(&vSrc) = (VT_ARRAY|VT_UI1);
1535   V_ARRAY(&vSrc) = sa;
1536   V_VT(&vDst) = VT_EMPTY;
1537
1538   hres = VariantCopy(&vDst, &vSrc);
1539   ok(hres == S_OK && V_VT(&vDst) == (VT_ARRAY|VT_UI1),
1540      "VariantCopy: hres 0x%x, Type %d\n", hres, V_VT(&vDst));
1541   ok(V_ARRAY(&vDst) != sa, "VariantClear: Performed shallow copy\n");
1542
1543   SafeArrayDestroy(V_ARRAY(&vSrc));
1544   SafeArrayDestroy(V_ARRAY(&vDst));
1545
1546   hres = SafeArrayAllocDescriptor(1, &sa);
1547   ok(hres == S_OK, "SafeArrayAllocDescriptor failed with error 0x%08x\n", hres);
1548
1549   hres = SafeArrayCopy(sa, &sa2);
1550   ok(hres == E_INVALIDARG,
1551     "SafeArrayCopy with empty array should have failed with error E_INVALIDARG instead of 0x%08x\n",
1552     hres);
1553   sa->cbElements = 16;
1554   hres = SafeArrayCopy(sa, &sa2);
1555   ok(hres == S_OK, "SafeArrayCopy failed with error 0x%08x\n", hres);
1556
1557   SafeArrayDestroy(sa);
1558 }
1559
1560 #define MKARRAY(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1561   sa = SafeArrayCreate(typ, 1, &sab); ok(sa != NULL, "Create() failed.\n"); \
1562   if (!sa) return; \
1563   V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1564
1565 #define MKARRAYCONT(low,num,typ) sab.lLbound = low; sab.cElements = num; \
1566   sa = SafeArrayCreate(typ, 1, &sab); if (!sa) continue; \
1567   V_VT(&v) = VT_ARRAY|typ; V_ARRAY(&v) = sa; VariantInit(&v2)
1568
1569 static void test_SafeArrayChangeTypeEx(void)
1570 {
1571   static const char *szHello = "Hello World";
1572   SAFEARRAYBOUND sab;
1573   SAFEARRAY *sa;
1574   VARIANTARG v,v2;
1575   VARTYPE vt;
1576   HRESULT hres;
1577
1578   /* VT_ARRAY|VT_UI1 -> VT_BSTR */
1579   MKARRAY(0,strlen(szHello)+1,VT_UI1);
1580   memcpy(sa->pvData, szHello, strlen(szHello)+1);
1581
1582   hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1583   ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1584   if (hres == S_OK)
1585   {
1586     ok(V_VT(&v2) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v2));
1587     ok(strcmp((char*)V_BSTR(&v2),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1588        (char*)V_BSTR(&v2));
1589     VariantClear(&v2);
1590   }
1591
1592   /* VT_VECTOR|VT_UI1 -> VT_BSTR */
1593   SafeArrayDestroy(sa);
1594   if (pSafeArrayCreateVector)
1595   {
1596     sa = pSafeArrayCreateVector(VT_UI1, 0, strlen(szHello)+1);
1597     ok(sa != NULL, "CreateVector() failed.\n");
1598     if (!sa)
1599       return;
1600
1601     memcpy(sa->pvData, szHello, strlen(szHello)+1);
1602     V_VT(&v) = VT_VECTOR|VT_UI1;
1603     V_ARRAY(&v) = sa;
1604     VariantInit(&v2);
1605
1606     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1607     ok(hres == DISP_E_BADVARTYPE, "CTE VT_VECTOR|VT_UI1 returned %x\n", hres);
1608
1609     /* (vector)VT_ARRAY|VT_UI1 -> VT_BSTR (In place) */
1610     V_VT(&v) = VT_ARRAY|VT_UI1;
1611     hres = VariantChangeTypeEx(&v, &v, 0, 0, VT_BSTR);
1612     ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR failed with %x\n", hres);
1613     if (hres == S_OK)
1614     {
1615       ok(V_VT(&v) == VT_BSTR, "CTE VT_ARRAY|VT_UI1 -> VT_BSTR did not return VT_BSTR, but %d.\n",V_VT(&v));
1616       ok(strcmp((char*)V_BSTR(&v),szHello) == 0,"Expected string '%s', got '%s'\n", szHello,
1617               (char*)V_BSTR(&v));
1618       VariantClear(&v);
1619     }
1620   }
1621
1622   /* To/from BSTR only works with arrays of VT_UI1 */
1623   for (vt = 0; vt <= VT_CLSID; vt++)
1624   {
1625     if (vt == VT_UI1)
1626       continue;
1627
1628     MKARRAYCONT(0,1,vt);
1629     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_BSTR);
1630     ok(hres != S_OK, "CTE VT_ARRAY|VT %d->BSTR succeeded\n", vt);
1631     VariantClear(&v2);
1632     VariantClear(&v);
1633   }
1634
1635   /* Can't change an array of one type into array of another type , even
1636    * if the other type is the same size
1637    */
1638   if (pSafeArrayCreateVector)
1639   {
1640     sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1641     ok(sa != NULL, "CreateVector() failed.\n");
1642     if (!sa)
1643       return;
1644
1645     V_VT(&v) = VT_ARRAY|VT_UI1;
1646     V_ARRAY(&v) = sa;
1647     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_I1);
1648     ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_I1 returned %x\n", hres);
1649
1650     /* But can change to the same array type */
1651     SafeArrayDestroy(sa);
1652     sa = pSafeArrayCreateVector(VT_UI1, 0, 1);
1653     ok(sa != NULL, "CreateVector() failed.\n");
1654     if (!sa)
1655       return;
1656     V_VT(&v) = VT_ARRAY|VT_UI1;
1657     V_ARRAY(&v) = sa;
1658     hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_ARRAY|VT_UI1);
1659     ok(hres == S_OK, "CTE VT_ARRAY|VT_UI1->VT_ARRAY|VT_UI1 returned %x\n", hres);
1660     SafeArrayDestroy(sa);
1661   }
1662
1663   /* NULL/EMPTY */
1664   MKARRAY(0,1,VT_UI1);
1665   hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_NULL);
1666   ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1667   MKARRAY(0,1,VT_UI1);
1668   hres = VariantChangeTypeEx(&v2, &v, 0, 0, VT_EMPTY);
1669   ok(hres == DISP_E_TYPEMISMATCH, "CTE VT_ARRAY|VT_UI1 returned %x\n", hres);
1670
1671 }
1672
1673 static void test_SafeArrayDestroyData (void)
1674 {
1675   SAFEARRAYBOUND sab;
1676   SAFEARRAY *sa;
1677   HRESULT hres;
1678   int value = 0xdeadbeef;
1679   LONG index[1];
1680   void HUGEP *temp_pvData;
1681
1682   sab.lLbound = 0;
1683   sab.cElements = 10;
1684   sa = SafeArrayCreate(VT_INT, 1, &sab);
1685   ok(sa != NULL, "Create() failed.\n");
1686   if (!sa)
1687     return;
1688   index[0] = 1;
1689   SafeArrayPutElement (sa, index, &value);
1690
1691 /* SafeArrayDestroyData shouldn't free pvData if FADF_STATIC is set. */
1692   sa->fFeatures |= FADF_STATIC;
1693   temp_pvData = sa->pvData;
1694   hres = SafeArrayDestroyData(sa);
1695   ok(hres == S_OK, "SADData FADF_STATIC failed, error code %x.\n",hres);
1696   ok(sa->pvData == temp_pvData, "SADData FADF_STATIC: pvData=%p, expected %p (fFeatures = %d).\n",
1697     sa->pvData, temp_pvData, sa->fFeatures);
1698   SafeArrayGetElement (sa, index, &value);
1699   ok(value == 0, "Data not cleared after SADData\n");
1700
1701 /* Clear FADF_STATIC, now really destroy the data. */
1702   sa->fFeatures ^= FADF_STATIC;
1703   hres = SafeArrayDestroyData(sa);
1704   ok(hres == S_OK, "SADData !FADF_STATIC failed, error code %x.\n",hres);
1705   ok(sa->pvData == NULL, "SADData !FADF_STATIC: pvData=%p, expected NULL.\n", sa->pvData);
1706
1707   hres = SafeArrayDestroy(sa);
1708   ok(hres == S_OK, "SAD failed, error code %x.\n", hres);
1709 }
1710
1711 START_TEST(safearray)
1712 {
1713     hOleaut32 = GetModuleHandleA("oleaut32.dll");
1714
1715     GETPTR(SafeArrayAllocDescriptorEx);
1716     GETPTR(SafeArrayCopyData);
1717     GETPTR(SafeArrayGetIID);
1718     GETPTR(SafeArraySetIID);
1719     GETPTR(SafeArrayGetVartype);
1720     GETPTR(SafeArrayCreateEx);
1721     GETPTR(SafeArrayCreateVector);
1722
1723     check_for_VT_INT_PTR();
1724     test_safearray();
1725     test_SafeArrayAllocDestroyDescriptor();
1726     test_SafeArrayCreateLockDestroy();
1727     test_VectorCreateLockDestroy();
1728     test_LockUnlock();
1729     test_SafeArrayChangeTypeEx();
1730     test_SafeArrayCopy();
1731     test_SafeArrayClear();
1732     test_SafeArrayCreateEx();
1733     test_SafeArrayCopyData();
1734     test_SafeArrayDestroyData();
1735     test_SafeArrayGetPutElement();
1736     test_SafeArrayGetPutElement_BSTR();
1737     test_SafeArrayGetPutElement_IUnknown();
1738     test_SafeArrayRedim_IUnknown();
1739     test_SafeArrayGetPutElement_VARIANT();
1740 }