/* * str_test.c * * test str functions */ /* Need to add tests for... const char *str_addutf8 ( str *s, const char *p ); void str_fprintf ( FILE *fp, str *s ); int str_fget ( FILE *fp, char *buf, int bufsize, int *pbufpos, str *outs ); int str_fgetline ( str *s, FILE *fp ); */ #include #include #include #include "str.h" char progname[] = "str_test"; char version[] = "0.3"; int _inconsistent_len( str *s, unsigned long numchars, const char *fn, unsigned long line ) { if ( s->len > s->dim ) { fprintf(stdout,"%s line %lu: failed consistency check found s->len=%lu, s->max=%lu\n",fn,line, s->len, s->dim ); } if ( s->data ) { if ( s->len != strlen( s->data ) ) { fprintf(stdout,"%s line %lu: failed consistency check found strlen=%d, s->len=%ld\n",fn,line,(int)strlen(s->data),s->len); return 1; } } else { if ( s->len != 0 ) { fprintf(stdout,"%s line %lu: failed consistency check found for unallocated string, s->len=%ld\n",fn,line,s->len); return 1; } } if ( s->len != numchars ) { fprintf(stdout,"%s line %lu: failed consistency check found %d, expected %lu\n",fn,line,(int)strlen(s->data),numchars); return 1; } return 0; } #define inconsistent_len( a, b ) _inconsistent_len( (a), (b), __FUNCTION__, __LINE__ ) int _test_identity( str *s, const char *expected, const char *fn, unsigned long line ) { /* Unallocated strings are considered identical to empty strings */ if ( expected[0]=='\0' ) { if ( s->data==NULL || s->data[0]=='\0' ) return 0; fprintf(stdout,"%s line %lu: failed identity check found '%s', expected ''\n",fn,line,s->data); return 1; } /* expected!="", so s->data must exist */ if ( !s->data ) { fprintf(stdout,"%s line %lu: failed identity check, s->data unallocated, expected '%s'\n",fn,line,expected); return 1; } if ( strcmp( s->data, expected ) == 0 ) return 0; fprintf(stdout,"%s line %lu: failed identity check, found '%s', expected '%s'\n",fn,line,s->data,expected); return 1; } #define test_identity( a, b ) _test_identity( (a), (b), __FUNCTION__, __LINE__ ) #define string_mismatch( a, b, c ) ( test_identity( (a), (c) ) || inconsistent_len( (a), (b) ) ) static int test_empty( str *s ) { int failed = 0; int numchars = 1000, i, j; str_empty( s ); if ( string_mismatch( s, 0, "" ) ) failed++; for ( i=0; idata ); failed++; } if ( str_is_uppercase( s ) ) { fprintf( stdout, "%s line %d: str_is_uppercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( str_is_mixedcase( s ) ) { fprintf( stdout, "%s line %d: str_is_mixedcase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } str_strcpyc( s, "ASDFJALSKJFLJASDFJLSFJD" ); if ( str_is_lowercase( s ) ) { fprintf( stdout, "%s line %d: str_is_lowercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( !str_is_uppercase( s ) ) { fprintf( stdout, "%s line %d: str_is_uppercase('%s') returned false\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( str_is_mixedcase( s ) ) { fprintf( stdout, "%s line %d: str_is_mixedcase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } str_strcpyc( s, "ASdfjalsKJFLJASdfjlsfjd" ); if ( str_is_lowercase( s ) ) { fprintf( stdout, "%s line %d: str_is_lowercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( str_is_uppercase( s ) ) { fprintf( stdout, "%s line %d: str_is_uppercase('%s') returned true\n", __FUNCTION__, __LINE__, s->data ); failed++; } if ( !str_is_mixedcase( s ) ) { fprintf( stdout, "%s line %d: str_is_mixedcase('%s') returned false\n", __FUNCTION__, __LINE__, s->data ); failed++; } return failed; } static int test_strcmp( str *s ) { int failed = 0; str t; str_init( &t ); str_empty( s ); if ( str_strcmp( s, s ) ) { fprintf( stdout, "%s line %d: str_strcmp(s,s) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } if ( str_strcmp( s, &t ) ) { fprintf( stdout, "%s line %d: str_strcmp(s,t) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } str_strcpyc( s, "lakjsdlfjdskljfklsjf" ); if ( str_strcmp( s, s ) ) { fprintf( stdout, "%s line %d: str_strcmp(s,s) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } if ( !str_strcmp( s, &t ) ) { fprintf( stdout, "%s line %d: str_strcmp(s,t) returned zero\n", __FUNCTION__, __LINE__ ); failed++; } str_strcpy( &t, s ); if ( str_strcmp( s, s ) ) { fprintf( stdout, "%s line %d: str_strcmp(s,s) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } if ( str_strcmp( s, &t ) ) { fprintf( stdout, "%s line %d: str_strcmp(s,t) returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } str_free( &t ); return failed; } static int test_match( str *s ) { int failed = 0; str_empty( s ); if ( str_match_first( s, '0' ) ) { fprintf( stdout, "%s line %d: str_match_first() returned non-zero\n", __FUNCTION__, __LINE__ ); failed++; } str_strcpyc( s, "012345" ); if ( !str_match_first( s, '0' ) ) { fprintf( stdout, "%s line %d: str_match_first() returned zero\n", __FUNCTION__, __LINE__ ); failed++; } if ( !str_match_end( s, '5' ) ) { fprintf( stdout, "%s line %d: str_match_end() returned zero\n", __FUNCTION__, __LINE__ ); failed++; } return failed; } static int test_char( str *s ) { unsigned long i; str t, u; int failed = 0; str_init( &t ); str_init( &u ); str_empty( s ); for ( i=0; i<5; ++i ) { if ( str_char( s, i ) != '\0' ) { fprintf( stdout, "%s line %d: str_char() did not return '\\0'\n", __FUNCTION__, __LINE__ ); failed++; } if ( str_revchar( s, i ) != '\0' ) { fprintf( stdout, "%s line %d: str_revchar() did not return '\\0'\n", __FUNCTION__, __LINE__ ); failed++; } } str_strcpyc( s, "0123456789" ); for ( i=0; ilen; ++i ) { str_addchar( &t, str_char( s, i ) ); str_addchar( &u, str_revchar( s, i ) ); } if ( string_mismatch( &t, s->len, s->data ) ) failed++; str_reverse( s ); if ( string_mismatch( &u, s->len, s->data ) ) failed++; str_free( &t ); str_free( &u ); return failed; } static int test_swapstrings( str *s ) { int failed = 0; str t; str_init( &t ); str_strcpyc( &t, "0123456789" ); str_strcpyc( s, "abcde" ); str_swapstrings( s, &t ); if ( string_mismatch( &t, 5, "abcde" ) ) failed++; if ( string_mismatch( s, 10, "0123456789" ) ) failed++; str_swapstrings( s, &t ); if ( string_mismatch( s, 5, "abcde" ) ) failed++; if ( string_mismatch( &t, 10, "0123456789" ) ) failed++; str_free( &t ); return failed; } int main ( int argc, char *argv[] ) { int failed = 0; int ntest = 2; int i; str s; str_init( &s ); /* ...core functions */ for ( i=0; i