MED fichier
Unittest_MEDfile_1.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2  *
3  * COPYRIGHT (C) 1999 - 2023 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 
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 */
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 */
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 */
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 */
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 }
MED_ACC_RDONLY
Definition: med.h:122
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:81
MEDfileCommentRd
MEDC_EXPORT med_err MEDfileCommentRd(const med_idt fid, char *const comment)
Lecture d'un descripteur dans un fichier MED.
Definition: MEDfileCommentRd.c:33
filename
#define filename
Definition: 4.0.1/test10.c:75
med_idt
hid_t med_idt
Definition: med.h:333
MED_MINOR_NUM
#define MED_MINOR_NUM
Definition: med.h:58
MED_ACC_RDWR
Definition: med.h:123
MEDfileNumVersionRd
MEDC_EXPORT med_err MEDfileNumVersionRd(const med_idt fid, med_int *const major, med_int *const minor, med_int *const release)
Lecture du numéro de version de la bibliothèque MED utilisée pour créer le fichier.
Definition: MEDfileNumVersionRd.c:34
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
med_int
int med_int
Definition: med.h:344
MED_MAJOR_NUM
#define MED_MAJOR_NUM
Definition: med.h:57
ISCRUTE
#define ISCRUTE(entier)
Definition: med_utils.h:313
med_bool
med_bool
Definition: med.h:262
main
int main(int argc, char **argv)
Definition: Unittest_MEDfile_1.c:28
IFORMAT
#define IFORMAT
Definition: med_utils.h:145
MED_ACC_RDEXT
Definition: med.h:124
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
MED_PATHNAME_SIZE
#define MED_PATHNAME_SIZE
Definition: med.h:91
SSCRUTE
#define SSCRUTE(chaine)
Definition: med_utils.h:323
med_utils.h
MEDfileStrVersionRd
MEDC_EXPORT med_err MEDfileStrVersionRd(const med_idt fid, char *const version)
Lecture du numéro de version de la bibliothèque MED utilisée pour créer le fichier (renvoyé sous la f...
Definition: MEDfileStrVersionRd.c:35
med.h
MED_RELEASE_NUM
#define MED_RELEASE_NUM
Definition: med.h:59
MEDfileCompatibility
MEDC_EXPORT med_err MEDfileCompatibility(const char *const filename, med_bool *const hdfok, med_bool *const medok)
Vérification de la compatibilité d'un fichier avec HDF et MED.
Definition: MEDfileCompatibility.c:34
MEDfileCommentWr
MEDC_EXPORT med_err MEDfileCommentWr(const med_idt fid, const char *const comment)
Ecriture d'un descripteur dans un fichier MED.
Definition: MEDfileCommentWr.c:34
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:42
MEDfileName
MEDC_EXPORT med_int MEDfileName(med_idt fid, char *const filename, const med_int filenamesize)
Renvoi le nom de fichier MED à partir d'un med_idt.
Definition: MEDfileName.c:35
MED_ACC_CREAT
Definition: med.h:125