kernel32: Cast-qual warnings fix.
[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 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_strdup(void)
103 {
104    char *str;
105    str = _strdup( 0 );
106    ok( str == 0, "strdup returns %s should be 0\n", str);
107    free( str );
108 }
109
110 START_TEST(string)
111 {
112     void *mem;
113     static const char xilstring[]="c:/xilinx";
114     int nLen=strlen(xilstring);
115     HMODULE hMsvcrt = LoadLibraryA("msvcrt.dll");
116     ok(hMsvcrt != 0, "LoadLibraryA failed\n");
117     SET(pmemcpy,"memcpy");
118     SET(pmemcmp,"memcmp");
119
120     /* MSVCRT memcpy behaves like memmove for overlapping moves,
121        MFC42 CString::Insert seems to rely on that behaviour */
122     mem = malloc(100);
123     ok(mem != NULL, "memory not allocated for size 0\n");
124     strcpy((char*)mem,xilstring);
125     pmemcpy((char*)mem+5, mem,nLen+1);
126     ok(pmemcmp((char*)mem+5,xilstring, nLen) == 0, 
127        "Got result %s\n",(char*)mem+5);
128
129     /* Test _swab function */
130     test_swab();
131
132     /* Test ismbblead*/
133     test_ismbblead();
134    /* test _mbsspn */
135     test_mbsspn();
136    /* test _strdup */
137     test_strdup();
138 }