/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/utils/eh_grid_unit_test.c

Go to the documentation of this file.
00001 #include <eh_utils.h>
00002 #include <check.h>
00003 
00004 START_TEST (test_create_grid)
00005 {
00006    Eh_dbl_grid g = eh_grid_new( double , 50 , 250 );
00007 
00008    fail_if( g==NULL , "Grid set to NULL" );
00009    fail_unless( eh_grid_n_x(g) == 50  , "Number of x-nodes not set correctly" );
00010    fail_unless( eh_grid_n_y(g) == 250 , "Number of y-nodes not set correctly" );
00011 
00012    g = eh_grid_destroy( g , TRUE );
00013 }
00014 END_TEST
00015 
00016 START_TEST (test_destroy_grid)
00017 {
00018    Eh_dbl_grid g = eh_grid_new( double , 1 , 1 );
00019 
00020    g = eh_grid_destroy( g , TRUE );
00021 
00022    fail_unless( g==NULL , "Destroy function should return NULL" );
00023 }
00024 END_TEST
00025 
00026 START_TEST (test_cmp_grid)
00027 {
00028    gboolean ok;
00029    Eh_dbl_grid g_1 = eh_grid_new( double , 50 , 250 );
00030    Eh_dbl_grid g_2;
00031 
00032    eh_dbl_grid_randomize( g_1 );
00033    g_2 = eh_grid_dup( g_1 );
00034 
00035    ok = eh_grid_cmp_x_data( g_1 , g_2 );
00036    fail_unless( ok , "Comparison of grid x-data failed" );
00037 
00038    ok = eh_grid_cmp_y_data( g_1 , g_2 );
00039    fail_unless( ok , "Comparison of grid y-data failed" );
00040 
00041    ok = eh_grid_cmp_data( g_1 , g_2 );
00042    fail_unless( ok , "Comparison of grid data failed" );
00043 
00044    eh_dbl_grid_set_val( g_2 , 0 , 0 , eh_dbl_grid_val(g_2,0,0)+1e-12 );
00045    ok = eh_dbl_grid_cmp( g_1 , g_2 , 1e-10 );
00046    fail_unless( ok , "Comparison of almost equal grids failed" );
00047 
00048    ok = eh_dbl_grid_cmp( g_1 , g_2 , -1 );
00049    fail_unless( !ok , "Comparison of unequal grids failed" );
00050 
00051    g_1 = eh_grid_destroy( g_1 , TRUE );
00052    g_2 = eh_grid_destroy( g_2 , TRUE );
00053 }
00054 END_TEST
00055 
00056 START_TEST (test_cmp_unequal_grid)
00057 {
00058    gboolean ok;
00059    Eh_dbl_grid g_1 = eh_grid_new( double , 50 , 250 );
00060    Eh_dbl_grid g_2 = eh_grid_new( double , 50 , 50  );
00061 
00062    ok = eh_dbl_grid_cmp( g_1 , g_2 , -1 );
00063 
00064    fail_if( ok , "Comparison of unequal sized grids failed" );
00065 
00066    g_1 = eh_grid_destroy( g_1 , TRUE );
00067    g_2 = eh_grid_destroy( g_2 , TRUE );
00068 }
00069 END_TEST
00070 
00071 START_TEST ( test_zero_create_grid )
00072 {
00073    Eh_dbl_grid g;
00074 
00075    g = eh_grid_new( double , 0 , 0 );
00076    fail_if( g==NULL , "Poorly created empty grid" );
00077    fail_unless( eh_grid_n_x(g)==0     , "Incorrect dimensions of empty grid" );
00078    fail_unless( eh_grid_n_y(g)==0     , "Incorrect dimensions of empty grid" );
00079    fail_unless( eh_grid_data(g)==NULL , "Memory should not be allocated for empty grid" );
00080    fail_unless( eh_grid_x(g)==NULL    , "Memory should not be allocated for empty grid" );
00081    fail_unless( eh_grid_x(g)==NULL    , "Memory should not be allocated for empty grid" );
00082    eh_grid_destroy( g , TRUE );
00083 
00084    g = eh_grid_new( double , 0 , 250 );
00085    fail_unless( eh_grid_n_x(g)==0     , "Incorrect dimensions of empty grid" );
00086    fail_unless( eh_grid_n_y(g)==250   , "Incorrect dimensions of empty grid" );
00087    fail_unless( eh_grid_data(g)==NULL , "Memory should not be allocated for empty grid" );
00088    fail_unless( eh_grid_x(g)==NULL    , "Memory should not be allocated for empty grid" );
00089    fail_unless( eh_grid_y(g)!=NULL    , "Memory should be allocated for y-coordinates" );
00090    eh_grid_destroy( g , TRUE );
00091 
00092    g = eh_grid_new( double , 50 , 0 );
00093    fail_unless( eh_grid_n_x(g)==50    , "Incorrect dimensions of empty grid" );
00094    fail_unless( eh_grid_n_y(g)==0     , "Incorrect dimensions of empty grid" );
00095    fail_unless( eh_grid_data(g)==NULL , "Memory should not be allocated for empty grid" );
00096    fail_unless( eh_grid_x(g)!=NULL    , "Memory should be allocated for x-coordinates" );
00097    fail_unless( eh_grid_y(g)==NULL    , "Memory should not be allocated for empty grid" );
00098    eh_grid_destroy( g , TRUE );
00099 }
00100 END_TEST
00101 
00102 START_TEST ( test_negative_create_grid )
00103 {
00104    Eh_dbl_grid g;
00105 
00106    g = eh_grid_new( double , -1 , 250 );
00107    fail_unless( g==NULL , "NULL not returned with negative dimension" );
00108 
00109    g = eh_grid_new( double , 50 , -1 );
00110    fail_unless( g==NULL , "NULL not returned with negative dimension" );
00111 
00112 }
00113 END_TEST
00114 
00115 START_TEST ( test_set_grid )
00116 {
00117    double total, n_elem = 50*250;
00118    Eh_dbl_grid g = eh_grid_new( double , 50 , 250 );
00119 
00120    eh_dbl_grid_set( g , 1 );
00121 
00122    total = eh_dbl_grid_sum( g );
00123 
00124    fail_unless( fabs(total-n_elem)<1e-16 , "Grid set incorrectly" );
00125 
00126    g = eh_grid_destroy( g , TRUE );
00127 }
00128 END_TEST
00129 
00130 START_TEST (test_dup_grid)
00131 {
00132    Eh_dbl_grid g = eh_grid_new( double , 50 , 250 );
00133    Eh_dbl_grid h;
00134    
00135    eh_dbl_grid_randomize( g );
00136 
00137    h = eh_grid_dup( g );
00138 
00139    fail_unless( eh_dbl_grid_cmp( g , h , 1e-6 ) ,
00140                 "Duplicated grid differs from original" );
00141 
00142    eh_grid_destroy( g , TRUE );
00143    eh_grid_destroy( h , TRUE );
00144 
00145 }
00146 END_TEST
00147 
00148 START_TEST ( test_copy_grid )
00149 {
00150    Eh_dbl_grid src  = eh_grid_new( double , 50 , 250 );
00151    Eh_dbl_grid dest = eh_grid_new( double , 50 , 250 );
00152 
00153    eh_dbl_grid_randomize( src );
00154 
00155    eh_grid_copy( dest , src );
00156 
00157    fail_unless( eh_grid_cmp_data  ( dest , src ) , "Grid copy failed"   );
00158    fail_unless( eh_grid_cmp_x_data( dest , src ) , "X-data copy failed" );
00159    fail_unless( eh_grid_cmp_y_data( dest , src ) , "Y-data copy failed" );
00160 
00161    src  = eh_grid_destroy( src  , TRUE );
00162    dest = eh_grid_destroy( dest , TRUE );
00163 }
00164 END_TEST
00165 
00166 START_TEST (test_sum_grid)
00167 {
00168    double total;
00169    Eh_dbl_grid g = eh_grid_new( double , 50 , 250 );
00170    
00171    eh_dbl_grid_set( g , 1. );
00172 
00173    total = eh_dbl_grid_sum( g );
00174 
00175    fail_unless( fabs( total-eh_grid_n_el(g) ) < 1e-6 , "Incorrect sum" );
00176 
00177    g = eh_grid_destroy( g , TRUE );
00178 }
00179 END_TEST
00180 
00181 START_TEST (test_bad_sum_grid)
00182 {
00183    int i, j;
00184    int n_bad_vals = 0;
00185    double total;
00186    Eh_dbl_grid g = eh_grid_new( double , 50 , 250 );
00187    
00188    for ( i=0 ; i<50 ; i++ )
00189       for ( j=0 ; j<250 ; j++ )
00190       {
00191          if ( g_random_boolean( ) )
00192             eh_dbl_grid_set_val( g , i , j , 1. );
00193          else
00194          {
00195             eh_dbl_grid_set_val( g , i , j , -1.+1e-16 );
00196             n_bad_vals++;
00197          }
00198       }
00199 
00200    total = eh_dbl_grid_sum_bad_val( g , -1 );
00201 
00202    fail_unless( fabs( total-(eh_grid_n_el(g)-n_bad_vals) ) < 1e-6 ,
00203                 "Incorrect sum using -1" );
00204 
00205    n_bad_vals = 0;
00206    for ( i=0 ; i<50 ; i++ )
00207       for ( j=0 ; j<250 ; j++ )
00208       {
00209          if ( g_random_boolean( ) )
00210             eh_dbl_grid_set_val(g,i,j,1.);
00211          else
00212          {
00213             eh_dbl_grid_set_val(g,i,j,eh_nan());
00214             n_bad_vals++;
00215          }
00216       }
00217 
00218    total = eh_dbl_grid_sum( g );
00219 
00220    fail_unless( fabs( total-(eh_grid_n_el(g)-n_bad_vals) ) < 1e-6 ,
00221                 "Incorrect sum using NaN" );
00222    g = eh_grid_destroy( g , TRUE );
00223 }
00224 END_TEST
00225 
00226 START_TEST (test_scalar_mult_grid)
00227 {
00228    double total_before, total_after;
00229    double multiplier;
00230    Eh_dbl_grid g = eh_grid_new( double , 50 , 250 );
00231    
00232    multiplier = g_random_double();
00233 
00234    eh_dbl_grid_randomize( g );
00235    total_before = eh_dbl_grid_sum( g );
00236 
00237    eh_dbl_grid_scalar_mult( g , multiplier );
00238    total_after = eh_dbl_grid_sum( g );
00239 
00240    fail_unless( fabs( total_before-total_after/multiplier ) < 1e-6 ,
00241                 "Incorrect multiplication" );
00242 
00243    g = eh_grid_destroy( g , TRUE );
00244 }
00245 END_TEST
00246 
00247 START_TEST ( test_reindex_grid )
00248 {
00249    Eh_int_grid g = eh_grid_new( int , 50 , 250 );
00250 
00251    eh_int_grid_set_val( g , 0 , 0 , 1 );
00252    eh_int_grid_set_val( g , eh_grid_n_x(g)-1 , eh_grid_n_y(g)-1  , eh_grid_n_el(g) );
00253 
00254    eh_grid_x(g)[0]                = 1;
00255    eh_grid_x(g)[eh_grid_n_x(g)-1] = eh_grid_n_x(g);
00256 
00257    eh_grid_y(g)[0]                = 1;
00258    eh_grid_y(g)[eh_grid_n_y(g)-1] = eh_grid_n_y(g);
00259 
00260    eh_grid_reindex( g , -5 , -10 );
00261 
00262    fail_unless( eh_int_grid_val(g,-5,-10) == 1 , "Lower limit of data not reindexed" );
00263    fail_unless( eh_grid_x(g)[-5]          == 1 , "Lower limit of x-data not reindexed" );
00264    fail_unless( eh_grid_y(g)[-10]         == 1 , "Lower limit of y-data not reindexed" );
00265 
00266    {
00267       gssize i = -5  + eh_grid_n_x(g) - 1;
00268       gssize j = -10 + eh_grid_n_y(g) - 1;
00269 
00270       fail_unless( eh_int_grid_val(g,i,j) == eh_grid_n_el(g) ,
00271                    "Upper limit of data not reindexed" );
00272       fail_unless( eh_grid_x(g)[i]  == eh_grid_n_x(g) ,
00273                    "Upper limit of x-data not reindexed" );
00274       fail_unless( eh_grid_y(g)[j] == eh_grid_n_y(g) ,
00275                    "Upper limit of y-data not reindexed" );
00276    }
00277 
00278    g = eh_grid_destroy( g , TRUE );
00279 }
00280 END_TEST
00281 
00282 START_TEST ( test_reduce_grid )
00283 {
00284    double initial_sum, final_sum;
00285    Eh_dbl_grid g = eh_grid_new( double , 1 , 50 );
00286    Eh_dbl_grid small_g;
00287 
00288    g = eh_dbl_grid_randomize( g );
00289    g = eh_dbl_grid_set( g , 1. );
00290    initial_sum = eh_dbl_grid_sum( g );
00291 
00292    small_g   = eh_dbl_grid_reduce( g , 1 , 10 );
00293    final_sum = eh_dbl_grid_sum( small_g );
00294 
00295    fail_unless( small_g!=NULL            , "NULL grid was returned"                );
00296    fail_unless( eh_grid_n_x(small_g)==1  , "x-direction was not reduced correctly" );
00297    fail_unless( eh_grid_n_y(small_g)==10 , "y-direction was not reduced correctly" );
00298    fail_if    ( final_sum==0             , "NaNs found in reduced grid"            );
00299 //eh_dbl_grid_fprintf( stderr , "%f " , small_g );
00300    fail_unless( fabs(initial_sum-5*final_sum)<1e-3 , "Mass lost in grid reduction" );
00301 
00302    g       = eh_grid_destroy( g       , TRUE );
00303    small_g = eh_grid_destroy( small_g , TRUE );
00304 }
00305 END_TEST
00306 
00307 START_TEST ( test_rebin_grid )
00308 {
00309    Eh_dbl_grid x, x_new;
00310    double sum, sum_new;
00311    gssize i, j;
00312 
00313    x     = eh_grid_new( double , 93 , 109  );
00314    x_new = eh_grid_new( double ,  2 , 1000 );
00315 
00316    for ( i=0 ; i<eh_grid_n_x(x) ; i++ )
00317       eh_grid_x(x)[i] = 33*i;
00318 
00319    for ( j=0 ; j<eh_grid_n_y(x) ; j++ )
00320       eh_grid_y(x)[j] = 300*j;
00321 
00322    eh_dbl_grid_set( x , 1 );
00323 
00324    for ( i=0 ; i<eh_grid_n_x(x_new) ; i++ )
00325       eh_grid_x(x_new)[i] = 10000*i;
00326 
00327    for ( j=0 ; j<eh_grid_n_y(x_new) ; j++ )
00328       eh_grid_y(x_new)[j] = 500*j;
00329 
00330    eh_dbl_grid_rebin_bad_val( x , x_new , 0 );
00331 
00332    sum     = eh_dbl_grid_sum( x )*33*300;
00333    sum_new = eh_dbl_grid_sum( x_new )*10000*500;
00334 /*
00335    eh_dbl_grid_fprintf( stderr , "%f " , x_new );
00336    eh_watch_dbl( sum );
00337    eh_watch_dbl( sum_new );
00338 */
00339 
00340    fail_unless( fabs(sum_new-sum)/sum < 1e-2 , "Grid not rebinned correctly" );
00341 
00342    eh_grid_destroy( x     , TRUE );
00343    eh_grid_destroy( x_new , TRUE );
00344 
00345 }
00346 END_TEST
00347 
00348 START_TEST ( test_line_path )
00349 {
00350    Eh_grid g = eh_grid_new( double , 10 , 10  );
00351    gssize* path;
00352 
00353    {
00354       path = eh_dbl_grid_line_ids( g , 2 , 2 , 2 , 2 );
00355       fail_unless( path==NULL , "NULL if start and end are the same point" );
00356    }
00357 
00358    {
00359       gssize ans[7] = { 0 , 1 , 2 , 3 , 4 , 5 , -1 };
00360       path = eh_dbl_grid_line_ids( g , 0 , 0 , 0 , 5 );
00361 
00362       fail_if( path==NULL , "Path should be non-NULL" );
00363       fail_unless( eh_grid_path_is_same(path,ans) , "Incorrect path" );
00364 
00365       eh_free( path );
00366    }
00367 
00368    {
00369       gssize ans[7] = { 0 , 11 , 22 , 33 , 44 , 55 , -1 };
00370       path = eh_dbl_grid_line_ids( g , 0 , 0 , 5 , 5 );
00371 
00372       fail_if( path==NULL , "Path should be non-NULL" );
00373       fail_unless( eh_grid_path_is_same(path,ans) , "Incorrect path" );
00374 
00375       eh_free( path );
00376    }
00377 
00378    {
00379       gssize ans[7] = { 0 , 10 , 20 , 30 , 40 , 50 , -1 };
00380       path = eh_dbl_grid_line_ids( g , 0 , 0 , 5 , 0 );
00381 
00382       fail_if( path==NULL , "Path should be non-NULL" );
00383       fail_unless( eh_grid_path_is_same(path,ans) , "Incorrect path" );
00384 
00385       eh_free( path );
00386    }
00387 
00388    {
00389       gssize ans[7] = { 5 , 4 , 3 , 2 , 1 , 0 , -1 };
00390       path = eh_dbl_grid_line_ids( g , 0 , 5 , 0 , 0 );
00391 
00392       fail_if( path==NULL , "Path should be non-NULL" );
00393       fail_unless( eh_grid_path_is_same(path,ans) , "Incorrect path" );
00394 
00395       eh_free( path );
00396    }
00397 
00398    {
00399       gssize ans[7] = { 50 , 40 , 30 , 20 , 10 , 0 , -1 };
00400       path = eh_dbl_grid_line_ids( g , 5 , 0 , 0 , 0 );
00401 
00402       fail_if( path==NULL , "Path should be non-NULL" );
00403       fail_unless( eh_grid_path_is_same(path,ans) , "Incorrect path" );
00404 
00405       eh_free( path );
00406    }
00407 
00408    {
00409       gssize ans[10] = { 0 , 10 , 21 , 31 , 42 , 53 , -1 };
00410       path = eh_dbl_grid_line_ids( g , 0 , 0 , 5 , 3 );
00411 
00412       fail_if( path==NULL , "Path should be non-NULL" );
00413       fail_unless( eh_grid_path_is_same(path,ans) , "Incorrect path" );
00414 
00415       eh_free( path );
00416    }
00417 
00418    eh_grid_destroy( g , TRUE );
00419 }
00420 END_TEST
00421 
00422 Suite *grid_suite( void )
00423 {
00424    Suite *s = suite_create( "Grid" );
00425    TCase *test_case_core = tcase_create( "Core" );
00426    TCase *test_case_set  = tcase_create( "Set" );
00427 
00428    suite_add_tcase( s , test_case_core );
00429    suite_add_tcase( s , test_case_set  );
00430 
00431    tcase_add_test( test_case_core , test_create_grid          );
00432    tcase_add_test( test_case_core , test_destroy_grid         );
00433    tcase_add_test( test_case_core , test_set_grid             );
00434    tcase_add_test( test_case_core , test_zero_create_grid     );
00435    tcase_add_test( test_case_core , test_negative_create_grid );
00436 
00437    tcase_add_test( test_case_set  , test_dup_grid             );
00438    tcase_add_test( test_case_set  , test_copy_grid            );
00439    tcase_add_test( test_case_set  , test_sum_grid             );
00440    tcase_add_test( test_case_set  , test_bad_sum_grid         );
00441    tcase_add_test( test_case_set  , test_scalar_mult_grid     );
00442    tcase_add_test( test_case_set  , test_cmp_grid             );
00443    tcase_add_test( test_case_set  , test_cmp_unequal_grid     );
00444    tcase_add_test( test_case_set  , test_reindex_grid         );
00445    tcase_add_test( test_case_set  , test_reduce_grid          );
00446    tcase_add_test( test_case_set  , test_rebin_grid           );
00447 //   tcase_add_test( test_case_set  , test_line_path            );
00448 
00449    return s;
00450 }
00451 
00452 int test_grid( void )
00453 {
00454    int n;
00455 
00456    {
00457       Suite *s = grid_suite();
00458       SRunner *sr = srunner_create( s );
00459 
00460       srunner_run_all( sr , CK_NORMAL );
00461       n = srunner_ntests_failed( sr );
00462       srunner_free( sr );
00463    }
00464 
00465    return n;
00466 }
00467 

Generated on Fri Jan 4 18:04:16 2008 for sedflux by  doxygen 1.5.2