Unittest_MEDparameter_1.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2015  EDF R&D, CEA/DEN
00004  *  MED is free software: you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation, either version 3 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  MED is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 /*
00019  * Unitary tests to create parameters in MED files
00020  */
00021 
00022 #include <med.h>
00023 #define MESGERR 1
00024 #include <med_utils.h>
00025 
00026 #include <string.h>
00027 
00028 int main (int argc, char **argv)
00029 {
00030   med_idt fid;
00031   const char filename[] = "Unittest_MEDparameter_1.med";
00032   const char comment[] = "Parameters unit tests";
00033   const char p1name[] = "FLOAT_PARAMETER";
00034   const char p2name[] = "INT_PARAMETER_1";
00035   const char p3name[] = "INT_PARAMETER_2";
00036   const med_parameter_type p1type = MED_FLOAT64;
00037   const med_parameter_type p2type = MED_INT;
00038   const med_parameter_type p3type = MED_INT;
00039   const char p1description[] = "PARAMETER 1 DESCRIPTION";
00040   const char p2description[] = "PARAMETER 2 DESCRIPTION";
00041   const char p3description[] = "PARAMETER 3 DESCRIPTION";
00042   const char dtunit1[] = "DT UNIT P1";
00043   const char dtunit2[] = "DT UNIT P2";
00044   const char dtunit3[] = "DT UNIT P3";
00045   med_int np;
00046   char nameToRead[MED_NAME_SIZE+1];
00047   char descriptionToRead[MED_COMMENT_SIZE+1];
00048   med_parameter_type typeToRead;
00049   char dtunitToRead[MED_SNAME_SIZE+1];
00050   med_int nstepToRead;
00051   int i,j;
00052   med_float p1v1 = 6.98;
00053   med_float p1v2 = 19.07;
00054   med_float p1v3 = 78.0;
00055   med_int p1numdt1 = MED_NO_DT;
00056   med_int p1numdt2 = 1;
00057   med_int p1numdt3 = 1;
00058   med_int p1numit1 = MED_NO_IT;
00059   med_int p1numit2 = 3;
00060   med_int p1numit3 = 2;
00061   med_float p1dt1 = 0.0;
00062   med_float p1dt2 = 0.1;
00063   med_float p1dt3 = 5.5;
00064   const med_int p1ncpst = 3;
00065   med_int p2v1 = 5;
00066   med_int p2v2 = 6;
00067   med_int p2numit1 = MED_NO_IT;
00068   med_int p2numdt1 = MED_NO_DT;
00069   med_float p2dt1 = 0.0;
00070   const med_int p2ncpst = 1;
00071   med_int p3v1 = 77;
00072   med_int p3v2 = 89;
00073   med_int p3numdt1 = 1;
00074   med_int p3numit1 = 1;
00075   med_float p3dt1 = 18.9;
00076   med_int p3numdt2 = MED_NO_DT;
00077   med_int p3numit2 = MED_NO_IT;
00078   med_float p3dt2 = 0.0;
00079   const med_int p3ncpst = 2;
00080   med_int numdtToRead;
00081   med_int numitToRead;
00082   med_float dtToRead; 
00083   med_float fvalueToRead;
00084   med_int ivalueToRead;
00085 
00086   /* file creation */
00087   fid = MEDfileOpen(filename,MED_ACC_CREAT);
00088   if (fid < 0) {
00089     MESSAGE("ERROR : file creation ...");
00090     SSCRUTE(filename);
00091     return -1;
00092   }
00093 
00094   if (MEDfileCommentWr(fid,comment) < 0) {
00095     MESSAGE("ERROR : write comment ...");
00096     SSCRUTE(comment);
00097     return -1;
00098   }
00099 
00100   /* create a MED_FLOAT64 parameter */
00101   if (MEDparameterCr(fid, p1name, p1type, p1description, dtunit1) < 0) {
00102         MESSAGE("ERROR : parameter p1 creation ...");
00103         SSCRUTE(p1name);
00104         return -1;
00105   }
00106 
00107   if (MEDparameterValueWr(fid, p1name, p1numdt1, p1numit1, p1dt1, (unsigned char*) &p1v1) < 0) {
00108         MESSAGE("ERROR : write p1v1   ...");
00109         SSCRUTE(p1name);
00110         return -1;
00111   }
00112   if (MEDparameterValueWr(fid, p1name, p1numdt2, p1numit2, p1dt2, (unsigned char*) &p1v2) < 0) {
00113         MESSAGE("ERROR : write p1v2   ...");
00114         SSCRUTE(p1name);
00115         return -1;
00116   }
00117   if (MEDparameterValueWr(fid, p1name, p1numdt3, p1numit3, p1dt3, (unsigned char*) &p1v3) < 0) {
00118         MESSAGE("ERROR : write p1v3   ...");
00119         SSCRUTE(p1name);
00120         return -1;
00121   }
00122 
00123   /* create a MED_INT parameter with different values */
00124   if (MEDparameterCr(fid, p2name, p2type, p2description, dtunit2) < 0) {
00125         MESSAGE("ERROR : paramter p2 creation ...");
00126         SSCRUTE(p2name);
00127         return -1;
00128   }
00129   if (MEDparameterValueWr(fid, p2name, p2numdt1, p2numit1, p2dt1, (unsigned char*) &p2v1) < 0) {
00130         MESSAGE("ERROR : write p2v1   ...");
00131         SSCRUTE(p2name);
00132         return -1;
00133   }
00134   if (MEDparameterValueWr(fid, p2name, p2numdt1, p2numit1, p2dt1, (unsigned char*) &p2v2) < 0) {
00135         MESSAGE("ERROR : write p2v2 on same computation step  ...");
00136         SSCRUTE(p2name);
00137         return -1;
00138   }
00139 
00140   /* create another MED_INT parameter */
00141   if (MEDparameterCr(fid, p3name, p3type, p3description, dtunit3) < 0) {
00142         MESSAGE("ERROR : paramter p3 creation ...");
00143         SSCRUTE(p3name);
00144         return -1;
00145   }
00146   if (MEDparameterValueWr(fid, p3name, p3numdt1, p3numit1, p3dt1, (unsigned char*) &p3v1) < 0) {
00147         MESSAGE("ERROR : write p3v1   ...");
00148         SSCRUTE(p3name);
00149         return -1;
00150   }
00151   if (MEDparameterValueWr(fid, p3name, p3numdt2, p3numit2, p3dt2, (unsigned char*) &p3v2) < 0) {
00152         MESSAGE("ERROR : write p3v2   ...");
00153         SSCRUTE(p3name);
00154         return -1;
00155   }
00156 
00157   /* close file */
00158   if (MEDfileClose(fid) < 0) {
00159     MESSAGE("ERROR : file closing ...");             
00160     return -1; 
00161   } 
00162 
00163   /* open file in READ ONLY access mode */
00164   fid = MEDfileOpen(filename,MED_ACC_RDONLY);
00165   if (fid < 0) {
00166     MESSAGE("ERROR : open in READ ONLY ACCESS mode ...");
00167     SSCRUTE(filename);
00168     return -1;
00169   }
00170 
00171   /* direct access to parameters */
00172   if (MEDparameterInfoByName(fid, p1name, &typeToRead, descriptionToRead,
00173                              dtunitToRead, &nstepToRead) < 0) {
00174     MESSAGE("ERROR : read information for parameter p1 ...");
00175     SSCRUTE(p1name);
00176     ISCRUTE(typeToRead);
00177     SSCRUTE(descriptionToRead);
00178     SSCRUTE(dtunitToRead);
00179     ISCRUTE(nstepToRead);
00180     return -1;
00181   }
00182   if ((typeToRead != p1type) || strcmp(descriptionToRead, p1description) ||
00183       strcmp(dtunitToRead,dtunit1) || (nstepToRead != p1ncpst)) {
00184     MESSAGE("ERROR : read information for parameter p1 : attributes ...");
00185     SSCRUTE(p1name);
00186     ISCRUTE(typeToRead);
00187     SSCRUTE(descriptionToRead);
00188     SSCRUTE(dtunitToRead);
00189     ISCRUTE(nstepToRead);
00190     return -1;
00191   }
00192 
00193   if (MEDparameterInfoByName(fid, p2name, &typeToRead, descriptionToRead,
00194                              dtunitToRead, &nstepToRead) < 0) {
00195     MESSAGE("ERROR : read information for parameter p2 ...");
00196     SSCRUTE(p2name);
00197     ISCRUTE(typeToRead);
00198     SSCRUTE(descriptionToRead);
00199     SSCRUTE(dtunitToRead);
00200     ISCRUTE(nstepToRead);
00201     return -1;
00202   }
00203   if ((typeToRead != p2type) || strcmp(descriptionToRead, p2description) ||
00204       strcmp(dtunitToRead,dtunit2) || (nstepToRead != p2ncpst)) {
00205     MESSAGE("ERROR : read information for parameter p2 : attributes ...");
00206     SSCRUTE(p2name); 
00207     ISCRUTE(typeToRead); ISCRUTE(p2type);
00208     SSCRUTE(descriptionToRead); SSCRUTE(p2description);
00209     SSCRUTE(dtunitToRead); SSCRUTE(dtunit2);
00210     ISCRUTE(nstepToRead); ISCRUTE(p2ncpst);
00211     return -1;
00212   }
00213 
00214   if (MEDparameterInfoByName(fid, p3name, &typeToRead, descriptionToRead,
00215                              dtunitToRead, &nstepToRead) < 0) {
00216     MESSAGE("ERROR : read information for parameter p3 ...");
00217     SSCRUTE(p3name);
00218     ISCRUTE(typeToRead);
00219     SSCRUTE(descriptionToRead);
00220     SSCRUTE(dtunitToRead);
00221     ISCRUTE(nstepToRead);
00222     return -1;
00223   }
00224   if ((typeToRead != p3type) || strcmp(descriptionToRead, p3description) ||
00225       strcmp(dtunitToRead,dtunit3) || (nstepToRead != p3ncpst)) {
00226     MESSAGE("ERROR : read information for parameter p2 : attributes ...");
00227     SSCRUTE(p3name);
00228     ISCRUTE(typeToRead);
00229     SSCRUTE(descriptionToRead);
00230     SSCRUTE(dtunitToRead);
00231     ISCRUTE(nstepToRead);
00232     return -1;
00233   }
00234   
00235   /* how many parameter(s) in the file ? */
00236   if ((np = MEDnParameter(fid)) < 0) {
00237     MESSAGE("ERROR : read number of paremeter ...");
00238     ISCRUTE(np);
00239     return -1;
00240   }
00241   if (np != 3) {
00242     MESSAGE("The number of parameter is 3 !");
00243     ISCRUTE(np);
00244     return -1;
00245   }
00246   /* TODO : A SUPPRIMER */
00247   np=0;
00248 
00249   /* read informations for each parameter */ 
00250   for (i=0;i<np;i++) {
00251 
00252     if (MEDparameterInfo(fid, i+1, nameToRead, &typeToRead, descriptionToRead,
00253                          dtunitToRead, &nstepToRead) < 0) {
00254       MESSAGE("ERROR : read information for parameter p3 ...");
00255       SSCRUTE(nameToRead);
00256       ISCRUTE(typeToRead);
00257       SSCRUTE(descriptionToRead);
00258       SSCRUTE(dtunitToRead);
00259       ISCRUTE(nstepToRead);
00260       return -1;
00261     }
00262 
00263     if (i == 0)
00264       if (strcmp(nameToRead, p1name) || (typeToRead != p1type) || 
00265           strcmp(descriptionToRead, p1description) || strcmp(dtunitToRead,dtunit1) 
00266           || (nstepToRead != p1ncpst)) {
00267         MESSAGE("ERROR : read information for parameter p1 : attributes ...");
00268         SSCRUTE(nameToRead);
00269         ISCRUTE(typeToRead);
00270         SSCRUTE(descriptionToRead);
00271         SSCRUTE(dtunitToRead);
00272         ISCRUTE(nstepToRead);
00273         return -1;
00274       }
00275 
00276     if (i == 1)
00277       if ( strcmp(nameToRead, p2name) || (typeToRead != p2type) || 
00278            strcmp(descriptionToRead, p2description) || strcmp(dtunitToRead,dtunit2) 
00279            || (nstepToRead != p2ncpst)) {
00280         MESSAGE("ERROR : read information for parameter p2 : attributes ...");
00281         SSCRUTE(nameToRead);
00282         ISCRUTE(typeToRead);
00283         SSCRUTE(descriptionToRead);
00284         SSCRUTE(dtunitToRead);
00285         ISCRUTE(nstepToRead);
00286         return -1;
00287       }
00288 
00289     if (i == 3)
00290       if (strcmp(nameToRead,p3name) || (typeToRead != p3type) || 
00291           strcmp(descriptionToRead, p3description) || strcmp(dtunitToRead,dtunit3) || 
00292           (nstepToRead != p3ncpst)) {
00293         MESSAGE("ERROR : read information for parameter p2 : attributes ...");
00294         SSCRUTE(nameToRead);
00295         ISCRUTE(typeToRead);
00296         SSCRUTE(descriptionToRead);
00297         SSCRUTE(dtunitToRead);
00298         ISCRUTE(nstepToRead);
00299         return -1;
00300       }
00301 
00302     /* read informations about each computation step
00303        and then read value for each parameter */
00304     for (j=0; j<nstepToRead; j++) {
00305 
00306       if (MEDparameterComputationStepInfo(fid, nameToRead, j+1, 
00307                                           &numdtToRead, &numitToRead, &dtToRead) < 0) {
00308         MESSAGE("ERROR : read information about computation step ...");
00309         SSCRUTE(nameToRead);
00310         ISCRUTE(j);
00311         ISCRUTE(numdtToRead);
00312         ISCRUTE(numitToRead);
00313         return -1;
00314       }
00315 
00316       if (typeToRead == MED_FLOAT64)
00317         if (MEDparameterValueRd(fid, nameToRead, numdtToRead, numitToRead,
00318                                 (unsigned char *) &fvalueToRead) < 0) {
00319           MESSAGE("ERROR : read parameter value ...");
00320           SSCRUTE(nameToRead);
00321           ISCRUTE(j);
00322           ISCRUTE(numdtToRead);
00323           ISCRUTE(numitToRead);
00324           return -1;
00325         }
00326 
00327       if (typeToRead != MED_FLOAT64)
00328         if (MEDparameterValueRd(fid, nameToRead, numdtToRead, numitToRead,
00329                                 (unsigned char *) &ivalueToRead) < 0) {
00330           MESSAGE("ERROR : read parameter value ...");
00331           SSCRUTE(nameToRead);
00332           ISCRUTE(j);
00333           ISCRUTE(numdtToRead);
00334           ISCRUTE(numitToRead);
00335           ISCRUTE(ivalueToRead);
00336           return -1;
00337         }
00338 
00339       /* data verifications */
00340       if ((i==0) && (j==0))
00341         if ((numdtToRead != p1numdt1) || (numitToRead != p1numit1) || (dtToRead != p1dt1) ||
00342             (fvalueToRead != p1v1)) {
00343           MESSAGE("ERROR : false informations for computation step : attributes ...");
00344           SSCRUTE(nameToRead);
00345           ISCRUTE(j);
00346           ISCRUTE(numdtToRead); ISCRUTE(p1numdt1);
00347           ISCRUTE(numitToRead); ISCRUTE(p1numit1);
00348           return -1;
00349         }
00350 
00351       if ((i==0) && (j==1))
00352         if ((numdtToRead != p1numdt3) || (numitToRead != p1numit3) || (dtToRead != p1dt3) ||
00353             (fvalueToRead != p1v3)) {
00354           MESSAGE("ERROR : false informations for computation step : attributes ...");
00355           SSCRUTE(nameToRead);
00356           ISCRUTE(j);
00357           ISCRUTE(numdtToRead); ISCRUTE(p1numdt3);
00358           ISCRUTE(numitToRead); ISCRUTE(p1numit3);
00359           return -1;
00360         }
00361 
00362       if ((i==0) && (j==2))
00363         if ((numdtToRead != p1numdt2) || (numitToRead != p1numit2) || (dtToRead != p1dt2) ||
00364             (fvalueToRead != p1v2)) {
00365           MESSAGE("ERROR : false informations for computation step : attributes ...");
00366           SSCRUTE(nameToRead);
00367           ISCRUTE(j);
00368           ISCRUTE(numdtToRead); ISCRUTE(p1numdt2);
00369           ISCRUTE(numitToRead); ISCRUTE(p1numit2);
00370           return -1;
00371         }
00372 
00373       if (i==1)
00374         if ((numdtToRead != p2numdt1) || (numitToRead != p2numit1) || (dtToRead != p2dt1) ||
00375             (ivalueToRead != p2v2)) {
00376           MESSAGE("ERROR : false informations for computation step : attributes ...");
00377           SSCRUTE(nameToRead);
00378           ISCRUTE(j);
00379           ISCRUTE(numdtToRead); ISCRUTE(p2numdt1);
00380           ISCRUTE(numitToRead); ISCRUTE(p2numit1);
00381           return -1;
00382         }
00383 
00384       if ((i == 2) && (j == 0))
00385         if ((numdtToRead != p3numdt2) || (numitToRead != p3numit2) || (dtToRead != p3dt2) ||
00386             (ivalueToRead != p3v2)) {
00387           MESSAGE("ERROR : false informations for computation step : attributes ...");
00388           SSCRUTE(nameToRead);
00389           ISCRUTE(j);
00390           ISCRUTE(numdtToRead); ISCRUTE(p3numdt2);
00391           ISCRUTE(numitToRead); ISCRUTE(p3numit2);
00392           return -1;
00393         }
00394 
00395       if ((i == 2) && (j == 1))
00396         if ((numdtToRead != p3numdt1) || (numitToRead != p3numit1) || (dtToRead != p3dt1) ||
00397             (ivalueToRead != p3v1)) {
00398           MESSAGE("ERROR : false informations for computation step : attributes ...");
00399           SSCRUTE(nameToRead);
00400           ISCRUTE(j);
00401           ISCRUTE(numdtToRead); ISCRUTE(p3numdt1);
00402           ISCRUTE(numitToRead); ISCRUTE(p3numit1);
00403           return -1;
00404         }
00405 
00406     }
00407 
00408   }
00409 
00410   /* close file */
00411   if (MEDfileClose(fid) < 0) {
00412     MESSAGE("ERROR : close file ...");
00413     return -1;
00414   }
00415   
00416   return 0;
00417 }

Généré le Thu Oct 8 14:26:17 2015 pour MED fichier par  doxygen 1.6.1