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