From 2f57d79a5a24856fdea69b2119d67e5fb5029b3e Mon Sep 17 00:00:00 2001 From: solar Date: Mon, 21 Nov 2005 18:08:43 +0000 Subject: [PATCH] Added test drivers. --- functions/string/memcpy.c | 2 +- functions/string/memmove.c | 2 +- functions/string/memset.c | 2 +- functions/string/strcat.c | 20 +++++++++++++++++--- functions/string/strchr.c | 13 ++++++++++--- functions/string/strcmp.c | 14 +++++++++++--- functions/string/strcoll.c | 14 +++++++++++--- functions/string/strcpy.c | 15 ++++++++++++--- functions/string/strcspn.c | 16 +++++++++++++--- functions/string/strlen.c | 9 ++++++--- functions/string/strncat.c | 27 ++++++++++++++++++++++++--- functions/string/strncmp.c | 18 +++++++++++++++--- functions/string/strpbrk.c | 15 ++++++++++++--- functions/string/strrchr.c | 12 +++++++++--- functions/string/strspn.c | 10 +++++++--- functions/string/strstr.c | 14 +++++++++++--- functions/string/strtok.c | 21 +++++++++++++++++---- functions/string/strxfrm.c | 19 ++++++++++++++++--- 18 files changed, 194 insertions(+), 49 deletions(-) diff --git a/functions/string/memcpy.c b/functions/string/memcpy.c index ca460ef..b2606b7 100644 --- a/functions/string/memcpy.c +++ b/functions/string/memcpy.c @@ -27,7 +27,7 @@ void * memcpy( void * _PDCLIB_restrict s1, const void * _PDCLIB_restrict s2, siz int main() { - char s[11] = "xxxxxxxxxxx"; + char s[] = "xxxxxxxxxxx"; BEGIN_TESTS; TESTCASE( memcpy( s, abcde, 6 ) == s ); TESTCASE( s[4] == 'e' ); diff --git a/functions/string/memmove.c b/functions/string/memmove.c index e9a52b2..7c50dee 100644 --- a/functions/string/memmove.c +++ b/functions/string/memmove.c @@ -38,7 +38,7 @@ void * memmove( void * s1, const void * s2, size_t n ) int main() { - char s[10] = "xxxxabcde"; + char s[] = "xxxxabcde"; BEGIN_TESTS; TESTCASE( memmove( s, s + 4, 5 ) == s ); TESTCASE( s[0] == 'a' ); diff --git a/functions/string/memset.c b/functions/string/memset.c index 735a8e6..753a333 100644 --- a/functions/string/memset.c +++ b/functions/string/memset.c @@ -25,7 +25,7 @@ void * memset( void * s, int c, size_t n ) int main() { - char s[10] = "xxxxxxxxx"; + char s[] = "xxxxxxxxx"; BEGIN_TESTS; TESTCASE( memset( s, 'o', 10 ) == s ); TESTCASE( s[9] == 'o' ); diff --git a/functions/string/strcat.c b/functions/string/strcat.c index a4eab14..d113575 100644 --- a/functions/string/strcat.c +++ b/functions/string/strcat.c @@ -21,11 +21,25 @@ char * strcat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 ) return rc; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char s[] = "xx\0xxxxxx"; + BEGIN_TESTS; + TESTCASE( strcat( s, abcde ) == s ); + TESTCASE( s[2] == 'a' ); + TESTCASE( s[6] == 'e' ); + TESTCASE( s[7] == '\0' ); + TESTCASE( s[8] == 'x' ); + s[0] = '\0'; + TESTCASE( strcat( s, abcdx ) == s ); + TESTCASE( s[4] == 'x' ); + TESTCASE( s[5] == '\0' ); + TESTCASE( strcat( s, "\0" ) == s ); + TESTCASE( s[5] == '\0' ); + TESTCASE( s[6] == 'e' ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strchr.c b/functions/string/strchr.c index ec296cb..96ab051 100644 --- a/functions/string/strchr.c +++ b/functions/string/strchr.c @@ -22,11 +22,18 @@ char * strchr( const char * s, int c ) return NULL; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char abccd[] = "abccd"; + BEGIN_TESTS; + TESTCASE( strchr( abccd, 'x' ) == NULL ); + TESTCASE( strchr( abccd, 'a' ) == &abccd[0] ); + TESTCASE( strchr( abccd, 'd' ) == &abccd[4] ); + TESTCASE( strchr( abccd, '\0' ) == &abccd[5] ); + TESTCASE( strchr( abccd, 'c' ) == &abccd[2] ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strcmp.c b/functions/string/strcmp.c index f6a555b..fea5d2d 100644 --- a/functions/string/strcmp.c +++ b/functions/string/strcmp.c @@ -18,11 +18,19 @@ int strcmp( const char * s1, const char * s2 ) return ( *s1 - *s2 ); } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char cmpabcde[] = "abcde"; + char empty[] = ""; + BEGIN_TESTS; + TESTCASE( strcmp( abcde, cmpabcde ) == 0 ); + TESTCASE( strcmp( abcde, abcdx ) < 0 ); + TESTCASE( strcmp( abcdx, abcde ) > 0 ); + TESTCASE( strcmp( empty, abcde ) < 0 ); + TESTCASE( strcmp( abcde, empty ) > 0 ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strcoll.c b/functions/string/strcoll.c index 6f8a0dc..74a8aad 100644 --- a/functions/string/strcoll.c +++ b/functions/string/strcoll.c @@ -17,11 +17,19 @@ int strcoll( const char * s1, const char * s2 ) return strcmp( s1, s2 ); } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char cmpabcde[] = "abcde"; + char empty[] = ""; + BEGIN_TESTS; + TESTCASE( strcmp( abcde, cmpabcde ) == 0 ); + TESTCASE( strcmp( abcde, abcdx ) < 0 ); + TESTCASE( strcmp( abcdx, abcde ) > 0 ); + TESTCASE( strcmp( empty, abcde ) < 0 ); + TESTCASE( strcmp( abcde, empty ) > 0 ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strcpy.c b/functions/string/strcpy.c index 4f02875..49204da 100644 --- a/functions/string/strcpy.c +++ b/functions/string/strcpy.c @@ -17,11 +17,20 @@ char * strcpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 ) return rc; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char s[] = "xxxxx"; + BEGIN_TESTS; + TESTCASE( strcpy( s, "" ) == s ); + TESTCASE( s[0] == '\0' ); + TESTCASE( s[1] == 'x' ); + TESTCASE( strcpy( s, abcde ) == s ); + TESTCASE( s[0] == 'a' ); + TESTCASE( s[4] == 'e' ); + TESTCASE( s[5] == '\0' ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strcspn.c b/functions/string/strcspn.c index 308fe36..01f10e5 100644 --- a/functions/string/strcspn.c +++ b/functions/string/strcspn.c @@ -29,11 +29,21 @@ size_t strcspn( const char * s1, const char * s2 ) return len; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + BEGIN_TESTS; + TESTCASE( strcspn( abcde, "x" ) == 5 ); + TESTCASE( strcspn( abcde, "xyz" ) == 5 ); + TESTCASE( strcspn( abcde, "zyx" ) == 5 ); + TESTCASE( strcspn( abcdx, "x" ) == 4 ); + TESTCASE( strcspn( abcdx, "xyz" ) == 4 ); + TESTCASE( strcspn( abcdx, "zyx" ) == 4 ); + TESTCASE( strcspn( abcde, "a" ) == 0 ); + TESTCASE( strcspn( abcde, "abc" ) == 0 ); + TESTCASE( strcspn( abcde, "cba" ) == 0 ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strlen.c b/functions/string/strlen.c index da5bc40..02d105c 100644 --- a/functions/string/strlen.c +++ b/functions/string/strlen.c @@ -20,11 +20,14 @@ size_t strlen( const char * s ) return rc; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + BEGIN_TESTS; + TESTCASE( strlen( abcde ) == 5 ); + TESTCASE( strlen( "" ) == 0 ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strncat.c b/functions/string/strncat.c index d9f9fe3..7fdaa26 100644 --- a/functions/string/strncat.c +++ b/functions/string/strncat.c @@ -29,11 +29,32 @@ char * strncat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, si return rc; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char s[] = "xx\0xxxxxx"; + BEGIN_TESTS; + TESTCASE( strncat( s, abcde, 10 ) == s ); + TESTCASE( s[2] == 'a' ); + TESTCASE( s[6] == 'e' ); + TESTCASE( s[7] == '\0' ); + TESTCASE( s[8] == 'x' ); + s[0] = '\0'; + TESTCASE( strncat( s, abcdx, 10 ) == s ); + TESTCASE( s[4] == 'x' ); + TESTCASE( s[5] == '\0' ); + TESTCASE( strncat( s, "\0", 10 ) == s ); + TESTCASE( s[5] == '\0' ); + TESTCASE( s[6] == 'e' ); + TESTCASE( strncat( s, abcde, 0 ) == s ); + TESTCASE( s[5] == '\0' ); + TESTCASE( s[6] == 'e' ); + TESTCASE( strncat( s, abcde, 3 ) == s ); + TESTCASE( s[5] == 'a' ); + TESTCASE( s[7] == 'c' ); + TESTCASE( s[8] == '\0' ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strncmp.c b/functions/string/strncmp.c index 7f398fa..35a9511 100644 --- a/functions/string/strncmp.c +++ b/functions/string/strncmp.c @@ -28,11 +28,23 @@ int strncmp( const char * s1, const char * s2, size_t n ) } } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char cmpabcde[] = "abcde"; + char empty[] = ""; + char x[] = "x"; + BEGIN_TESTS; + TESTCASE( strncmp( abcde, cmpabcde, 5 ) == 0 ); + TESTCASE( strncmp( abcde, abcdx, 5 ) < 0 ); + TESTCASE( strncmp( abcdx, abcde, 5 ) > 0 ); + TESTCASE( strncmp( empty, abcde, 5 ) < 0 ); + TESTCASE( strncmp( abcde, empty, 5 ) > 0 ); + TESTCASE( strncmp( abcde, abcdx, 4 ) == 0 ); + TESTCASE( strncmp( abcde, x, 0 ) == 0 ); + TESTCASE( strncmp( abcde, x, 1 ) < 0 ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strpbrk.c b/functions/string/strpbrk.c index bc40263..b317aed 100644 --- a/functions/string/strpbrk.c +++ b/functions/string/strpbrk.c @@ -29,11 +29,20 @@ char * strpbrk( const char * s1, const char * s2 ) return NULL; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + BEGIN_TESTS; + TESTCASE( strpbrk( abcde, "x" ) == NULL ); + TESTCASE( strpbrk( abcde, "xyz" ) == NULL ); + TESTCASE( strpbrk( abcdx, "x" ) == &abcdx[4] ); + TESTCASE( strpbrk( abcdx, "xyz" ) == &abcdx[4] ); + TESTCASE( strpbrk( abcdx, "zyx" ) == &abcdx[4] ); + TESTCASE( strpbrk( abcde, "a" ) == &abcde[0] ); + TESTCASE( strpbrk( abcde, "abc" ) == &abcde[0] ); + TESTCASE( strpbrk( abcde, "cba" ) == &abcde[0] ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strrchr.c b/functions/string/strrchr.c index 4fe9a6b..aa4c930 100644 --- a/functions/string/strrchr.c +++ b/functions/string/strrchr.c @@ -24,11 +24,17 @@ char * strrchr( const char * s, int c ) return NULL; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char abccd[] = "abccd"; + BEGIN_TESTS; + TESTCASE( strrchr( abcde, '\0' ) == &abcde[5] ); + TESTCASE( strrchr( abcde, 'e' ) == &abcde[4] ); + TESTCASE( strrchr( abcde, 'a' ) == &abcde[0] ); + TESTCASE( strrchr( abccd, 'c' ) == &abccd[3] ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strspn.c b/functions/string/strspn.c index b8e703d..a5859cc 100644 --- a/functions/string/strspn.c +++ b/functions/string/strspn.c @@ -34,11 +34,15 @@ size_t strspn( const char * s1, const char * s2 ) return len; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + BEGIN_TESTS; + TESTCASE( strspn( abcde, "abc" ) == 3 ); + TESTCASE( strspn( abcde, "b" ) == 0 ); + TESTCASE( strspn( abcde, abcde ) == 5 ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strstr.c b/functions/string/strstr.c index 0566d5d..2ad7505 100644 --- a/functions/string/strstr.c +++ b/functions/string/strstr.c @@ -32,11 +32,19 @@ char * strstr( const char * s1, const char * s2 ) return NULL; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char s[] = "abcabcabcdabcde"; + BEGIN_TESTS; + TESTCASE( strstr( s, "x" ) == NULL ); + TESTCASE( strstr( s, "xyz" ) == NULL ); + TESTCASE( strstr( s, "a" ) == &s[0] ); + TESTCASE( strstr( s, "abc" ) == &s[0] ); + TESTCASE( strstr( s, "abcd" ) == &s[6] ); + TESTCASE( strstr( s, "abcde" ) == &s[10] ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strtok.c b/functions/string/strtok.c index 9daf329..071f4f8 100644 --- a/functions/string/strtok.c +++ b/functions/string/strtok.c @@ -15,7 +15,6 @@ char * strtok( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 ) { static char * tmp = NULL; const char * p = s2; - size_t len; if ( s1 != NULL ) { @@ -73,11 +72,25 @@ char * strtok( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 ) return ( tmp = NULL ); } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char s[] = "_a_bc__d_"; + BEGIN_TESTS; + TESTCASE( strtok( s, "_" ) == &s[1] ); + TESTCASE( s[1] == 'a' ); + TESTCASE( s[2] == '\0' ); + TESTCASE( strtok( NULL, "_" ) == &s[3] ); + TESTCASE( s[3] == 'b' ); + TESTCASE( s[4] == 'c' ); + TESTCASE( s[5] == '\0' ); + TESTCASE( strtok( NULL, "_" ) == &s[7] ); + TESTCASE( s[6] == '_' ); + TESTCASE( s[7] == 'd' ); + TESTCASE( s[8] == '\0' ); + TESTCASE( strtok( NULL, "_" ) == NULL ); + return TEST_RESULTS; } #endif diff --git a/functions/string/strxfrm.c b/functions/string/strxfrm.c index af98c18..650cfbf 100644 --- a/functions/string/strxfrm.c +++ b/functions/string/strxfrm.c @@ -26,11 +26,24 @@ size_t strxfrm( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, si return len; } -#warning Test driver missing. - #ifdef TEST +#include <_PDCLIB_test.h> + int main() { - return 0; + char s[] = "xxxxxxxxxxx"; + BEGIN_TESTS; + TESTCASE( strxfrm( NULL, "123456789012", 0 ) == 12 ); + TESTCASE( strxfrm( s, "123456789012", 12 ) == 12 ); + /* + The following test case is true in *this* implementation, but doesn't have to. + TESTCASE( s[0] == 'x' ); + */ + TESTCASE( strxfrm( s, "1234567890", 11 ) == 10 ); + TESTCASE( s[0] == '1' ); + TESTCASE( s[9] == '0' ); + TESTCASE( s[10] == '\0' ); + return TEST_RESULTS; } #endif + -- 2.40.0