Unittest_MEDfile_1.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2014  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 
00040   /* file creation */
00041   fid = MEDfileOpen(filename,MED_ACC_CREAT);
00042   if (fid < 0) {
00043     MESSAGE("ERROR : file creation");
00044     return -1;
00045   }
00046 
00047   /* write a comment */
00048   if (MEDfileCommentWr(fid,comment) < 0) {
00049     MESSAGE("ERROR : file comment writing");
00050     return -1;
00051   }
00052 
00053   /* file closing */
00054   if (MEDfileClose(fid) < 0) {
00055     MESSAGE("ERROR : file closing");
00056     return -1;
00057   }
00058 
00059   /* file opening in READ ONLY access mode */
00060   fid = MEDfileOpen(filename,MED_ACC_RDONLY);
00061   if (fid < 0) {
00062     MESSAGE("ERROR : file opening in READ ONLY ACCESS mode");
00063     return -1;
00064   }
00065 
00066   /* med library version is read in the file */
00067   if (MEDfileNumVersionRd(fid,&major,&minor,&release) < 0) {
00068     MESSAGE("ERROR : MED version reading");
00069     ISCRUTE(major);
00070     ISCRUTE(minor);
00071     ISCRUTE(release);
00072     return -1;
00073   }
00074   if ((major != MED_MAJOR_NUM) ||
00075       (minor != MED_MINOR_NUM) ||
00076       (release != MED_RELEASE_NUM)) {
00077     MESSAGE("ERROR : The MED num version is not the good one");
00078     ISCRUTE(major);
00079     ISCRUTE(minor);
00080     ISCRUTE(release);
00081     return -1;
00082   }
00083 
00084   if (MEDfileStrVersionRd(fid,medversion) < 0) {
00085     MESSAGE("ERROR : MED str version reading");
00086     SSCRUTE(medversion);
00087     return -1;
00088   }
00089   sscanf(medversion,"MED-"IFORMAT"."IFORMAT"."IFORMAT,
00090          &majorFromStr,&minorFromStr,&releaseFromStr);
00091   if ((major != majorFromStr) ||
00092       (minor != minorFromStr) ||
00093       (release != releaseFromStr)) {
00094     ISCRUTE(majorFromStr);
00095     ISCRUTE(minorFromStr);
00096     ISCRUTE(releaseFromStr);
00097     MESSAGE("ERROR : The MED num version is not the good one");
00098     SSCRUTE(medversion);
00099     return -1;
00100   }
00101 
00102   /* file comment reading */
00103   if (MEDfileCommentRd(fid,commentToRead) < 0) {
00104     MESSAGE("ERROR : file comment reading");
00105     return -1;
00106   }
00107   if (strcmp(comment,commentToRead)) {
00108     MESSAGE("ERROR : file comment is not the good one");
00109     SSCRUTE(comment);
00110     SSCRUTE(commentToRead);
00111     return -1;
00112   }
00113 
00114   /* file closing */
00115   if (MEDfileClose(fid) < 0) {
00116     MESSAGE("ERROR : file closing");
00117     return -1;
00118   }
00119 
00120   /* file opening in READ and WRITE access mode */
00121   fid = MEDfileOpen(filename,MED_ACC_RDWR);
00122   if (fid < 0) {
00123     MESSAGE("ERROR : file opening in read and write access mode");
00124     return -1;
00125   }
00126 
00127   /* comment writing */
00128   if (MEDfileCommentWr(fid,comment2) < 0) {
00129     MESSAGE("ERROR : file comment writing");
00130     return -1;
00131   }
00132 
00133   /* file closing */
00134   if (MEDfileClose(fid) < 0) {
00135     MESSAGE("ERROR : file closing");
00136     return -1;
00137   }
00138 
00139   /* file opening in READ and EXTENSION access mode */
00140   fid = MEDfileOpen(filename,MED_ACC_RDEXT);
00141   if (fid < 0) {
00142     MESSAGE("ERROR : file opening in READ and EXTENSION access mode");
00143     return -1;
00144   }
00145 
00146   /* write a comment has to be impossible */
00147   printf("Un message d'erreur est attendu :\n");
00148   if (MEDfileCommentWr(fid,comment) == 0) {
00149     MESSAGE("ERROR : write comment has to be impossible");
00150     return -1;
00151   }
00152   printf("Fin du message d'erreur attendu.\n");
00153 
00154   /* file closing */
00155   if (MEDfileClose(fid) < 0) {
00156     MESSAGE("ERROR : file closing");
00157     return -1;
00158   }
00159 
00160   /* file compatibility test with hdf5
00161      and med library version */
00162   if (MEDfileCompatibility(filename,&hdfok,&medok) < 0) {
00163     MESSAGE("ERROR : file compatibility test");
00164     return -1;
00165   }
00166 
00167   if (! hdfok) {
00168     MESSAGE("ERROR : the file must be in hdf5 format");
00169     ISCRUTE(hdfok);
00170     return -1;
00171   }
00172 
00173   if (! medok) {
00174     MESSAGE("ERROR : the file must be compatible");
00175     ISCRUTE(medok);
00176     return -1;
00177   }
00178 
00179   return 0;
00180 
00181 }

Généré le Mon Nov 3 10:50:54 2014 pour MED fichier par  doxygen 1.6.1