MED fichier: Fichier source de Unittest_MEDparameter_1.c

MED fichier

- Documentation MED
- Guides d'utilisation
- Guides de référence

Unittest_MEDparameter_1.c
Aller à la documentation de ce fichier.
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 parameters in 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 const char filename[] = "Unittest_MEDparameter_1.med";
32 const char comment[] = "Parameters unit tests";
33 const char p1name[] = "FLOAT_PARAMETER";
34 const char p2name[] = "INT_PARAMETER_1";
35 const char p3name[] = "INT_PARAMETER_2";
36 const med_parameter_type p1type = MED_FLOAT64;
37 const med_parameter_type p2type = MED_INT;
38 const med_parameter_type p3type = MED_INT;
39 const char p1description[] = "PARAMETER 1 DESCRIPTION";
40 const char p2description[] = "PARAMETER 2 DESCRIPTION";
41 const char p3description[] = "PARAMETER 3 DESCRIPTION";
42 const char dtunit1[] = "DT UNIT P1";
43 const char dtunit2[] = "DT UNIT P2";
44 const char dtunit3[] = "DT UNIT P3";
45 med_int np;
46 char nameToRead[MED_NAME_SIZE+1];
47 char descriptionToRead[MED_COMMENT_SIZE+1];
48 med_parameter_type typeToRead;
49 char dtunitToRead[MED_SNAME_SIZE+1];
50 med_int nstepToRead;
51 int i,j;
52 med_float p1v1 = 6.98;
53 med_float p1v2 = 19.07;
54 med_float p1v3 = 78.0;
55 med_int p1numdt1 = MED_NO_DT;
56 med_int p1numdt2 = 1;
57 med_int p1numdt3 = 1;
58 med_int p1numit1 = MED_NO_IT;
59 med_int p1numit2 = 3;
60 med_int p1numit3 = 2;
61 med_float p1dt1 = 0.0;
62 med_float p1dt2 = 0.1;
63 med_float p1dt3 = 5.5;
64 const med_int p1ncpst = 3;
65 med_int p2v1 = 5;
66 med_int p2v2 = 6;
67 med_int p2numit1 = MED_NO_IT;
68 med_int p2numdt1 = MED_NO_DT;
69 med_float p2dt1 = 0.0;
70 const med_int p2ncpst = 1;
71 med_int p3v1 = 77;
72 med_int p3v2 = 89;
73 med_int p3numdt1 = 1;
74 med_int p3numit1 = 1;
75 med_float p3dt1 = 18.9;
76 med_int p3numdt2 = MED_NO_DT;
77 med_int p3numit2 = MED_NO_IT;
78 med_float p3dt2 = 0.0;
79 const med_int p3ncpst = 2;
80 med_int numdtToRead;
81 med_int numitToRead;
82 med_float dtToRead;
83 med_float fvalueToRead;
84 med_int ivalueToRead;
85
86 /* file creation */
87 fid = MEDfileOpen(filename,MED_ACC_CREAT);
88 if (fid < 0) {
89 MESSAGE("ERROR : file creation ...");
90 SSCRUTE(filename);
91 return -1;
92 }
93
94 if (MEDfileCommentWr(fid,comment) < 0) {
95 MESSAGE("ERROR : write comment ...");
96 SSCRUTE(comment);
97 return -1;
98 }
99
100 /* create a MED_FLOAT64 parameter */
101 if (MEDparameterCr(fid, p1name, p1type, p1description, dtunit1) < 0) {
102 MESSAGE("ERROR : parameter p1 creation ...");
103 SSCRUTE(p1name);
104 return -1;
105 }
106
107 if (MEDparameterValueWr(fid, p1name, p1numdt1, p1numit1, p1dt1, (unsigned char*) &p1v1) < 0) {
108 MESSAGE("ERROR : write p1v1 ...");
109 SSCRUTE(p1name);
110 return -1;
111 }
112 if (MEDparameterValueWr(fid, p1name, p1numdt2, p1numit2, p1dt2, (unsigned char*) &p1v2) < 0) {
113 MESSAGE("ERROR : write p1v2 ...");
114 SSCRUTE(p1name);
115 return -1;
116 }
117 if (MEDparameterValueWr(fid, p1name, p1numdt3, p1numit3, p1dt3, (unsigned char*) &p1v3) < 0) {
118 MESSAGE("ERROR : write p1v3 ...");
119 SSCRUTE(p1name);
120 return -1;
121 }
122
123 /* create a MED_INT parameter with different values */
124 if (MEDparameterCr(fid, p2name, p2type, p2description, dtunit2) < 0) {
125 MESSAGE("ERROR : paramter p2 creation ...");
126 SSCRUTE(p2name);
127 return -1;
128 }
129 if (MEDparameterValueWr(fid, p2name, p2numdt1, p2numit1, p2dt1, (unsigned char*) &p2v1) < 0) {
130 MESSAGE("ERROR : write p2v1 ...");
131 SSCRUTE(p2name);
132 return -1;
133 }
134 if (MEDparameterValueWr(fid, p2name, p2numdt1, p2numit1, p2dt1, (unsigned char*) &p2v2) < 0) {
135 MESSAGE("ERROR : write p2v2 on same computation step ...");
136 SSCRUTE(p2name);
137 return -1;
138 }
139
140 /* create another MED_INT parameter */
141 if (MEDparameterCr(fid, p3name, p3type, p3description, dtunit3) < 0) {
142 MESSAGE("ERROR : paramter p3 creation ...");
143 SSCRUTE(p3name);
144 return -1;
145 }
146 if (MEDparameterValueWr(fid, p3name, p3numdt1, p3numit1, p3dt1, (unsigned char*) &p3v1) < 0) {
147 MESSAGE("ERROR : write p3v1 ...");
148 SSCRUTE(p3name);
149 return -1;
150 }
151 if (MEDparameterValueWr(fid, p3name, p3numdt2, p3numit2, p3dt2, (unsigned char*) &p3v2) < 0) {
152 MESSAGE("ERROR : write p3v2 ...");
153 SSCRUTE(p3name);
154 return -1;
155 }
156
157 /* close file */
158 if (MEDfileClose(fid) < 0) {
159 MESSAGE("ERROR : file closing ...");
160 return -1;
161 }
162
163 /* open file in READ ONLY access mode */
164 fid = MEDfileOpen(filename,MED_ACC_RDONLY);
165 if (fid < 0) {
166 MESSAGE("ERROR : open in READ ONLY ACCESS mode ...");
167 SSCRUTE(filename);
168 return -1;
169 }
170
171 /* direct access to parameters */
172 if (MEDparameterInfoByName(fid, p1name, &typeToRead, descriptionToRead,
173 dtunitToRead, &nstepToRead) < 0) {
174 MESSAGE("ERROR : read information for parameter p1 ...");
175 SSCRUTE(p1name);
176 ISCRUTE(typeToRead);
177 SSCRUTE(descriptionToRead);
178 SSCRUTE(dtunitToRead);
179 ISCRUTE(nstepToRead);
180 return -1;
181 }
182 if ((typeToRead != p1type) || strcmp(descriptionToRead, p1description) ||
183 strcmp(dtunitToRead,dtunit1) || (nstepToRead != p1ncpst)) {
184 MESSAGE("ERROR : read information for parameter p1 : attributes ...");
185 SSCRUTE(p1name);
186 ISCRUTE(typeToRead);
187 SSCRUTE(descriptionToRead);
188 SSCRUTE(dtunitToRead);
189 ISCRUTE(nstepToRead);
190 return -1;
191 }
192
193 if (MEDparameterInfoByName(fid, p2name, &typeToRead, descriptionToRead,
194 dtunitToRead, &nstepToRead) < 0) {
195 MESSAGE("ERROR : read information for parameter p2 ...");
196 SSCRUTE(p2name);
197 ISCRUTE(typeToRead);
198 SSCRUTE(descriptionToRead);
199 SSCRUTE(dtunitToRead);
200 ISCRUTE(nstepToRead);
201 return -1;
202 }
203 if ((typeToRead != p2type) || strcmp(descriptionToRead, p2description) ||
204 strcmp(dtunitToRead,dtunit2) || (nstepToRead != p2ncpst)) {
205 MESSAGE("ERROR : read information for parameter p2 : attributes ...");
206 SSCRUTE(p2name);
207 ISCRUTE(typeToRead); ISCRUTE(p2type);
208 SSCRUTE(descriptionToRead); SSCRUTE(p2description);
209 SSCRUTE(dtunitToRead); SSCRUTE(dtunit2);
210 ISCRUTE(nstepToRead); ISCRUTE(p2ncpst);
211 return -1;
212 }
213
214 if (MEDparameterInfoByName(fid, p3name, &typeToRead, descriptionToRead,
215 dtunitToRead, &nstepToRead) < 0) {
216 MESSAGE("ERROR : read information for parameter p3 ...");
217 SSCRUTE(p3name);
218 ISCRUTE(typeToRead);
219 SSCRUTE(descriptionToRead);
220 SSCRUTE(dtunitToRead);
221 ISCRUTE(nstepToRead);
222 return -1;
223 }
224 if ((typeToRead != p3type) || strcmp(descriptionToRead, p3description) ||
225 strcmp(dtunitToRead,dtunit3) || (nstepToRead != p3ncpst)) {
226 MESSAGE("ERROR : read information for parameter p2 : attributes ...");
227 SSCRUTE(p3name);
228 ISCRUTE(typeToRead);
229 SSCRUTE(descriptionToRead);
230 SSCRUTE(dtunitToRead);
231 ISCRUTE(nstepToRead);
232 return -1;
233 }
234
235 /* how many parameter(s) in the file ? */
236 if ((np = MEDnParameter(fid)) < 0) {
237 MESSAGE("ERROR : read number of paremeter ...");
238 ISCRUTE(np);
239 return -1;
240 }
241 if (np != 3) {
242 MESSAGE("The number of parameter is 3 !");
243 ISCRUTE(np);
244 return -1;
245 }
246 /* TODO : A SUPPRIMER */
247 np=0;
248
249 /* read informations for each parameter */
250 for (i=0;i<np;i++) {
251
252 if (MEDparameterInfo(fid, i+1, nameToRead, &typeToRead, descriptionToRead,
253 dtunitToRead, &nstepToRead) < 0) {
254 MESSAGE("ERROR : read information for parameter p3 ...");
255 SSCRUTE(nameToRead);
256 ISCRUTE(typeToRead);
257 SSCRUTE(descriptionToRead);
258 SSCRUTE(dtunitToRead);
259 ISCRUTE(nstepToRead);
260 return -1;
261 }
262
263 if (i == 0)
264 if (strcmp(nameToRead, p1name) || (typeToRead != p1type) ||
265 strcmp(descriptionToRead, p1description) || strcmp(dtunitToRead,dtunit1)
266 || (nstepToRead != p1ncpst)) {
267 MESSAGE("ERROR : read information for parameter p1 : attributes ...");
268 SSCRUTE(nameToRead);
269 ISCRUTE(typeToRead);
270 SSCRUTE(descriptionToRead);
271 SSCRUTE(dtunitToRead);
272 ISCRUTE(nstepToRead);
273 return -1;
274 }
275
276 if (i == 1)
277 if ( strcmp(nameToRead, p2name) || (typeToRead != p2type) ||
278 strcmp(descriptionToRead, p2description) || strcmp(dtunitToRead,dtunit2)
279 || (nstepToRead != p2ncpst)) {
280 MESSAGE("ERROR : read information for parameter p2 : attributes ...");
281 SSCRUTE(nameToRead);
282 ISCRUTE(typeToRead);
283 SSCRUTE(descriptionToRead);
284 SSCRUTE(dtunitToRead);
285 ISCRUTE(nstepToRead);
286 return -1;
287 }
288
289 if (i == 3)
290 if (strcmp(nameToRead,p3name) || (typeToRead != p3type) ||
291 strcmp(descriptionToRead, p3description) || strcmp(dtunitToRead,dtunit3) ||
292 (nstepToRead != p3ncpst)) {
293 MESSAGE("ERROR : read information for parameter p2 : attributes ...");
294 SSCRUTE(nameToRead);
295 ISCRUTE(typeToRead);
296 SSCRUTE(descriptionToRead);
297 SSCRUTE(dtunitToRead);
298 ISCRUTE(nstepToRead);
299 return -1;
300 }
301
302 /* read informations about each computation step
303 and then read value for each parameter */
304 for (j=0; j<nstepToRead; j++) {
305
306 if (MEDparameterComputationStepInfo(fid, nameToRead, j+1,
307 &numdtToRead, &numitToRead, &dtToRead) < 0) {
308 MESSAGE("ERROR : read information about computation step ...");
309 SSCRUTE(nameToRead);
310 ISCRUTE(j);
311 ISCRUTE(numdtToRead);
312 ISCRUTE(numitToRead);
313 return -1;
314 }
315
316 if (typeToRead == MED_FLOAT64)
317 if (MEDparameterValueRd(fid, nameToRead, numdtToRead, numitToRead,
318 (unsigned char *) &fvalueToRead) < 0) {
319 MESSAGE("ERROR : read parameter value ...");
320 SSCRUTE(nameToRead);
321 ISCRUTE(j);
322 ISCRUTE(numdtToRead);
323 ISCRUTE(numitToRead);
324 return -1;
325 }
326
327 if (typeToRead != MED_FLOAT64)
328 if (MEDparameterValueRd(fid, nameToRead, numdtToRead, numitToRead,
329 (unsigned char *) &ivalueToRead) < 0) {
330 MESSAGE("ERROR : read parameter value ...");
331 SSCRUTE(nameToRead);
332 ISCRUTE(j);
333 ISCRUTE(numdtToRead);
334 ISCRUTE(numitToRead);
335 ISCRUTE(ivalueToRead);
336 return -1;
337 }
338
339 /* data verifications */
340 if ((i==0) && (j==0))
341 if ((numdtToRead != p1numdt1) || (numitToRead != p1numit1) || (dtToRead != p1dt1) ||
342 (fvalueToRead != p1v1)) {
343 MESSAGE("ERROR : false informations for computation step : attributes ...");
344 SSCRUTE(nameToRead);
345 ISCRUTE(j);
346 ISCRUTE(numdtToRead); ISCRUTE(p1numdt1);
347 ISCRUTE(numitToRead); ISCRUTE(p1numit1);
348 return -1;
349 }
350
351 if ((i==0) && (j==1))
352 if ((numdtToRead != p1numdt3) || (numitToRead != p1numit3) || (dtToRead != p1dt3) ||
353 (fvalueToRead != p1v3)) {
354 MESSAGE("ERROR : false informations for computation step : attributes ...");
355 SSCRUTE(nameToRead);
356 ISCRUTE(j);
357 ISCRUTE(numdtToRead); ISCRUTE(p1numdt3);
358 ISCRUTE(numitToRead); ISCRUTE(p1numit3);
359 return -1;
360 }
361
362 if ((i==0) && (j==2))
363 if ((numdtToRead != p1numdt2) || (numitToRead != p1numit2) || (dtToRead != p1dt2) ||
364 (fvalueToRead != p1v2)) {
365 MESSAGE("ERROR : false informations for computation step : attributes ...");
366 SSCRUTE(nameToRead);
367 ISCRUTE(j);
368 ISCRUTE(numdtToRead); ISCRUTE(p1numdt2);
369 ISCRUTE(numitToRead); ISCRUTE(p1numit2);
370 return -1;
371 }
372
373 if (i==1)
374 if ((numdtToRead != p2numdt1) || (numitToRead != p2numit1) || (dtToRead != p2dt1) ||
375 (ivalueToRead != p2v2)) {
376 MESSAGE("ERROR : false informations for computation step : attributes ...");
377 SSCRUTE(nameToRead);
378 ISCRUTE(j);
379 ISCRUTE(numdtToRead); ISCRUTE(p2numdt1);
380 ISCRUTE(numitToRead); ISCRUTE(p2numit1);
381 return -1;
382 }
383
384 if ((i == 2) && (j == 0))
385 if ((numdtToRead != p3numdt2) || (numitToRead != p3numit2) || (dtToRead != p3dt2) ||
386 (ivalueToRead != p3v2)) {
387 MESSAGE("ERROR : false informations for computation step : attributes ...");
388 SSCRUTE(nameToRead);
389 ISCRUTE(j);
390 ISCRUTE(numdtToRead); ISCRUTE(p3numdt2);
391 ISCRUTE(numitToRead); ISCRUTE(p3numit2);
392 return -1;
393 }
394
395 if ((i == 2) && (j == 1))
396 if ((numdtToRead != p3numdt1) || (numitToRead != p3numit1) || (dtToRead != p3dt1) ||
397 (ivalueToRead != p3v1)) {
398 MESSAGE("ERROR : false informations for computation step : attributes ...");
399 SSCRUTE(nameToRead);
400 ISCRUTE(j);
401 ISCRUTE(numdtToRead); ISCRUTE(p3numdt1);
402 ISCRUTE(numitToRead); ISCRUTE(p3numit1);
403 return -1;
404 }
405
406 }
407
408 }
409
410 /* close file */
411 if (MEDfileClose(fid) < 0) {
412 MESSAGE("ERROR : close file ...");
413 return -1;
414 }
415
416 return 0;
417 }
main
int main(int argc, char **argv)
Definition: Unittest_MEDparameter_1.c:28
MEDparameterComputationStepInfo
MEDC_EXPORT med_err MEDparameterComputationStepInfo(const med_idt fid, const char *const paramname, const int csit, med_int *const numdt, med_int *const numit, med_float *const dt)
Cette routine permet la lecture des informations relatives à une séquence de calcul du paramètre numé...
Definition: MEDparameterComputationStepInfo.c:38
MED_NO_IT
#define MED_NO_IT
Definition: med.h:316
med_utils.h
MEDparameterValueWr
MEDC_EXPORT med_err MEDparameterValueWr(const med_idt fid, const char *const paramname, const med_int numdt, const med_int numit, const med_float dt, const unsigned char *const value)
Cette routine permet l'écriture de la valeur d'un paramètre numérique scalaire.
Definition: MEDparameterValueWr.c:39
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:41
MED_ACC_CREAT
Definition: med.h:121
med_float
double med_float
Definition: med.h:331
MEDparameterValueRd
MEDC_EXPORT med_err MEDparameterValueRd(const med_idt fid, const char *const paramname, const med_int numdt, const med_int numit, unsigned char *const value)
Cette routine permet la lecture de la valeur d'un paramètre numérique scalaire.
Definition: MEDparameterValueRd.c:37
MEDparameterInfoByName
MEDC_EXPORT med_err MEDparameterInfoByName(const med_idt fid, const char *const paramname, med_parameter_type *const paramtype, char *const description, char *const dtunit, med_int *const nstep)
Cette routine permet la lecture des informations relatives à un paramètre scalaire.
Definition: MEDparameterInfoByName.c:37
MEDnParameter
MEDC_EXPORT med_int MEDnParameter(const med_idt fid)
Cette routine permet la lecture du nombre de paramètre numérique scalaire dans un fichier...
Definition: MEDnParameter.c:34
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:316
MED_FLOAT64
Definition: med.h:163
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
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
MED_INT
Definition: med.h:167
MEDparameterCr
MEDC_EXPORT med_err MEDparameterCr(const med_idt fid, const char *const paramname, const med_parameter_type paramtype, const char *const description, const char *const dtunit)
Cette routine permet la création d'un paramètre numérique scalaire.
Definition: MEDparameterCr.c:38
med_int
int med_int
Definition: med.h:337
ISCRUTE
#define ISCRUTE(entier)
Definition: med_utils.h:306
med_field_type
med_field_type
Definition: med.h:162
MEDparameterInfo
MEDC_EXPORT med_err MEDparameterInfo(const med_idt fid, const int paramit, char *const paramname, med_parameter_type *const paramtype, char *const description, char *const dtunit, med_int *const nstep)
Cette routine permet la lecture des informations relatives à un paramètre scalaire via un itérateur...
Definition: MEDparameterInfo.c:37
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:77
med_idt
hid_t med_idt
Definition: med.h:326
MED_NO_DT
#define MED_NO_DT
Definition: med.h:315
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:79
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:80
med.h
filename
#define filename
Definition: test10.c:73
SSCRUTE
#define SSCRUTE(chaine)
Definition: med_utils.h:315
MED_ACC_RDONLY
Definition: med.h:118