#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
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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 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 );
};
/* 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
--- /dev/null
+/* 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 );
-/* 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 );
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
$(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
+
#include "common.h"
#include "list.h"
+#include "mem.h"
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ERROR HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
}
-/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 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 */
#include "common.h"
+#include "mem.h"
#include "fasta.h"
#include "list.h"
#include "common.h"
+#include "mem.h"
#include "filesys.h"
{
/* 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;
{
/* 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;
{
/* 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;
{
/* 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();
}
}
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;
}
{
/* 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();
}
}
{
/* 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();
}
}
#include "common.h"
+#include "mem.h"
#include "hash.h"
#include "list.h"
#include "common.h"
+#include "mem.h"
#include "list.h"
MEM_GET( elem );
elem->val = val;
- elem->next = *( list_ppt );
+// elem->next = *( list_ppt );
*( list_ppt ) = ( elem );
}
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;
--- /dev/null
+#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;
+ }
+}
+
+
#include "common.h"
+#include "mem.h"
#include "seq.h"
#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 ) {
}
if ( count <= mismatch ) {
- return pos;
+ return i - substr_len + 1;
}
}
#include "common.h"
+#include "mem.h"
#include "ucsc.h"
void bed_get_entry( FILE *fp, struct bed_entry3 *bed, int cols )
return;
}
- return NULL;
+// return NULL;
}
*/
#include "common.h"
+#include "mem.h"
#include "filesys.h"
#include "fasta.h"
CC = gcc
-# Cflags = -Wall -Werror
-Cflags = -Wall -Werror -g -pg # for gprof
+Cflags = -Wall -Werror # for gprof
INC_DIR = ../inc/
LIB_DIR = ../lib/
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
--- /dev/null
+#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" );
+}
--- /dev/null
+#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" );
+}
#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" );
+}
--- /dev/null
+#!/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"