]> git.donarmstrong.com Git - biopieces.git/commitdiff
update of c libs
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 13 Aug 2008 23:35:20 +0000 (23:35 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 13 Aug 2008 23:35:20 +0000 (23:35 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@206 74ccb610-7750-0410-82ae-013aeee3265d

21 files changed:
code_c/Maasha/src/gmon.out
code_c/Maasha/src/inc/common.h
code_c/Maasha/src/inc/filesys.h
code_c/Maasha/src/inc/mem.h [new file with mode: 0644]
code_c/Maasha/src/inc/strings.h
code_c/Maasha/src/lib/Makefile
code_c/Maasha/src/lib/common.c
code_c/Maasha/src/lib/fasta.c
code_c/Maasha/src/lib/filesys.c
code_c/Maasha/src/lib/hash.c
code_c/Maasha/src/lib/list.c
code_c/Maasha/src/lib/mem.c [new file with mode: 0644]
code_c/Maasha/src/lib/seq.c
code_c/Maasha/src/lib/strings.c
code_c/Maasha/src/lib/ucsc.c
code_c/Maasha/src/repeat-O-matic.c
code_c/Maasha/src/test/Makefile
code_c/Maasha/src/test/test_filesys.c [new file with mode: 0644]
code_c/Maasha/src/test/test_mem.c [new file with mode: 0644]
code_c/Maasha/src/test/test_strings.c
code_c/Maasha/src/test_all.pl [new file with mode: 0755]

index 0459c2d5876f181f6cec9a2ab4b5582f0afd1d15..5176fb2725a133e7561246dd60826927015b4842 100644 (file)
Binary files a/code_c/Maasha/src/gmon.out and b/code_c/Maasha/src/gmon.out differ
index 687fd429defe0f27f30eae540575141908f4f76f..320f0a99acafcd89bc676aae87b1e465399d0355 100644 (file)
@@ -7,6 +7,7 @@
 #include <ctype.h>
 #include <math.h>
 #include <assert.h>
+#include <errno.h>
 
 /* Define a shorthand for unsigned int */
 #define uint unsigned int
 #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 );
     
index 87e5e898b471e5a320c1a659adfa2d9427cf1979..6f9e872328cb46bac39a967f761cd5d16f58263c 100644 (file)
@@ -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 (file)
index 0000000..08c976d
--- /dev/null
@@ -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 );
index c2cae78f2cc40541df5ae22c74e77467ae458153..fa1468654103927d9aa4fbcbb0f145eb69da1416 100644 (file)
@@ -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 );
index dfdb4f974b987724b2df9c70110bb921e369987d..7b9c64e82bcb2e7c49f8c411d719a7eb39c137be 100644 (file)
@@ -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
+
index 7f1c99d4c8674e2a0e8b9e835cba824ba168e5ad..05e7793f89549af9ab7cb900ef84c78ccbe17e89 100644 (file)
@@ -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 */
index 5a2ca7faf75496689273a229581ab05e13668004..21fc66f682e7fae97477c09e3689850adbcfb499 100644 (file)
@@ -1,4 +1,5 @@
 #include "common.h"
+#include "mem.h"
 #include "fasta.h"
 #include "list.h"
 
index a7489a6b55b7caa5c5b6f8c6e201a5883c99e9db..d3bf3db1550ef4eae055f3808aae326c131ffadf 100644 (file)
@@ -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();
     }
 }
 
index 17e453143ba02f8eeef61cad0fd04bfdc90b9dc2..ee25d12c527e618b4d0b66538e74555e57c82aa9 100644 (file)
@@ -1,4 +1,5 @@
 #include "common.h"
+#include "mem.h"
 #include "hash.h"
 #include "list.h"
 
index efc0f01c66b79287e036c949784618c0b0132a4f..4b08c51d4ac56bd4d7b05793bf51eddd969c4ea0 100644 (file)
@@ -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 (file)
index 0000000..72c0280
--- /dev/null
@@ -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;
+    }
+}
+
+
index c2a5e3e3e8e184426b380ccb7a618f3cf813d52e..f11c6b97f83865a09d8f592714671cc009636d07 100644 (file)
@@ -1,4 +1,5 @@
 #include "common.h"
+#include "mem.h"
 #include "seq.h"
 
 
index 8a4d5fe74ecccc57a68f2772fcd77cab415b28be..d7e5bfd64d55182dc812266a9927d44ff47cf94a 100644 (file)
 #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;
         }
     }
 
index 2bc6dba43f5fd6213e56bc2340931f79defac83a..c36bff08efe111e07a2ddeb97c65646e7f33305b 100644 (file)
@@ -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;
 }
 
 
index 80a66990c51b81149163275fa64d642de4b08bfd..6e7908a13a4c2a13ab88d611608bf83c7b2381b6 100644 (file)
@@ -12,6 +12,7 @@
 */
 
 #include "common.h"
+#include "mem.h"
 #include "filesys.h"
 #include "fasta.h"
 
index 1d6298164c40a7a947f6a716505c06db6c7e0132..d781cb47c77df2e56a315a25dc90830d7ef47953 100644 (file)
@@ -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 (file)
index 0000000..9624668
--- /dev/null
@@ -0,0 +1,158 @@
+#include "common.h"
+#include "filesys.h"
+#include <errno.h>
+
+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 (file)
index 0000000..29ef64d
--- /dev/null
@@ -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" );
+}
index 15016d6e362cded72f666d90e51639ed1bddc99a..30d04b80225464376de3246dc33be190b74e81bb 100644 (file)
@@ -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 (executable)
index 0000000..0868fa4
--- /dev/null
@@ -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"