OSDN Git Service

Apply LICENSE to all files as appropriate.
[mingw/mingw-org-wsl.git] / tests / libcrt / testwmem.c
1 /**
2  * @file testwmem.c
3  * @copy 2012 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #include <memory.h>
25 #include <wchar.h>
26 #include <stdio.h>
27
28 wchar_t fmt1[] =   L"         1         2         3         4         5";
29 wchar_t fmt2[] =   L"12345678901234567890123456789012345678901234567890";
30
31 void test_wmemchr( void )
32 {
33    wchar_t* dest;
34    wint_t result;
35    wint_t ch = L'r';
36    wchar_t str[] =   L"lazy";
37    wchar_t string1[60] = L"The quick brown dog jumps over the lazy fox";
38
39    wprintf( L"Wmemchr\n" );
40    wprintf( L"String to be searched:\n\t\t%s\n", string1 );
41    wprintf( L"\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
42
43    wprintf( L"Search char:\t%c\n", ch );
44    dest = wmemchr( string1, ch, sizeof( string1 ) );
45    result = dest - string1 + 1;
46    if( dest != NULL )
47       wprintf( L"Result:\t\t%c found at position %d\n\n", ch, result );
48    else
49       wprintf( L"Result:\t\t%c not found\n\n" );
50 return;
51 }
52 void test_wmemset( void )
53 {/*                               1         2 
54                         0123456789012345678901234567890 */
55    wchar_t buffer[] = L"This is a test of the wmemset function";
56    wprintf( L"Before: %s\n", buffer );
57    wmemset( buffer+22, L'*', 7 );
58    wprintf( L"After:  %s\n\n", buffer );
59 return;
60 }
61
62 void test_wmemmove( void )
63 {
64     wchar_t string1[60] = L"The quick brown dog jumps over the lazy fox";
65     wchar_t string2[60] = L"The quick brown fox jumps over the lazy dog";
66
67     wprintf( L"Wmemcpy without overlap\n" );
68     wprintf( L"Source:\t\t%s\n", string1 + 40 );
69     wprintf( L"Destination:\t%s\n", string1 + 16 );
70     wmemcpy( string1 + 16, string1 + 40, 3 );
71     wprintf( L"Result:\t\t%s\n", string1 );
72     wprintf( L"Length:\t\t%d characters\n\n", wcslen( string1 ) );
73     wmemcpy( string1 + 16, string2 + 40, 3 );
74
75    wprintf( L"Wmemmove with overlap\n" );
76    wprintf( L"Source:\t\t%s\n", string2 + 4 );
77    wprintf( L"Destination:\t%s\n", string2 + 10 );
78    wmemmove( string2 + 10, string2 + 4, 40 );
79    wprintf( L"Result:\t\t%s\n", string2 );
80    wprintf( L"Length:\t\t%d characters\n\n", wcslen( string2 ) );
81
82    wprintf( L"Wmemcpy with overlap\n" );
83    wprintf( L"Source:\t\t%s\n", string1 + 4 );
84    wprintf( L"Destination:\t%s\n", string1 + 10 );
85    wmemcpy( string1 + 10, string1 + 4, 40 );
86    wprintf( L"Result:\t\t%s\n", string1 );
87    wprintf( L"Length:\t\t%d characters\n\n", wcslen( string1 ) );
88 }
89
90
91 void test_wmemcmp( void )
92 {
93    wchar_t first[]  = L"12345678901234567890";
94    wchar_t second[] = L"12345678901234567891";
95    wint_t result;
96    wprintf(L"Wmemcmp\n"); 
97    wprintf( L"Compare '%.19s' to '%.19s':\n", first, second );
98    result = wmemcmp( first, second, 19 );
99    if( result < 0 )
100       wprintf( L"First is less than second.\n" );
101    else if( result == 0 )
102       wprintf( L"First is equal to second.\n" );
103    else if( result > 0 )
104       wprintf( L"First is greater than second.\n" );
105    wprintf( L"\nCompare '%.20s' to '%.20s':\n", first, second );
106    result = wmemcmp( first, second, 20 );
107    if( result < 0 )
108       wprintf( L"First is less than second.\n\n" );
109    else if( result == 0 )
110       wprintf( L"First is equal to second.\n\n" );
111    else if( result > 0 )
112       wprintf( L"First is greater than second.\n\n" );
113 }
114
115
116
117 int main(){
118 test_wmemset();
119 test_wmemmove();
120 test_wmemchr();
121 test_wmemcmp();
122 return 0;
123 }
124
125
126
127