1 /*  This file is part of MED.
2  *
3  *  COPYRIGHT (C) 1999 - 2019  EDF R&D, CEA/DEN
4  *  MED is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  MED is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Unitary tests to create, open, close MED files
20  */
21 
22 #include <med.h>
23 #define MESGERR 1
24 #include <med_utils.h>
25 
26 #include <string.h>
27 
main(int argc,char ** argv)28 int main (int argc, char **argv)
29 {
30   med_idt fid;
31   char filename[] = "Unittest_MEDfile_1.med";
32   char comment[] = "My first comment";
33   char comment2[] = "My second comment";
34   char commentToRead[MED_COMMENT_SIZE+1];
35   med_bool hdfok, medok;
36   med_int major,minor,release;
37   char medversion[10];
38   med_int majorFromStr, minorFromStr, releaseFromStr;
39   char filenameFromId[MED_PATHNAME_SIZE+1] = "";
40   char* filenameFromIdPtr = NULL;
41   med_int filenamesize = 0;
42 
43   /* file creation */
44   fid = MEDfileOpen(filename,MED_ACC_CREAT);
45   if (fid < 0) {
46     MESSAGE("ERROR : file creation");
47     return -1;
48   }
49 
50   /* write a comment */
51   if (MEDfileCommentWr(fid,comment) < 0) {
52     MESSAGE("ERROR : file comment writing");
53     return -1;
54   }
55 
56   /* Get filename */
57   if ( (MEDfileName(fid,filenameFromId,MED_PATHNAME_SIZE+1) < 0 ) ||
58        ( strncmp(filenameFromId,filename,MED_PATHNAME_SIZE) )) {
59     MESSAGE("ERROR : file getting filename");
60     return -1;
61   }
62 
63   /* file closing */
64   if (MEDfileClose(fid) < 0) {
65     MESSAGE("ERROR : file closing");
66     return -1;
67   }
68 
69   /* file opening in READ ONLY access mode */
70   fid = MEDfileOpen(filename,MED_ACC_RDONLY);
71   if (fid < 0) {
72     MESSAGE("ERROR : file opening in READ ONLY ACCESS mode");
73     return -1;
74   }
75 
76   /* med library version is read in the file */
77   if (MEDfileNumVersionRd(fid,&major,&minor,&release) < 0) {
78     MESSAGE("ERROR : MED version reading");
79     ISCRUTE(major);
80     ISCRUTE(minor);
81     ISCRUTE(release);
82     return -1;
83   }
84   if ((major != MED_MAJOR_NUM) ||
85       (minor != MED_MINOR_NUM) ||
86       (release != MED_RELEASE_NUM)) {
87     MESSAGE("ERROR : The MED num version is not the good one");
88     ISCRUTE(major);
89     ISCRUTE(minor);
90     ISCRUTE(release);
91     return -1;
92   }
93 
94   if (MEDfileStrVersionRd(fid,medversion) < 0) {
95     MESSAGE("ERROR : MED str version reading");
96     SSCRUTE(medversion);
97     return -1;
98   }
99   sscanf(medversion,"MED-"IFORMAT"."IFORMAT"."IFORMAT,
100 	 &majorFromStr,&minorFromStr,&releaseFromStr);
101   if ((major != majorFromStr) ||
102       (minor != minorFromStr) ||
103       (release != releaseFromStr)) {
104     ISCRUTE(majorFromStr);
105     ISCRUTE(minorFromStr);
106     ISCRUTE(releaseFromStr);
107     MESSAGE("ERROR : The MED num version is not the good one");
108     SSCRUTE(medversion);
109     return -1;
110   }
111 
112   /* file comment reading */
113   if (MEDfileCommentRd(fid,commentToRead) < 0) {
114     MESSAGE("ERROR : file comment reading");
115     return -1;
116   }
117   if (strcmp(comment,commentToRead)) {
118     MESSAGE("ERROR : file comment is not the good one");
119     SSCRUTE(comment);
120     SSCRUTE(commentToRead);
121     return -1;
122   }
123 
124   /* Get filename */
125   if ( (filenamesize=MEDfileName(fid,NULL,0)) < 0 ) {
126     MESSAGE("ERROR : file getting filename");
127     return -1;
128   } else
129     filenameFromIdPtr = (char * ) malloc((filenamesize+1)*sizeof(char));
130 
131   if ( (MEDfileName(fid,filenameFromIdPtr,filenamesize) < 0) ||
132        ( strncmp(filenameFromIdPtr,filename,filenamesize) )) {
133     MESSAGE("ERROR : file getting filename");
134     free(filenameFromIdPtr);
135     return -1;
136   }
137   free(filenameFromIdPtr);
138 
139   /* file closing */
140   if (MEDfileClose(fid) < 0) {
141     MESSAGE("ERROR : file closing");
142     return -1;
143   }
144 
145   /* file opening in READ and WRITE access mode */
146   fid = MEDfileOpen(filename,MED_ACC_RDWR);
147   if (fid < 0) {
148     MESSAGE("ERROR : file opening in read and write access mode");
149     return -1;
150   }
151 
152   /* comment writing */
153   if (MEDfileCommentWr(fid,comment2) < 0) {
154     MESSAGE("ERROR : file comment writing");
155     return -1;
156   }
157 
158   /* file closing */
159   if (MEDfileClose(fid) < 0) {
160     MESSAGE("ERROR : file closing");
161     return -1;
162   }
163 
164   /* file opening in READ and EXTENSION access mode */
165   fid = MEDfileOpen(filename,MED_ACC_RDEXT);
166   if (fid < 0) {
167     MESSAGE("ERROR : file opening in READ and EXTENSION access mode");
168     return -1;
169   }
170 
171   /* write a comment has to be impossible */
172   printf("Un message d'erreur est attendu :\n");
173   if (MEDfileCommentWr(fid,comment) == 0) {
174     MESSAGE("ERROR : write comment has to be impossible");
175     return -1;
176   }
177   printf("Fin du message d'erreur attendu.\n");
178 
179   /* file closing */
180   if (MEDfileClose(fid) < 0) {
181     MESSAGE("ERROR : file closing");
182     return -1;
183   }
184 
185   /* file compatibility test with hdf5
186      and med library version */
187   if (MEDfileCompatibility(filename,&hdfok,&medok) < 0) {
188     MESSAGE("ERROR : file compatibility test");
189     return -1;
190   }
191 
192   if (! hdfok) {
193     MESSAGE("ERROR : the file must be in hdf5 format");
194     ISCRUTE(hdfok);
195     return -1;
196   }
197 
198   if (! medok) {
199     MESSAGE("ERROR : the file must be compatible");
200     ISCRUTE(medok);
201     return -1;
202   }
203 
204   return 0;
205 
206 }
207