Release 2.13.
[wine.git] / dlls / msvcp60 / tests / string.c
blobf61ad4af9a19391ad3e0826a8306805b8c86ba61
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 typedef SIZE_T MSVCP_size_t;
28 /* basic_string<char, char_traits<char>, allocator<char>> */
29 typedef struct
31 void *allocator;
32 char *ptr;
33 MSVCP_size_t size;
34 MSVCP_size_t res;
35 } basic_string_char;
37 /* basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>> */
38 typedef struct
40 void *allocator;
41 wchar_t *ptr;
42 MSVCP_size_t size;
43 MSVCP_size_t res;
44 } basic_string_wchar;
46 static basic_string_char* (__cdecl *p_basic_string_char_concatenate)(basic_string_char*, const basic_string_char*, const basic_string_char*);
47 static basic_string_char* (__cdecl *p_basic_string_char_concatenate_cstr)(basic_string_char*, const basic_string_char*, const char*);
49 #undef __thiscall
50 #ifdef __i386__
51 #define __thiscall __stdcall
52 #else
53 #define __thiscall __cdecl
54 #endif
56 static basic_string_char* (__thiscall *p_basic_string_char_ctor)(basic_string_char*);
57 static basic_string_char* (__thiscall *p_basic_string_char_copy_ctor)(basic_string_char*, basic_string_char*);
58 static basic_string_char* (__thiscall *p_basic_string_char_ctor_cstr_alloc)(basic_string_char*, const char*, void*);
59 static void (__thiscall *p_basic_string_char_dtor)(basic_string_char*);
60 static basic_string_char* (__thiscall *p_basic_string_char_erase)(basic_string_char*, size_t, size_t);
61 static basic_string_char* (__thiscall *p_basic_string_char_assign_cstr_len)(basic_string_char*, const char*, size_t);
62 static const char* (__thiscall *p_basic_string_char_cstr)(basic_string_char*);
63 static const char* (__thiscall *p_basic_string_char_data)(basic_string_char*);
64 static size_t (__thiscall *p_basic_string_char_size)(basic_string_char*);
65 static size_t (__thiscall *p_basic_string_char_capacity)(basic_string_char*);
66 static void (__thiscall *p_basic_string_char_swap)(basic_string_char*, basic_string_char*);
67 static basic_string_char* (__thiscall *p_basic_string_char_append)(basic_string_char*, basic_string_char*);
68 static basic_string_char* (__thiscall *p_basic_string_char_append_substr)(basic_string_char*, basic_string_char*, size_t, size_t);
69 static int (__thiscall *p_basic_string_char_compare_substr_substr)(basic_string_char*, size_t, size_t, basic_string_char*, size_t, size_t);
70 static int (__thiscall *p_basic_string_char_compare_substr_cstr_len)(basic_string_char*, size_t, size_t, const char*, size_t);
71 static size_t (__thiscall *p_basic_string_char_find_cstr_substr)(basic_string_char*, const char*, size_t, size_t);
72 static size_t (__thiscall *p_basic_string_char_rfind_cstr_substr)(basic_string_char*, const char*, size_t, size_t);
73 static basic_string_char* (__thiscall *p_basic_string_char_replace_cstr)(basic_string_char*, size_t, size_t, const char*);
75 static size_t *p_basic_string_char_npos;
77 static basic_string_wchar* (__thiscall *p_basic_string_wchar_ctor)(basic_string_wchar*);
78 static basic_string_wchar* (__thiscall *p_basic_string_wchar_copy_ctor)(basic_string_wchar*, basic_string_wchar*);
79 static basic_string_wchar* (__thiscall *p_basic_string_wchar_ctor_cstr_alloc)(basic_string_wchar*, const wchar_t*, void*);
80 static void (__thiscall *p_basic_string_wchar_dtor)(basic_string_wchar*);
81 static basic_string_wchar* (__thiscall *p_basic_string_wchar_erase)(basic_string_wchar*, size_t, size_t);
82 static basic_string_wchar* (__thiscall *p_basic_string_wchar_assign_cstr_len)(basic_string_wchar*, const wchar_t*, size_t);
83 static const wchar_t* (__thiscall *p_basic_string_wchar_cstr)(basic_string_wchar*);
84 static const wchar_t* (__thiscall *p_basic_string_wchar_data)(basic_string_wchar*);
85 static size_t (__thiscall *p_basic_string_wchar_size)(basic_string_wchar*);
86 static size_t (__thiscall *p_basic_string_wchar_capacity)(basic_string_wchar*);
87 static void (__thiscall *p_basic_string_wchar_swap)(basic_string_wchar*, basic_string_wchar*);
89 /* Emulate a __thiscall */
90 #ifdef __i386__
92 #include "pshpack1.h"
93 struct thiscall_thunk
95 BYTE pop_eax; /* popl %eax (ret addr) */
96 BYTE pop_edx; /* popl %edx (func) */
97 BYTE pop_ecx; /* popl %ecx (this) */
98 BYTE push_eax; /* pushl %eax */
99 WORD jmp_edx; /* jmp *%edx */
101 #include "poppack.h"
103 static void * (WINAPI *call_thiscall_func1)( void *func, void *this );
104 static void * (WINAPI *call_thiscall_func2)( void *func, void *this, const void *a );
105 static void * (WINAPI *call_thiscall_func3)( void *func, void *this, const void *a, const void *b );
106 static void * (WINAPI *call_thiscall_func4)( void *func, void *this, const void *a, const void *b,
107 const void *c );
108 static void * (WINAPI *call_thiscall_func5)( void *func, void *this, const void *a, const void *b,
109 const void *c, const void *d );
110 static void * (WINAPI *call_thiscall_func6)( void *func, void *this, const void *a, const void *b,
111 const void *c, const void *d, const void *e );
113 static void init_thiscall_thunk(void)
115 struct thiscall_thunk *thunk = VirtualAlloc( NULL, sizeof(*thunk),
116 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
117 thunk->pop_eax = 0x58; /* popl %eax */
118 thunk->pop_edx = 0x5a; /* popl %edx */
119 thunk->pop_ecx = 0x59; /* popl %ecx */
120 thunk->push_eax = 0x50; /* pushl %eax */
121 thunk->jmp_edx = 0xe2ff; /* jmp *%edx */
122 call_thiscall_func1 = (void *)thunk;
123 call_thiscall_func2 = (void *)thunk;
124 call_thiscall_func3 = (void *)thunk;
125 call_thiscall_func4 = (void *)thunk;
126 call_thiscall_func5 = (void *)thunk;
127 call_thiscall_func6 = (void *)thunk;
130 #define call_func1(func,_this) call_thiscall_func1(func,_this)
131 #define call_func2(func,_this,a) call_thiscall_func2(func,_this,(const void*)(a))
132 #define call_func3(func,_this,a,b) call_thiscall_func3(func,_this,(const void*)(a),(const void*)(b))
133 #define call_func4(func,_this,a,b,c) call_thiscall_func4(func,_this,(const void*)(a),\
134 (const void*)(b),(const void*)(c))
135 #define call_func5(func,_this,a,b,c,d) call_thiscall_func5(func,_this,(const void*)(a),\
136 (const void*)(b),(const void*)(c),(const void*)(d))
137 #define call_func6(func,_this,a,b,c,d,e) call_thiscall_func6(func,_this,(const void*)(a),\
138 (const void*)(b),(const void*)(c),(const void*)(d),(const void*)(e))
140 #else
142 #define init_thiscall_thunk()
143 #define call_func1(func,_this) func(_this)
144 #define call_func2(func,_this,a) func(_this,a)
145 #define call_func3(func,_this,a,b) func(_this,a,b)
146 #define call_func4(func,_this,a,b,c) func(_this,a,b,c)
147 #define call_func5(func,_this,a,b,c,d) func(_this,a,b,c,d)
148 #define call_func6(func,_this,a,b,c,d,e) func(_this,a,b,c,d,e)
150 #endif /* __i386__ */
152 static HMODULE msvcp;
153 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(msvcp,y)
154 #define SET(x,y) do { SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y); } while(0)
155 static BOOL init(void)
157 msvcp = LoadLibraryA("msvcp60.dll");
158 if(!msvcp) {
159 win_skip("msvcp60.dll not installed\n");
160 return FALSE;
163 if(sizeof(void*) == 8) { /* 64-bit initialization */
164 SET(p_basic_string_char_ctor,
165 "??_F?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAXXZ");
166 SET(p_basic_string_char_copy_ctor,
167 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@AEBV01@@Z");
168 SET(p_basic_string_char_ctor_cstr_alloc,
169 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@PEBDAEBV?$allocator@D@1@@Z");
170 SET(p_basic_string_char_dtor,
171 "??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@XZ");
172 SET(p_basic_string_char_erase,
173 "?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@_K0@Z");
174 SET(p_basic_string_char_assign_cstr_len,
175 "?assign@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@PEBD_K@Z");
176 SET(p_basic_string_char_cstr,
177 "?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAPEBDXZ");
178 SET(p_basic_string_char_data,
179 "?data@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAPEBDXZ");
180 SET(p_basic_string_char_size,
181 "?size@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KXZ");
182 SET(p_basic_string_char_capacity,
183 "?capacity@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KXZ");
184 SET(p_basic_string_char_swap,
185 "?swap@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAXAEAV12@@Z");
186 SET(p_basic_string_char_append,
187 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@AEBV12@@Z");
188 SET(p_basic_string_char_append_substr,
189 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@AEBV12@_K1@Z");
190 SET(p_basic_string_char_compare_substr_substr,
191 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAH_K0AEBV12@00@Z");
192 SET(p_basic_string_char_compare_substr_cstr_len,
193 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAH_K0PEBD0@Z");
194 SETNOFAIL(p_basic_string_char_concatenate,
195 "??$?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");
196 SETNOFAIL(p_basic_string_char_concatenate_cstr,
197 "??$?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");
198 SET(p_basic_string_char_find_cstr_substr,
199 "?find@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KPEBD_K1@Z");
200 SET(p_basic_string_char_rfind_cstr_substr,
201 "?rfind@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBA_KPEBD_K1@Z");
202 SET(p_basic_string_char_replace_cstr,
203 "?replace@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAAAEAV12@_K0PEBD@Z");
204 SET(p_basic_string_char_npos,
205 "?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2_KB");
207 SET(p_basic_string_wchar_ctor,
208 "??_F?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEAAXXZ");
209 SET(p_basic_string_wchar_copy_ctor,
210 "??0?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEAA@AEBV01@@Z");
211 SET(p_basic_string_wchar_ctor_cstr_alloc,
212 "??0?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEAA@PEBGAEBV?$allocator@G@1@@Z");
213 SET(p_basic_string_wchar_dtor,
214 "??1?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEAA@XZ");
215 SET(p_basic_string_wchar_erase,
216 "?erase@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEAAAEAV12@_K0@Z");
217 SET(p_basic_string_wchar_assign_cstr_len,
218 "?assign@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEAAAEAV12@PEBG_K@Z");
219 SET(p_basic_string_wchar_cstr,
220 "?c_str@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEBAPEBGXZ");
221 SET(p_basic_string_wchar_data,
222 "?data@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEBAPEBGXZ");
223 SET(p_basic_string_wchar_size,
224 "?size@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEBA_KXZ");
225 SET(p_basic_string_wchar_capacity,
226 "?capacity@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEBA_KXZ");
227 SET(p_basic_string_wchar_swap,
228 "?swap@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QEAAXAEAV12@@Z");
229 } else {
230 SET(p_basic_string_char_ctor,
231 "??_F?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEXXZ");
232 SET(p_basic_string_char_copy_ctor,
233 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z");
234 SET(p_basic_string_char_ctor_cstr_alloc,
235 "??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBDABV?$allocator@D@1@@Z");
236 SET(p_basic_string_char_dtor,
237 "??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ");
238 SET(p_basic_string_char_erase,
239 "?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@II@Z");
240 SET(p_basic_string_char_assign_cstr_len,
241 "?assign@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@PBDI@Z");
242 SET(p_basic_string_char_cstr,
243 "?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ");
244 SET(p_basic_string_char_data,
245 "?data@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ");
246 SET(p_basic_string_char_size,
247 "?size@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIXZ");
248 SET(p_basic_string_char_capacity,
249 "?capacity@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIXZ");
250 SET(p_basic_string_char_swap,
251 "?swap@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEXAAV12@@Z");
252 SET(p_basic_string_char_append,
253 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@ABV12@@Z");
254 SET(p_basic_string_char_append_substr,
255 "?append@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@ABV12@II@Z");
256 SET(p_basic_string_char_compare_substr_substr,
257 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEHIIABV12@II@Z");
258 SET(p_basic_string_char_compare_substr_cstr_len,
259 "?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEHIIPBDI@Z");
260 SETNOFAIL(p_basic_string_char_concatenate,
261 "??$?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");
262 SETNOFAIL(p_basic_string_char_concatenate_cstr,
263 "??$?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");
264 SET(p_basic_string_char_find_cstr_substr,
265 "?find@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIPBDII@Z");
266 SET(p_basic_string_char_rfind_cstr_substr,
267 "?rfind@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIPBDII@Z");
268 SET(p_basic_string_char_replace_cstr,
269 "?replace@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@IIPBD@Z");
270 SET(p_basic_string_char_npos,
271 "?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2IB");
273 SET(p_basic_string_wchar_ctor,
274 "??_F?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAEXXZ");
275 SET(p_basic_string_wchar_copy_ctor,
276 "??0?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAE@ABV01@@Z");
277 SET(p_basic_string_wchar_ctor_cstr_alloc,
278 "??0?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAE@PBGABV?$allocator@G@1@@Z");
279 SET(p_basic_string_wchar_dtor,
280 "??1?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAE@XZ");
281 SET(p_basic_string_wchar_erase,
282 "?erase@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAEAAV12@II@Z");
283 SET(p_basic_string_wchar_assign_cstr_len,
284 "?assign@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAEAAV12@PBGI@Z");
285 SET(p_basic_string_wchar_cstr,
286 "?c_str@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QBEPBGXZ");
287 SET(p_basic_string_wchar_data,
288 "?data@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QBEPBGXZ");
289 SET(p_basic_string_wchar_size,
290 "?size@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QBEIXZ");
291 SET(p_basic_string_wchar_capacity,
292 "?capacity@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QBEIXZ");
293 SET(p_basic_string_wchar_swap,
294 "?swap@?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAEXAAV12@@Z");
297 init_thiscall_thunk();
298 return TRUE;
301 static char fake_allocator;
303 static void test_basic_string_char(void) {
304 basic_string_char str1, str2, *pstr;
305 const char *str;
306 size_t size, capacity;
308 call_func1(p_basic_string_char_ctor, &str1);
309 str = NULL;
310 str = call_func1(p_basic_string_char_cstr, &str1);
311 ok(str != NULL, "str = NULL\n");
312 ok(*str == '\0', "*str = %c\n", *str);
313 str = call_func1(p_basic_string_char_data, &str1);
314 ok(str != NULL, "str = NULL\n");
315 ok(*str == '\0', "*str = %c\n", *str);
316 call_func1(p_basic_string_char_dtor, &str1);
318 pstr = call_func3(p_basic_string_char_ctor_cstr_alloc, &str1, "test", &fake_allocator);
319 ok(pstr == &str1, "pstr != &str1\n");
320 str = call_func1(p_basic_string_char_cstr, &str1);
321 ok(!memcmp(str, "test", 5), "str = %s\n", str);
322 str = call_func1(p_basic_string_char_data, &str1);
323 ok(!memcmp(str, "test", 5), "str = %s\n", str);
324 size = (size_t)call_func1(p_basic_string_char_size, &str1);
325 ok(size == 4, "size = %lu\n", (unsigned long)size);
326 capacity = (size_t)call_func1(p_basic_string_char_capacity, &str1);
327 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
329 pstr = call_func2(p_basic_string_char_copy_ctor, &str2, &str1);
330 ok(pstr == &str2, "pstr != &str2\n");
331 str = call_func1(p_basic_string_char_cstr, &str2);
332 ok(!memcmp(str, "test", 5), "str = %s\n", str);
333 str = call_func1(p_basic_string_char_data, &str2);
334 ok(!memcmp(str, "test", 5), "str = %s\n", str);
336 call_func3(p_basic_string_char_erase, &str2, 1, 2);
337 str = call_func1(p_basic_string_char_cstr, &str2);
338 ok(!memcmp(str, "tt", 3), "str = %s\n", str);
339 str = call_func1(p_basic_string_char_data, &str2);
340 ok(!memcmp(str, "tt", 3), "str = %s\n", str);
341 size = (size_t)call_func1(p_basic_string_char_size, &str1);
342 ok(size == 4, "size = %lu\n", (unsigned long)size);
343 capacity = (size_t)call_func1(p_basic_string_char_capacity, &str1);
344 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
346 call_func3(p_basic_string_char_erase, &str2, 1, 100);
347 str = call_func1(p_basic_string_char_cstr, &str2);
348 ok(!memcmp(str, "t", 2), "str = %s\n", str);
349 str = call_func1(p_basic_string_char_data, &str2);
350 ok(!memcmp(str, "t", 2), "str = %s\n", str);
351 size = (size_t)call_func1(p_basic_string_char_size, &str1);
352 ok(size == 4, "size = %lu\n", (unsigned long)size);
353 capacity = (size_t)call_func1(p_basic_string_char_capacity, &str1);
354 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
356 call_func3(p_basic_string_char_assign_cstr_len, &str2, "test", 4);
357 str = call_func1(p_basic_string_char_cstr, &str2);
358 ok(!memcmp(str, "test", 5), "str = %s\n", str);
359 str = call_func1(p_basic_string_char_data, &str2);
360 ok(!memcmp(str, "test", 5), "str = %s\n", str);
362 call_func3(p_basic_string_char_assign_cstr_len, &str2, (str+1), 2);
363 str = call_func1(p_basic_string_char_cstr, &str2);
364 ok(!memcmp(str, "es", 3), "str = %s\n", str);
365 str = call_func1(p_basic_string_char_data, &str2);
366 ok(!memcmp(str, "es", 3), "str = %s\n", str);
368 call_func1(p_basic_string_char_dtor, &str1);
369 call_func1(p_basic_string_char_dtor, &str2);
372 static void test_basic_string_char_swap(void) {
373 basic_string_char str1, str2;
374 char atmp1[32], atmp2[32];
376 /* Swap self, local */
377 strcpy(atmp1, "qwerty");
378 call_func3(p_basic_string_char_ctor_cstr_alloc, &str1, atmp1, &fake_allocator);
379 call_func2(p_basic_string_char_swap, &str1, &str1);
380 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
381 call_func2(p_basic_string_char_swap, &str1, &str1);
382 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
383 call_func1(p_basic_string_char_dtor, &str1);
385 /* str1 allocated, str2 local */
386 strcpy(atmp1, "qwerty12345678901234567890");
387 strcpy(atmp2, "asd");
388 call_func3(p_basic_string_char_ctor_cstr_alloc, &str1, atmp1, &fake_allocator);
389 call_func3(p_basic_string_char_ctor_cstr_alloc, &str2, atmp2, &fake_allocator);
390 call_func2(p_basic_string_char_swap, &str1, &str2);
391 ok(strcmp(atmp2, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
392 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str2)) == 0, "Invalid value of str2\n");
393 call_func2(p_basic_string_char_swap, &str1, &str2);
394 ok(strcmp(atmp1, call_func1(p_basic_string_char_cstr, &str1)) == 0, "Invalid value of str1\n");
395 ok(strcmp(atmp2, call_func1(p_basic_string_char_cstr, &str2)) == 0, "Invalid value of str2\n");
396 call_func1(p_basic_string_char_dtor, &str1);
397 call_func1(p_basic_string_char_dtor, &str2);
400 static void test_basic_string_char_append(void) {
401 basic_string_char str1, str2;
402 const char *str;
404 call_func3(p_basic_string_char_ctor_cstr_alloc, &str1, "", &fake_allocator);
405 call_func3(p_basic_string_char_ctor_cstr_alloc, &str2, "append", &fake_allocator);
407 call_func2(p_basic_string_char_append, &str1, &str2);
408 str = call_func1(p_basic_string_char_cstr, &str1);
409 ok(!memcmp(str, "append", 7), "str = %s\n", str);
411 call_func4(p_basic_string_char_append_substr, &str1, &str2, 3, 1);
412 str = call_func1(p_basic_string_char_cstr, &str1);
413 ok(!memcmp(str, "appende", 8), "str = %s\n", str);
415 call_func4(p_basic_string_char_append_substr, &str1, &str2, 5, 100);
416 str = call_func1(p_basic_string_char_cstr, &str1);
417 ok(!memcmp(str, "appended", 9), "str = %s\n", str);
419 call_func4(p_basic_string_char_append_substr, &str1, &str2, 6, 100);
420 str = call_func1(p_basic_string_char_cstr, &str1);
421 ok(!memcmp(str, "appended", 9), "str = %s\n", str);
423 call_func1(p_basic_string_char_dtor, &str1);
424 call_func1(p_basic_string_char_dtor, &str2);
427 static void test_basic_string_char_compare(void) {
428 basic_string_char str1, str2, str3;
429 int ret;
431 call_func3(p_basic_string_char_ctor_cstr_alloc, &str1, "str1str", &fake_allocator);
432 call_func3(p_basic_string_char_ctor_cstr_alloc, &str2, "str9str", &fake_allocator);
433 call_func3(p_basic_string_char_ctor_cstr_alloc, &str3, "splash.png", &fake_allocator);
435 ret = (int)call_func6(p_basic_string_char_compare_substr_substr,
436 &str1, 0, 3, &str2, 0, 3);
437 ok(ret == 0, "ret = %d\n", ret);
438 ret = (int)call_func6(p_basic_string_char_compare_substr_substr,
439 &str1, 4, 3, &str2, 4, 10);
440 ok(ret == 0, "ret = %d\n", ret);
441 ret = (int)call_func6(p_basic_string_char_compare_substr_substr,
442 &str1, 1, 3, &str2, 1, 4);
443 ok(ret == -1, "ret = %d\n", ret);
445 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
446 &str1, 0, 1000, "str1str", 7);
447 ok(ret == 0, "ret = %d\n", ret);
448 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
449 &str3, 6, UINT_MAX, ".png", 4);
450 ok(ret == 0, "ret = %d\n", ret);
451 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
452 &str1, 1, 2, "tr", 2);
453 ok(ret == 0, "ret = %d\n", ret);
454 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
455 &str1, 1, 0, "aaa", 0);
456 ok(ret == 0, "ret = %d\n", ret);
457 ret = (int)call_func5(p_basic_string_char_compare_substr_cstr_len,
458 &str1, 1, 0, "aaa", 1);
459 ok(ret == -1, "ret = %d\n", ret);
461 call_func1(p_basic_string_char_dtor, &str1);
462 call_func1(p_basic_string_char_dtor, &str2);
463 call_func1(p_basic_string_char_dtor, &str3);
466 static void test_basic_string_char_concatenate(void) {
467 basic_string_char str, ret;
468 const char *cstr;
470 if(!p_basic_string_char_concatenate || !p_basic_string_char_concatenate_cstr) {
471 win_skip("basic_string<char>::concatenate is not available\n");
472 return;
475 call_func3(p_basic_string_char_ctor_cstr_alloc, &str, "test ", &fake_allocator);
476 /* CDECL calling convention with return bigger than 8 bytes */
477 p_basic_string_char_concatenate(&ret, &str, &str);
478 cstr = call_func1(p_basic_string_char_cstr, &ret);
479 ok(cstr != NULL, "cstr = NULL\n");
480 ok(!strcmp(cstr, "test test "), "cstr = %s\n", cstr);
481 call_func1(p_basic_string_char_dtor, &ret);
483 p_basic_string_char_concatenate_cstr(&ret, &str, "passed");
484 cstr = call_func1(p_basic_string_char_cstr, &ret);
485 ok(cstr != NULL, "cstr = NULL\n");
486 ok(!strcmp(cstr, "test passed"), "cstr = %s\n", cstr);
487 call_func1(p_basic_string_char_dtor, &ret);
489 call_func1(p_basic_string_char_dtor, &str);
492 static void test_basic_string_char_find(void) {
493 static const char tmp[] = {'a','a','a','\0','b','b','b',0};
494 basic_string_char str;
495 size_t ret;
497 call_func1(p_basic_string_char_ctor, &str);
498 call_func3(p_basic_string_char_assign_cstr_len, &str, tmp, 7);
499 ret = (size_t)call_func4(p_basic_string_char_find_cstr_substr, &str, "aaa", 0, 3);
500 ok(ret == 0, "ret = %lu\n", (unsigned long)ret);
501 ret = (size_t)call_func4(p_basic_string_char_find_cstr_substr, &str, "aaa", 1, 3);
502 ok(ret == -1, "ret = %lu\n", (unsigned long)ret);
503 ret = (size_t)call_func4(p_basic_string_char_find_cstr_substr, &str, "bbb", 0, 3);
504 ok(ret == 4, "ret = %lu\n", (unsigned long)ret);
505 call_func1(p_basic_string_char_dtor, &str);
508 static void test_basic_string_char_rfind(void) {
509 struct rfind_char_test {
510 const char *str;
511 const char *find;
512 size_t pos;
513 size_t len;
514 size_t ret;
517 int i;
518 basic_string_char str;
519 size_t ret;
520 struct rfind_char_test tests[] = {
521 { "", "a", 0, 1, *p_basic_string_char_npos }, /* empty string */
522 { "a", "", 0, 0, 0 }, /* empty find */
523 { "aaa", "aaa", 0, 3, 0 }, /* simple case */
524 { "aaa", "a", 0, 1, 0 }, /* start of string */
525 { "aaa", "a", 2, 1, 2 }, /* end of string */
526 { "aaa", "a", *p_basic_string_char_npos, 1, 2 }, /* off == npos */
527 { "aaa", "z", 0, 1, *p_basic_string_char_npos } /* can't find */
530 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
531 call_func3(p_basic_string_char_ctor_cstr_alloc, &str, tests[i].str, &fake_allocator);
533 ret = (size_t)call_func4(p_basic_string_char_rfind_cstr_substr, &str,
534 tests[i].find, tests[i].pos, tests[i].len);
535 ok(ret == tests[i].ret, "str = '%s' find = '%s' ret = %lu\n",
536 tests[i].str, tests[i].find, (unsigned long)ret);
538 call_func1(p_basic_string_char_dtor, &str);
542 static void test_basic_string_char_replace(void) {
543 struct replace_char_test {
544 const char *str;
545 size_t off;
546 size_t len;
547 const char *replace;
548 const char *ret;
551 int i;
552 basic_string_char str;
553 basic_string_char *ret;
554 struct replace_char_test tests[] = {
555 { "", 0, 0, "", "" }, /* empty string */
556 { "", 0, 10, "", "" }, /* empty string with invalid len */
558 { "ABCDEF", 0, 0, "", "ABCDEF" }, /* replace with empty string */
559 { "ABCDEF", 0, 0, "-", "-ABCDEF"}, /* replace with 0 len */
560 { "ABCDEF", 0, 1, "-", "-BCDEF" }, /* replace 1 at beginning */
561 { "ABCDEF", 0, 3, "-", "-DEF" }, /* replace 3 at beginning */
562 { "ABCDEF", 0, 42, "-", "-" }, /* replace whole string with invalid long len */
563 { "ABCDEF", 0, *p_basic_string_char_npos, "-", "-" }, /* replace whole string with npos */
565 { "ABCDEF", 5, 0, "", "ABCDEF" }, /* replace at end with empty string */
566 { "ABCDEF", 5, 0, "-", "ABCDE-F"}, /* replace at end with 0 len */
567 { "ABCDEF", 5, 1, "-", "ABCDE-" }, /* replace 1 at end */
568 { "ABCDEF", 5, 42, "-", "ABCDE-" }, /* replace end with invalid long len */
569 { "ABCDEF", 5, *p_basic_string_char_npos, "-", "ABCDE-" }, /* replace end with npos */
571 { "ABCDEF", 6, 0, "", "ABCDEF" }, /* replace after end with empty string */
572 { "ABCDEF", 6, 0, "-", "ABCDEF-"}, /* replace after end with 0 len */
573 { "ABCDEF", 6, 1, "-", "ABCDEF-" }, /* replace 1 after end */
574 { "ABCDEF", 6, 42, "-", "ABCDEF-" }, /* replace after end with invalid long len */
575 { "ABCDEF", 6, *p_basic_string_char_npos, "-", "ABCDEF-" }, /* replace after end with npos */
578 for(i=0; i<sizeof(tests)/sizeof(tests[0]); i++) {
579 call_func3(p_basic_string_char_ctor_cstr_alloc, &str, tests[i].str, &fake_allocator);
581 ret = call_func4(p_basic_string_char_replace_cstr, &str, tests[i].off, tests[i].len, tests[i].replace);
582 ok(ret == &str, "str = %p ret = %p\n", ret, &str);
583 ok(strcmp(tests[i].ret, call_func1(p_basic_string_char_cstr, ret)) == 0, "str = %s ret = %s\n",
584 tests[i].ret, (const char*)call_func1(p_basic_string_char_cstr, ret));
586 call_func1(p_basic_string_char_dtor, &str);
590 static void test_basic_string_wchar(void) {
591 static const wchar_t test[] = { 't','e','s','t',0 };
593 basic_string_wchar str1, str2, *pstr;
594 const wchar_t *str;
595 size_t size, capacity;
597 call_func1(p_basic_string_wchar_ctor, &str1);
598 str = NULL;
599 str = call_func1(p_basic_string_wchar_cstr, &str1);
600 ok(str != NULL, "str = NULL\n");
601 ok(*str == '\0', "*str = %c\n", *str);
602 str = call_func1(p_basic_string_wchar_data, &str1);
603 ok(str != NULL, "str = NULL\n");
604 ok(*str == '\0', "*str = %c\n", *str);
605 call_func1(p_basic_string_wchar_dtor, &str1);
607 pstr = call_func3(p_basic_string_wchar_ctor_cstr_alloc, &str1, test, &fake_allocator);
608 ok(pstr == &str1, "pstr != &str1\n");
609 str = call_func1(p_basic_string_wchar_cstr, &str1);
610 ok(!memcmp(str, test, 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
611 str = call_func1(p_basic_string_wchar_data, &str1);
612 ok(!memcmp(str, test, 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
613 size = (size_t)call_func1(p_basic_string_wchar_size, &str1);
614 ok(size == 4, "size = %lu\n", (unsigned long)size);
615 capacity = (size_t)call_func1(p_basic_string_wchar_capacity, &str1);
616 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
618 memset(&str2, 0, sizeof(basic_string_wchar));
619 pstr = call_func2(p_basic_string_wchar_copy_ctor, &str2, &str1);
620 ok(pstr == &str2, "pstr != &str2\n");
621 str = call_func1(p_basic_string_wchar_cstr, &str2);
622 ok(!memcmp(str, test, 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
623 str = call_func1(p_basic_string_wchar_data, &str2);
624 ok(!memcmp(str, test, 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
626 call_func3(p_basic_string_wchar_erase, &str2, 1, 2);
627 str = call_func1(p_basic_string_wchar_cstr, &str2);
628 ok(str[0]=='t' && str[1]=='t' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
629 str = call_func1(p_basic_string_wchar_data, &str2);
630 ok(str[0]=='t' && str[1]=='t' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
631 size = (size_t)call_func1(p_basic_string_wchar_size, &str1);
632 ok(size == 4, "size = %lu\n", (unsigned long)size);
633 capacity = (size_t)call_func1(p_basic_string_wchar_capacity, &str1);
634 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
636 call_func3(p_basic_string_wchar_erase, &str2, 1, 100);
637 str = call_func1(p_basic_string_wchar_cstr, &str2);
638 ok(str[0]=='t' && str[1]=='\0', "str = %s\n", wine_dbgstr_w(str));
639 str = call_func1(p_basic_string_wchar_data, &str2);
640 ok(str[0]=='t' && str[1]=='\0', "str = %s\n", wine_dbgstr_w(str));
641 size = (size_t)call_func1(p_basic_string_wchar_size, &str1);
642 ok(size == 4, "size = %lu\n", (unsigned long)size);
643 capacity = (size_t)call_func1(p_basic_string_wchar_capacity, &str1);
644 ok(capacity >= size, "capacity = %lu < size = %lu\n", (unsigned long)capacity, (unsigned long)size);
646 call_func3(p_basic_string_wchar_assign_cstr_len, &str2, test, 4);
647 str = call_func1(p_basic_string_wchar_cstr, &str2);
648 ok(!memcmp(str, test, 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
649 str = call_func1(p_basic_string_wchar_data, &str2);
650 ok(!memcmp(str, test, 5*sizeof(wchar_t)), "str = %s\n", wine_dbgstr_w(str));
652 call_func3(p_basic_string_wchar_assign_cstr_len, &str2, (str+1), 2);
653 str = call_func1(p_basic_string_wchar_cstr, &str2);
654 ok(str[0]=='e' && str[1]=='s' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
655 str = call_func1(p_basic_string_wchar_data, &str2);
656 ok(str[0]=='e' && str[1]=='s' && str[2]=='\0', "str = %s\n", wine_dbgstr_w(str));
658 call_func1(p_basic_string_wchar_dtor, &str1);
659 call_func1(p_basic_string_wchar_dtor, &str2);
662 static void test_basic_string_wchar_swap(void) {
663 basic_string_wchar str1, str2;
664 wchar_t wtmp1[32], wtmp2[32];
666 /* Swap self, local */
667 mbstowcs(wtmp1, "qwerty", 32);
668 call_func3(p_basic_string_wchar_ctor_cstr_alloc, &str1, wtmp1, &fake_allocator);
669 call_func2(p_basic_string_wchar_swap, &str1, &str1);
670 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
671 call_func2(p_basic_string_wchar_swap, &str1, &str1);
672 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
673 call_func1(p_basic_string_wchar_dtor, &str1);
675 /* str1 allocated, str2 local */
676 mbstowcs(wtmp1, "qwerty12345678901234567890", 32);
677 mbstowcs(wtmp2, "asd", 32);
678 call_func3(p_basic_string_wchar_ctor_cstr_alloc, &str1, wtmp1, &fake_allocator);
679 call_func3(p_basic_string_wchar_ctor_cstr_alloc, &str2, wtmp2, &fake_allocator);
680 call_func2(p_basic_string_wchar_swap, &str1, &str2);
681 ok(wcscmp(wtmp2, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
682 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str2)) == 0, "Invalid value of str2\n");
683 call_func2(p_basic_string_wchar_swap, &str1, &str2);
684 ok(wcscmp(wtmp1, call_func1(p_basic_string_wchar_cstr, &str1)) == 0, "Invalid value of str1\n");
685 ok(wcscmp(wtmp2, call_func1(p_basic_string_wchar_cstr, &str2)) == 0, "Invalid value of str2\n");
686 call_func1(p_basic_string_wchar_dtor, &str1);
687 call_func1(p_basic_string_wchar_dtor, &str2);
690 START_TEST(string)
692 if(!init())
693 return;
695 test_basic_string_char();
696 test_basic_string_char_swap();
697 test_basic_string_char_append();
698 test_basic_string_char_compare();
699 test_basic_string_char_concatenate();
700 test_basic_string_char_find();
701 test_basic_string_char_rfind();
702 test_basic_string_char_replace();
703 test_basic_string_wchar();
704 test_basic_string_wchar_swap();
706 FreeLibrary(msvcp);