00001 #include <eh_utils.h>
00002 #include <check.h>
00003
00004 gchar* test_dlmfile[] =
00005 {
00006 "/* a single line comment */\n" ,
00007 "0, 1\n" ,
00008 "/* a multiple line comment\n" ,
00009 " that doesn't end until here. */\n" ,
00010 "2, 3 // c++ style comment\n" ,
00011 " \n" ,
00012 "4, 5, # another type of comment and an extra comma\n" ,
00013 "6; /* ; is also a valid delimiter */ 7\n" ,
00014 "// comment at the end without a newline character" ,
00015 NULL
00016 };
00017
00018 START_TEST ( test_dlmread )
00019 {
00020 char* tmp_file = tempnam( "/tmp" , "DLM_TEST_" );
00021 double** data;
00022 gint i, j;
00023 gint n_rows, n_cols;
00024
00025
00026 {
00027 FILE* fp = eh_fopen( tmp_file , "w" );
00028 char** line;
00029
00030 for ( line=test_dlmfile ; *line ; line++ )
00031 fprintf( fp , "%s" , *line );
00032 fclose( fp );
00033 }
00034
00035
00036 {
00037 FILE* fp = eh_fopen( tmp_file , "r" );
00038 data = eh_dlm_read( tmp_file , ",;:" , &n_rows , &n_cols , NULL );
00039 fclose( fp );
00040 }
00041
00042 fail_unless( n_rows==4 , "Number of rows read incorrectly" );
00043 fail_unless( n_cols==3 , "Number of columns read incorrectly" );
00044
00045 for ( i=0 ; i<n_rows ; i++ )
00046 for ( j=0 ; j<2 ; j++ )
00047 fail_unless( eh_compare_dbl(data[i][j],2.*i+j,1e-12) ,
00048 "Incorrect value read" );
00049 for ( i=0 ; i<n_rows ; i++ )
00050 fail_unless( eh_compare_dbl(data[i][2],0.,1e-12) ,
00051 "Incorrect value read" );
00052
00053 eh_free_2( data );
00054 }
00055 END_TEST
00056
00057 gchar* test_dlm_seq_file[] =
00058 {
00059 "/* a single line comment */\n" ,
00060 " [ rec number: 1 ]" ,
00061 "0, 1\n" ,
00062 "/* a multiple line comment\n" ,
00063 " that doesn't end until here. */\n" ,
00064 " [ name: a new record \n" ,
00065 " label: value] \n" ,
00066 "2, 3 // c++ style comment\n" ,
00067 " \n" ,
00068 "4, 5, # another type of comment and an extra comma\n" ,
00069 "6; /* ; is also a valid delimiter */ 7\n" ,
00070 " \n" ,
00071 " \n" ,
00072 NULL
00073 };
00074
00075 START_TEST ( test_dlmread_seq )
00076 {
00077 char* tmp_file = tempnam( "/tmp" , "DLM_TEST_" );
00078 double*** data;
00079 gchar** rec_data;
00080 gint *n_rows, *n_cols;
00081
00082
00083 {
00084 FILE* fp = eh_fopen( tmp_file , "w" );
00085 char** line;
00086
00087 for ( line=test_dlm_seq_file ; *line ; line++ )
00088 fprintf( fp , "%s" , *line );
00089 fclose( fp );
00090 }
00091
00092
00093 {
00094 FILE* fp = eh_fopen( tmp_file , "r" );
00095 data = eh_dlm_read_full( tmp_file , ",;:" , &n_rows , &n_cols , &rec_data , -1 , NULL );
00096 fclose( fp );
00097 }
00098
00099 fail_unless( data!=NULL && data[0]!=NULL && data[1]!=NULL && data[2]==NULL );
00100
00101 fail_unless( n_rows[0]==1 , "Number of rows of first record read incorrectly" );
00102 fail_unless( n_cols[0]==2 , "Number of columns of first record read incorrectly" );
00103
00104 fail_unless( n_rows[1]==3 , "Number of rows of second record read incorrectly" );
00105 fail_unless( n_cols[1]==3 , "Number of columns of second record read incorrectly" );
00106
00107 fail_unless( eh_compare_dbl(data[0][0][0],0,1e-12)
00108 && eh_compare_dbl(data[0][0][1],1,1e-12) ,
00109 "Incorrect value read" );
00110
00111 fail_unless( eh_compare_dbl(data[1][0][0],2,1e-12)
00112 && eh_compare_dbl(data[1][0][1],3,1e-12)
00113 && eh_compare_dbl(data[1][1][0],4,1e-12)
00114 && eh_compare_dbl(data[1][1][1],5,1e-12)
00115 && eh_compare_dbl(data[1][2][0],6,1e-12)
00116 && eh_compare_dbl(data[1][2][1],7,1e-12) ,
00117 "Incorrect value read" );
00118
00119 fail_unless( eh_compare_dbl(data[1][0][2],0,1e-12)
00120 && eh_compare_dbl(data[1][1][2],0,1e-12)
00121 && eh_compare_dbl(data[1][2][2],0,1e-12) ,
00122 "Incorrect value read" );
00123
00124 eh_free_2( data );
00125 }
00126 END_TEST
00127
00128 Suite *eh_utils_suite( void )
00129 {
00130 Suite *s = suite_create( "Eh_utils" );
00131 TCase *test_case_core = tcase_create( "Core" );
00132
00133 suite_add_tcase( s , test_case_core );
00134
00135 tcase_add_test( test_case_core , test_dlmread );
00136 tcase_add_test( test_case_core , test_dlmread_seq );
00137
00138 return s;
00139 }
00140
00141 int test_utils( void )
00142 {
00143 int n;
00144
00145 {
00146 Suite *s = eh_utils_suite();
00147 SRunner *sr = srunner_create( s );
00148
00149 srunner_run_all( sr , CK_NORMAL );
00150 n = srunner_ntests_failed( sr );
00151 srunner_free( sr );
00152 }
00153
00154 return n;
00155 }
00156