]> pd.if.org Git - pdclib/blob - functions/string/strncmp.c
Added test drivers.
[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 int strncmp( const char * s1, const char * s2, size_t n )
14 {
15     while ( n && ( *s1 == *s2 ) )
16     {
17         ++s1;
18         ++s2;
19         --n;
20     }
21     if ( ( n == 0 ) )
22     {
23         return 0;
24     }
25     else
26     {
27         return ( *s1 - *s2 );
28     }
29 }
30
31 #ifdef TEST
32 #include <_PDCLIB_test.h>
33
34 int main()
35 {
36     char cmpabcde[] = "abcde";
37     char empty[] = "";
38     char x[] = "x";
39     BEGIN_TESTS;
40     TESTCASE( strncmp( abcde, cmpabcde, 5 ) == 0 );
41     TESTCASE( strncmp( abcde, abcdx, 5 ) < 0 );
42     TESTCASE( strncmp( abcdx, abcde, 5 ) > 0 );
43     TESTCASE( strncmp( empty, abcde, 5 ) < 0 );
44     TESTCASE( strncmp( abcde, empty, 5 ) > 0 );
45     TESTCASE( strncmp( abcde, abcdx, 4 ) == 0 );
46     TESTCASE( strncmp( abcde, x, 0 ) == 0 );
47     TESTCASE( strncmp( abcde, x, 1 ) < 0 );
48     return TEST_RESULTS;
49 }
50 #endif