mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / msvcp90 / tests / string.c
blob6c919f84d954c04b6666997a102af050646ba6cc
1 /*
2 * Copyright 2010 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdio.h>
20 #include <limits.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include "wine/test.h"
26 /* basic_string<char, char_traits<char>, allocator<char>> */
27 #define BUF_SIZE_CHAR 16
28 typedef struct _basic_string_char
30 void *allocator;
31 union {
32 char buf[BUF_SIZE_CHAR];
33 char *ptr;
34 } data;
35 size_t size;
36 size_t res;
37 } basic_string_char;
39 /* basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>> */
40 #define BUF_SIZE_WCHAR 8
41 typedef struct _basic_string_wchar
43 void *allocator;
44 union {
45 wchar_t buf[BUF_SIZE_WCHAR];
46 wchar_t *ptr;
47 } data;
48 size_t size;
49 size_t res;
50 } basic_string_wchar;
52 static void* (__cdecl *p_set_invalid_parameter_handler)(void*);
53 static basic_string_char* (__cdecl *p_basic_string_char_concatenate)(basic_string_char*, const basic_string_char*, const basic_string_char*);
54 static basic_string_char* (__cdecl *p_basic_string_char_concatenate_cstr)(basic_string_char*, const basic_string_char*, const char*);
56 #undef __thiscall
57 #ifdef __i386__
58 #define __thiscall __stdcall
59 #else
60 #define __thiscall __cdecl
61 #endif
63 static basic_string_char* (__thiscall *p_basic_string_char_ctor)(basic_string_char*);
64 static basic_string_char* (__thiscall *p_basic_string_char_copy_ctor)(basic_string_char*, basic_string_char*);
65 static basic_string_char* (__thiscall *p_basic_string_char_ctor_cstr)(basic_string_char*, const char*);
66 static void* (__thiscall *p_basic_string_char_dtor)(basic_string_char*);
67 static basic_string_char* (__thiscall *p_basic_string_char_erase)(basic_string_char*, size_t, size_t);
68 static basic_string_char* (__thiscall *p_basic_string_char_assign_cstr_len)(basic_string_char*, const char*, size_t);
69 static const char* (__thiscall *p_basic_string_char_cstr)(basic_string_char*);
70 static const char* (__thiscall *p_basic_string_char_data)(basic_string_char*);
71 static size_t (__thiscall *p_basic_string_char_size)(basic_string_char*);
72 static size_t (__thiscall *p_basic_string_char_capacity)(basic_string_char*);
73 static void (__thiscall *p_basic_string_char_swap)(basic_string_char*, basic_string_char*);
74 static basic_string_char* (__thiscall *p_basic_string_char_append)(basic_string_char*, basic_string_char*);
75 static basic_string_char* (__thiscall *p_basic_string_char_append_substr)(basic_string_char*, basic_string_char*, size_t, size_t);
76 static int (__thiscall *p_basic_string_char_compare_substr_substr)(basic_string_char*, size_t, size_t, basic_string_char*, size_t, size_t);
77 static int (__thiscall *p_basic_string_char_compare_substr_cstr_len)(basic_string_char*, size_t, size_t, const char*, size_t);
78 static size_t (__thiscall *p_basic_string_char_find_cstr_substr)(basic_string_char*, const char*, size_t, size_t);
79 static size_t (__thiscall *p_basic_string_char_rfind_cstr_substr)(basic_string_char*, const char*, size_t, size_t);
80 static basic_string_char* (__thiscall *p_basic_string_char_replace_cstr)(basic_string_char*, size_t, size_t, const char*);
81 static size_t (__thiscall *p_basic_string_char_find_last_not_of_cstr_substr)(const basic_string_char*, const char*, size_t, size_t);
83 static size_t *p_basic_string_char_npos;
85 static basic_string_wchar* (__thiscall *p_basic_string_wchar_ctor)(basic_string_wchar*);
86 static basic_string_wchar* (__thiscall *p_basic_string_wchar_copy_ctor)(basic_string_wchar*, basic_string_wchar*);
87 static basic_string_wchar* (__thiscall *p_basic_string_wchar_ctor_cstr)(basic_string_wchar*, const wchar_t*);
88 static void* (__thiscall *p_basic_string_wchar_dtor)(basic_string_wchar*);
89 static basic_string_wchar* (__thiscall *p_basic_string_wchar_erase)(basic_string_wchar*, size_t, size_t);
90 static basic_string_wchar* (__thiscall *p_basic_string_wchar_assign_cstr_len)(basic_string_wchar*, const wchar_t*, size_t);
91 static const wchar_t* (__thiscall *p_basic_string_wchar_cstr)(basic_string_wchar*);
92 static const wchar_t* (__thiscall *p_basic_string_wchar_data)(basic_string_wchar*);
93 static size_t (__thiscall *p_basic_string_wchar_size)(basic_string_wchar*);
94 static size_t (__thiscall *p_basic_string_wchar_capacity)(basic_string_wchar*);
95 static void (__thiscall *p_basic_string_wchar_swap)(basic_string_wchar*, basic_string_wchar*);
97 static int invalid_parameter = 0;
98 static void __cdecl test_invalid_parameter_handler(const wchar_t *expression,
99 const wchar_t *function, const wchar_t *file,
100 unsigned line, uintptr_t arg)
102 ok(expression == NULL, "expression is not NULL\n");
103 ok(function == NULL, "function is not NULL\n");
104 ok(file == NULL, "file is not NULL\n");
105 ok(line == 0, "line = %u\n", line);
106 ok(arg == 0, "arg = %lx\n", (UINT_PTR)arg);
107 invalid_parameter++;
110 /* Emulate a __thiscall */
111 #ifdef __i386__
113 #include "pshpack1.h"
114 struct thiscall_thunk
116 BYTE pop_eax; /* popl %eax (ret addr) */
117 BYTE pop_edx; /* popl %edx (func) */
118 BYTE pop_ecx; /* popl %ecx (this) */
119 BYTE push_eax; /* pushl %eax */
120 WORD jmp_edx; /* jmp *%edx */
122 #include "poppack.h"
124 static void * (WINAPI *call_thiscall_func1)( void *func, void *this );
125 static void * (WINAPI *call_thiscall_func2)( void *func, void *this, const void *a );
126 static void * (WINAPI *call_thiscall_func3)( void *func, void *this, const void *a, const void *b );
127 static void * (WINAPI *call_thiscall_func4)( void *func, void *this, const void *a, const void *b,
128 const void *c );
129 static void * (WINAPI *call_thiscall_func5)( void *func, void *this, const void *a, const void *b,
130 const void *c, const void *d );
131 static void * (WINAPI *call_thiscall_func6)( void *func, void *this, const void *a, const void *b,
132 const void *c, const void *d, const void *e );
134 static void init_thiscall_thunk(void)
136 struct thiscall_thunk *thunk = VirtualAlloc( NULL, sizeof(*thunk),
137 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
138 thunk->pop_eax = 0x58; /* popl %eax */
139 thunk->pop_edx = 0x5a; /* popl %edx */
140 thunk->pop_ecx = 0x59; /* popl %ecx */
141 thunk->push_eax = 0x50; /* pushl %eax */
142 thunk->jmp_edx = 0xe2ff; /* jmp *%edx */
143 call_thiscall_func1 = (void *)thunk;
144 call_thiscall_func2 = (void *)thunk;
145 call_thiscall_func3 = (void *)thunk;
146 call_thiscall_func4 = (void *)thunk;
147 call_thiscall_func5 = (void *)thunk;
148 call_thiscall_func6 = (void *)thunk;
151 #define call_func1(func,_this) call_thiscall_func1(func,_this)
152 #define call_func2(func,_this,a) call_thiscall_func2(func,_this,(const void*)(a))
153 #define call_func3(func,_this,a,b) call_thiscall_func3(func,_this,(const void*)(a),(const void*)(b))
154 #define call_func4(func,_this,a,b,c) call_thiscall_func4(func,_this,(const void*)(a),\
155 (const void*)(b),(const void*)(c))
156 #define call_func5(func,_this,a,b,c,d) call_thiscall_func5(func,_this,(const void*)(a),\
157 (const void*)(b),(const void*)(c),(const void*)(d))
158 #define call_func6(func,_this,a,b,c,d,e) call_thiscall_func6(func,_this,(const void*)(a),\
159 (const void*)(b),(const void*)(c),(const void*)(d),(const void*)(e))
161 #else
163 #define init_thiscall_thunk()
164 #define call_func1(func,_this) func(_this)
165 #define call_func2(func,_this,a) func(_this,a)
166 #define call_func3(func,_this,a,b) func(_this,a,b)
167 #define call_func4(func,_this,a,b,c) func(_this,a,b,c)
168 #define call_func5(func,_this,a,b,c,d) func(_this,a,b,c,d)
169 #define call_func6(func,_this,a,b,c,d,e) func(_this,a,b,c,d,e)
171 #endif /* __i386__ */
173 static HMODULE msvcr, msvcp;
174 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(msvcp,y)
175 #define SET(x,y) do { SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y); } while(0)
176 static BOOL init(void)
178 msvcr = LoadLibraryA("msvcr90.dll");
179 msvcp = LoadLibraryA("msvcp90.dll");
180 if(!msvcr || !msvcp) {
181 win_skip("msvcp90.dll or msvcrt90.dll not installed\n");
182 return FALSE;
185 p_set_invalid_parameter_handler = (void*)GetProcAddress(msvcr, "_set_invalid_parameter_handler");
186 if(!p_set_invalid_parameter_handler) {
187 win_skip("Error setting tests environment\n");
188 return FALSE;
191 p_set_invalid_parameter_handler(test_invalid_parameter_handler);
193 if(sizeof(void*) == 8) { /* 64-bit initialization */
194 SET(p_basic_string_char_ctor,
195 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@XZ");
196 SET(p_basic_string_char_copy_ctor,
197 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@AEBV01@@Z");
198 SET(p_basic_string_char_ctor_cstr,
199 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@PEBD@Z");
200 SET(p_basic_string_char_dtor,
201 "??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@XZ");
202 SET(p_basic_string_char_erase,
203 "?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@_K0@Z");
204 SET(p_basic_string_char_assign_cstr_len,
205 "?assign@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@PEBD_K@Z");
206 SET(p_basic_string_char_cstr,
207 "?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAPEBDXZ");
208 SET(p_basic_string_char_data,
209 "?data@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAPEBDXZ");
210 SET(p_basic_string_char_size,
211 "?size@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KXZ");
212 SET(p_basic_string_char_capacity,
213 "?capacity@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KXZ");
214 SET(p_basic_string_char_swap,
215 "?swap@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAXAEAV12@@Z");
216 SET(p_basic_string_char_append,
217 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@AEBV12@@Z");
218 SET(p_basic_string_char_append_substr,
219 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@AEBV12@_K1@Z");
220 SET(p_basic_string_char_compare_substr_substr,
221 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAH_K0AEBV12@00@Z");
222 SET(p_basic_string_char_compare_substr_cstr_len,
223 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAH_K0PEBD0@Z");
224 SET(p_basic_string_char_concatenate,
225 "??$?HDU?$char_traits@D@std@@V?$allocator@D@1@@std@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@AEBV10@0@Z");
226 SET(p_basic_string_char_concatenate_cstr,
227 "??$?HDU?$char_traits@D@std@@V?$allocator@D@1@@std@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@AEBV10@PEBD@Z");
228 SET(p_basic_string_char_find_cstr_substr,
229 "?find@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KPEBD_K1@Z");
230 SET(p_basic_string_char_rfind_cstr_substr,
231 "?rfind@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KPEBD_K1@Z");
232 SET(p_basic_string_char_replace_cstr,
233 "?replace@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@_K0PEBD@Z");
234 SET(p_basic_string_char_find_last_not_of_cstr_substr,
235 "?find_last_not_of@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KPEBD_K1@Z");
236 SET(p_basic_string_char_npos,
237 "?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2_KB");
239 SET(p_basic_string_wchar_ctor,
240 "??0?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEAA@XZ");
241 SET(p_basic_string_wchar_copy_ctor,
242 "??0?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEAA@AEBV01@@Z");
243 SET(p_basic_string_wchar_ctor_cstr,
244 "??0?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEAA@PEB_W@Z");
245 SET(p_basic_string_wchar_dtor,
246 "??1?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEAA@XZ");
247 SET(p_basic_string_wchar_erase,
248 "?erase@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEAAAEAV12@_K0@Z");
249 SET(p_basic_string_wchar_assign_cstr_len,
250 "?assign@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEAAAEAV12@PEB_W_K@Z");
251 SET(p_basic_string_wchar_cstr,
252 "?c_str@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEBAPEB_WXZ");
253 SET(p_basic_string_wchar_data,
254 "?data@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEBAPEB_WXZ");
255 SET(p_basic_string_wchar_size,
256 "?size@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEBA_KXZ");
257 SET(p_basic_string_wchar_capacity,
258 "?capacity@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEBA_KXZ");
259 SET(p_basic_string_wchar_swap,
260 "?swap@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QEAAXAEAV12@@Z");
261 } else {
262 SET(p_basic_string_char_ctor,
263 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ");
264 SET(p_basic_string_char_copy_ctor,
265 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z");
266 SET(p_basic_string_char_ctor_cstr,
267 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z");
268 SET(p_basic_string_char_dtor,
269 "??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ");
270 SET(p_basic_string_char_erase,
271 "?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@II@Z");
272 SET(p_basic_string_char_assign_cstr_len,
273 "?assign@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@PBDI@Z");
274 SET(p_basic_string_char_cstr,
275 "?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ");
276 SET(p_basic_string_char_data,
277 "?data@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ");
278 SET(p_basic_string_char_size,
279 "?size@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIXZ");
280 SET(p_basic_string_char_capacity,
281 "?capacity@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIXZ");
282 SET(p_basic_string_char_swap,
283 "?swap@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEXAAV12@@Z");
284 SET(p_basic_string_char_append,
285 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@ABV12@@Z");
286 SET(p_basic_string_char_append_substr,
287 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@ABV12@II@Z");
288 SET(p_basic_string_char_compare_substr_substr,
289 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEHIIABV12@II@Z");
290 SET(p_basic_string_char_compare_substr_cstr_len,
291 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEHIIPBDI@Z");
292 SET(p_basic_string_char_concatenate,
293 "??$?HDU?$char_traits@D@std@@V?$allocator@D@1@@std@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@ABV10@0@Z");
294 SET(p_basic_string_char_concatenate_cstr,
295 "??$?HDU?$char_traits@D@std@@V?$allocator@D@1@@std@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@ABV10@PBD@Z");
296 SET(p_basic_string_char_find_cstr_substr,
297 "?find@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIPBDII@Z");
298 SET(p_basic_string_char_rfind_cstr_substr,
299 "?rfind@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIPBDII@Z");
300 SET(p_basic_string_char_replace_cstr,
301 "?replace@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@IIPBD@Z");
302 SET(p_basic_string_char_find_last_not_of_cstr_substr,
303 "?find_last_not_of@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIPBDII@Z");
304 SET(p_basic_string_char_npos,
305 "?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2IB");
307 SET(p_basic_string_wchar_ctor,
308 "??0?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QAE@XZ");
309 SET(p_basic_string_wchar_copy_ctor,
310 "??0?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QAE@ABV01@@Z");
311 SET(p_basic_string_wchar_ctor_cstr,
312 "??0?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QAE@PB_W@Z");
313 SET(p_basic_string_wchar_dtor,
314 "??1?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QAE@XZ");
315 SET(p_basic_string_wchar_erase,
316 "?erase@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QAEAAV12@II@Z");
317 SET(p_basic_string_wchar_assign_cstr_len,
318 "?assign@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QAEAAV12@PB_WI@Z");
319 SET(p_basic_string_wchar_cstr,
320 "?c_str@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QBEPB_WXZ");
321 SET(p_basic_string_wchar_data,
322 "?data@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QBEPB_WXZ");
323 SET(p_basic_string_wchar_size,
324 "?size@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QBEIXZ");
325 SET(p_basic_string_wchar_capacity,
326 "?capacity@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QBEIXZ");
327 SET(p_basic_string_wchar_swap,
328 "?swap@?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@QAEXAAV12@@Z");
331 init_thiscall_thunk();
332 return TRUE;
335 static void test_basic_string_char(void) {
336 basic_string_char str1, str2, *pstr;
337 const char *str;
338 size_t size, capacity;
340 call_func1(p_basic_string_char_ctor, &str1);
341 str = NULL;
342 str = call_func1(p_basic_string_char_cstr, &str1);
343 ok(str != NULL, "str = NULL\n");
344 ok(*str == '\0', "*str = %c\n", *str);
345 str = call_func1(p_basic_string_char_data, &str1);
346 ok(str != NULL, "str = NULL\n");
347 ok(*str == '\0', "*str = %c\n", *str);
348 call_func1(p_basic_string_char_dtor, &str1);
350 pstr = call_func2(p_basic_string_char_ctor_cstr, &str1, "test");
351 ok(pstr == &str1, "pstr != &str1\n");
352 str = call_func1(p_basic_string_char_cstr, &str1);
353 ok(!memcmp(str, "test", 5), "str = %s\n", str);
354 str = call_func1(p_basic_string_char_data, &str1);
355 ok(!memcmp(str, "test", 5), "str = %s\n", str);
356 size = (size_t)call_func1(p_basic_string_char_size, &str1);
357 ok(size == 4, "size = %lu\n", (unsigned long)size);
358 capacity = (size_t)call_func1(p_basic_string_char_capacity, &str1);
359 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
361 pstr = call_func2(p_basic_string_char_copy_ctor, &str2, &str1);
362 ok(pstr == &str2, "pstr != &str2\n");
363 str = call_func1(p_basic_string_char_cstr, &str2);
364 ok(!memcmp(str, "test", 5), "str = %s\n", str);
365 str = call_func1(p_basic_string_char_data, &str2);
366 ok(!memcmp(str, "test", 5), "str = %s\n", str);
368 call_func3(p_basic_string_char_erase, &str2, 1, 2);
369 str = call_func1(p_basic_string_char_cstr, &str2);
370 ok(!memcmp(str, "tt", 3), "str = %s\n", str);
371 str = call_func1(p_basic_string_char_data, &str2);
372 ok(!memcmp(str, "tt", 3), "str = %s\n", str);
373 size = (size_t)call_func1(p_basic_string_char_size, &str1);
374 ok(size == 4, "size = %lu\n", (unsigned long)size);
375 capacity = (size_t)call_func1(p_basic_string_char_capacity, &str1);
376 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
378 call_func3(p_basic_string_char_erase, &str2, 1, 100);
379 str = call_func1(p_basic_string_char_cstr, &str2);
380 ok(!memcmp(str, "t", 2), "str = %s\n", str);
381 str = call_func1(p_basic_string_char_data, &str2);
382 ok(!memcmp(str, "t", 2), "str = %s\n", str);
383 size = (size_t)call_func1(p_basic_string_char_size, &str1);
384 ok(size == 4, "size = %lu\n", (unsigned long)size);
385 capacity = (size_t)call_func1(p_basic_string_char_capacity, &str1);
386 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
388 call_func3(p_basic_string_char_assign_cstr_len, &str2, "test", 4);
389 str = call_func1(p_basic_string_char_cstr, &str2);
390 ok(!memcmp(str, "test", 5), "str = %s\n", str);
391 str = call_func1(p_basic_string_char_data, &str2);
392 ok(!memcmp(str, "test", 5), "str = %s\n", str);
394 call_func3(p_basic_string_char_assign_cstr_len, &str2, (str+1), 2);
395 str = call_func1(p_basic_string_char_cstr, &str2);
396 ok(!memcmp(str, "es", 3), "str = %s\n", str);
397 str = call_func1(p_basic_string_char_data, &str2);
398 ok(!memcmp(str, "es", 3), "str = %s\n", str);
400 call_func1(p_basic_string_char_dtor, &str1);
401 call_func1(p_basic_string_char_dtor, &str2);
404 static void test_basic_string_char_swap(void) {
405 basic_string_char str1, str2;
406 char atmp1[32], atmp2[32];
408 /* Swap self, local */
409 strcpy(atmp1, "qwerty");
410 call_func2(p_basic_string_char_ctor_cstr, &str1, atmp1);
411 call_func2(p_basic_string_char_swap, &str1, &str1);
412 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
413 call_func2(p_basic_string_char_swap, &str1, &str1);
414 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
415 call_func1(p_basic_string_char_dtor, &str1);
417 /* str1 allocated, str2 local */
418 strcpy(atmp1, "qwerty12345678901234567890");
419 strcpy(atmp2, "asd");
420 call_func2(p_basic_string_char_ctor_cstr, &str1, atmp1);
421 call_func2(p_basic_string_char_ctor_cstr, &str2, atmp2);
422 call_func2(p_basic_string_char_swap, &str1, &str2);
423 ok(strcmp(atmp2, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
424 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str2)) == 0, "Invalid value of str2\n");
425 call_func2(p_basic_string_char_swap, &str1, &str2);
426 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
427 ok(strcmp(atmp2, call_func1(p_basic_string_char_cstr, &str2)) == 0, "Invalid value of str2\n");
428 call_func1(p_basic_string_char_dtor, &str1);
429 call_func1(p_basic_string_char_dtor, &str2);
432 static void test_basic_string_char_append(void) {
433 basic_string_char str1, str2;
434 const char *str;
436 call_func2(p_basic_string_char_ctor_cstr, &str1, "");
437 call_func2(p_basic_string_char_ctor_cstr, &str2, "append");
439 call_func2(p_basic_string_char_append, &str1, &str2);
440 str = call_func1(p_basic_string_char_cstr, &str1);
441 ok(!memcmp(str, "append", 7), "str = %s\n", str);
443 call_func4(p_basic_string_char_append_substr, &str1, &str2, 3, 1);
444 str = call_func1(p_basic_string_char_cstr, &str1);
445 ok(!memcmp(str, "appende", 8), "str = %s\n", str);
447 call_func4(p_basic_string_char_append_substr, &str1, &str2, 5, 100);
448 str = call_func1(p_basic_string_char_cstr, &str1);
449 ok(!memcmp(str, "appended", 9), "str = %s\n", str);
451 call_func4(p_basic_string_char_append_substr, &str1, &str2, 6, 100);
452 str = call_func1(p_basic_string_char_cstr, &str1);
453 ok(!memcmp(str, "appended", 9), "str = %s\n", str);
455 call_func1(p_basic_string_char_dtor, &str1);
456 call_func1(p_basic_string_char_dtor, &str2);
459 static void test_basic_string_char_compare(void) {
460 basic_string_char str1, str2, str3;
461 int ret;
463 call_func2(p_basic_string_char_ctor_cstr, &str1, "str1str");
464 call_func2(p_basic_string_char_ctor_cstr, &str2, "str9str");
465 call_func2(p_basic_string_char_ctor_cstr, &str3, "splash.png");
467 ret = (int)call_func6(p_basic_string_char_compare_substr_substr,
468 &str1, 0, 3, &str2, 0, 3);
469 ok(ret == 0, "ret = %d\n", ret);
470 ret = (int)call_func6(p_basic_string_char_compare_substr_substr,
471 &str1, 4, 3, &str2, 4, 10);
472 ok(ret == 0, "ret = %d\n", ret);
473 ret = (int)call_func6(p_basic_string_char_compare_substr_substr,
474 &str1, 1, 3, &str2, 1, 4);
475 ok(ret == -1, "ret = %d\n", ret);
477 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
478 &str1, 0, 1000, "str1str", 7);
479 ok(ret == 0, "ret = %d\n", ret);
480 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
481 &str3, 6, UINT_MAX, ".png", 4);
482 ok(ret == 0, "ret = %d\n", ret);
483 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
484 &str1, 1, 2, "tr", 2);
485 ok(ret == 0, "ret = %d\n", ret);
486 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
487 &str1, 1, 0, "aaa", 0);
488 ok(ret == 0, "ret = %d\n", ret);
489 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
490 &str1, 1, 0, "aaa", 1);
491 ok(ret == -1, "ret = %d\n", ret);
493 call_func1(p_basic_string_char_dtor, &str1);
494 call_func1(p_basic_string_char_dtor, &str2);
495 call_func1(p_basic_string_char_dtor, &str3);
498 static void test_basic_string_char_concatenate(void) {
499 basic_string_char str, ret;
500 const char *cstr;
502 call_func2(p_basic_string_char_ctor_cstr, &str, "test ");
503 /* CDECL calling convention with return bigger than 8 bytes */
504 p_basic_string_char_concatenate(&ret, &str, &str);
505 cstr = call_func1(p_basic_string_char_cstr, &ret);
506 ok(cstr != NULL, "cstr = NULL\n");
507 ok(!strcmp(cstr, "test test "), "cstr = %s\n", cstr);
508 call_func1(p_basic_string_char_dtor, &ret);
510 p_basic_string_char_concatenate_cstr(&ret, &str, "passed");
511 cstr = call_func1(p_basic_string_char_cstr, &ret);
512 ok(cstr != NULL, "cstr = NULL\n");
513 ok(!strcmp(cstr, "test passed"), "cstr = %s\n", cstr);
514 call_func1(p_basic_string_char_dtor, &ret);
516 call_func1(p_basic_string_char_dtor, &str);
519 static void test_basic_string_char_find(void) {
520 basic_string_char str;
521 size_t ret;
523 call_func1(p_basic_string_char_ctor, &str);
524 call_func3(p_basic_string_char_assign_cstr_len, &str, "aaa\0bbb", 7);
525 ret = (size_t)call_func4(p_basic_string_char_find_cstr_substr, &str, "aaa", 0, 3);
526 ok(ret == 0, "ret = %lu\n", (unsigned long)ret);
527 ret = (size_t)call_func4(p_basic_string_char_find_cstr_substr, &str, "aaa", 1, 3);
528 ok(ret == -1, "ret = %lu\n", (unsigned long)ret);
529 ret = (size_t)call_func4(p_basic_string_char_find_cstr_substr, &str, "bbb", 0, 3);
530 ok(ret == 4, "ret = %lu\n", (unsigned long)ret);
531 call_func1(p_basic_string_char_dtor, &str);
534 static void test_basic_string_char_rfind(void) {
535 struct rfind_char_test {
536 const char *str;
537 const char *find;
538 size_t pos;
539 size_t len;
540 size_t ret;
543 int i;
544 basic_string_char str;
545 size_t ret;
546 struct rfind_char_test tests[] = {
547 { "", "a", 0, 1, *p_basic_string_char_npos }, /* empty string */
548 { "a", "", 0, 0, 0 }, /* empty find */
549 { "aaa", "aaa", 0, 3, 0 }, /* simple case */
550 { "aaa", "a", 0, 1, 0 }, /* start of string */
551 { "aaa", "a", 2, 1, 2 }, /* end of string */
552 { "aaa", "a", *p_basic_string_char_npos, 1, 2 }, /* off == npos */
553 { "aaa", "z", 0, 1, *p_basic_string_char_npos } /* can't find */
556 for(i=0; i<ARRAY_SIZE(tests); i++) {
557 call_func2(p_basic_string_char_ctor_cstr, &str, tests[i].str);
559 ret = (size_t)call_func4(p_basic_string_char_rfind_cstr_substr, &str,
560 tests[i].find, tests[i].pos, tests[i].len);
561 ok(ret == tests[i].ret, "str = '%s' find = '%s' ret = %lu\n",
562 tests[i].str, tests[i].find, (unsigned long)ret);
564 call_func1(p_basic_string_char_dtor, &str);
568 static void test_basic_string_char_replace(void) {
569 struct replace_char_test {
570 const char *str;
571 size_t off;
572 size_t len;
573 const char *replace;
574 const char *ret;
577 int i;
578 basic_string_char str;
579 basic_string_char *ret;
580 struct replace_char_test tests[] = {
581 { "", 0, 0, "", "" }, /* empty string */
582 { "", 0, 10, "", "" }, /* empty string with invalid len */
584 { "ABCDEF", 0, 0, "", "ABCDEF" }, /* replace with empty string */
585 { "ABCDEF", 0, 0, "-", "-ABCDEF"}, /* replace with 0 len */
586 { "ABCDEF", 0, 1, "-", "-BCDEF" }, /* replace 1 at beginning */
587 { "ABCDEF", 0, 3, "-", "-DEF" }, /* replace 3 at beginning */
588 { "ABCDEF", 0, 42, "-", "-" }, /* replace whole string with invalid long len */
589 { "ABCDEF", 0, *p_basic_string_char_npos, "-", "-" }, /* replace whole string with npos */
591 { "ABCDEF", 5, 0, "", "ABCDEF" }, /* replace at end with empty string */
592 { "ABCDEF", 5, 0, "-", "ABCDE-F"}, /* replace at end with 0 len */
593 { "ABCDEF", 5, 1, "-", "ABCDE-" }, /* replace 1 at end */
594 { "ABCDEF", 5, 42, "-", "ABCDE-" }, /* replace end with invalid long len */
595 { "ABCDEF", 5, *p_basic_string_char_npos, "-", "ABCDE-" }, /* replace end with npos */
597 { "ABCDEF", 6, 0, "", "ABCDEF" }, /* replace after end with empty string */
598 { "ABCDEF", 6, 0, "-", "ABCDEF-"}, /* replace after end with 0 len */
599 { "ABCDEF", 6, 1, "-", "ABCDEF-" }, /* replace 1 after end */
600 { "ABCDEF", 6, 42, "-", "ABCDEF-" }, /* replace after end with invalid long len */
601 { "ABCDEF", 6, *p_basic_string_char_npos, "-", "ABCDEF-" }, /* replace after end with npos */
604 for(i=0; i<ARRAY_SIZE(tests); i++) {
605 call_func2(p_basic_string_char_ctor_cstr, &str, tests[i].str);
607 ret = call_func4(p_basic_string_char_replace_cstr, &str, tests[i].off, tests[i].len, tests[i].replace);
608 ok(ret == &str, "str = %p ret = %p\n", ret, &str);
609 ok(strcmp(tests[i].ret, call_func1(p_basic_string_char_cstr, ret)) == 0, "str = %s ret = %s\n",
610 tests[i].ret, (const char*)call_func1(p_basic_string_char_cstr, ret));
612 call_func1(p_basic_string_char_dtor, &str);
616 static void test_basic_string_wchar(void) {
617 basic_string_wchar str1, str2, *pstr;
618 const wchar_t *str;
619 size_t size, capacity;
621 call_func1(p_basic_string_wchar_ctor, &str1);
622 str = NULL;
623 str = call_func1(p_basic_string_wchar_cstr, &str1);
624 ok(str != NULL, "str = NULL\n");
625 ok(*str == '\0', "*str = %c\n", *str);
626 str = call_func1(p_basic_string_wchar_data, &str1);
627 ok(str != NULL, "str = NULL\n");
628 ok(*str == '\0', "*str = %c\n", *str);
629 call_func1(p_basic_string_wchar_dtor, &str1);
631 pstr = call_func2(p_basic_string_wchar_ctor_cstr, &str1, L"test");
632 ok(pstr == &str1, "pstr != &str1\n");
633 str = call_func1(p_basic_string_wchar_cstr, &str1);
634 ok(!memcmp(str, L"test", 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
635 str = call_func1(p_basic_string_wchar_data, &str1);
636 ok(!memcmp(str, L"test", 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
637 size = (size_t)call_func1(p_basic_string_wchar_size, &str1);
638 ok(size == 4, "size = %lu\n", (unsigned long)size);
639 capacity = (size_t)call_func1(p_basic_string_wchar_capacity, &str1);
640 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
642 memset(&str2, 0, sizeof(basic_string_wchar));
643 pstr = call_func2(p_basic_string_wchar_copy_ctor, &str2, &str1);
644 ok(pstr == &str2, "pstr != &str2\n");
645 str = call_func1(p_basic_string_wchar_cstr, &str2);
646 ok(!memcmp(str, L"test", 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
647 str = call_func1(p_basic_string_wchar_data, &str2);
648 ok(!memcmp(str, L"test", 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
650 call_func3(p_basic_string_wchar_erase, &str2, 1, 2);
651 str = call_func1(p_basic_string_wchar_cstr, &str2);
652 ok(str[0]=='t' && str[1]=='t' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
653 str = call_func1(p_basic_string_wchar_data, &str2);
654 ok(str[0]=='t' && str[1]=='t' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
655 size = (size_t)call_func1(p_basic_string_wchar_size, &str1);
656 ok(size == 4, "size = %lu\n", (unsigned long)size);
657 capacity = (size_t)call_func1(p_basic_string_wchar_capacity, &str1);
658 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
660 call_func3(p_basic_string_wchar_erase, &str2, 1, 100);
661 str = call_func1(p_basic_string_wchar_cstr, &str2);
662 ok(str[0]=='t' && str[1]=='\0', "str = %s\n", wine_dbgstr_w(str));
663 str = call_func1(p_basic_string_wchar_data, &str2);
664 ok(str[0]=='t' && str[1]=='\0', "str = %s\n", wine_dbgstr_w(str));
665 size = (size_t)call_func1(p_basic_string_wchar_size, &str1);
666 ok(size == 4, "size = %lu\n", (unsigned long)size);
667 capacity = (size_t)call_func1(p_basic_string_wchar_capacity, &str1);
668 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
670 call_func3(p_basic_string_wchar_assign_cstr_len, &str2, L"test", 4);
671 str = call_func1(p_basic_string_wchar_cstr, &str2);
672 ok(!memcmp(str, L"test", 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
673 str = call_func1(p_basic_string_wchar_data, &str2);
674 ok(!memcmp(str, L"test", 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
676 call_func3(p_basic_string_wchar_assign_cstr_len, &str2, (str+1), 2);
677 str = call_func1(p_basic_string_wchar_cstr, &str2);
678 ok(str[0]=='e' && str[1]=='s' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
679 str = call_func1(p_basic_string_wchar_data, &str2);
680 ok(str[0]=='e' && str[1]=='s' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
682 call_func1(p_basic_string_wchar_dtor, &str1);
683 call_func1(p_basic_string_wchar_dtor, &str2);
686 static void test_basic_string_wchar_swap(void) {
687 basic_string_wchar str1, str2;
688 wchar_t wtmp1[32], wtmp2[32];
690 /* Swap self, local */
691 mbstowcs(wtmp1, "qwerty", 32);
692 call_func2(p_basic_string_wchar_ctor_cstr, &str1, wtmp1);
693 call_func2(p_basic_string_wchar_swap, &str1, &str1);
694 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
695 call_func2(p_basic_string_wchar_swap, &str1, &str1);
696 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
697 call_func1(p_basic_string_wchar_dtor, &str1);
699 /* str1 allocated, str2 local */
700 mbstowcs(wtmp1, "qwerty12345678901234567890", 32);
701 mbstowcs(wtmp2, "asd", 32);
702 call_func2(p_basic_string_wchar_ctor_cstr, &str1, wtmp1);
703 call_func2(p_basic_string_wchar_ctor_cstr, &str2, wtmp2);
704 call_func2(p_basic_string_wchar_swap, &str1, &str2);
705 ok(wcscmp(wtmp2, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
706 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str2)) == 0, "Invalid value of str2\n");
707 call_func2(p_basic_string_wchar_swap, &str1, &str2);
708 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
709 ok(wcscmp(wtmp2, call_func1(p_basic_string_wchar_cstr, &str2)) == 0, "Invalid value of str2\n");
710 call_func1(p_basic_string_wchar_dtor, &str1);
711 call_func1(p_basic_string_wchar_dtor, &str2);
714 static void test_basic_string_char_find_last_not_of(void) {
715 struct find_last_not_of_test {
716 const char *str;
717 const char *find;
718 size_t off;
719 size_t len;
720 size_t ret;
723 int i;
724 size_t ret;
725 basic_string_char str;
726 struct find_last_not_of_test tests[] = {
727 /* simple cases where find is not in string */
728 { "AAAAA", "B", 0, 1, 0 },
729 { "AAAAA", "B", 5, 1, 4 },
730 { "AAAAA", "BCDE", 0, 4, 0 },
731 { "AAAAA", "BCDE", 5, 4, 4 },
733 /* simple cases where find is in string */
734 { "AAAAA", "A", 5, 1, -1 },
735 { "AAAAB", "A", 5, 1, 4 },
736 { "AAAAB", "A", 4, 1, 4 },
737 { "AAAAB", "A", 3, 1, -1 },
738 { "ABCDE", "ABCDE", 0, 5, -1 },
739 { "ABCDE", "ABCDE", 5, 5, -1 },
740 { "ABCDE", "AB DE", 5, 5, 2 },
742 /* cases where find appears in multiple spots */
743 { "ABABA", "A", 0, 1, -1 },
744 { "ABABA", "A", 1, 1, 1 },
745 { "ABABA", "A", 2, 1, 1 },
746 { "ABABA", "A", 3, 1, 3 },
748 /* using empty strings */
749 { "", "", 0, 0, -1 },
750 { "", "A", 0, 1, -1 },
751 { "ABCDE", "", 0, 0, 0 },
752 { "ABCDE", "", 3, 0, 3 },
753 { "ABCDE", "", 5, 0, 4 },
756 for(i=0; i<ARRAY_SIZE(tests); i++) {
757 call_func2(p_basic_string_char_ctor_cstr, &str, tests[i].str);
759 ret = (size_t)call_func4(p_basic_string_char_find_last_not_of_cstr_substr,
760 &str, tests[i].find, tests[i].off, tests[i].len);
761 ok(ret == tests[i].ret, "ret = %li tests[%i].ret = %li\n", (long)ret, i, (long)tests[i].ret);
763 call_func1(p_basic_string_char_dtor, &str);
767 static void test_basic_string_dtor(void) {
768 #ifdef __i386__
769 basic_string_wchar str1;
770 basic_string_char str2;
771 void *ret;
773 /* FEAR 1 installer expects that string destructors set EAX to
774 * zero on return (see bug 37358). */
776 call_func2(p_basic_string_wchar_ctor_cstr, &str1, L"qwerty");
777 ret = call_func1(p_basic_string_wchar_dtor, &str1);
778 ok(ret == NULL, "expected NULL, got %p\n", ret);
780 call_func2(p_basic_string_char_ctor_cstr, &str2, "qwerty");
781 ret = call_func1(p_basic_string_char_dtor, &str2);
782 ok(ret == NULL, "expected NULL, got %p\n", ret);
783 #endif
786 START_TEST(string)
788 if(!init())
789 return;
791 test_basic_string_char();
792 test_basic_string_char_swap();
793 test_basic_string_char_append();
794 test_basic_string_char_compare();
795 test_basic_string_char_concatenate();
796 test_basic_string_char_find();
797 test_basic_string_char_rfind();
798 test_basic_string_char_replace();
799 test_basic_string_wchar();
800 test_basic_string_wchar_swap();
801 test_basic_string_char_find_last_not_of();
802 test_basic_string_dtor();
804 ok(!invalid_parameter, "invalid_parameter_handler was invoked too many times\n");
806 FreeLibrary(msvcr);
807 FreeLibrary(msvcp);