test_pydim_utils.cpp

Go to the documentation of this file.
00001 #include "pydim_utils.cpp"
00002 
00003 using namespace std;
00004     
00005 char good_format01[]="i:3;F:2;C";
00006 char good_format02[]="I:3;F:2;C:4;";
00007 char good_format03[]="D";
00008 char good_format04[]="I:1;D:1;F:1;X:1;S:1";
00009 char good_format05[]="C:13;X:2;C:1";
00010 char good_format06[]="i:3;s:1;x:1;S:2;C:5";
00011 
00012 char bad_format01[] ="D:";
00013 char bad_format02[] ="D:4C";
00014 char bad_format03[] ="D:;I";
00015 char bad_format04[] ="i:1;V:";
00016 char bad_format05[] ="i:123;i:289;:21";
00017     
00018 struct test01{
00019     int i1;
00020     int i2;
00021     int i3;
00022     float f1;
00023     float f2;
00024     char c1;        
00025 } __attribute__((__packed__));
00026 
00027 struct test011{
00028     int i1;
00029     int i2;
00030     int i3;
00031     float f1;
00032     float f2;
00033     char c1[20];        
00034 } __attribute__((__packed__));
00035     
00036 struct test02{
00037     int i1;
00038     double d1;        
00039     float f1;
00040     long long ll1;
00041     short s1;        
00042 } __attribute__((__packed__));
00043 
00044 int main () {
00045     
00046     // Testing verify_dim_format()
00047     print ("testing verify_dim_format()...");
00048     if (verify_dim_format(good_format01)) {
00049        print("SUCCESS: Validity test for %s (good format) PASSED", 
00050               good_format01);
00051     } else {
00052        print("ERROR  : Validity test for %s (good format) FAILED", 
00053               good_format01);
00054     }
00055         
00056     if (verify_dim_format(good_format02)){
00057        print("SUCCESS: Validity test for %s (good format) PASSED", 
00058               good_format02);
00059     } else {
00060        print("ERROR  : Validity test for %s (good format) FAILED", 
00061               good_format02);
00062     }
00063 
00064     if (verify_dim_format(good_format03)){
00065        print("SUCCESS: Validity test for %s (good format) PASSED", 
00066               good_format03);
00067     } else {
00068        print("ERROR  : Validity test for %s (good format) FAILED", 
00069               good_format03);
00070     }
00071 
00072     if (!verify_dim_format(bad_format01)) {        
00073        print("SUCCESS: Validity test for %s (bad format) PASSED", 
00074               bad_format01);
00075     } else {
00076        print("ERROR  : Validity test for %s (bad format) FAILED", 
00077               bad_format01);
00078     }
00079 
00080     if (!verify_dim_format(bad_format02)) {
00081        print("SUCCESS: Validity test for %s (bad format) PASSED", 
00082               bad_format02);
00083     } else {
00084        print("ERROR  : Validity test for %s (bad format) FAILED", 
00085               bad_format02);
00086     }
00087 
00088     if (!verify_dim_format(bad_format03)) {
00089        print("SUCCESS: Validity test for %s (bad format) PASSED", 
00090               bad_format03);
00091     } else {
00092        print("ERROR  : Validity test for %s (bad format) FAILED", 
00093               bad_format03);
00094     }
00095 
00096     if (!verify_dim_format(bad_format04)) {
00097        print("SUCCESS: Validity test for %s (bad format) PASSED", 
00098               bad_format04);
00099     } else {
00100        print("ERROR  : Validity test for %s (bad format) FAILED", 
00101               bad_format04);
00102     }
00103 
00104     if (!verify_dim_format(bad_format05)) {
00105        print("SUCCESS: Validity test for %s (bad format) PASSED\n\n", 
00106               bad_format05);
00107     } else {
00108        print("ERROR  : Validity test for %s (bad format) FAILED\n\n", 
00109               bad_format05);
00110     }
00111     
00112     
00113     // testing next_element()
00114     
00115     int type, multiplicity, failed=0;
00116     unsigned int internal_ptr=0; 
00117     print ("testing next_element()...");
00118     // TEST 1         
00119     if (!next_element(good_format01, &internal_ptr, &type, &multiplicity))
00120         failed=1;
00121     if (internal_ptr!=4 || type!=_DIM_INT || multiplicity!=3)
00122         failed=2;
00123     if (!next_element(good_format01, &internal_ptr, &type, &multiplicity))
00124         failed=3;
00125     if (internal_ptr!=8 || type!=_DIM_FLOAT || multiplicity!=2)
00126         failed=4;
00127     if (!next_element(good_format01, &internal_ptr, &type, &multiplicity))
00128         failed=5;        
00129     if (internal_ptr!=9 || type!=_DIM_STRING || multiplicity!=-1)
00130         failed=6;
00131     if (next_element(good_format01, &internal_ptr, &type, &multiplicity))
00132         failed=7;
00133     if (failed) { 
00134         print("ERROR  : next_element subtest %d FAILED for %s", 
00135               failed, good_format01);
00136     } else {
00137         print("SUCCESS: next_element tests PASSED for %s", good_format01);
00138     }         
00139 //    printf("Internal ptr: %d,  Type: %d,  Multiplicity: %d\n",
00140 //            internal_ptr, type, multiplicity);
00141 
00142     // TEST 2
00143     internal_ptr=0;
00144     failed=0;
00145     
00146     if (!next_element(good_format02, &internal_ptr, &type, &multiplicity))
00147         failed=1;
00148     if (internal_ptr!=4 || type!=_DIM_INT || multiplicity!=3)
00149         failed=2;
00150     if (!next_element(good_format02, &internal_ptr, &type, &multiplicity))
00151         failed=3;
00152     if (internal_ptr!=8 || type!=_DIM_FLOAT || multiplicity!=2)
00153         failed=4;
00154     if (!next_element(good_format02, &internal_ptr, &type, &multiplicity))
00155         failed=5;        
00156     if (internal_ptr!=12 || type!=_DIM_STRING || multiplicity!=4)
00157         failed=6;
00158 //    printf("Internal ptr: %d,  Type: %d,  Multiplicity: %d\n",
00159 //            internal_ptr, type, multiplicity);
00160     
00161     if (next_element(good_format01, &internal_ptr, &type, &multiplicity))
00162         failed=7;
00163     if (failed) { 
00164         print("ERROR  : next_element subtest %d FAILED for %s", 
00165                failed, good_format02);
00166     } else {
00167         print("SUCCESS: next_element tests PASSED for %s", good_format02);
00168     } 
00169     
00170     // TEST 3
00171     internal_ptr=0;
00172     failed=0;
00173     
00174     if (!next_element(good_format03, &internal_ptr, &type, &multiplicity))
00175         failed=1;
00176     if (internal_ptr!=1 || type!=_DIM_DOUBLE || multiplicity!=-1)
00177         failed=2;
00178 //    printf("Internal ptr: %d,  Type: %d,  Multiplicity: %d\n",
00179 //            internal_ptr, type, multiplicity);
00180     
00181     if (next_element(good_format03, &internal_ptr, &type, &multiplicity))
00182         failed=3;
00183     if (failed) {
00184         print("ERROR  : next_element subtest %d FAILED for %s", 
00185                failed, good_format03);
00186     } else {
00187         print("SUCCESS: next_elemnt tests PASSED for %s", good_format03);
00188     }
00189     
00190     
00191     // testing next_element()
00192         
00193     PyObject *list1, *list2, *list3;
00194 
00195 
00196     test01 teststrct1, teststrct2;
00197     teststrct1.i1=10240;
00198     teststrct1.i2=102400;
00199     teststrct1.i3=1024000;
00200     teststrct1.f1=10240.1023;
00201     teststrct1.f2=11111111.11111;
00202     teststrct1.c1='X';
00203     
00204     test02 teststrct3, teststrct4;
00205     teststrct3.i1=255255;
00206     teststrct3.d1=1.1234567;
00207     teststrct3.f1=123456.654321;
00208     teststrct3.ll1=1234567123;
00209     teststrct3.s1=1023;
00210     printf("\n\n");
00211     print ("testing dim_buf_to_list()...");       
00212     list1 = dim_buf_to_list(good_format01, (const char*)&teststrct1, 
00213                             sizeof(teststrct1));
00214     if (list1) {
00215         //printPyObject(list1);
00216         if (!iterator_to_buffer(list1,(char*)&teststrct2, 
00217                     sizeof(test01), good_format01)) {
00218             print("FAIL  : Could not convert tuple object to C buffer");
00219         } else {
00220             if (!memcmp(&teststrct1, &teststrct2, sizeof(test01))) {
00221                 print("SUCCESS: The two structures are the same for arguments converted with format %s", good_format01);
00222             } else {
00223                 print("FAIL  :The two structures differ"); 
00224             }
00225         }
00226     } else {
00227         print("FAIL  : Could not convert C buffer to Python list");
00228     }
00229     
00230     list2 = dim_buf_to_list(good_format04, (const char*)&teststrct3, 
00231                             sizeof(teststrct3));
00232     if (list2) {
00233         //printPyObject(list2);
00234         if (!iterator_to_buffer(list2,(char*)&teststrct4, 
00235                     sizeof(test02), good_format04)) {
00236             print("FAIL  : Could not convert tuple object to C buffer");
00237         } else {
00238             if (!memcmp(&teststrct3, &teststrct4, sizeof(test02))) {
00239                 print("SUCCESS: The two structures are the same for arguments converted with format %s", good_format04);
00240             } else {
00241                 print("FAIL  : The two structures differ");        
00242             }
00243         }
00244     } else {
00245         print("FAIL  : Could not convert C buffer to Python list");
00246     }
00247     
00248     double d1=100.101, d2;
00249     list3 = dim_buf_to_list("d", (const char*)&d1, sizeof(d1));
00250     if (list3) {
00251         //printPyObject(list2);
00252         if (!iterator_to_buffer(list3,(char*)&d2, sizeof(d2), "D:1")) {
00253             print("FAIL  : Could not convert tuple object to C buffer");
00254         } else {
00255             if (d1==d2) {
00256                 print("SUCCESS: Two doubles are the same after back and forth conversion");
00257             } else {
00258                 print("FAIL  : The two doubles");        
00259             }
00260         }
00261     } else {
00262         print("FAIL  : Could not convert C buffer to Python list");
00263     }
00264     
00265     
00266     printf("\n\n");
00267     print ("testing getElemNrFromFormat()...");
00268     
00269     if (getElemNrFromFormat(good_format02)==6) {
00270        print("SUCCESS: Elem nr. test for %s (good format) PASSED", 
00271               good_format02);
00272     } else {
00273        print("ERROR  : Elem nr. test for %s (good format) FAILED.",
00274              good_format02);
00275        print("-->Got result %d, expected 6", 
00276              getElemNrFromFormat(good_format02));
00277        
00278     }
00279         
00280     if (getElemNrFromFormat(good_format04)==5) {
00281        print("SUCCESS: Elem nr. test for %s (good format) PASSED", 
00282               good_format04);
00283     } else {
00284        print("ERROR  : Elem nr. test for %s (good format) FAILED", 
00285               good_format04);
00286     }
00287     
00288     if (getElemNrFromFormat(good_format05)==4) {
00289        print("SUCCESS: Elem nr. test for %s (good format) PASSED", 
00290               good_format05);
00291     } else {
00292        print("ERROR  : Elem nr. test for %s (good format) FAILED", 
00293               good_format05);
00294     }
00295     
00296     if (getElemNrFromFormat(good_format06)==8) {
00297        print("SUCCESS: Elem nr. test for %s (good format) PASSED", 
00298               good_format06);
00299     } else {
00300        print("ERROR  : Elem nr. test for %s (good format) FAILED", 
00301               good_format06);
00302     } 
00303     
00304     
00305     printf("\n\n");
00306     print("Testing iterator_to_allocated_buffer...");
00307     
00308     double test04d1[]={100.101, 123}, *test04d2;
00309     unsigned int test04s=0;
00310     
00311     list3 = dim_buf_to_list("d", (const char*)&test04d1, sizeof(test04d1));
00312     if (list3) {
00313         //printPyObject(list2);
00314         if (!iterator_to_allocated_buffer(list3,"D", (char**)&test04d2, &test04s)) {
00315             print("FAIL  : Could not convert tuple object to C buffer");
00316         } else {
00317             if (test04d1[0]==test04d2[0] && test04d1[1]==test04d2[1]) {
00318                 print("SUCCESS: Two doubles are the same after back and forth conversion using dynamic allocated buffers");                
00319             } else {
00320                 print("FAIL  : The two doubles differ");                        
00321             }
00322             delete test04d2;
00323         }
00324     } else {
00325         print("FAIL  : Could not convert C buffer to Python list");
00326     }
00327     
00328     test011 teststrct0401, *teststrct0402;
00329     teststrct0401.i1=10240;
00330     teststrct0401.i2=102400;
00331     teststrct0401.i3=1024000;
00332     teststrct0401.f1=10240.1023;
00333     teststrct0401.f2=11111111.11111;
00334     memcpy(teststrct0401.c1, "ALA BALA PORTOCALA\0\0", 20);
00335     unsigned int test04s02=0;
00336     
00337     list3 = dim_buf_to_list(good_format01, (const char*)&teststrct0401, sizeof(test011));
00338     if (list3) {
00339         //printPyObject(list3);
00340         if (!iterator_to_allocated_buffer(list3, good_format01, (char**)&teststrct0402, &test04s02)) {
00341             print("FAIL  : Could not convert tuple object to C buffer");
00342         } else {
00343             if (!memcmp(&teststrct0401, teststrct0402, sizeof(test011))) {
00344                 print("SUCCESS: Structures are the same after back and forth conversion using dynamic allocated buffers");                
00345             } else {
00346                 print("FAIL  : The two structures differ");                        
00347             }
00348             delete teststrct0402;
00349         }
00350     } else {
00351         print("FAIL  : Could not convert C buffer to Python list");
00352     }
00353     
00354     memcpy(teststrct0401.c1, "CUC\0", 4);
00355     list3 = dim_buf_to_list(good_format02, (const char*)&teststrct0401, sizeof(test011));
00356     if (list3) {
00357         //printPyObject(list3);
00358         if (!iterator_to_allocated_buffer(list3, good_format02, (char**)&teststrct0402, &test04s02)) {
00359             print("FAIL  : Could not convert tuple object to C buffer");
00360         } else {
00361             if (!memcmp(&teststrct0401, teststrct0402, sizeof(test011)-16) ) {
00362                 print("SUCCESS: Structures are the same after back and forth conversion using dynamic allocated buffers");                
00363             } else {
00364                 print("FAIL  : The two structures differ");                        
00365             }
00366             delete teststrct0402;
00367         }
00368     } else {
00369         print("FAIL  : Could not convert C buffer to Python list");
00370     }
00371 }

Generated on 5 Feb 2014 for PyDIM by  doxygen 1.4.7