From c1342e3edfdf2ce3808dfc9f53f9e81d579fac43 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Wed, 13 Aug 2008 23:35:20 +0000 Subject: [PATCH] update of c libs git-svn-id: http://biopieces.googlecode.com/svn/trunk@206 74ccb610-7750-0410-82ae-013aeee3265d --- code_c/Maasha/src/gmon.out | Bin 730921 -> 736585 bytes code_c/Maasha/src/inc/common.h | 40 +------ code_c/Maasha/src/inc/filesys.h | 14 +-- code_c/Maasha/src/inc/mem.h | 29 +++++ code_c/Maasha/src/inc/strings.h | 16 ++- code_c/Maasha/src/lib/Makefile | 16 ++- code_c/Maasha/src/lib/common.c | 155 +------------------------ code_c/Maasha/src/lib/fasta.c | 1 + code_c/Maasha/src/lib/filesys.c | 70 +++++++----- code_c/Maasha/src/lib/hash.c | 1 + code_c/Maasha/src/lib/list.c | 5 +- code_c/Maasha/src/lib/mem.c | 129 +++++++++++++++++++++ code_c/Maasha/src/lib/seq.c | 1 + code_c/Maasha/src/lib/strings.c | 140 +++++++++++++++++++++-- code_c/Maasha/src/lib/ucsc.c | 3 +- code_c/Maasha/src/repeat-O-matic.c | 1 + code_c/Maasha/src/test/Makefile | 15 ++- code_c/Maasha/src/test/test_filesys.c | 158 ++++++++++++++++++++++++++ code_c/Maasha/src/test/test_mem.c | 67 +++++++++++ code_c/Maasha/src/test/test_strings.c | 123 +++++++++++--------- code_c/Maasha/src/test_all.pl | 23 ++++ 21 files changed, 707 insertions(+), 300 deletions(-) create mode 100644 code_c/Maasha/src/inc/mem.h create mode 100644 code_c/Maasha/src/lib/mem.c create mode 100644 code_c/Maasha/src/test/test_filesys.c create mode 100644 code_c/Maasha/src/test/test_mem.c create mode 100755 code_c/Maasha/src/test_all.pl diff --git a/code_c/Maasha/src/gmon.out b/code_c/Maasha/src/gmon.out index 0459c2d5876f181f6cec9a2ab4b5582f0afd1d15..5176fb2725a133e7561246dd60826927015b4842 100644 GIT binary patch delta 675 zcmZwFt53sF6bA5bTf0XY;|(3pLG8pu|AGZUNgxOk#$!B7vIG=Cl2jl-ASRF`U=X+s zLr4%<3~GW91fpUBK~Mz6SMHV`f%ua9JLkUA+neY7(@(xy&iEEk9Rti=z_;pq2ibbH zVt6qCg(z?}4%{yR5jw~N$FD83jW>?lD4#fkP~4o50zexXqd|g}!PtfTdV7`4|j)fx@+McAF!c%m~4f_@y{3rkaR>zKg Z;@A-;%ETCr(U~}tV3JJg&asX6uU}#9UXcI* delta 334 zcmX^4OK0Udoe8mgHIfVr7bF-MI=mSeY9u%Js})JsGczzSJOE*!oGDP85y%GNEeZ?_ zN2M4Tn1F0HAU08CV33f8$guW|Xc))<5T6T(1AvZ;RDtk8mZkvtZBX^h zKz;#`{}9UO0rD$={4!ODdOjc@WDaToFfdd~Y@V!^C)u3o)t>3a2*gZ4%nZaVK+Fon TY(UHo#2i4(xjoa1>%k`gCjvCd diff --git a/code_c/Maasha/src/inc/common.h b/code_c/Maasha/src/inc/common.h index 687fd42..320f0a9 100644 --- a/code_c/Maasha/src/inc/common.h +++ b/code_c/Maasha/src/inc/common.h @@ -7,6 +7,7 @@ #include #include #include +#include /* Define a shorthand for unsigned int */ #define uint unsigned int @@ -16,15 +17,6 @@ #define TRUE 1 #define FALSE 0 -/* Macro for resetting a pointer to all \0's. */ -#define MEM_ZERO( pt ) ( memset( pt, '\0', sizeof( *pt ) ) ) - -/* Macro for dynamic allocation of memory. */ -#define MEM_GET( pt ) ( pt = mem_get( sizeof( *pt ) ) ) - -/* Macro for cloning a structure in memroy. */ -#define MEM_CLONE( pt ) mem_clone( pt, sizeof( ( pt )[ 0 ] ) ) - /* Macros for determining min or max of two given values. */ #define MAX( a, b ) a < b ? b : a #define MIN( a, b ) a > b ? b : a @@ -65,30 +57,6 @@ void die( char *error_msg ); void warn( char *warn_msg ); -/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MEMORY HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ - - -/* Get a pointer with a given size of allocated memory. */ -void *mem_get( size_t size ); - -/* Get a pointer with a given size of allocated and zero'ed memory. */ -void *mem_get_zero( size_t size ); - -/* Resize allocated memory for a given pointer. */ -void *mem_resize( void* pt, size_t size ); - -/* Resize allocated memory for a given pointer with extra memory zero'ed. */ -void *mem_resize_zero( void* pt, size_t old_size, size_t new_size ); - -/* Clone a structure in memory and return a pointer to the clone. */ -void *mem_clone( void *old_pt, size_t size ); - -/* Free memory from a given pointer. */ -void mem_free( void *pt ); - -/* Zero and then free memory from a given pointer. */ -void mem_free( void *pt ); - /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ @@ -99,12 +67,6 @@ bool binary_search_array( int *array, int array_size, int val ); /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ -/* Remove the last char from a string. */ -void chop( char *string ); - -/* Remove the last char from a string if the char is a newline (safer than chop). */ -void chomp( char *string ); - /* Split a given line and a delimiter return the split result as a list. */ void split( char *string, char delimit, struct list **fields ); diff --git a/code_c/Maasha/src/inc/filesys.h b/code_c/Maasha/src/inc/filesys.h index 87e5e89..6f9e872 100644 --- a/code_c/Maasha/src/inc/filesys.h +++ b/code_c/Maasha/src/inc/filesys.h @@ -13,26 +13,26 @@ struct file_buffer }; /* Read-open a file and return a file pointer. */ -FILE *read_open( char *file ); +FILE *read_open( char *file ); /* Write-open a file and return a file pointer. */ -FILE *write_open( char *file ); +FILE *write_open( char *file ); /* Append-open a file and return a file pointer. */ -FILE *append_open( char *file ); +FILE *append_open( char *file ); /* Close a stream defined by a file pointer. */ -void close_stream( FILE *fp ); +void close_stream( FILE *fp ); /* Read in len number of bytes from the current position of a */ /* file pointer into a string that is allocated and null terminated. */ -char *file_read( FILE *fp, size_t len ); +char *file_read( FILE *fp, size_t len ); /* Delete a file. */ -void file_unlink( char *file ); +void file_unlink( char *file ); /* Rename a file. */ -void file_rename( char *old_name, char *new_name ); +void file_rename( char *old_name, char *new_name ); /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FILE BUFFER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ diff --git a/code_c/Maasha/src/inc/mem.h b/code_c/Maasha/src/inc/mem.h new file mode 100644 index 0000000..08c976d --- /dev/null +++ b/code_c/Maasha/src/inc/mem.h @@ -0,0 +1,29 @@ +/* Macro for dynamic allocation of memory. */ +#define MEM_GET( pt ) ( pt = mem_get( sizeof( *pt ) ) ) + +/* Macro for resetting a pointer to all \0's. */ +#define MEM_ZERO( pt ) ( memset( pt, '\0', sizeof( *pt ) ) ) + +/* Macro for cloning a structure in memroy. */ +#define MEM_CLONE( pt ) mem_clone( pt, sizeof( ( pt )[ 0 ] ) ) + +/* Get a pointer with a given size of allocated memory. */ +void *mem_get( size_t size ); + +/* Get a pointer with a given size of allocated and zero'ed memory. */ +void *mem_get_zero( size_t size ); + +/* Resize allocated memory for a given pointer. */ +void *mem_resize( void* pt, size_t size ); + +/* Resize allocated memory for a given pointer with extra memory zero'ed. */ +void *mem_resize_zero( void* pt, size_t old_size, size_t new_size ); + +/* Clone a structure in memory and return a pointer to the clone. */ +void *mem_clone( void *old_pt, size_t size ); + +/* Free memory from a given pointer. */ +void mem_free( void *pt ); + +/* Zero and then free memory from a given pointer. */ +void mem_free_zero( void *pt ); diff --git a/code_c/Maasha/src/inc/strings.h b/code_c/Maasha/src/inc/strings.h index c2cae78..fa14686 100644 --- a/code_c/Maasha/src/inc/strings.h +++ b/code_c/Maasha/src/inc/strings.h @@ -1,3 +1,15 @@ -/* Locate a substr in a str allowing for a given number of mismatches. */ +/* Remove the last char from a string. Returns the length of the chopped string.*/ +size_t chop( char *string ); + +/* Removes the last char from a string if the char is a newline. */ +/* Returns the length of the chomped string or -1 is no newline was found. */ +size_t chomp( char *string ); + +/* Locate a substr in a str starting at pos allowing for a given number of mismatches. */ /* Returns position of match begin or -1 if not found. */ -int match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ); +size_t match_substr( size_t pos, char *str, size_t str_len, char *substr, size_t substr_len, size_t mismatch ); + +/* Locate a substr in a str backwards starting at the end of */ +/* str minus pos allowing for a given number of mismatches. */ +/* Returns position of match begin or -1 if not found. */ +size_t match_substr_rev( size_t pos, char *str, size_t str_len, char *substr, size_t substr_len, size_t mismatch ); diff --git a/code_c/Maasha/src/lib/Makefile b/code_c/Maasha/src/lib/Makefile index dfdb4f9..7b9c64e 100644 --- a/code_c/Maasha/src/lib/Makefile +++ b/code_c/Maasha/src/lib/Makefile @@ -3,11 +3,14 @@ CC = gcc Cflags = -Wall -Werror -g -pg # gprof INC_DIR = -I ../inc/ -all: common.o strings.o seq.o filesys.o fasta.o list.o hash.o ucsc.o +all: common.o mem.o strings.o seq.o filesys.o fasta.o list.o hash.o ucsc.o common.o: common.c $(CC) $(Cflags) $(INC_DIR) -c common.c +mem.o: mem.c + $(CC) $(Cflags) $(INC_DIR) -c mem.c + strings.o: strings.c $(CC) $(Cflags) $(INC_DIR) -c strings.c @@ -30,4 +33,13 @@ ucsc.o: ucsc.c $(CC) $(Cflags) $(INC_DIR) -c ucsc.c clean: - rm common.o strings.o seq.o filesys.o fasta.o list.o hash.o ucsc.o + rm common.o + rm mem.o + rm strings.o + rm seq.o + rm filesys.o + rm fasta.o + rm list.o + rm hash.o + rm ucsc.o + diff --git a/code_c/Maasha/src/lib/common.c b/code_c/Maasha/src/lib/common.c index 7f1c99d..05e7793 100644 --- a/code_c/Maasha/src/lib/common.c +++ b/code_c/Maasha/src/lib/common.c @@ -1,5 +1,6 @@ #include "common.h" #include "list.h" +#include "mem.h" /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ERROR HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ @@ -27,130 +28,6 @@ void warn( char *msg ) } -/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MEMORY HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ - - -void *mem_get( size_t size ) -{ - /* Martin A. Hansen, May 2008 */ - - /* Allocate a given chunk of memory to a pointer that is returned. */ - - void *pt; - - if ( size == 0 ) { - die( "could not allocate 0 bytes of memory." ); - } else if ( ( pt = malloc( size ) ) == NULL ) { - die( "could not allocate memory." ); - } - - return pt; -} - - -void *mem_get_zero( size_t size ) -{ - /* Martin A. Hansen, May 2008 */ - - /* Allocate a given chunk of zero'ed memory to a pointer that is returned. */ - - void *pt; - - if ( size == 0 ) { - die( "could not allocate 0 bytes of memory." ); - } else if ( ( pt = malloc( size ) ) == NULL ) { - die( "could not allocate memory." ); - } - - memset( pt, '\0', size ); - - return pt; -} - - -void *mem_resize( void *pt, size_t size ) -{ - /* Martin A. Hansen, May 2008 */ - - /* Resize an allocated chunk of memory for a given pointer and new size. */ - - void *pt_new; - - if ( size == 0 ) { - die( "could not re-allocate 0 bytes of memory." ); - } else if ( ( pt_new = realloc( pt, size ) ) == NULL ) { - die( "could not re-allocate memory." ); - } - - return pt_new; -} - - -void *mem_resize_zero( void *pt, size_t old_size, size_t new_size ) -{ - /* Martin A. Hansen, May 2008 */ - - /* Resize an allocated chunk of memory for a given pointer and zero any extra memory. */ - - void *pt_new; - - pt_new = mem_resize( pt, new_size ); - - if ( new_size > old_size ) { - memset( ( ( void * ) pt_new ) + old_size, '\0', new_size - old_size ); - } - - return pt_new; -} - - -void *mem_clone( void *old_pt, size_t size ) -{ - /* Martin A. Hansen, June 2008 */ - - /* Clone a structure in memory and return a pointer to the clone. */ - - void *new_pt; - - new_pt = mem_get( size ); - - memcpy( new_pt, old_pt, size ); - - return new_pt; -} - - -void mem_free( void *pt ) -{ - /* Martin A. Hansen, May 2008 */ - - /* Free memory from a given pointer. */ - - if ( pt != NULL ) - { - free( pt ); - - pt = NULL; - } -} - - -void mem_free_zero( void *pt ) -{ - /* Martin A. Hansen, July 2008 */ - - /* Zero and then free memory from a given pointer. */ - - if ( pt != NULL ) - { - MEM_ZERO( pt ); - free( pt ); - - pt = NULL; - } -} - - /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ @@ -187,36 +64,6 @@ bool binary_search_array( int *array, int array_size, int val ) /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ -void chop( char *string ) -{ - /* Martin A. Hansen, June 2008 */ - - /* Removes the last char from a string. */ - - int len; - - len = strlen( string ); - - string[ len - 1 ] = '\0'; -} - - -void chomp( char *string ) -{ - /* Martin A. Hansen, June 2008 */ - - /* Removes the last char from a string if the char is a newline. */ - - int len; - - len = strlen( string ); - - if ( string[ len - 1 ] == '\n' ) { - string[ len - 1 ] = '\0'; - } -} - - void split( char *string, char delimit, struct list **fields ) { /* Martin A. Hansen, June 2008 */ diff --git a/code_c/Maasha/src/lib/fasta.c b/code_c/Maasha/src/lib/fasta.c index 5a2ca7f..21fc66f 100644 --- a/code_c/Maasha/src/lib/fasta.c +++ b/code_c/Maasha/src/lib/fasta.c @@ -1,4 +1,5 @@ #include "common.h" +#include "mem.h" #include "fasta.h" #include "list.h" diff --git a/code_c/Maasha/src/lib/filesys.c b/code_c/Maasha/src/lib/filesys.c index a7489a6..d3bf3db 100644 --- a/code_c/Maasha/src/lib/filesys.c +++ b/code_c/Maasha/src/lib/filesys.c @@ -1,4 +1,5 @@ #include "common.h" +#include "mem.h" #include "filesys.h" @@ -6,17 +7,17 @@ FILE *read_open( char *file ) { /* Martin A. Hansen, November 2005 */ + /* Unit test done. */ + /* Given a file name, read-opens the file, */ /* and returns a file pointer. */ FILE *fp; - char *msg; if ( ( fp = fopen( file, "r" ) ) == NULL ) { - sprintf( msg, "Could not read-open file '%s'.", file ); - - die( msg ); + fprintf( stderr, "ERROR: Could not read-open file '%s': %s\n", file, strerror( errno ) ); + abort(); } return fp; @@ -27,17 +28,17 @@ FILE *write_open( char *file ) { /* Martin A. Hansen, November 2005 */ + /* Unit test done. */ + /* Given a file name, write-opens the file, */ /* and returns a file pointer. */ FILE *fp; - char *msg; if ( ( fp = fopen( file, "w" ) ) == NULL ) { - sprintf( msg, "Could not write-open file '%s'.", file ); - - die( msg ); + fprintf( stderr, "ERROR: Could not write-open file '%s': %s\n", file, strerror( errno ) ); + abort(); } return fp; @@ -48,17 +49,17 @@ FILE *append_open( char *file ) { /* Martin A. Hansen, November 2005 */ + /* Unit test done. */ + /* Given a file name, append-opens the file, */ /* and returns a file pointer. */ FILE *fp; - char *msg; if ( ( fp = fopen( file, "a" ) ) == NULL ) { - sprintf( msg, "Could not append-open file '%s'.", file ); - - die( msg ); + fprintf( stderr, "ERROR: Could not append-open file '%s': %s\n", file, strerror( errno ) ); + abort(); } return fp; @@ -69,10 +70,14 @@ void close_stream( FILE *fp ) { /* Martin A. Hansen, May 2008 */ + /* Unit test done. */ + /* Closes a stream or file associated with a given file pointer. */ - if ( ( fclose( fp ) ) != 0 ) { - die( "Could not close stream." ); + if ( ( fclose( fp ) ) != 0 ) + { + fprintf( stderr, "ERROR: Could not close stream: %s\n", strerror( errno ) ); + abort(); } } @@ -86,15 +91,25 @@ char *file_read( FILE *fp, size_t len ) char *string; + assert( len > 0 ); + string = mem_get( len + 1 ); fread( string, len, 1, fp ); - if ( ferror( fp ) != 0 ) { - die( "fread failed." ); + if ( ferror( fp ) != 0 ) + { + fprintf( stderr, "ERROR: file_read failed\n" ); + abort(); + } + else if ( feof( fp ) ) + { + fprintf( stderr, "ERROR: file_read failed - end-of-file reached\n" ); + + abort(); } - string[ len + 1 ] = '\0'; + string[ len ] = '\0'; return string; } @@ -104,15 +119,14 @@ void file_unlink( char *file ) { /* Martin A. Hansen, June 2008 */ - /* Delete a file. */ + /* Unit test done. */ - char *msg; + /* Delete a file. */ if ( unlink( file ) == -1 ) { - sprintf( msg, "Could not delete file '%s'.", file ); - - die( msg ); + fprintf( stderr, "ERROR: Could not unlink file '%s': %s\n", file, strerror( errno ) ); + abort(); } } @@ -121,15 +135,15 @@ void file_rename( char *old_name, char *new_name ) { /* Martin A. Hansen, June 2008 */ - /* Rename a file. */ + /* Unit test done. */ - char *msg; + /* Rename a file. */ - if ( rename( old_name, new_name ) == -1 ) + if ( rename( old_name, new_name ) != 0 ) { - sprintf( msg, "Could not rename file '%s' -> '%s'.", old_name, new_name ); - - die( msg ); + fprintf( stderr, "ERROR: Could not rename file '%s' -> '%s': %s\n", old_name, new_name, strerror( errno ) ); + + abort(); } } diff --git a/code_c/Maasha/src/lib/hash.c b/code_c/Maasha/src/lib/hash.c index 17e4531..ee25d12 100644 --- a/code_c/Maasha/src/lib/hash.c +++ b/code_c/Maasha/src/lib/hash.c @@ -1,4 +1,5 @@ #include "common.h" +#include "mem.h" #include "hash.h" #include "list.h" diff --git a/code_c/Maasha/src/lib/list.c b/code_c/Maasha/src/lib/list.c index efc0f01..4b08c51 100644 --- a/code_c/Maasha/src/lib/list.c +++ b/code_c/Maasha/src/lib/list.c @@ -1,4 +1,5 @@ #include "common.h" +#include "mem.h" #include "list.h" @@ -29,7 +30,7 @@ void list_add_int( struct list_int **list_ppt, int val ) MEM_GET( elem ); elem->val = val; - elem->next = *( list_ppt ); +// elem->next = *( list_ppt ); *( list_ppt ) = ( elem ); } @@ -90,7 +91,7 @@ bool list_exists_int( struct list_int *list_pt, int val ) MEM_GET( elem ); - for ( elem = list_pt; elem != NULL; elem = elem->next ) +// for ( elem = list_pt; elem != NULL; elem = elem->next ) { if ( elem->val == val ) { return TRUE; diff --git a/code_c/Maasha/src/lib/mem.c b/code_c/Maasha/src/lib/mem.c new file mode 100644 index 0000000..72c0280 --- /dev/null +++ b/code_c/Maasha/src/lib/mem.c @@ -0,0 +1,129 @@ +#include "common.h" +#include "mem.h" + + +void *mem_get( size_t size ) +{ + /* Martin A. Hansen, May 2008 */ + + /* Unit test done.*/ + + /* Allocate a given chunk of memory to a pointer that is returned. */ + + void *pt; + + assert( size > 0 ); + + if ( ( pt = malloc( size ) ) == NULL ) + { + fprintf( stderr, "ERROR: Could not allocate %ld byte(s): %s\n", size, strerror( errno ) ); + abort(); + } + + return pt; +} + + +void *mem_get_zero( size_t size ) +{ + /* Martin A. Hansen, May 2008 */ + + /* Unit test done.*/ + + /* Allocate a given chunk of zero'ed memory to a pointer that is returned. */ + + void *pt; + + assert( size > 0 ); + + pt = mem_get( size ); + + memset( pt, '\0', size ); + + return pt; +} + + +void *mem_resize( void *pt, size_t size ) +{ + /* Martin A. Hansen, May 2008 */ + + /* Resize an allocated chunk of memory for a given pointer and new size. */ + + void *pt_new; + + if ( size == 0 ) { + die( "could not re-allocate 0 bytes of memory." ); + } else if ( ( pt_new = realloc( pt, size ) ) == NULL ) { + die( "could not re-allocate memory." ); + } + + return pt_new; +} + + +void *mem_resize_zero( void *pt, size_t old_size, size_t new_size ) +{ + /* Martin A. Hansen, May 2008 */ + + /* Resize an allocated chunk of memory for a given pointer and zero any extra memory. */ + + void *pt_new; + + pt_new = mem_resize( pt, new_size ); + + if ( new_size > old_size ) { + memset( ( ( void * ) pt_new ) + old_size, '\0', new_size - old_size ); + } + + return pt_new; +} + + +void *mem_clone( void *old_pt, size_t size ) +{ + /* Martin A. Hansen, June 2008 */ + + /* Clone a structure in memory and return a pointer to the clone. */ + + void *new_pt; + + new_pt = mem_get( size ); + + memcpy( new_pt, old_pt, size ); + + return new_pt; +} + + +void mem_free( void *pt ) +{ + /* Martin A. Hansen, May 2008 */ + + /* Free memory from a given pointer. */ + + if ( pt != NULL ) + { + free( pt ); + + pt = NULL; + } +} + + +void mem_free_zero( void *pt ) +{ + /* Martin A. Hansen, July 2008 */ + + /* Zero and then free memory from a given pointer. */ + + if ( pt != NULL ) + { + MEM_ZERO( pt ); + free( pt ); + + pt = NULL; + } +} + + diff --git a/code_c/Maasha/src/lib/seq.c b/code_c/Maasha/src/lib/seq.c index c2a5e3e..f11c6b9 100644 --- a/code_c/Maasha/src/lib/seq.c +++ b/code_c/Maasha/src/lib/seq.c @@ -1,4 +1,5 @@ #include "common.h" +#include "mem.h" #include "seq.h" diff --git a/code_c/Maasha/src/lib/strings.c b/code_c/Maasha/src/lib/strings.c index 8a4d5fe..d7e5bfd 100644 --- a/code_c/Maasha/src/lib/strings.c +++ b/code_c/Maasha/src/lib/strings.c @@ -1,25 +1,147 @@ #include "common.h" #include "strings.h" -int match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ) + +size_t chop( char *string ) +{ + /* Martin A. Hansen, June 2008 */ + + /* Unit test done.*/ + + /* Remove the last char from a string. */ + /* Returns the length of the chopped string.*/ + + assert( string != NULL ); + assert( string[ 0 ] != '\0' ); + + size_t len; + + len = strlen( string ); + + string[ len - 1 ] = '\0'; + + return len - 1; +} + + +size_t chomp( char *string ) +{ + /* Martin A. Hansen, June 2008 */ + + /* Unit test done.*/ + + /* Removes the last char from a string if the char is a newline. */ + /* Returns the length of the chomped string or -1 is no newline was found. */ + + int len; + + assert( string != NULL ); + assert( string[ 0 ] != '\0' ); + + len = strlen( string ); + + if ( string[ len - 1 ] == '\n' ) + { + string[ len - 1 ] = '\0'; + + return len - 1; + } + else + { + return -1; + } +} + + +size_t match_substr( size_t pos, char *str, size_t str_len, char *substr, size_t substr_len, size_t mismatch ) { /* Martin A. Hansen, August 2008. */ - /* Locate a substr in a str allowing for a given number of mismatches. */ + /* Unit test done.*/ + + /* Locate a substr in a str starting at pos allowing for a given number of mismatches. */ /* Returns position of match begin or -1 if not found. */ - int pos; - int i; - int count; + size_t i; + size_t j; + size_t count; + + assert( pos >= 0 ); + assert( pos < str_len ); + assert( str != NULL ); + assert( substr != NULL ); + assert( str_len > 0 ); + assert( substr_len > 0 ); + assert( strlen( str ) == str_len ); + assert( strlen( substr ) == substr_len ); + assert( mismatch >= 0 ); + assert( mismatch < substr_len ); + assert( substr_len <= str_len ); + assert( str[ str_len ] == '\0' ); + assert( substr[ substr_len ] == '\0' ); - for ( pos = 0; pos < str_len - substr_len + 1; pos++ ) + for ( i = pos; i < str_len - substr_len + 1; i++ ) { count = 0; - for ( i = 0; i < substr_len; i++ ) + for ( j = 0; j < substr_len; j++ ) + { + if ( str[ i + j ] != substr[ j ] ) + { + count++; + + if ( count > mismatch ) { + break; + } + } + } + + if ( count <= mismatch ) { + return i; + } + } + + return -1; +} + + +size_t match_substr_rev( size_t pos, char *str, size_t str_len, char *substr, size_t substr_len, size_t mismatch ) +{ + /* Martin A. Hansen, August 2008. */ + + /* Unit test done.*/ + + /* Locate a substr in a str backwards starting at the end of */ + /* str minus pos allowing for a given number of mismatches. */ + /* Returns position of match begin or -1 if not found. */ + + size_t i; + size_t j; + size_t count; + + assert( pos >= 0 ); + assert( pos < str_len ); + assert( str != NULL ); + assert( substr != NULL ); + assert( str_len > 0 ); + assert( substr_len > 0 ); + assert( strlen( str ) == str_len ); + assert( strlen( substr ) == substr_len ); + assert( mismatch >= 0 ); + assert( mismatch < substr_len ); + assert( substr_len <= str_len ); + assert( str[ str_len ] == '\0' ); + assert( substr[ substr_len ] == '\0' ); + + for ( i = str_len - pos - 1; i >= substr_len - 1; i-- ) + { + count = 0; + + for ( j = substr_len - 1; j > 0; j-- ) { - if ( str[ pos + i ] != substr[ i ] ) + if ( str[ i - ( substr_len - j - 1 ) ] != substr[ j ] ) { + /* printf( "i:%ld j:%ld count:%ld str:%c substr:%c\n", i, j, count, str[ i - ( substr_len - j - 1 ) ], substr[ j ] ); // DEBUG */ count++; if ( count > mismatch ) { @@ -29,7 +151,7 @@ int match_substr( char *str, int str_len, char *substr, int substr_len, int mism } if ( count <= mismatch ) { - return pos; + return i - substr_len + 1; } } diff --git a/code_c/Maasha/src/lib/ucsc.c b/code_c/Maasha/src/lib/ucsc.c index 2bc6dba..c36bff0 100644 --- a/code_c/Maasha/src/lib/ucsc.c +++ b/code_c/Maasha/src/lib/ucsc.c @@ -1,4 +1,5 @@ #include "common.h" +#include "mem.h" #include "ucsc.h" void bed_get_entry( FILE *fp, struct bed_entry3 *bed, int cols ) @@ -21,7 +22,7 @@ void bed_get_entry( FILE *fp, struct bed_entry3 *bed, int cols ) return; } - return NULL; +// return NULL; } diff --git a/code_c/Maasha/src/repeat-O-matic.c b/code_c/Maasha/src/repeat-O-matic.c index 80a6699..6e7908a 100644 --- a/code_c/Maasha/src/repeat-O-matic.c +++ b/code_c/Maasha/src/repeat-O-matic.c @@ -12,6 +12,7 @@ */ #include "common.h" +#include "mem.h" #include "filesys.h" #include "fasta.h" diff --git a/code_c/Maasha/src/test/Makefile b/code_c/Maasha/src/test/Makefile index 1d62981..d781cb4 100644 --- a/code_c/Maasha/src/test/Makefile +++ b/code_c/Maasha/src/test/Makefile @@ -1,6 +1,5 @@ CC = gcc -# Cflags = -Wall -Werror -Cflags = -Wall -Werror -g -pg # for gprof +Cflags = -Wall -Werror # for gprof INC_DIR = ../inc/ LIB_DIR = ../lib/ @@ -10,10 +9,18 @@ LIB = -lm $(LIB_DIR)*.o all: test -test: test_strings +test: test_filesys test_mem test_strings -test_strings: test_strings.c +test_filesys: test_filesys.c $(LIB_DIR)filesys.c + $(CC) $(Cflags) $(INC) $(LIB) test_filesys.c -o test_filesys + +test_mem: test_mem.c $(LIB_DIR)mem.c + $(CC) $(Cflags) $(INC) $(LIB) test_mem.c -o test_mem + +test_strings: test_strings.c $(LIB_DIR)strings.c $(CC) $(Cflags) $(INC) $(LIB) test_strings.c -o test_strings clean: + rm -f test_filesys + rm -f test_mem rm -f test_strings diff --git a/code_c/Maasha/src/test/test_filesys.c b/code_c/Maasha/src/test/test_filesys.c new file mode 100644 index 0000000..9624668 --- /dev/null +++ b/code_c/Maasha/src/test/test_filesys.c @@ -0,0 +1,158 @@ +#include "common.h" +#include "filesys.h" +#include + +static void test_read_open(); +static void test_write_open(); +static void test_append_open(); +static void test_close_stream(); +static void test_file_read(); +static void test_file_unlink(); +static void test_file_rename(); + + +int main() +{ + fprintf( stderr, "Running all tests for filesys.c\n" ); + + test_read_open(); + test_write_open(); + test_append_open(); + test_close_stream(); + test_file_read(); + test_file_unlink(); + test_file_rename(); + + fprintf( stderr, "Done\n\n" ); + + return EXIT_SUCCESS; +} + + +void test_read_open() +{ + FILE *fp; + + fprintf( stderr, " Testing read_open ... " ); + + // fp = read_open( "/tmp/asdf" ); + // fp = read_open( "/private/etc/ssh_host_rsa_key" ); + fp = read_open( "/dev/null" ); + + close_stream( fp ); + + fprintf( stderr, "OK\n" ); +} + + +void test_write_open() +{ + FILE *fp; + + fprintf( stderr, " Testing write_open ... " ); + + // fp = write_open( "/tmp/asdf" ); + // fp = write_open( "/private/etc/ssh_host_rsa_key" ); + fp = write_open( "/dev/null" ); + + close_stream( fp ); + + fprintf( stderr, "OK\n" ); +} + + +void test_append_open() +{ + FILE *fp; + + fprintf( stderr, " Testing append_open ... " ); + + //fp = append_open( "/tmp/asdf" ); + //fp = append_open( "/private/etc/ssh_host_rsa_key" ); + fp = append_open( "/dev/null" ); + + close_stream( fp ); + + fprintf( stderr, "OK\n" ); +} + + +void test_close_stream() +{ + FILE *fp; + + fprintf( stderr, " Testing close_stream ... " ); + + fp = read_open( "/dev/null" ); + + close_stream( fp ); + + fprintf( stderr, "OK\n" ); +} + + +void test_file_read() +{ + char *test_file = "/etc/passwd"; + char *buffer; + FILE *fp; + size_t len = 1000; + + fprintf( stderr, " Testing file_read ... " ); + + fp = read_open( test_file ); + + buffer = file_read( fp, len ); + + close_stream( fp ); + + assert( strlen( buffer ) == len ); + assert( buffer[ len ] == '\0' ); + + fprintf( stderr, "OK\n" ); +} + + +void test_file_unlink() +{ + char *test_file = "/tmp/test"; + FILE *fp; + + fprintf( stderr, " Testing file_unlink ... " ); + + fp = write_open( test_file ); + + close_stream( fp ); + + //file_unlink( "" ); + //file_unlink( "/dev/null" ); + //file_unlink( "/tmp/" ); + //file_unlink( "/private/etc/ssh_host_rsa_key" ); + file_unlink( test_file ); + + fprintf( stderr, "OK\n" ); +} + + +void test_file_rename() +{ + char *file_before = "/tmp/before"; + char *file_after = "/tmp/after"; + + FILE *fp; + + fprintf( stderr, " Testing file_rename ... " ); + + fp = write_open( file_before ); + + close_stream( fp ); + + //file_rename( file_after, file_after ); + file_rename( file_before, file_before ); + file_rename( file_before, file_after ); + + //file_unlink( file_before ); + file_unlink( file_after ); + + fprintf( stderr, "OK\n" ); +} diff --git a/code_c/Maasha/src/test/test_mem.c b/code_c/Maasha/src/test/test_mem.c new file mode 100644 index 0000000..29ef64d --- /dev/null +++ b/code_c/Maasha/src/test/test_mem.c @@ -0,0 +1,67 @@ +#include "common.h" +#include "mem.h" + +static void test_mem_get(); +static void test_mem_get_zero(); +static void test_mem_free(); + + +int main() +{ + fprintf( stderr, "Running all tests for mem.c\n" ); + + test_mem_get(); + test_mem_get_zero(); + test_mem_free(); + + fprintf( stderr, "Done\n\n" ); + + return EXIT_SUCCESS; +} + + +void test_mem_get() +{ + fprintf( stderr, " Testing mem_get ... " ); + + size_t len = 1000000000; + char *pt = mem_get( len ); + + mem_free( pt ); + + fprintf( stderr, "OK\n" ); +} + + +void test_mem_get_zero() +{ + fprintf( stderr, " Testing mem_get_zero ... " ); + + size_t i = 0; + size_t len = 5555; + char *pt = mem_get_zero( len ); + + for ( i = 0; i <= len; i++ ) { + assert( pt[ i ] == '\0' ); + } + + mem_free( pt ); + + fprintf( stderr, "OK\n" ); +} + + +void test_mem_free() +{ + fprintf( stderr, " Testing mem_free ... " ); + + void *pt = mem_get( 100 ); + + mem_free( pt ); + + printf( "pt: %s\n", ( char * ) pt ); + + assert( pt == NULL ); + + fprintf( stderr, "OK\n" ); +} diff --git a/code_c/Maasha/src/test/test_strings.c b/code_c/Maasha/src/test/test_strings.c index 15016d6..30d04b8 100644 --- a/code_c/Maasha/src/test/test_strings.c +++ b/code_c/Maasha/src/test/test_strings.c @@ -1,78 +1,97 @@ #include "common.h" +#include "strings.h" -static void test_strings(); -static void test_match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ); +static void test_chop(); +static void test_chomp(); +static void test_match_substr(); +static void test_match_substr_rev(); + + +/* + if ((foo_c = malloc(strlen(foo) + 1)) == NULL) + return errno; +*/ int main() { - test_strings(); + fprintf( stderr, "Running all tests for strings.c\n" ); - return 0; -} + test_chop(); + test_chomp(); + test_match_substr(); + test_match_substr_rev(); + fprintf( stderr, "Done\n\n" ); -static void test_strings() -{ - char *str = "MARTIN"; - char *substr = "TXN"; - int str_len = strlen( str ); - int substr_len = strlen( substr ); - int mismatch = 1; - - test_match_substr( str, str_len, substr, substr_len, mismatch ); + return EXIT_SUCCESS; } -static void test_match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ) +static void test_chop() { - fprintf( stderr, "Running test_match_substr\n" ); - - fprintf( stderr, " Testing that str is not NULL ... " ); - assert( str != NULL ); - fprintf( stderr, "done\n" ); - - fprintf( stderr, " Testing that substr is not NULL ... " ); - assert( substr != NULL ); - fprintf( stderr, "done\n" ); + char test[] = "ABCDE"; - fprintf( stderr, " Testing that str_len > zero ... " ); - assert( str_len > 0 ); - fprintf( stderr, "done\n" ); + fprintf( stderr, " Testing chop ... " ); - fprintf( stderr, " Testing that substr_len > zero ... " ); - assert( substr_len > 0 ); - fprintf( stderr, "done\n" ); + assert( chop( test ) == 4 ); + assert( chop( test ) == 3 ); + assert( chop( test ) == 2 ); + assert( chop( test ) == 1 ); + assert( chop( test ) == 0 ); - fprintf( stderr, " Testing str_len ... " ); - assert( strlen( str ) == str_len ); - fprintf( stderr, "done\n" ); + fprintf( stderr, "OK\n" ); +} - fprintf( stderr, " Testing substr_len ... " ); - assert( strlen( substr ) == substr_len ); - fprintf( stderr, "done\n" ); - fprintf( stderr, " Testing that mismatch >= zero ... " ); - assert( mismatch > 0 ); - fprintf( stderr, "done\n" ); +static void test_chomp() +{ + char test[] = "AB\nCDE\n\n\n"; - fprintf( stderr, " Testing that mismatch < substr_len ... " ); - assert( mismatch < substr_len ); - fprintf( stderr, "done\n" ); + fprintf( stderr, " Testing chomp ... " ); - fprintf( stderr, " Testing that substr_len <= str_len ... " ); - assert( substr_len <= str_len ); - fprintf( stderr, "done\n" ); + assert( chomp( test ) == 8 ); + assert( chomp( test ) == 7 ); + assert( chomp( test ) == 6 ); + assert( chomp( test ) == -1 ); + assert( chomp( test ) == -1 ); - fprintf( stderr, " Testing that str is '\\0' terminated ... " ); - assert( str[ str_len ] == '\0' ); - fprintf( stderr, "done\n" ); + fprintf( stderr, "OK\n" ); +} - fprintf( stderr, " Testing that substr is '\\0' terminated ... " ); - assert( substr[ substr_len ] == '\0' ); - fprintf( stderr, "done\n" ); - fprintf( stderr, "test_match_substr OK\n" ); +static void test_match_substr() +{ + fprintf( stderr, " Testing match_substr ... " ); + + assert( match_substr( 0, "MARTIN", 6, "TXN", 3, 0 ) == -1 ); + assert( match_substr( 0, "MARTIN", 6, "TIN", 3, 0 ) == 3 ); + assert( match_substr( 0, "MARTIN", 6, "TXN", 3, 1 ) == 3 ); + assert( match_substr( 0, "MARTIN", 6, "MXR", 3, 0 ) == -1 ); + assert( match_substr( 0, "MARTIN", 6, "MXR", 3, 1 ) == 0 ); + assert( match_substr( 1, "MARTIN", 6, "MXR", 3, 1 ) == -1 ); + assert( match_substr( 5, "MARTIN", 6, "N", 1, 0 ) == 5 ); + assert( match_substr( 0, "M", 1, "M", 1, 0 ) == 0 ); + + fprintf( stderr, "OK\n" ); } +static void test_match_substr_rev() +{ + fprintf( stderr, " Testing match_substr_rev ... " ); + + assert( match_substr_rev( 0, "MARTIN", 6, "TXN", 3, 0 ) == -1 ); + assert( match_substr_rev( 0, "MARTIN", 6, "TIN", 3, 0 ) == 3 ); + assert( match_substr_rev( 2, "MARTIN", 6, "TIN", 3, 0 ) == -1 ); + assert( match_substr_rev( 0, "MARTIN", 6, "MAR", 3, 0 ) == 0 ); + assert( match_substr_rev( 3, "MARTIN", 6, "MAR", 3, 0 ) == 0 ); + assert( match_substr_rev( 4, "MARTIN", 6, "MAR", 3, 0 ) == -1 ); + assert( match_substr_rev( 0, "MARTIN", 6, "TXN", 3, 1 ) == 3 ); + assert( match_substr_rev( 0, "MARTIN", 6, "MXR", 3, 1 ) == 0 ); + assert( match_substr_rev( 4, "MARTIN", 6, "MXR", 3, 1 ) == -1 ); + assert( match_substr_rev( 5, "MARTIN", 6, "M", 1, 0 ) == 0 ); + assert( match_substr_rev( 0, "M", 1, "M", 1, 0 ) == 0 ); + + fprintf( stderr, "OK\n" ); +} diff --git a/code_c/Maasha/src/test_all.pl b/code_c/Maasha/src/test_all.pl new file mode 100755 index 0000000..0868fa4 --- /dev/null +++ b/code_c/Maasha/src/test_all.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +my ( $test_dir, @tests, $test ); + +$test_dir = "test"; + +@tests = qw( + test_mem + test_filesys + test_strings +); + +print STDERR "\nRunning all unit tests:\n\n"; + +foreach $test ( @tests ) +{ + system( "$test_dir/$test" ) == 0 or print STDERR "FAILED\n\n" and exit; +} + +print STDERR "All unit tests OK.\n\n" -- 2.39.2