]> pd.if.org Git - pdclib/blob - functions/string/memcmp.c
Added test driver; fixed sign bug.
[pdclib] / functions / string / memcmp.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* memcmp( const void *, const void *, 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 memcmp( const void * s1, const void * s2, size_t n )
14 {
15     const unsigned char * p1 = (const unsigned char *) s1;
16     const unsigned char * p2 = (const unsigned char *) s2;
17     while ( n-- )
18     {
19         if ( *p1 != *p2 )
20         {
21             return *p1 - *p2;
22         }
23         ++p1;
24         ++p2;
25     }
26     return 0;
27 }
28
29 #ifdef TEST
30 #include <_PDCLIB_test.h>
31
32 int main()
33 {
34     char const xxxxx[] = "xxxxx";
35     BEGIN_TESTS;
36     TESTCASE( memcmp( abcde, abcdx, 5 ) < 0 );
37     TESTCASE( memcmp( abcde, abcdx, 4 ) == 0 );
38     TESTCASE( memcmp( abcde, xxxxx, 0 ) == 0 );
39     TESTCASE( memcmp( xxxxx, abcde, 1 ) > 0 );
40     return 0;
41 }
42 #endif