00001 #include "utils/utils.h" 00002 #include <check.h> 00003 00004 #include "sed_hydro.h" 00005 00006 START_TEST ( test_sed_hydro_new ) 00007 { 00008 Sed_hydro h = sed_hydro_new( 5 ); 00009 00010 fail_if ( h==NULL , "NULL is not a valid object" ); 00011 fail_unless( sed_hydro_size(h)==5 , "Incorrect number of grain types" ); 00012 00013 { 00014 double qb = sed_hydro_bedload(h); 00015 double u = sed_hydro_velocity(h); 00016 double w = sed_hydro_width(h); 00017 double d = sed_hydro_depth(h); 00018 double qs = sed_hydro_suspended_concentration(h); 00019 double dt = sed_hydro_duration(h); 00020 00021 fail_unless( eh_compare_dbl( qb , 0 , 1e-12 ) , "Bedload should be 0" ); 00022 fail_unless( eh_compare_dbl( u , 0 , 1e-12 ) , "Veloctiy should be 0" ); 00023 fail_unless( eh_compare_dbl( w , 0 , 1e-12 ) , "Width should be 0" ); 00024 fail_unless( eh_compare_dbl( d , 0 , 1e-12 ) , "Depth should be 0" ); 00025 fail_unless( eh_compare_dbl( qs , 0 , 1e-12 ) , "Depth should be 0" ); 00026 fail_unless( eh_compare_dbl( dt , 1 , 1e-12 ) , "Duration should be 1" ); 00027 } 00028 00029 sed_hydro_destroy( h ); 00030 } 00031 END_TEST 00032 00033 START_TEST ( test_sed_hydro_copy ) 00034 { 00035 Sed_hydro a = sed_hydro_new( 5 ); 00036 Sed_hydro a_0 = sed_hydro_new( 5 ); 00037 Sed_hydro b = sed_hydro_new( 5 ); 00038 Sed_hydro b_0; 00039 00040 sed_hydro_set_width ( a , 500 ); 00041 sed_hydro_set_depth ( a , 7 ); 00042 sed_hydro_set_velocity( a , 1.23 ); 00043 sed_hydro_set_duration( a , 33 ); 00044 00045 sed_hydro_set_width ( a_0 , 500 ); 00046 sed_hydro_set_depth ( a_0 , 7 ); 00047 sed_hydro_set_velocity( a_0 , 1.23 ); 00048 sed_hydro_set_duration( a_0 , 33 ); 00049 00050 b_0 = sed_hydro_copy( b , a ); 00051 00052 fail_unless( b_0==b , "Destination should be returned" ); 00053 fail_unless( sed_hydro_is_same(b,a) , "Destination should be same a source" ); 00054 fail_unless( sed_hydro_is_same(a,a_0) , "Source should not change" ); 00055 00056 sed_hydro_destroy( a ); 00057 sed_hydro_destroy( a_0 ); 00058 sed_hydro_destroy( b ); 00059 } 00060 END_TEST 00061 00062 START_TEST ( test_sed_hydro_add_cell ) 00063 { 00064 double f[5] = { .2 , .2 , .2 , .2 , .2 }; 00065 Sed_hydro a = sed_hydro_new( 4 ); 00066 Sed_cell c = sed_cell_new_sized( 5 , 1. , f ); 00067 double volume = 12.; 00068 double load_0, load_1, cell_load; 00069 00070 sed_hydro_set_width ( a , 500 ); 00071 sed_hydro_set_depth ( a , 7 ); 00072 sed_hydro_set_velocity( a , 1.23 ); 00073 sed_hydro_set_duration( a , 33 ); 00074 00075 load_0 = sed_hydro_total_load( a ); 00076 00077 sed_cell_resize( c , volume ); 00078 sed_hydro_add_cell( a , c ); 00079 00080 load_1 = sed_hydro_total_load( a ); 00081 00082 cell_load = sed_cell_density( c )*volume; 00083 00084 fail_unless( eh_compare_dbl( load_1 , load_0+cell_load , 1e-12 ) , "" ); 00085 00086 sed_hydro_destroy( a ); 00087 sed_cell_destroy ( c ); 00088 } 00089 END_TEST 00090 00091 START_TEST ( test_sed_hydro_subtract_cell ) 00092 { 00093 double f[5] = { .2 , .2 , .2 , .2 , .2 }; 00094 Sed_hydro a = sed_hydro_new( 4 ); 00095 Sed_cell c = sed_cell_new_sized( 5 , 1. , f ); 00096 double volume = 12.; 00097 double load_0, load_1, cell_load; 00098 00099 sed_hydro_set_width ( a , 500 ); 00100 sed_hydro_set_depth ( a , 7 ); 00101 sed_hydro_set_velocity( a , 1.23 ); 00102 sed_hydro_set_duration( a , 33 ); 00103 00104 sed_cell_resize( c , volume ); 00105 sed_hydro_add_cell( a , c ); 00106 00107 load_0 = sed_hydro_total_load( a ); 00108 00109 sed_cell_resize( c , volume/5. ); 00110 sed_hydro_subtract_cell( a , c ); 00111 00112 load_1 = sed_hydro_total_load( a ); 00113 cell_load = sed_cell_density( c )*volume/5.; 00114 00115 fail_unless( eh_compare_dbl( load_1 , load_0-cell_load , 1e-12 ) , "" ); 00116 00117 sed_cell_resize( c , volume ); 00118 sed_hydro_subtract_cell( a , c ); 00119 00120 load_1 = sed_hydro_total_load( a ); 00121 00122 fail_unless( eh_compare_dbl( load_1 , 0 , 1e-12 ) , "" ); 00123 00124 sed_hydro_destroy( a ); 00125 sed_cell_destroy ( c ); 00126 } 00127 END_TEST 00128 00129 START_TEST ( test_sed_hydro_copy_null ) 00130 { 00131 Sed_hydro a = sed_hydro_new( 5 ); 00132 Sed_hydro b; 00133 00134 sed_hydro_set_width ( a , 500 ); 00135 sed_hydro_set_depth ( a , 7 ); 00136 sed_hydro_set_velocity( a , 1.23 ); 00137 sed_hydro_set_duration( a , 33 ); 00138 00139 b = sed_hydro_copy( NULL , a ); 00140 00141 fail_if( b==NULL , "Duplicate should be valid" ); 00142 fail_if( b==a , "A copy should be made" ); 00143 fail_unless( sed_hydro_is_same(b,a) , "Destination should be same a source" ); 00144 00145 sed_hydro_destroy( a ); 00146 sed_hydro_destroy( b ); 00147 } 00148 END_TEST 00149 00150 START_TEST ( test_sed_hydro_init ) 00151 { 00152 Sed_hydro* a = sed_hydro_scan( NULL , NULL ); 00153 Sed_hydro b = sed_hydro_new( 4 ); 00154 00155 sed_hydro_set_duration( b , 365. ); 00156 sed_hydro_set_bedload ( b , 23.1 ); 00157 sed_hydro_set_velocity( b , 1.06 ); 00158 sed_hydro_set_width ( b , 263 ); 00159 sed_hydro_set_depth ( b , 8.3 ); 00160 00161 { 00162 double qb = sed_hydro_bedload (a[0]); 00163 double u = sed_hydro_velocity(a[0]); 00164 double w = sed_hydro_width (a[0]); 00165 double d = sed_hydro_depth (a[0]); 00166 double dt = sed_hydro_duration(a[0]); 00167 00168 fail_unless( eh_compare_dbl( qb , 23.1 , 1e-12 ) , "Bedload not scanned properly" ); 00169 fail_unless( eh_compare_dbl( u , 1.06 , 1e-12 ) , "Veloctiy not scanned properly" ); 00170 fail_unless( eh_compare_dbl( w , 263 , 1e-12 ) , "Width not scanned properly" ); 00171 fail_unless( eh_compare_dbl( d , 8.3 , 1e-12 ) , "Depth not scanned properly" ); 00172 fail_unless( eh_compare_dbl( dt , 365 , 1e-12 ) , "Duration not scanned properly" ); 00173 } 00174 00175 fail_unless( sed_hydro_is_same(b,a[0]) , "Record not scanned properly" ); 00176 00177 sed_hydro_array_destroy( a ); 00178 sed_hydro_destroy( b ); 00179 } 00180 END_TEST 00181 00182 START_TEST ( test_sed_hydro_init_file ) 00183 { 00184 Sed_hydro* a = sed_hydro_scan( NULL , NULL ); 00185 Sed_hydro* b = sed_hydro_scan( SED_HYDRO_TEST_INLINE_FILE , NULL ); 00186 00187 fail_unless( sed_hydro_is_same(b[0],a[0]) , "Record not scanned properly" ); 00188 00189 sed_hydro_array_destroy( a ); 00190 sed_hydro_array_destroy( b ); 00191 } 00192 END_TEST 00193 00194 START_TEST ( test_sed_hydro_file_new_inline ) 00195 { 00196 Sed_hydro_file f = sed_hydro_file_new( SED_HYDRO_TEST_INLINE_FILE , SED_HYDRO_INLINE , FALSE , TRUE , NULL ); 00197 Sed_hydro a = sed_hydro_file_read_record( f ); 00198 Sed_hydro b = sed_hydro_file_read_record( f ); 00199 00200 { 00201 double qb = sed_hydro_bedload (a); 00202 double u = sed_hydro_velocity(a); 00203 double w = sed_hydro_width (a); 00204 double d = sed_hydro_depth (a); 00205 double dt = sed_hydro_duration(a); 00206 00207 fail_unless( eh_compare_dbl( qb , 23.1 , 1e-12 ) , "Bedload not scanned properly" ); 00208 fail_unless( eh_compare_dbl( u , 1.06 , 1e-12 ) , "Veloctiy not scanned properly" ); 00209 fail_unless( eh_compare_dbl( w , 263 , 1e-12 ) , "Width not scanned properly" ); 00210 fail_unless( eh_compare_dbl( d , 8.3 , 1e-12 ) , "Depth not scanned properly" ); 00211 fail_unless( eh_compare_dbl( dt , 365 , 1e-12 ) , "Duration not scanned properly" ); 00212 } 00213 00214 fail_unless( sed_hydro_is_same(a,b) , "File should rewind at EOF" ); 00215 00216 sed_hydro_destroy( a ); 00217 sed_hydro_destroy( b ); 00218 sed_hydro_file_destroy( f ); 00219 } 00220 END_TEST 00221 00222 START_TEST ( test_sed_hydro_file_new_binary ) 00223 { 00224 Sed_hydro_file f = sed_hydro_file_new( SED_HYDRO_TEST_FILE , SED_HYDRO_HYDROTREND , FALSE , TRUE , NULL ); 00225 Sed_hydro a = sed_hydro_file_read_record( f ); 00226 00227 { 00228 double qb = sed_hydro_bedload (a); 00229 double u = sed_hydro_velocity(a); 00230 double w = sed_hydro_width (a); 00231 double d = sed_hydro_depth (a); 00232 double dt = sed_hydro_duration(a); 00233 00234 fail_unless( eh_compare_dbl( qb , 4.467378 , 1e-6 ) , "Bedload not scanned properly" ); 00235 fail_unless( eh_compare_dbl( u , .901985 , 1e-6 ) , "Veloctiy not scanned properly" ); 00236 fail_unless( eh_compare_dbl( w , 95.524864 , 1e-6 ) , "Width not scanned properly" ); 00237 fail_unless( eh_compare_dbl( d , 4.236207 , 1e-6 ) , "Depth not scanned properly" ); 00238 fail_unless( eh_compare_dbl( dt , 1 , 1e-6 ) , "Duration not scanned properly" ); 00239 } 00240 00241 sed_hydro_destroy( a ); 00242 sed_hydro_file_destroy( f ); 00243 } 00244 END_TEST 00245 00246 START_TEST ( test_sed_hydro_file_new_buffer ) 00247 { 00248 double a_load, b_load; 00249 00250 { 00251 Sed_hydro a; 00252 Sed_hydro_file f = sed_hydro_file_new( SED_HYDRO_TEST_FILE , 00253 SED_HYDRO_HYDROTREND , 00254 TRUE , TRUE , NULL ); 00255 double dt; 00256 00257 for ( dt=0 ; dt<365 ; ) 00258 { 00259 a = sed_hydro_file_read_record( f ); 00260 dt += sed_hydro_duration( a ); 00261 if ( dt <= 365 ) 00262 a_load += sed_hydro_total_load( a ); 00263 sed_hydro_destroy( a ); 00264 } 00265 00266 sed_hydro_file_destroy( f ); 00267 } 00268 00269 { 00270 gssize i; 00271 Sed_hydro b; 00272 Sed_hydro_file f = sed_hydro_file_new( SED_HYDRO_TEST_FILE , 00273 SED_HYDRO_HYDROTREND , 00274 FALSE , TRUE , NULL ); 00275 00276 for ( i=0,b_load=0 ; i<365 ; i++ ) 00277 { 00278 b = sed_hydro_file_read_record( f ); 00279 b_load += sed_hydro_total_load( b ); 00280 sed_hydro_destroy( b ); 00281 } 00282 00283 sed_hydro_file_destroy( f ); 00284 } 00285 00286 fail_unless( eh_compare_dbl(a_load,b_load,1e-12) , "Mass balance error" ); 00287 } 00288 END_TEST 00289 00290 Suite *sed_hydro_suite( void ) 00291 { 00292 Suite *s = suite_create( "Sed_hydro" ); 00293 TCase *test_case_core = tcase_create( "Core" ); 00294 TCase *test_case_limits = tcase_create( "Limits" ); 00295 00296 suite_add_tcase( s , test_case_core ); 00297 suite_add_tcase( s , test_case_limits ); 00298 00299 tcase_add_test( test_case_core , test_sed_hydro_new ); 00300 tcase_add_test( test_case_core , test_sed_hydro_copy ); 00301 tcase_add_test( test_case_core , test_sed_hydro_add_cell ); 00302 tcase_add_test( test_case_core , test_sed_hydro_subtract_cell ); 00303 tcase_add_test( test_case_core , test_sed_hydro_init ); 00304 tcase_add_test( test_case_core , test_sed_hydro_init_file ); 00305 tcase_add_test( test_case_core , test_sed_hydro_file_new_inline ); 00306 tcase_add_test( test_case_core , test_sed_hydro_file_new_binary ); 00307 tcase_add_test( test_case_core , test_sed_hydro_file_new_buffer ); 00308 00309 tcase_add_test( test_case_limits , test_sed_hydro_copy_null ); 00310 00311 return s; 00312 } 00313 00314 int test_sed_hydro( void ) 00315 { 00316 int n; 00317 00318 { 00319 Suite *s = sed_hydro_suite(); 00320 SRunner *sr = srunner_create( s ); 00321 00322 srunner_run_all( sr , CK_NORMAL ); 00323 n = srunner_ntests_failed( sr ); 00324 srunner_free( sr ); 00325 } 00326 00327 return n; 00328 } 00329 00330