quartz: Use proper alloc/free functions for COM objects.
[wine] / dlls / msvcrt / tests / string.c
1 /*
2  * Unit test suite for string functions.
3  *
4  * Copyright 2004 Uwe Bonnes
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include <string.h>
24 #include <mbstring.h>
25 #include <stdlib.h>
26 #include <mbctype.h>
27
28 static void* (*pmemcpy)(void *, const void *, size_t n);
29 static int* (*pmemcmp)(void *, const void *, size_t n);
30
31 #define SETNOFAIL(x,y) x = (void*)GetProcAddress(hMsvcrt,y)
32 #define SET(x,y) SETNOFAIL(x,y); ok(x != NULL, "Export '%s' not found\n", y)
33
34 static void test_swab( void ) {
35     char original[]  = "BADCFEHGJILKNMPORQTSVUXWZY@#";
36     char expected1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
37     char expected2[] = "ABCDEFGHIJKLMNOPQRSTUVWX$";
38     char expected3[] = "$";
39     
40     char from[30];
41     char to[30];
42     
43     int testsize;
44     
45     /* Test 1 - normal even case */                               
46     memset(to,'$', sizeof(to));
47     memset(from,'@', sizeof(from));
48     testsize = 26;
49     memcpy(from, original, testsize);
50     _swab( from, to, testsize );
51     ok(memcmp(to,expected1,testsize) == 0, "Testing even size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
52
53     /* Test 2 - uneven case  */                               
54     memset(to,'$', sizeof(to));
55     memset(from,'@', sizeof(from));
56     testsize = 25;
57     memcpy(from, original, testsize);
58     _swab( from, to, testsize );
59     ok(memcmp(to,expected2,testsize) == 0, "Testing odd size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
60
61     /* Test 3 - from = to */                               
62     memset(to,'$', sizeof(to));
63     memset(from,'@', sizeof(from));
64     testsize = 26;
65     memcpy(to, original, testsize);
66     _swab( to, to, testsize );
67     ok(memcmp(to,expected1,testsize) == 0, "Testing overlapped size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
68
69     /* Test 4 - 1 bytes */                               
70     memset(to,'$', sizeof(to));
71     memset(from,'@', sizeof(from));
72     testsize = 1;
73     memcpy(from, original, testsize);
74     _swab( from, to, testsize );
75     ok(memcmp(to,expected3,testsize) == 0, "Testing small size %d returned '%*.*s'\n", testsize, testsize, testsize, to);
76 }
77
78 static void test_ismbblead(void)
79 {
80     unsigned int s = '\354';
81
82     _setmbcp(936);
83     todo_wine ok(_ismbblead(s), "got result %d\n", _ismbblead(s));
84     _setmbcp(1252);
85 }
86
87 static void test_mbsspn( void)
88 {
89     unsigned char str1[]="cabernet";
90     unsigned char str2[]="shiraz";
91     unsigned char set[]="abc";
92     unsigned char empty[]="";
93     int ret;
94     ret=_mbsspn( str1, set);
95     ok( ret==3, "_mbsspn returns %d should be 3\n", ret);
96     ret=_mbsspn( str2, set);
97     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
98     ret=_mbsspn( str1, empty);
99     ok( ret==0, "_mbsspn returns %d should be 0\n", ret);
100 }
101
102 static void test_mbsspnp( void)
103 {
104     unsigned char str1[]="cabernet";
105     unsigned char str2[]="shiraz";
106     unsigned char set[]="abc";
107     unsigned char empty[]="";
108     unsigned char full[]="abcenrt";
109     unsigned char* ret;
110     ret=_mbsspnp( str1, set);
111     ok( ret[0]=='e', "_mbsspnp returns %c should be e\n", ret[0]);
112     ret=_mbsspnp( str2, set);
113     ok( ret[0]=='s', "_mbsspnp returns %c should be s\n", ret[0]);
114     ret=_mbsspnp( str1, empty);
115     ok( ret[0]=='c', "_mbsspnp returns %c should be c\n", ret[0]);
116     ret=_mbsspnp( str1, full);
117     ok( ret==NULL, "_mbsspnp returns %p should be NULL\n", ret);
118 }
119
120 static void test_strdup(void)
121 {
122    char *str;
123    str = _strdup( 0 );
124    ok( str == 0, "strdup returns %s should be 0\n", str);
125    free( str );
126 }
127
128 START_TEST(string)
129 {
130     void *mem;
131     static const char xilstring[]="c:/xilinx";
132     int nLen;
133     HMODULE hMsvcrt;
134
135     hMsvcrt = GetModuleHandleA("msvcrt.dll");
136     if (!hMsvcrt)
137         hMsvcrt = GetModuleHandleA("msvcrtd.dll");
138     ok(hMsvcrt != 0, "LoadLibraryA failed\n");
139     SET(pmemcpy,"memcpy");
140     SET(pmemcmp,"memcmp");
141
142     /* MSVCRT memcpy behaves like memmove for overlapping moves,
143        MFC42 CString::Insert seems to rely on that behaviour */
144     mem = malloc(100);
145     ok(mem != NULL, "memory not allocated for size 0\n");
146     strcpy((char*)mem,xilstring);
147     nLen=strlen(xilstring);
148     pmemcpy((char*)mem+5, mem,nLen+1);
149     ok(pmemcmp((char*)mem+5,xilstring, nLen) == 0, 
150        "Got result %s\n",(char*)mem+5);
151
152     /* Test _swab function */
153     test_swab();
154
155     /* Test ismbblead*/
156     test_ismbblead();
157    /* test _mbsspn */
158     test_mbsspn();
159     test_mbsspnp();
160    /* test _strdup */
161     test_strdup();
162 }