Reverse the order for deleting the items in resetcontent to correctly
[wine] / dlls / msvcrt / tests / cpp.c
1 /* Unit test suite for msvcrt C++ objects
2  *
3  * Copyright 2003 Jon Griffiths
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * NOTES
20  * This tests is only valid for ix86 platforms, on others it's a no-op.
21  * Some tests cannot be checked with ok(), for example the dtors. We simply
22  * call them to ensure we don't crash ;-)
23  *
24  * If we build this test with VC++ in debug mode, we will fail in _chkstk()
25  * or at program exit malloc() checking if these methods haven't been
26  * implemented correctly (they have).
27  *
28  * Tested with a range of native msvcrt's from v4 -> v7.
29  */
30 #include "wine/test.h"
31 #include "winbase.h"
32 #include "winnt.h"
33
34 #ifndef __i386__
35 /* Skip these tests for non x86 platforms */
36 START_TEST(cpp)
37 {
38 }
39 #else
40
41 typedef struct __exception
42 {
43   void *vtable;
44   char *name;
45   int   do_free;
46 } exception;
47
48 typedef struct __type_info
49 {
50   void *vtable;
51   char *name;
52   char  mangled[16];
53 } type_info;
54
55 /* Function pointers. We need to use these to call these funcs as __thiscall */
56 static HMODULE hMsvcrt;
57
58 static void* (*poperator_new)(unsigned int);
59 static void  (*poperator_delete)(void*);
60 static void* (*pmalloc)(unsigned int);
61 static void  (*pfree)(void*);
62
63 /* exception */
64 static void (WINAPI *pexception_ctor)(exception*,LPCSTR*);
65 static void (WINAPI *pexception_copy_ctor)(exception*,exception*);
66 static void (WINAPI *pexception_default_ctor)(exception*);
67 static void (WINAPI *pexception_dtor)(exception*);
68 static exception* (WINAPI *pexception_opequals)(exception*,exception*);
69 static char* (WINAPI *pexception_what)(exception*);
70 static void* (WINAPI *pexception_vtable)(exception*);
71 static void (WINAPI *pexception_vector_dtor)(exception*,unsigned int);
72 static void (WINAPI *pexception_scalar_dtor)(exception*,unsigned int);
73
74 /* bad_typeid */
75 static void (WINAPI *pbad_typeid_ctor)(exception*,LPCSTR);
76 static void (WINAPI *pbad_typeid_ctor_closure)(exception*);
77 static void (WINAPI *pbad_typeid_copy_ctor)(exception*,exception*);
78 static void (WINAPI *pbad_typeid_dtor)(exception*);
79 static exception* (WINAPI *pbad_typeid_opequals)(exception*,exception*);
80 static char* (WINAPI *pbad_typeid_what)(exception*);
81 static void* (WINAPI *pbad_typeid_vtable)(exception*);
82 static void (WINAPI *pbad_typeid_vector_dtor)(exception*,unsigned int);
83 static void (WINAPI *pbad_typeid_scalar_dtor)(exception*,unsigned int);
84
85 /* bad_cast */
86 static void (WINAPI *pbad_cast_ctor)(exception*,LPCSTR*);
87 static void (WINAPI *pbad_cast_ctor2)(exception*,LPCSTR);
88 static void (WINAPI *pbad_cast_ctor_closure)(exception*);
89 static void (WINAPI *pbad_cast_copy_ctor)(exception*,exception*);
90 static void (WINAPI *pbad_cast_dtor)(exception*);
91 static exception* (WINAPI *pbad_cast_opequals)(exception*,exception*);
92 static char* (WINAPI *pbad_cast_what)(exception*);
93 static void* (WINAPI *pbad_cast_vtable)(exception*);
94 static void (WINAPI *pbad_cast_vector_dtor)(exception*,unsigned int);
95 static void (WINAPI *pbad_cast_scalar_dtor)(exception*,unsigned int);
96
97 /* __non_rtti_object */
98 static void (WINAPI *p__non_rtti_object_ctor)(exception*,LPCSTR);
99 static void (WINAPI *p__non_rtti_object_copy_ctor)(exception*,exception*);
100 static void (WINAPI *p__non_rtti_object_dtor)(exception*);
101 static exception* (WINAPI *p__non_rtti_object_opequals)(exception*,exception*);
102 static char* (WINAPI *p__non_rtti_object_what)(exception*);
103 static void* (WINAPI *p__non_rtti_object_vtable)(exception*);
104 static void (WINAPI *p__non_rtti_object_vector_dtor)(exception*,unsigned int);
105 static void (WINAPI *p__non_rtti_object_scalar_dtor)(exception*,unsigned int);
106
107 /* type_info */
108 static void  (WINAPI *ptype_info_dtor)(type_info*);
109 static char* (WINAPI *ptype_info_raw_name)(type_info*);
110 static char* (WINAPI *ptype_info_name)(type_info*);
111 static int   (WINAPI *ptype_info_before)(type_info*,type_info*);
112 static int   (WINAPI *ptype_info_opequals_equals)(type_info*,type_info*);
113 static int   (WINAPI *ptype_info_opnot_equals)(type_info*,type_info*);
114
115 /* RTTI */
116 static type_info* (*p__RTtypeid)(void*);
117 static void* (*p__RTCastToVoid)(void*);
118 static void* (*p__RTDynamicCast)(void*,int,void*,void*,int);
119
120 /* _very_ early native versions have serious RTTI bugs, so we check */
121 static void* bAncientVersion;
122
123 /* Emulate a __thiscall */
124 #ifdef _MSC_VER
125 inline static void* do_call_func1(void *func, void *_this)
126 {
127   volatile void* retval = 0;
128   __asm
129   {
130     push ecx
131     mov ecx, _this
132     call func
133     mov retval, eax
134     pop ecx
135   }
136   return (void*)retval;
137 }
138
139 inline static void* do_call_func2(void *func, void *_this, void* arg)
140 {
141   volatile void* retval = 0;
142   __asm
143   {
144     push ecx
145     push arg
146     mov ecx, _this
147     call func
148     mov retval, eax
149     pop ecx
150   }
151   return (void*)retval;
152 }
153 #else
154 static void* do_call_func1(void *func, void *_this)
155 {
156   void* ret;
157   __asm__ __volatile__ ("call *%1"
158                         : "=a" (ret)
159                         : "g" (func), "c" (_this)
160                         : "memory" );
161   return ret;
162 }
163 static void* do_call_func2(void *func, void *_this, void* arg)
164 {
165   void* ret;
166   __asm__ __volatile__ ("pushl %2\n\tcall *%1"
167                         : "=a" (ret)
168                         : "r" (func), "g" (arg), "c" (_this)
169                         : "memory" );
170   return ret;
171 }
172 #endif
173
174 #define call_func1(x,y)   do_call_func1((void*)x,(void*)y)
175 #define call_func2(x,y,z) do_call_func2((void*)x,(void*)y,(void*)z)
176
177 /* Some exports are only available in later versions */
178 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
179 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
180
181 static void InitFunctionPtrs()
182 {
183   hMsvcrt = LoadLibraryA("msvcrt.dll");
184   ok(hMsvcrt != 0, "LoadLibraryA failed\n");
185   if (hMsvcrt)
186   {
187     SETNOFAIL(poperator_new, "??_U@YAPAXI@Z");
188     SETNOFAIL(poperator_delete, "??_V@YAXPAX@Z");
189     SET(pmalloc, "malloc");
190     SET(pfree, "free");
191
192     if (!poperator_new)
193       poperator_new = pmalloc;
194     if (!poperator_delete)
195       poperator_delete = pfree;
196
197     SET(pexception_ctor, "??0exception@@QAE@ABQBD@Z");
198     SET(pexception_copy_ctor, "??0exception@@QAE@ABV0@@Z");
199     SET(pexception_default_ctor, "??0exception@@QAE@XZ");
200     SET(pexception_dtor, "??1exception@@UAE@XZ");
201     SET(pexception_opequals, "??4exception@@QAEAAV0@ABV0@@Z");
202     SET(pexception_what, "?what@exception@@UBEPBDXZ");
203     SET(pexception_vtable, "??_7exception@@6B@");
204     SET(pexception_vector_dtor, "??_Eexception@@UAEPAXI@Z");
205     SET(pexception_scalar_dtor, "??_Gexception@@UAEPAXI@Z");
206
207     SET(pbad_typeid_ctor, "??0bad_typeid@@QAE@PBD@Z");
208     SETNOFAIL(pbad_typeid_ctor_closure, "??_Fbad_typeid@@QAEXXZ");
209     SET(pbad_typeid_copy_ctor, "??0bad_typeid@@QAE@ABV0@@Z");
210     SET(pbad_typeid_dtor, "??1bad_typeid@@UAE@XZ");
211     SET(pbad_typeid_opequals, "??4bad_typeid@@QAEAAV0@ABV0@@Z");
212     SET(pbad_typeid_what, "?what@exception@@UBEPBDXZ");
213     SET(pbad_typeid_vtable, "??_7bad_typeid@@6B@");
214     SET(pbad_typeid_vector_dtor, "??_Ebad_typeid@@UAEPAXI@Z");
215     SET(pbad_typeid_scalar_dtor, "??_Gbad_typeid@@UAEPAXI@Z");
216
217     SETNOFAIL(pbad_cast_ctor, "??0bad_cast@@QAE@ABQBD@Z");
218     if (!pbad_cast_ctor)
219       SET(pbad_cast_ctor, "??0bad_cast@@AAE@PBQBD@Z");
220     SETNOFAIL(pbad_cast_ctor2, "??0bad_cast@@QAE@PBD@Z");
221     SETNOFAIL(pbad_cast_ctor_closure, "??_Fbad_cast@@QAEXXZ");
222     SET(pbad_cast_copy_ctor, "??0bad_cast@@QAE@ABV0@@Z");
223     SET(pbad_cast_dtor, "??1bad_cast@@UAE@XZ");
224     SET(pbad_cast_opequals, "??4bad_cast@@QAEAAV0@ABV0@@Z");
225     SET(pbad_cast_what, "?what@exception@@UBEPBDXZ");
226     SET(pbad_cast_vtable, "??_7bad_cast@@6B@");
227     SET(pbad_cast_vector_dtor, "??_Ebad_cast@@UAEPAXI@Z");
228     SET(pbad_cast_scalar_dtor, "??_Gbad_cast@@UAEPAXI@Z");
229
230     SET(p__non_rtti_object_ctor, "??0__non_rtti_object@@QAE@PBD@Z");
231     SET(p__non_rtti_object_copy_ctor, "??0__non_rtti_object@@QAE@ABV0@@Z");
232     SET(p__non_rtti_object_dtor, "??1__non_rtti_object@@UAE@XZ");
233     SET(p__non_rtti_object_opequals, "??4__non_rtti_object@@QAEAAV0@ABV0@@Z");
234     SET(p__non_rtti_object_what, "?what@exception@@UBEPBDXZ");
235     SET(p__non_rtti_object_vtable, "??_7__non_rtti_object@@6B@");
236     SET(p__non_rtti_object_vector_dtor, "??_E__non_rtti_object@@UAEPAXI@Z");
237     SET(p__non_rtti_object_scalar_dtor, "??_G__non_rtti_object@@UAEPAXI@Z");
238
239     SET(ptype_info_dtor, "??1type_info@@UAE@XZ");
240     SET(ptype_info_raw_name, "?raw_name@type_info@@QBEPBDXZ");
241     SET(ptype_info_name, "?name@type_info@@QBEPBDXZ");
242     SET(ptype_info_before, "?before@type_info@@QBEHABV1@@Z");
243     SET(ptype_info_opequals_equals, "??8type_info@@QBEHABV0@@Z");
244     SET(ptype_info_opnot_equals, "??9type_info@@QBEHABV0@@Z");
245
246     SET(p__RTtypeid, "__RTtypeid");
247     SET(p__RTCastToVoid, "__RTCastToVoid");
248     SET(p__RTDynamicCast, "__RTDynamicCast");
249
250     /* Extremely early versions export logic_error, and crash in RTTI */
251     SETNOFAIL(bAncientVersion, "??0logic_error@@QAE@ABQBD@Z");
252   }
253 }
254
255 static void test_exception(void)
256 {
257   static const char* e_name = "An exception name";
258   char* name;
259   exception e, e2, e3, *pe;
260
261   if (!poperator_new || !poperator_delete ||
262       !pexception_ctor || !pexception_copy_ctor || !pexception_default_ctor ||
263       !pexception_dtor || !pexception_opequals || !pexception_what ||
264       !pexception_vtable || !pexception_vector_dtor || !pexception_scalar_dtor)
265     return;
266
267   /* 'const char*&' ctor */
268   memset(&e, 0, sizeof(e));
269   call_func2(pexception_ctor, &e, &e_name);
270   ok(e.vtable != NULL, "Null exception vtable for e\n");
271   ok(e.name && e.name != e_name && !strcmp(e.name, "An exception name"), "Bad name '%s' for e\n", e.name);
272   ok(e.do_free == 1, "do_free set to %d for e\n", e.do_free);
273
274   /* Copy ctor */
275   memset(&e2, 0, sizeof(e2));
276   call_func2(pexception_copy_ctor, &e2, &e);
277   ok(e2.vtable != NULL, "Null exception vtable for e2\n");
278   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "An exception name"), "Bad exception name for e2\n");
279   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
280
281   /* Default ctor */
282   memset(&e3, 1, sizeof(e3));
283   call_func1(pexception_default_ctor, &e3);
284   ok(e3.vtable != NULL, "Null exception vtable for e3\n");
285   ok(e3.name == NULL, "Bad exception name for e3\n");
286   ok(e3.do_free == 0, "do_free set to %d for e3\n", e3.do_free);
287
288   ok(e.vtable == e2.vtable && e.vtable == e3.vtable, "exception vtables differ!\n");
289
290   /* Test calling the dtors */
291   call_func1(pexception_dtor, &e2);
292   call_func1(pexception_dtor, &e3);
293
294   /* Operator equals */
295   memset(&e2, 0, sizeof(e2));
296   pe = call_func2(pexception_opequals, &e2, &e);
297   ok(e2.vtable != NULL, "Null exception vtable for e2\n");
298   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "An exception name"), "Bad exception name for e2\n");
299   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
300   ok(pe == &e2, "opequals didn't return e2\n");
301
302   /* what() */
303   name = call_func1(pexception_what, &e2);
304   ok(e2.name == name, "Bad exception name from e2::what()\n");
305
306   /* vtable ptr */
307   ok(e2.vtable == pexception_vtable, "Bad vtable for e2\n");
308   call_func1(pexception_dtor, &e2);
309
310   /* new() */
311   pe = poperator_new(sizeof(exception));
312   ok(pe != NULL, "new() failed\n");
313   if (pe)
314   {
315     call_func2(pexception_ctor, pe, &e_name);
316     /* scalar dtor */
317     call_func2(pexception_scalar_dtor, pe, 0); /* Shouldn't delete pe */
318     pe->name = NULL;
319     pe->do_free = 0;
320     call_func2(pexception_scalar_dtor, pe, 1); /* Should delete pe */
321   }
322
323   pe = poperator_new(sizeof(exception));
324   ok(pe != NULL, "new() failed\n");
325   if (pe)
326   {
327     /* vector dtor, single element */
328     call_func2(pexception_ctor, pe, &e_name);
329     call_func2(pexception_vector_dtor, pe, 1); /* Should delete pe as single element*/
330   }
331
332   pe = poperator_new(sizeof(exception) * 4 + sizeof(int));
333   ok(pe != NULL, "new() failed\n");
334   if (pe)
335   {
336     /* vector dtor, multiple elements */
337     char name[] = "a constant";
338     *((int*)pe) = 3;
339     pe = (exception*)((int*)pe + 1);
340     call_func2(pexception_ctor, &pe[0], &e_name);
341     call_func2(pexception_ctor, &pe[1], &e_name);
342     call_func2(pexception_ctor, &pe[2], &e_name);
343     pe[3].name = name;
344     pe[3].do_free = 1; /* Crash if we try to free this */
345     call_func2(pexception_vector_dtor, pe, 3); /* Should delete all 3 and then pe block */
346   }
347
348   /* test our exported vtable is kosher */
349   pe = (void*)pexception_vtable; /* Use the exception struct to get vtable ptrs */
350   pexception_vector_dtor = (void*)pe->vtable;
351   pexception_what = (void*)pe->name;
352
353   name = call_func1(pexception_what, &e);
354   ok(e.name == name, "Bad exception name from vtable e::what()\n");
355
356   if (p__RTtypeid && !bAncientVersion)
357   {
358     /* Check the rtti */
359     type_info *ti = p__RTtypeid(&e);
360     ok (ti && ti->mangled &&
361         !strcmp(ti->mangled, ".?AVexception@@"), "bad rtti for e\n");
362
363     if (ti)
364     {
365       /* Check the returned type_info has rtti too */
366       type_info *ti2 = p__RTtypeid(ti);
367       ok (ti2 != NULL && !strcmp(ti2->mangled, ".?AVtype_info@@"), "bad rtti for e's type_info\n");
368     }
369   }
370
371   call_func2(pexception_vector_dtor, &e, 0); /* Should delete e.name, but not e */
372 }
373
374 /* This test is basically a cut 'n' paste of the exception test. but it verifies that
375  * bad_typeid works the exact same way... */
376 static void test_bad_typeid(void)
377 {
378   static const char* e_name = "A bad_typeid name";
379   char* name;
380   exception e, e2, e3, *pe;
381
382   if (!poperator_new || !poperator_delete ||
383       !pbad_typeid_ctor || !pbad_typeid_copy_ctor ||
384       !pbad_typeid_dtor || !pbad_typeid_opequals || !pbad_typeid_what ||
385       !pbad_typeid_vtable || !pbad_typeid_vector_dtor || !pbad_typeid_scalar_dtor)
386     return;
387
388   /* 'const char*' ctor */
389   memset(&e, 0, sizeof(e));
390   call_func2(pbad_typeid_ctor, &e, e_name);
391   ok(e.vtable != NULL, "Null bad_typeid vtable for e\n");
392   ok(e.name && e.name != e_name && !strcmp(e.name, "A bad_typeid name"), "Bad name '%s' for e\n", e.name);
393   ok(e.do_free == 1, "do_free set to %d for e\n", e.do_free);
394
395   /* Copy ctor */
396   memset(&e2, 0, sizeof(e2));
397   call_func2(pbad_typeid_copy_ctor, &e2, &e);
398   ok(e2.vtable != NULL, "Null bad_typeid vtable for e2\n");
399   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "A bad_typeid name"), "Bad name '%s' for e2\n", e2.name);
400   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
401
402   /* Ctor closure */
403   if (pbad_typeid_ctor_closure)
404   {
405     memset(&e3, 1, sizeof(e3));
406     call_func1(pbad_typeid_ctor_closure, &e3);
407     ok(e3.vtable != NULL, "Null bad_typeid vtable for e3\n");
408     ok(e3.name && !strcmp(e3.name, "bad typeid"), "Bad bad_typeid name for e3\n");
409     ok(e3.do_free == 1, "do_free set to %d for e3\n", e3.do_free);
410     ok(e.vtable == e3.vtable, "bad_typeid closure vtables differ!\n");
411     call_func1(pbad_typeid_dtor, &e3);
412   }
413   ok(e.vtable == e2.vtable, "bad_typeid vtables differ!\n");
414
415   /* Test calling the dtors */
416   call_func1(pbad_typeid_dtor, &e2);
417
418   /* Operator equals */
419   memset(&e2, 1, sizeof(e2));
420   pe = call_func2(pbad_typeid_opequals, &e2, &e);
421   ok(e2.vtable != NULL, "Null bad_typeid vtable for e2\n");
422   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "A bad_typeid name"), "Bad bad_typeid name for e2\n");
423   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
424   ok(pe == &e2, "opequals didn't return e2\n");
425
426   /* what() */
427   name = call_func1(pbad_typeid_what, &e2);
428   ok(e2.name == name, "Bad bad_typeid name from e2::what()\n");
429
430   /* vtable ptr */
431   ok(e2.vtable == pexception_vtable, "Bad vtable for e2\n");
432   call_func1(pbad_typeid_dtor, &e2);
433
434   /* new() */
435   pe = poperator_new(sizeof(exception));
436   ok(pe != NULL, "new() failed\n");
437   if (pe)
438   {
439     call_func2(pbad_typeid_ctor, pe, e_name);
440     /* scalar dtor */
441     call_func2(pbad_typeid_scalar_dtor, pe, 0); /* Shouldn't delete pe */
442     pe->name = NULL;
443     pe->do_free = 0;
444     call_func2(pbad_typeid_scalar_dtor, pe, 1); /* Should delete pe */
445   }
446
447   pe = poperator_new(sizeof(exception));
448   ok(pe != NULL, "new() failed\n");
449   if (pe)
450   {
451     /* vector dtor, single element */
452     call_func2(pbad_typeid_ctor, pe, e_name);
453     call_func2(pbad_typeid_vector_dtor, pe, 1); /* Should delete pe as single element*/
454   }
455
456   pe = poperator_new(sizeof(exception) * 4 + sizeof(int));
457   ok(pe != NULL, "new() failed\n");
458   if (pe)
459   {
460     /* vector dtor, multiple elements */
461     *((int*)pe) = 3;
462     pe = (exception*)((int*)pe + 1);
463     call_func2(pbad_typeid_ctor, &pe[0], e_name);
464     call_func2(pbad_typeid_ctor, &pe[1], e_name);
465     call_func2(pbad_typeid_ctor, &pe[2], e_name);
466     pe[3].name = 0;
467     pe[3].do_free = 1; /* Crash if we try to free this element */
468     call_func2(pbad_typeid_vector_dtor, pe, 3); /* Should delete all 3 and then pe block */
469   }
470
471   /* test our exported vtable is kosher */
472   pe = (void*)pbad_typeid_vtable; /* Use the exception struct to get vtable ptrs */
473   pbad_typeid_vector_dtor = (void*)pe->vtable;
474   pbad_typeid_what = (void*)pe->name;
475
476   name = call_func1(pbad_typeid_what, &e);
477   ok(e.name == name, "Bad bad_typeid name from vtable e::what()\n");
478
479   if (p__RTtypeid && !bAncientVersion)
480   {
481     /* Check the rtti */
482     type_info *ti = p__RTtypeid(&e);
483     ok (ti != NULL && !strcmp(ti->mangled, ".?AVbad_typeid@@"), "bad rtti for e (%s)\n",
484         !ti ? "null" : ti->mangled);
485   }
486
487   call_func2(pbad_typeid_vector_dtor, &e, 0); /* Should delete e.name, but not e */
488 }
489
490
491 /* Ditto for this test... */
492 static void test_bad_cast(void)
493 {
494   static const char* e_name = "A bad_cast name";
495   char* name;
496   exception e, e2, e3, *pe;
497
498   if (!poperator_new || !poperator_delete ||
499       !pbad_cast_ctor || !pbad_cast_copy_ctor ||
500       !pbad_cast_dtor || !pbad_cast_opequals || !pbad_cast_what ||
501       !pbad_cast_vtable || !pbad_cast_vector_dtor || !pbad_cast_scalar_dtor)
502     return;
503
504   if (pbad_cast_ctor2)
505   {
506     /* 'const char*' ctor */
507     memset(&e, 0, sizeof(e));
508     call_func2(pbad_cast_ctor2, &e, e_name);
509     ok(e.vtable != NULL, "Null bad_cast vtable for e\n");
510     ok(e.name && e.name != e_name && !strcmp(e.name, "A bad_cast name"), "Bad name '%s' for e\n", e.name);
511     ok(e.do_free == 1, "do_free set to %d for e\n", e.do_free);
512     call_func1(pbad_cast_dtor, &e);
513   }
514
515   /* 'const char*&' ctor */
516   memset(&e, 0, sizeof(e));
517   call_func2(pbad_cast_ctor, &e, &e_name);
518   ok(e.vtable != NULL, "Null bad_cast vtable for e\n");
519   ok(e.name && e.name != e_name && !strcmp(e.name, "A bad_cast name"), "Bad name '%s' for e\n", e.name);
520   ok(e.do_free == 1, "do_free set to %d for e\n", e.do_free);
521
522   /* Copy ctor */
523   memset(&e2, 0, sizeof(e2));
524   call_func2(pbad_cast_copy_ctor, &e2, &e);
525   ok(e2.vtable != NULL, "Null bad_cast vtable for e2\n");
526   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "A bad_cast name"), "Bad name '%s' for e2\n", e2.name);
527   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
528
529   /* Ctor closure */
530   if (pbad_cast_ctor_closure)
531   {
532     memset(&e3, 1, sizeof(e3));
533     call_func1(pbad_cast_ctor_closure, &e3);
534     ok(e3.vtable != NULL, "Null bad_cast vtable for e3\n");
535     ok(e3.name && !strcmp(e3.name, "bad cast"), "Bad bad_cast name for e3\n");
536     ok(e3.do_free == 1, "do_free set to %d for e3\n", e3.do_free);
537     ok(e.vtable == e3.vtable, "bad_cast closure vtables differ!\n");
538     call_func1(pbad_cast_dtor, &e3);
539   }
540   ok(e.vtable == e2.vtable, "bad_cast vtables differ!\n");
541
542   /* Test calling the dtors */
543   call_func1(pbad_cast_dtor, &e2);
544
545   /* Operator equals */
546   memset(&e2, 1, sizeof(e2));
547   pe = call_func2(pbad_cast_opequals, &e2, &e);
548   ok(e2.vtable != NULL, "Null bad_cast vtable for e2\n");
549   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "A bad_cast name"), "Bad bad_cast name for e2\n");
550   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
551   ok(pe == &e2, "opequals didn't return e2\n");
552
553   /* what() */
554   name = call_func1(pbad_cast_what, &e2);
555   ok(e2.name == name, "Bad bad_cast name from e2::what()\n");
556
557   /* vtable ptr */
558   ok(e2.vtable == pexception_vtable, "Bad vtable for e2\n");
559   call_func1(pbad_cast_dtor, &e2);
560
561   /* new() */
562   pe = poperator_new(sizeof(exception));
563   ok(pe != NULL, "new() failed\n");
564   if (pe)
565   {
566     call_func2(pbad_cast_ctor, pe, &e_name);
567     /* scalar dtor */
568     call_func2(pbad_cast_scalar_dtor, pe, 0); /* Shouldn't delete pe */
569     pe->name = NULL;
570     pe->do_free = 0;
571     call_func2(pbad_cast_scalar_dtor, pe, 1); /* Should delete pe */
572   }
573
574   pe = poperator_new(sizeof(exception));
575   ok(pe != NULL, "new() failed\n");
576   if (pe)
577   {
578     /* vector dtor, single element */
579     call_func2(pbad_cast_ctor, pe, &e_name);
580     call_func2(pbad_cast_vector_dtor, pe, 1); /* Should delete pe as single element*/
581   }
582
583   pe = poperator_new(sizeof(exception) * 4 + sizeof(int));
584   ok(pe != NULL, "new() failed\n");
585   if (pe)
586   {
587     /* vector dtor, multiple elements */
588     *((int*)pe) = 3;
589     pe = (exception*)((int*)pe + 1);
590     call_func2(pbad_cast_ctor, &pe[0], &e_name);
591     call_func2(pbad_cast_ctor, &pe[1], &e_name);
592     call_func2(pbad_cast_ctor, &pe[2], &e_name);
593     pe[3].name = 0;
594     pe[3].do_free = 1; /* Crash if we try to free this element */
595     call_func2(pbad_cast_vector_dtor, pe, 3); /* Should delete all 3 and then pe block */
596   }
597
598   /* test our exported vtable is kosher */
599   pe = (void*)pbad_cast_vtable; /* Use the exception struct to get vtable ptrs */
600   pbad_cast_vector_dtor = (void*)pe->vtable;
601   pbad_cast_what = (void*)pe->name;
602
603   name = call_func1(pbad_cast_what, &e);
604   ok(e.name == name, "Bad bad_cast name from vtable e::what()\n");
605
606   if (p__RTtypeid && !bAncientVersion)
607   {
608     /* Check the rtti */
609     type_info *ti = p__RTtypeid(&e);
610     ok (ti != NULL && !strcmp(ti->mangled, ".?AVbad_cast@@"), "bad rtti for e\n");
611   }
612   call_func2(pbad_cast_vector_dtor, &e, 0); /* Should delete e.name, but not e */
613 }
614
615 /* ... and this one */
616 static void test___non_rtti_object(void)
617 {
618   static const char* e_name = "A __non_rtti_object name";
619   char* name;
620   exception e, e2, *pe;
621
622   if (!poperator_new || !poperator_delete ||
623       !p__non_rtti_object_ctor || !p__non_rtti_object_copy_ctor ||
624       !p__non_rtti_object_dtor || !p__non_rtti_object_opequals || !p__non_rtti_object_what ||
625       !p__non_rtti_object_vtable || !p__non_rtti_object_vector_dtor || !p__non_rtti_object_scalar_dtor)
626     return;
627
628   /* 'const char*' ctor */
629   memset(&e, 0, sizeof(e));
630   call_func2(p__non_rtti_object_ctor, &e, e_name);
631   ok(e.vtable != NULL, "Null __non_rtti_object vtable for e\n");
632   ok(e.name && e.name != e_name && !strcmp(e.name, "A __non_rtti_object name"), "Bad name '%s' for e\n", e.name);
633   ok(e.do_free == 1, "do_free set to %d for e\n", e.do_free);
634
635   /* Copy ctor */
636   memset(&e2, 0, sizeof(e2));
637   call_func2(p__non_rtti_object_copy_ctor, &e2, &e);
638   ok(e2.vtable != NULL, "Null __non_rtti_object vtable for e2\n");
639   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "A __non_rtti_object name"), "Bad name '%s' for e2\n", e2.name);
640   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
641   ok(e.vtable == e2.vtable, "__non_rtti_object vtables differ!\n");
642
643   /* Test calling the dtors */
644   call_func1(p__non_rtti_object_dtor, &e2);
645
646   /* Operator equals */
647   memset(&e2, 1, sizeof(e2));
648   pe = call_func2(p__non_rtti_object_opequals, &e2, &e);
649   ok(e2.vtable != NULL, "Null __non_rtti_object vtable for e2\n");
650   ok(e2.name && e2.name != e.name && !strcmp(e2.name, "A __non_rtti_object name"), "Bad __non_rtti_object name for e2\n");
651   ok(e2.do_free == 1, "do_free set to %d for e2\n", e2.do_free);
652   ok(pe == &e2, "opequals didn't return e2\n");
653
654   /* what() */
655   name = call_func1(p__non_rtti_object_what, &e2);
656   ok(e2.name == name, "Bad __non_rtti_object name from e2::what()\n");
657
658   /* vtable ptr */
659   ok(e2.vtable == pexception_vtable, "Bad vtable for e2\n");
660   call_func1(p__non_rtti_object_dtor, &e2);
661
662   /* new() */
663   pe = poperator_new(sizeof(exception));
664   ok(pe != NULL, "new() failed\n");
665   if (pe)
666   {
667     call_func2(p__non_rtti_object_ctor, pe, e_name);
668     /* scalar dtor */
669     call_func2(p__non_rtti_object_scalar_dtor, pe, 0); /* Shouldn't delete pe */
670     pe->name = NULL;
671     pe->do_free = 0;
672     call_func2(p__non_rtti_object_scalar_dtor, pe, 1); /* Should delete pe */
673   }
674
675   pe = poperator_new(sizeof(exception));
676   ok(pe != NULL, "new() failed\n");
677   if (pe)
678   {
679     /* vector dtor, single element */
680     call_func2(p__non_rtti_object_ctor, pe, e_name);
681     call_func2(p__non_rtti_object_vector_dtor, pe, 1); /* Should delete pe as single element*/
682   }
683
684   pe = poperator_new(sizeof(exception) * 4 + sizeof(int));
685   ok(pe != NULL, "new() failed\n");
686   if (pe)
687   {
688     /* vector dtor, multiple elements */
689     *((int*)pe) = 3;
690     pe = (exception*)((int*)pe + 1);
691     call_func2(p__non_rtti_object_ctor, &pe[0], e_name);
692     call_func2(p__non_rtti_object_ctor, &pe[1], e_name);
693     call_func2(p__non_rtti_object_ctor, &pe[2], e_name);
694     pe[3].name = 0;
695     pe[3].do_free = 1; /* Crash if we try to free this element */
696     call_func2(p__non_rtti_object_vector_dtor, pe, 3); /* Should delete all 3 and then pe block */
697   }
698
699   /* test our exported vtable is kosher */
700   pe = (void*)p__non_rtti_object_vtable; /* Use the exception struct to get vtable ptrs */
701   p__non_rtti_object_vector_dtor = (void*)pe->vtable;
702   p__non_rtti_object_what = (void*)pe->name;
703
704   name = call_func1(p__non_rtti_object_what, &e);
705   ok(e.name == name, "Bad __non_rtti_object name from vtable e::what()\n");
706
707   if (p__RTtypeid && !bAncientVersion)
708   {
709     /* Check the rtti */
710     type_info *ti = p__RTtypeid(&e);
711     ok (ti != NULL && !strcmp(ti->mangled, ".?AV__non_rtti_object@@"), "bad rtti for e\n");
712   }
713   call_func2(p__non_rtti_object_vector_dtor, &e, 0); /* Should delete e.name, but not e */
714 }
715
716
717 static void test_type_info(void)
718 {
719   static type_info t1 = { NULL, NULL,{'.','?','A','V','t','e','s','t','1','@','@',0,0,0,0,0 } };
720   static type_info t1_1 = { NULL, NULL,{'?','?','A','V','t','e','s','t','1','@','@',0,0,0,0,0 } };
721   static type_info t2 = { NULL, NULL, {'.','?','A','V','t','e','s','t','2','@','@',0,0,0,0,0 } };
722   char* name;
723   int res;
724
725   if (!pmalloc || !pfree || !ptype_info_dtor || !ptype_info_raw_name ||
726       !ptype_info_name || !ptype_info_before ||
727       !ptype_info_opequals_equals || !ptype_info_opnot_equals)
728     return;
729
730   /* Test calling the dtors */
731   call_func1(ptype_info_dtor, &t1); /* No effect, since name is NULL */
732   t1.name = pmalloc(64);
733   strcpy(t1.name, "foo");
734   call_func1(ptype_info_dtor, &t1); /* Frees t1.name using 'free' */
735
736   /* raw_name */
737   t1.name = NULL;
738   name = call_func1(ptype_info_raw_name, &t1);
739
740   /* FIXME: This fails on native; it shouldn't though - native bug?
741    * ok(name && !strcmp(name, t1.mangled), "bad raw_name '%s' for t1 (expected '%s')\n", name, t1.mangled);
742    */
743   ok(t1.name == NULL, "raw_name() set name for t1\n");
744
745   /* name */
746   t1.name = NULL;
747   name = call_func1(ptype_info_name, &t1);
748   ok(name && t1.name && !strcmp(name, t1.name), "bad name '%s' for t1\n", name);
749
750   ok(t1.name && !strcmp(t1.name, "class test1"), "demangled to '%s' for t1\n", t1.name);
751   call_func1(ptype_info_dtor, &t1);
752
753   /* before */
754   t1.name = NULL;
755   res = (int)call_func2(ptype_info_before, &t1, &t1);
756   ok(res == 0, "expected 0, got %d\n", res);
757   res = (int)call_func2(ptype_info_before, &t2, &t1);
758   ok(res == 0, "expected 0, got %d\n", res);
759   res = (int)call_func2(ptype_info_before, &t1, &t2);
760   ok(res == 1, "expected 1, got %d\n", res);
761   /* Doesn't check first char */
762   res = (int)call_func2(ptype_info_before, &t1, &t1_1);
763   ok(res == 0, "expected 0, got %d\n", res);
764
765   /* opequals_equals */
766   t1.name = NULL;
767   res = (int)call_func2(ptype_info_opequals_equals, &t1, &t1);
768   ok(res == 1, "expected 1, got %d\n", res);
769   res = (int)call_func2(ptype_info_opequals_equals, &t1, &t2);
770   ok(res == 0, "expected 0, got %d\n", res);
771   res = (int)call_func2(ptype_info_opequals_equals, &t2, &t1);
772   ok(res == 0, "expected 0, got %d\n", res);
773
774   /* opnot_equals */
775   t1.name = NULL;
776   res = (int)call_func2(ptype_info_opnot_equals, &t1, &t1);
777   ok(res == 0, "expected 0, got %d\n", res);
778   res = (int)call_func2(ptype_info_opnot_equals, &t1, &t2);
779   ok(res == 1, "expected 1, got %d\n", res);
780   res = (int)call_func2(ptype_info_opnot_equals, &t2, &t1);
781   ok(res == 1, "expected 1, got %d\n", res);
782 }
783
784 /* Test RTTI functions */
785 static void test_rtti(void)
786 {
787   static const char* e_name = "name";
788   type_info *ti,*bti;
789   exception e,b;
790   void *casted;
791
792   if (bAncientVersion ||
793       !p__RTCastToVoid || !p__RTtypeid || !pexception_ctor || !pbad_typeid_ctor || !p__RTDynamicCast)
794     return;
795
796   call_func2(pexception_ctor, &e, &e_name);
797   call_func2(pbad_typeid_ctor, &b, e_name);
798
799   /* dynamic_cast to void* */
800   casted = p__RTCastToVoid(&e);
801   ok (casted == (void*)&e, "failed cast to void\n");
802
803   /* dynamic_cast up */
804   ti = p__RTtypeid(&e);
805   bti = p__RTtypeid(&b);
806
807   casted = p__RTDynamicCast(&b, 0, NULL, ti, 0);
808   ok (casted == (void*)&b, "failed cast from bad_cast to exception\n");
809
810   /* dynamic_cast down */
811   casted = p__RTDynamicCast(&e, 0, NULL, bti, 0);
812   ok (casted == NULL, "Cast succeeded\n");
813 }
814
815 START_TEST(cpp)
816 {
817   InitFunctionPtrs();
818
819   test_exception();
820   test_bad_typeid();
821   test_bad_cast();
822   test___non_rtti_object();
823   test_type_info();
824   test_rtti();
825
826   if (hMsvcrt)
827     FreeLibrary(hMsvcrt);
828 }
829 #endif /* __i386__ */