]> pd.if.org Git - pdclib/blob - functions/wchar/wcsncmp.c
dos2unix
[pdclib] / functions / wchar / wcsncmp.c
1 /* wcsncmp( const wchar_t *, const wchar_t *, size_t )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <wchar.h>
8
9 #ifndef REGTEST
10
11 int wcsncmp( const wchar_t * s1, const wchar_t * s2, size_t n )
12 {
13     while ( *s1 && n && ( *s1 == *s2 ) )
14     {
15         ++s1;
16         ++s2;
17         --n;
18     }
19     if ( n == 0 )
20     {
21         return 0;
22     }
23     else
24     {
25         return ( *(wchar_t *)s1 - *(wchar_t *)s2 );
26     }
27 }
28
29 #endif
30
31 #ifdef TEST
32 #include "_PDCLIB_test.h"
33
34 int main( void )
35 {
36     wchar_t cmpabcde[] = L"abcde\0f";
37     wchar_t cmpabcd_[] = L"abcde\xfc";
38     wchar_t empty[] = L"";
39     wchar_t x[] = L"x";
40     TESTCASE( wcsncmp( wabcde, cmpabcde, 5 ) == 0 );
41     TESTCASE( wcsncmp( wabcde, cmpabcde, 10 ) == 0 );
42     TESTCASE( wcsncmp( wabcde, wabcdx, 5 ) < 0 );
43     TESTCASE( wcsncmp( wabcdx, wabcde, 5 ) > 0 );
44     TESTCASE( wcsncmp( empty, wabcde, 5 ) < 0 );
45     TESTCASE( wcsncmp( wabcde, empty, 5 ) > 0 );
46     TESTCASE( wcsncmp( wabcde, wabcdx, 4 ) == 0 );
47     TESTCASE( wcsncmp( wabcde, x, 0 ) == 0 );
48     TESTCASE( wcsncmp( wabcde, x, 1 ) < 0 );
49     TESTCASE( wcsncmp( wabcde, cmpabcd_, 10 ) < 0 );
50     return TEST_RESULTS;
51 }
52 #endif