Unittest_MEDfile_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, open, close 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   char filename[] = "Unittest_MEDfile_1.med";
00032   char comment[] = "My first comment";
00033   char comment2[] = "My second comment";
00034   char commentToRead[MED_COMMENT_SIZE+1];
00035   med_bool hdfok, medok;
00036   med_int major,minor,release;
00037   char medversion[10];
00038   med_int majorFromStr, minorFromStr, releaseFromStr;
00039   char filenameFromId[MED_PATHNAME_SIZE+1] = "";
00040   char* filenameFromIdPtr = NULL;
00041   med_int filenamesize = 0;
00042 
00043   /* file creation */
00044   fid = MEDfileOpen(filename,MED_ACC_CREAT);
00045   if (fid < 0) {
00046     MESSAGE("ERROR : file creation");
00047     return -1;
00048   }
00049 
00050   /* write a comment */
00051   if (MEDfileCommentWr(fid,comment) < 0) {
00052     MESSAGE("ERROR : file comment writing");
00053     return -1;
00054   }
00055 
00056   /* Get filename */
00057   if ( (MEDfileName(fid,filenameFromId,MED_PATHNAME_SIZE+1) < 0 ) || 
00058        ( strncmp(filenameFromId,filename,MED_PATHNAME_SIZE) )) {
00059     MESSAGE("ERROR : file getting filename");
00060     return -1;
00061   }
00062 
00063   /* file closing */
00064   if (MEDfileClose(fid) < 0) {
00065     MESSAGE("ERROR : file closing");
00066     return -1;
00067   }
00068 
00069   /* file opening in READ ONLY access mode */
00070   fid = MEDfileOpen(filename,MED_ACC_RDONLY);
00071   if (fid < 0) {
00072     MESSAGE("ERROR : file opening in READ ONLY ACCESS mode");
00073     return -1;
00074   }
00075 
00076   /* med library version is read in the file */
00077   if (MEDfileNumVersionRd(fid,&major,&minor,&release) < 0) {
00078     MESSAGE("ERROR : MED version reading");
00079     ISCRUTE(major);
00080     ISCRUTE(minor);
00081     ISCRUTE(release);
00082     return -1;
00083   }
00084   if ((major != MED_MAJOR_NUM) ||
00085       (minor != MED_MINOR_NUM) ||
00086       (release != MED_RELEASE_NUM)) {
00087     MESSAGE("ERROR : The MED num version is not the good one");
00088     ISCRUTE(major);
00089     ISCRUTE(minor);
00090     ISCRUTE(release);
00091     return -1;
00092   }
00093 
00094   if (MEDfileStrVersionRd(fid,medversion) < 0) {
00095     MESSAGE("ERROR : MED str version reading");
00096     SSCRUTE(medversion);
00097     return -1;
00098   }
00099   sscanf(medversion,"MED-"IFORMAT"."IFORMAT"."IFORMAT,
00100          &majorFromStr,&minorFromStr,&releaseFromStr);
00101   if ((major != majorFromStr) ||
00102       (minor != minorFromStr) ||
00103       (release != releaseFromStr)) {
00104     ISCRUTE(majorFromStr);
00105     ISCRUTE(minorFromStr);
00106     ISCRUTE(releaseFromStr);
00107     MESSAGE("ERROR : The MED num version is not the good one");
00108     SSCRUTE(medversion);
00109     return -1;
00110   }
00111 
00112   /* file comment reading */
00113   if (MEDfileCommentRd(fid,commentToRead) < 0) {
00114     MESSAGE("ERROR : file comment reading");
00115     return -1;
00116   }
00117   if (strcmp(comment,commentToRead)) {
00118     MESSAGE("ERROR : file comment is not the good one");
00119     SSCRUTE(comment);
00120     SSCRUTE(commentToRead);
00121     return -1;
00122   }
00123 
00124   /* Get filename */
00125   if ( (filenamesize=MEDfileName(fid,NULL,0)) < 0 ) { 
00126     MESSAGE("ERROR : file getting filename");
00127     return -1;
00128   } else 
00129     filenameFromIdPtr = (char * ) malloc((filenamesize+1)*sizeof(char));
00130 
00131   if ( (MEDfileName(fid,filenameFromIdPtr,filenamesize) < 0) || 
00132        ( strncmp(filenameFromIdPtr,filename,filenamesize) )) {
00133     MESSAGE("ERROR : file getting filename");
00134     free(filenameFromIdPtr);
00135     return -1;
00136   }
00137   free(filenameFromIdPtr);
00138   
00139   /* file closing */
00140   if (MEDfileClose(fid) < 0) {
00141     MESSAGE("ERROR : file closing");
00142     return -1;
00143   }
00144 
00145   /* file opening in READ and WRITE access mode */
00146   fid = MEDfileOpen(filename,MED_ACC_RDWR);
00147   if (fid < 0) {
00148     MESSAGE("ERROR : file opening in read and write access mode");
00149     return -1;
00150   }
00151 
00152   /* comment writing */
00153   if (MEDfileCommentWr(fid,comment2) < 0) {
00154     MESSAGE("ERROR : file comment writing");
00155     return -1;
00156   }
00157 
00158   /* file closing */
00159   if (MEDfileClose(fid) < 0) {
00160     MESSAGE("ERROR : file closing");
00161     return -1;
00162   }
00163 
00164   /* file opening in READ and EXTENSION access mode */
00165   fid = MEDfileOpen(filename,MED_ACC_RDEXT);
00166   if (fid < 0) {
00167     MESSAGE("ERROR : file opening in READ and EXTENSION access mode");
00168     return -1;
00169   }
00170 
00171   /* write a comment has to be impossible */
00172   printf("Un message d'erreur est attendu :\n");
00173   if (MEDfileCommentWr(fid,comment) == 0) {
00174     MESSAGE("ERROR : write comment has to be impossible");
00175     return -1;
00176   }
00177   printf("Fin du message d'erreur attendu.\n");
00178 
00179   /* file closing */
00180   if (MEDfileClose(fid) < 0) {
00181     MESSAGE("ERROR : file closing");
00182     return -1;
00183   }
00184 
00185   /* file compatibility test with hdf5
00186      and med library version */
00187   if (MEDfileCompatibility(filename,&hdfok,&medok) < 0) {
00188     MESSAGE("ERROR : file compatibility test");
00189     return -1;
00190   }
00191 
00192   if (! hdfok) {
00193     MESSAGE("ERROR : the file must be in hdf5 format");
00194     ISCRUTE(hdfok);
00195     return -1;
00196   }
00197 
00198   if (! medok) {
00199     MESSAGE("ERROR : the file must be compatible");
00200     ISCRUTE(medok);
00201     return -1;
00202   }
00203 
00204   return 0;
00205 
00206 }

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