Unittest_MEDparameter_1.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2011  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 
00247   /* read informations for each parameter */ 
00248   for (i=0;i<np;i++) {
00249 
00250     if (MEDparameterInfo(fid, i+1, nameToRead, &typeToRead, descriptionToRead,
00251                          dtunitToRead, &nstepToRead) < 0) {
00252       MESSAGE("ERROR : read information for parameter p3 ...");
00253       SSCRUTE(nameToRead);
00254       ISCRUTE(typeToRead);
00255       SSCRUTE(descriptionToRead);
00256       SSCRUTE(dtunitToRead);
00257       ISCRUTE(nstepToRead);
00258       return -1;
00259     }
00260 
00261     if (i == 0)
00262       if (strcmp(nameToRead, p1name) || (typeToRead != p1type) || 
00263           strcmp(descriptionToRead, p1description) || strcmp(dtunitToRead,dtunit1) 
00264           || (nstepToRead != p1ncpst)) {
00265         MESSAGE("ERROR : read information for parameter p1 : attributes ...");
00266         SSCRUTE(nameToRead);
00267         ISCRUTE(typeToRead);
00268         SSCRUTE(descriptionToRead);
00269         SSCRUTE(dtunitToRead);
00270         ISCRUTE(nstepToRead);
00271         return -1;
00272       }
00273 
00274     if (i == 1)
00275       if ( strcmp(nameToRead, p2name) || (typeToRead != p2type) || 
00276            strcmp(descriptionToRead, p2description) || strcmp(dtunitToRead,dtunit2) 
00277            || (nstepToRead != p2ncpst)) {
00278         MESSAGE("ERROR : read information for parameter p2 : attributes ...");
00279         SSCRUTE(nameToRead);
00280         ISCRUTE(typeToRead);
00281         SSCRUTE(descriptionToRead);
00282         SSCRUTE(dtunitToRead);
00283         ISCRUTE(nstepToRead);
00284         return -1;
00285       }
00286 
00287     if (i == 3)
00288       if (strcmp(nameToRead,p3name) || (typeToRead != p3type) || 
00289           strcmp(descriptionToRead, p3description) || strcmp(dtunitToRead,dtunit3) || 
00290           (nstepToRead != p3ncpst)) {
00291         MESSAGE("ERROR : read information for parameter p2 : attributes ...");
00292         SSCRUTE(nameToRead);
00293         ISCRUTE(typeToRead);
00294         SSCRUTE(descriptionToRead);
00295         SSCRUTE(dtunitToRead);
00296         ISCRUTE(nstepToRead);
00297         return -1;
00298       }
00299 
00300     /* read informations about each computation step
00301        and then read value for each parameter */
00302     for (j=0; j<nstepToRead; j++) {
00303 
00304       if (MEDparameterComputationStepInfo(fid, nameToRead, j+1, 
00305                                           &numdtToRead, &numitToRead, &dtToRead) < 0) {
00306         MESSAGE("ERROR : read information about computation step ...");
00307         SSCRUTE(nameToRead);
00308         ISCRUTE(j);
00309         ISCRUTE(numdtToRead);
00310         ISCRUTE(numitToRead);
00311         return -1;
00312       }
00313 
00314       if (typeToRead == MED_FLOAT64)
00315         if (MEDparameterValueRd(fid, nameToRead, numdtToRead, numitToRead,
00316                                 (unsigned char *) &fvalueToRead) < 0) {
00317           MESSAGE("ERROR : read parameter value ...");
00318           SSCRUTE(nameToRead);
00319           ISCRUTE(j);
00320           ISCRUTE(numdtToRead);
00321           ISCRUTE(numitToRead);
00322           return -1;
00323         }
00324 
00325       if (typeToRead != MED_FLOAT64)
00326         if (MEDparameterValueRd(fid, nameToRead, numdtToRead, numitToRead,
00327                                 (unsigned char *) &ivalueToRead) < 0) {
00328           MESSAGE("ERROR : read parameter value ...");
00329           SSCRUTE(nameToRead);
00330           ISCRUTE(j);
00331           ISCRUTE(numdtToRead);
00332           ISCRUTE(numitToRead);
00333           ISCRUTE(ivalueToRead);
00334           return -1;
00335         }
00336 
00337       /* data verifications */
00338       if ((i==0) && (j==0))
00339         if ((numdtToRead != p1numdt1) || (numitToRead != p1numit1) || (dtToRead != p1dt1) ||
00340             (fvalueToRead != p1v1)) {
00341           MESSAGE("ERROR : false informations for computation step : attributes ...");
00342           SSCRUTE(nameToRead);
00343           ISCRUTE(j);
00344           ISCRUTE(numdtToRead); ISCRUTE(p1numdt1);
00345           ISCRUTE(numitToRead); ISCRUTE(p1numit1);
00346           return -1;
00347         }
00348 
00349       if ((i==0) && (j==1))
00350         if ((numdtToRead != p1numdt3) || (numitToRead != p1numit3) || (dtToRead != p1dt3) ||
00351             (fvalueToRead != p1v3)) {
00352           MESSAGE("ERROR : false informations for computation step : attributes ...");
00353           SSCRUTE(nameToRead);
00354           ISCRUTE(j);
00355           ISCRUTE(numdtToRead); ISCRUTE(p1numdt3);
00356           ISCRUTE(numitToRead); ISCRUTE(p1numit3);
00357           return -1;
00358         }
00359 
00360       if ((i==0) && (j==2))
00361         if ((numdtToRead != p1numdt2) || (numitToRead != p1numit2) || (dtToRead != p1dt2) ||
00362             (fvalueToRead != p1v2)) {
00363           MESSAGE("ERROR : false informations for computation step : attributes ...");
00364           SSCRUTE(nameToRead);
00365           ISCRUTE(j);
00366           ISCRUTE(numdtToRead); ISCRUTE(p1numdt2);
00367           ISCRUTE(numitToRead); ISCRUTE(p1numit2);
00368           return -1;
00369         }
00370 
00371       if (i==1)
00372         if ((numdtToRead != p2numdt1) || (numitToRead != p2numit1) || (dtToRead != p2dt1) ||
00373             (ivalueToRead != p2v2)) {
00374           MESSAGE("ERROR : false informations for computation step : attributes ...");
00375           SSCRUTE(nameToRead);
00376           ISCRUTE(j);
00377           ISCRUTE(numdtToRead); ISCRUTE(p2numdt1);
00378           ISCRUTE(numitToRead); ISCRUTE(p2numit1);
00379           return -1;
00380         }
00381 
00382       if ((i == 2) && (j == 0))
00383         if ((numdtToRead != p3numdt2) || (numitToRead != p3numit2) || (dtToRead != p3dt2) ||
00384             (ivalueToRead != p3v2)) {
00385           MESSAGE("ERROR : false informations for computation step : attributes ...");
00386           SSCRUTE(nameToRead);
00387           ISCRUTE(j);
00388           ISCRUTE(numdtToRead); ISCRUTE(p3numdt2);
00389           ISCRUTE(numitToRead); ISCRUTE(p3numit2);
00390           return -1;
00391         }
00392 
00393       if ((i == 2) && (j == 1))
00394         if ((numdtToRead != p3numdt1) || (numitToRead != p3numit1) || (dtToRead != p3dt1) ||
00395             (ivalueToRead != p3v1)) {
00396           MESSAGE("ERROR : false informations for computation step : attributes ...");
00397           SSCRUTE(nameToRead);
00398           ISCRUTE(j);
00399           ISCRUTE(numdtToRead); ISCRUTE(p3numdt1);
00400           ISCRUTE(numitToRead); ISCRUTE(p3numit1);
00401           return -1;
00402         }
00403 
00404     }
00405 
00406   }
00407 
00408   /* close file */
00409   if (MEDfileClose(fid) < 0) {
00410     MESSAGE("ERROR : close file ...");
00411     return -1;
00412   }
00413 
00414 }

Généré le Mon May 16 17:10:24 2011 pour MED fichier par  doxygen 1.6.1