source: trunk/MagicSoft/Simulation/Corsika/Mmcs614/testio.c

Last change on this file was 1444, checked in by blanch, 22 years ago
*** empty log message ***
File size: 18.8 KB
Line 
1/** @file testio.c
2 @short Test program for eventio data format.
3
4 @author Konrad Bernloehr
5 @date 1994, 1997, 2000
6 @date CVS $Date: 2002-07-25 17:57:05 $
7 @version CVS $Revision: 1.1 $
8*/
9
10/* Copyright (C) 1994, 1997, 2000 Konrad Bernloehr. All rights reserved. */
11
12#include "initial.h"
13#include "warning.h"
14#include "io_basic.h"
15
16struct test_struct
17{
18 long lvar[2];
19 int ilvar[2];
20 int isvar[2];
21 short svar[3];
22 double fvar[2];
23 double dvar[2];
24 int8_t i8var[2];
25 uint8_t u8var[2];
26 int16_t i16var[2];
27 uint16_t u16var[2];
28 int32_t i32var[2];
29 uint32_t u32var[2];
30#ifdef HAVE_64BIT_INT
31 int64_t i64var[2];
32 uint64_t u64var[2];
33#endif
34};
35
36typedef struct test_struct TEST_DATA;
37
38static int care_long, care_int, care_short;
39
40int datacmp ARGLIST((TEST_DATA *data1, TEST_DATA *data2));
41int write_test1 ARGLIST((TEST_DATA *data, IO_BUFFER *iobuf));
42int read_test1 ARGLIST((TEST_DATA *data, IO_BUFFER *iobuf));
43int write_test2 ARGLIST((TEST_DATA *data, IO_BUFFER *iobuf));
44int read_test2 ARGLIST((TEST_DATA *data, IO_BUFFER *iiobuf));
45
46/* ------------------------ datacmp ---------------------- */
47/**
48 * @short Compare elements of test data structures.
49 * Compare elements of test data structures with the accuracy
50 * relevant to the I/O package.
51 *
52 * @param data1 first data structure
53 * @param data2 second data structure
54 *
55 * @return 0 (something did not match), 1 (O.K.)
56 *
57 */
58
59int datacmp(data1,data2)
60 TEST_DATA *data1;
61 TEST_DATA *data2;
62{
63 int i, ok;
64
65 ok = 1;
66
67 for (i=0; i<2; i++)
68 if ( (int32_t)data1->lvar[i] != (int32_t)data2->lvar[i] )
69 {
70 fprintf(stderr,"Long variable %d does not match: %08lx <--> %08lx\n",
71 i+1,data1->lvar[i],data2->lvar[i]);
72 ok = 0;
73 }
74 else if ( data1->lvar[i] != data2->lvar[i] )
75 care_long = 1;
76
77 for (i=0; i<2; i++)
78 if ( (int32_t)data1->ilvar[i] != (int32_t)data2->ilvar[i] )
79 {
80 fprintf(stderr,"Int 'l' variable %d does not match: %08x <--> %08x\n",
81 i+1,data1->ilvar[i],data2->ilvar[i]);
82 ok = 0;
83 }
84
85 for (i=0; i<2; i++)
86 if ( (int16_t)data1->isvar[i] != (int16_t)data2->isvar[i] )
87 {
88 fprintf(stderr,"Int 's' variable %d does not match: %04x <--> %04x\n",
89 i+1,data1->isvar[i],data2->isvar[i]);
90 ok = 0;
91 }
92 else if ( data1->isvar[i] != data2->isvar[i] )
93 care_short = 1;
94
95 for (i=0; i<3; i++)
96 if ( data1->svar[i] != data2->svar[i] )
97 {
98 fprintf(stderr,"Short variable %d does not match: %04x <--> %04x\n",
99 i+1,data1->svar[i],data2->svar[i]);
100 ok = 0;
101 }
102
103 for (i=0; i<2; i++)
104 {
105 float f1, f2;
106 f1 = (float) data1->fvar[i];
107 f2 = (float) data2->fvar[i];
108 if ( f1 != f2 )
109 {
110 fprintf(stderr,"Float variable %d does not match: %08x <--> %08x\n",
111 i+1,*((int *)(&f1)),*((int *)(&f2)));
112 ok = 0;
113 }
114 }
115
116 for (i=0; i<2; i++)
117 if ( data1->dvar[i] != data2->dvar[i] )
118 {
119 double f1, f2;
120 f1 = data1->dvar[i];
121 f2 = data2->dvar[i];
122 fprintf(stderr,
123 "Double variable %d does not match: %08x%08x <--> %08x%08x\n",
124 i+1,*((int *)(&f1)),*((int *)((char *)(&f1)+4)),
125 *((int *)(&f2)),*((int *)((char *)(&f2)+4)));
126 ok = 0;
127 }
128
129 for (i=0; i<2; i++)
130 if ( data1->i8var[i] != data2->i8var[i] )
131 {
132 fprintf(stderr,"Int8 variable %d does not match: %08x <--> %08x\n",
133 i+1,data1->i8var[i],data2->i8var[i]);
134 ok = 0;
135 }
136
137 for (i=0; i<2; i++)
138 if ( data1->u8var[i] != data2->u8var[i] )
139 {
140 fprintf(stderr,"UInt8 variable %d does not match: %08x <--> %08x\n",
141 i+1,data1->u8var[i],data2->u8var[i]);
142 ok = 0;
143 }
144
145 for (i=0; i<2; i++)
146 if ( data1->i16var[i] != data2->i16var[i] )
147 {
148 fprintf(stderr,"Int16 variable %d does not match: %08x <--> %08x\n",
149 i+1,data1->i16var[i],data2->i16var[i]);
150 ok = 0;
151 }
152
153 for (i=0; i<2; i++)
154 if ( data1->u16var[i] != data2->u16var[i] )
155 {
156 fprintf(stderr,"UInt16 variable %d does not match: %08x <--> %08x\n",
157 i+1,data1->u16var[i],data2->u16var[i]);
158 ok = 0;
159 }
160
161 for (i=0; i<2; i++)
162 if ( data1->i32var[i] != data2->i32var[i] )
163 {
164 fprintf(stderr,"Int32 variable %d does not match: %08x <--> %08x\n",
165 i+1,data1->i32var[i],data2->i32var[i]);
166 ok = 0;
167 }
168
169 for (i=0; i<2; i++)
170 if ( data1->u32var[i] != data2->u32var[i] )
171 {
172 fprintf(stderr,"UInt32 variable %d does not match: %08x <--> %08x\n",
173 i+1,data1->u32var[i],data2->u32var[i]);
174 ok = 0;
175 }
176
177#ifdef HAVE_64BIT_INT
178 for (i=0; i<2; i++)
179 if ( data1->i64var[i] != data2->i64var[i] )
180 {
181#ifdef SIXTY_FOUR_BITS
182 fprintf(stderr,"Int64 variable %d does not match: %016lx <--> %016lx\n",
183 i+1,data1->i64var[i],data2->i64var[i]);
184#else
185 fprintf(stderr,"Int64 variable %d does not match: %016llx <--> %016llx\n",
186 i+1,data1->i64var[i],data2->i64var[i]);
187#endif
188 ok = 0;
189 }
190
191 for (i=0; i<2; i++)
192 if ( data1->u64var[i] != data2->u64var[i] )
193 {
194#ifdef SIXTY_FOUR_BITS
195 fprintf(stderr,"UInt64 variable %d does not match: %016lx <--> %016lx\n",
196 i+1,data1->u64var[i],data2->u64var[i]);
197#else
198 fprintf(stderr,"UInt64 variable %d does not match: %016llx <--> %016llx\n",
199 i+1,data1->u64var[i],data2->u64var[i]);
200#endif
201 ok = 0;
202 }
203#endif
204
205 return ok;
206}
207
208/* --------------------- write_test1 ---------------------- */
209/**
210 * @short Write test data with single-element functions
211 *
212 * @param data Pointer to test data structure
213 * @param iobuf Pointer to I/O buffer
214 *
215 * @return 0 (O.K.), <0 (error as for put_item_end())
216 *
217 */
218
219int write_test1(data,iobuf)
220 TEST_DATA *data;
221 IO_BUFFER *iobuf;
222{
223 IO_ITEM_HEADER item_header;
224
225 item_header.type = 99; /* test data */
226 item_header.version = 0; /* Version 0 (test) */
227 item_header.ident = 123;
228
229 put_item_begin(iobuf,&item_header);
230
231 put_long(data->lvar[0],iobuf);
232 put_long(data->lvar[1],iobuf);
233 put_long((long)data->ilvar[0],iobuf);
234 put_long((long)data->ilvar[1],iobuf);
235 put_short(data->isvar[0],iobuf);
236 put_short(data->isvar[1],iobuf);
237 put_short(data->svar[0],iobuf);
238 put_short(data->svar[1],iobuf);
239 put_short(data->svar[2],iobuf);
240 put_real(data->fvar[0],iobuf);
241 put_real(data->fvar[1],iobuf);
242 put_double(data->dvar[0],iobuf);
243 put_double(data->dvar[1],iobuf);
244 put_byte(data->i8var[0],iobuf);
245 put_byte(data->i8var[1],iobuf);
246 put_byte(data->u8var[0],iobuf);
247 put_byte(data->u8var[1],iobuf);
248 put_short(data->i16var[0],iobuf);
249 put_short(data->i16var[1],iobuf);
250 put_short(data->u16var[0],iobuf);
251 put_short(data->u16var[1],iobuf);
252 put_int32(data->i32var[0],iobuf);
253 put_int32(data->i32var[1],iobuf);
254 put_uint32(data->u32var[0],iobuf);
255 put_uint32(data->u32var[1],iobuf);
256#ifdef HAVE_64BIT_INT
257 put_vector_of_int64(&data->i64var[0],1,iobuf);
258 put_vector_of_int64(&data->i64var[1],1,iobuf);
259 put_vector_of_uint64(&data->u64var[0],1,iobuf);
260 put_vector_of_uint64(&data->u64var[1],1,iobuf);
261#endif
262 return(put_item_end(iobuf,&item_header));
263}
264
265/* ---------------------- read_test1 ---------------------- */
266/**
267 * @short Read test data with single-element functions
268 *
269 * @param data Pointer to test data structure
270 * @param iobuf Pointer to I/O buffer
271 *
272 * @return 0 (ok), <0 (error as for get_item_end())
273 *
274 */
275
276int read_test1(data,iobuf)
277 TEST_DATA *data;
278 IO_BUFFER *iobuf;
279{
280 IO_ITEM_HEADER item_header;
281
282 item_header.type = 99; /* test data */
283 if ( get_item_begin(iobuf,&item_header) < 0 )
284 {
285 Warning("Missing or invalid test data block.");
286 return -4;
287 }
288
289 data->lvar[0] = get_long(iobuf);
290 data->lvar[1] = get_long(iobuf);
291 data->ilvar[0] = get_long(iobuf);
292 data->ilvar[1] = get_long(iobuf);
293 data->isvar[0] = get_short(iobuf);
294 data->isvar[1] = get_short(iobuf);
295 data->svar[0] = get_short(iobuf);
296 data->svar[1] = get_short(iobuf);
297 data->svar[2] = get_short(iobuf);
298 data->fvar[0] = get_real(iobuf);
299 data->fvar[1] = get_real(iobuf);
300 data->dvar[0] = get_double(iobuf);
301 data->dvar[1] = get_double(iobuf);
302 data->i8var[0] = get_byte(iobuf);
303 data->i8var[1] = get_byte(iobuf);
304 data->u8var[0] = get_byte(iobuf);
305 data->u8var[1] = get_byte(iobuf);
306 data->i16var[0] = get_short(iobuf);
307 data->i16var[1] = get_short(iobuf);
308 data->u16var[0] = get_short(iobuf);
309 data->u16var[1] = get_short(iobuf);
310 data->i32var[0] = get_int32(iobuf);
311 data->i32var[1] = get_int32(iobuf);
312 data->u32var[0] = get_uint32(iobuf);
313 data->u32var[1] = get_uint32(iobuf);
314#ifdef HAVE_64BIT_INT
315 get_vector_of_int64(&data->i64var[0],1,iobuf);
316 get_vector_of_int64(&data->i64var[1],1,iobuf);
317 get_vector_of_uint64(&data->u64var[0],1,iobuf);
318 get_vector_of_uint64(&data->u64var[1],1,iobuf);
319#endif
320
321 return(get_item_end(iobuf,&item_header));
322}
323
324/* --------------------- write_test2 ---------------------- */
325/**
326 * @short Write test data with vector functions as far as possible
327 *
328 * @param data Pointer to test data structure
329 * @param iobuf Pointer to I/O buffer
330 *
331 * @return 0 (ok), <0 (error as for put_item_end())
332 *
333 */
334
335int write_test2(data,iobuf)
336 TEST_DATA *data;
337 IO_BUFFER *iobuf;
338{
339 IO_ITEM_HEADER item_header;
340
341 item_header.type = 99; /* test data */
342 item_header.version = 0; /* Version 0 (test) */
343 item_header.ident = 123;
344
345 put_item_begin(iobuf,&item_header);
346
347 put_vector_of_long(data->lvar,2,iobuf);
348 put_long((long)data->ilvar[0],iobuf);
349 put_long((long)data->ilvar[1],iobuf);
350 put_vector_of_int(data->isvar,2,iobuf);
351 put_vector_of_short(data->svar,3,iobuf);
352 put_vector_of_real(data->fvar,2,iobuf);
353 put_vector_of_double(data->dvar,2,iobuf);
354 put_vector_of_byte((uint8_t *)data->i8var,2,iobuf);
355 put_vector_of_byte(data->u8var,2,iobuf);
356 put_vector_of_short(data->i16var,2,iobuf);
357 put_vector_of_short((int16_t *)data->u16var,2,iobuf);
358 put_vector_of_int32(data->i32var,2,iobuf);
359 put_vector_of_uint32(data->u32var,2,iobuf);
360#ifdef HAVE_64BIT_INT
361 put_vector_of_int64(data->i64var,2,iobuf);
362 put_vector_of_uint64(data->u64var,2,iobuf);
363#endif
364
365 return(put_item_end(iobuf,&item_header));
366}
367
368/* ---------------------- read_test2 ---------------------- */
369/**
370 * @short Read test data with vector functions as far as possible
371 *
372 * @param data Pointer to test data structure
373 * @param iobuf Pointer to I/O buffer
374 *
375 * @return 0 (ok), <0 (error as for get_item_end())
376 *
377 */
378
379int read_test2(data,iobuf)
380 TEST_DATA *data;
381 IO_BUFFER *iobuf;
382{
383 IO_ITEM_HEADER item_header;
384
385 item_header.type = 99; /* test data */
386 if ( get_item_begin(iobuf,&item_header) < 0 )
387 {
388 Warning("Missing or invalid test data block.");
389 return -4;
390 }
391
392 get_vector_of_long(data->lvar,2,iobuf);
393 data->ilvar[0] = get_long(iobuf);
394 data->ilvar[1] = get_long(iobuf);
395 get_vector_of_int(data->isvar,2,iobuf);
396 get_vector_of_short(data->svar,3,iobuf);
397 get_vector_of_real(data->fvar,2,iobuf);
398 get_vector_of_double(data->dvar,2,iobuf);
399 get_vector_of_byte((uint8_t *)data->i8var,2,iobuf);
400 get_vector_of_byte(data->u8var,2,iobuf);
401 get_vector_of_short(data->i16var,2,iobuf);
402 get_vector_of_short((int16_t *)data->u16var,2,iobuf);
403 get_vector_of_int32(data->i32var,2,iobuf);
404 get_vector_of_uint32(data->u32var,2,iobuf);
405#ifdef HAVE_64BIT_INT
406 get_vector_of_int64(data->i64var,2,iobuf);
407 get_vector_of_uint64(data->u64var,2,iobuf);
408#endif
409
410 return(get_item_end(iobuf,&item_header));
411}
412
413/* ---------------------- perror ------------------------- */
414/**
415 * @short Replacement for function missing on OS-9
416*/
417
418#ifdef OS_OS9
419int perror(text)
420 char *text;
421{
422 fprintf(stderr,"%s: Error\n",text);
423 return 0;
424}
425#endif
426
427/* ------------------------ main ------------------------- */
428/**
429 * @short Main function for I/O test program.
430 * First writes a test data structure with the vector
431 * functions, then the same data structure with the
432 * single-element functions. The output file is then
433 * closed and reopened for reading. The first structure
434 * is then read with the single-element functions and
435 * the second with the vector functions (i.e. the other
436 * way as done for writing).
437 * The data from the file is compared with the original
438 * data, taking the relevant accuracy into account.
439 * Note that if an 'int' variable is written via 'put_short()'
440 * and then read again via 'get_short()' not only the
441 * upper two bytes (on a 32-bit machine) are lost but
442 * also the sign bit is propagated from bit 15 to the
443 * upper 16 bits. Similarly, if a 'long' variable is written
444 * via 'put_long()' and later read via 'get_long()' on a
445 * 64-bit-machine, not only the upper 4 bytes are lost but
446 * also the sign in bit 31 is propagated to the upper 32 bits.
447 */
448
449#ifdef ANSI_C
450int main (int argc, char **argv)
451#else
452int main (argc, argv)
453 int argc;
454 char **argv;
455#endif
456{
457 IO_BUFFER *iobuf;
458 IO_ITEM_HEADER item_header;
459 FILE *output = NULL, *input;
460 TEST_DATA tdata, cdata1, cdata2;
461 int ok;
462
463 tdata.lvar[0] = 0x01020304L; tdata.lvar[1] = 0xf1f2f3f4L;
464 tdata.ilvar[0] = 0x01020304; tdata.ilvar[1] = 0xf1f2f3f4;
465 tdata.isvar[0] = 0x0102; tdata.isvar[1] = 0xf1f2;
466 tdata.svar[0] = 0x0102;
467 tdata.svar[1] = (short) 0xf1f2;
468 tdata.svar[2] = 0x0a0b;
469 tdata.fvar[0] = 2.38793926059e-38;
470 tdata.fvar[1] = -2.40608939547e+30;
471 tdata.dvar[0] = 2.38793926059674673672e-140;
472 tdata.dvar[1] = -2.40608939547354636548e+180;
473 tdata.i8var[0] = 0x1e; tdata.i8var[1] = 0xe1;
474 tdata.u8var[0] = 0x1e; tdata.u8var[1] = 0xe1;
475 tdata.i16var[0] = 0x1e2e; tdata.i16var[1] = 0xe2e1;
476 tdata.u16var[0] = 0x1e2e; tdata.u16var[1] = 0xe2e1;
477 tdata.i32var[0] = 0x1e2e3e4e; tdata.i32var[1] = 0xe4e3e2e1;
478 tdata.u32var[0] = 0x1e2e3e4e; tdata.u32var[1] = 0xe4e3e2e1;
479#ifdef HAVE_64BIT_INT
480 tdata.i64var[0] = 0x1a2a3a4a5a6a7a8a;
481 tdata.i64var[1] = 0xa8a7a6a5a4a3a2a1;
482 tdata.u64var[0] = 0x1b2b3b4b5b6b7b8b;
483 tdata.u64var[1] = 0xb8b7b6b5b4b3b2b1;
484#endif
485
486 if ( (iobuf = allocate_io_buffer((size_t)1000)) == (IO_BUFFER *) NULL )
487 exit(1);
488 if ( argc > 1 )
489 {
490 if ( (output = fopen(argv[1],WRITE_BINARY)) == (FILE *) NULL )
491 {
492 perror(argv[1]);
493 exit(1);
494 }
495 iobuf->output_file = output;
496 }
497 else
498 {
499 fprintf(stderr,"Syntax: %s filename\n",argv[0]);
500 exit(1);
501 }
502
503 fprintf(stderr,"\nWrite test data to file '%s'.\n",argv[1]);
504 fprintf(stderr,"Default byte order, using mainly vector functions.\n");
505 write_test2(&tdata,iobuf);
506 fprintf(stderr,"Default byte order, using single-element functions.\n");
507 write_test1(&tdata,iobuf);
508 iobuf->byte_order = 1;
509 fprintf(stderr,"Reversed byte order, using single-element functions.\n");
510 write_test1(&tdata,iobuf);
511 iobuf->byte_order = 0;
512 fprintf(stderr,"Normal byte order, using single-element functions.\n");
513 write_test1(&tdata,iobuf);
514 fprintf(stderr,"Write tests done.\n\n");
515
516 fclose(output);
517 iobuf->output_file = output = NULL;
518 if ( (input = fopen(argv[1],READ_BINARY)) == (FILE *) NULL )
519 {
520 perror(argv[1]);
521 exit(1);
522 }
523 iobuf->input_file = input;
524 ok = 1;
525
526 fprintf(stderr,"Read test data from file '%s'.\n",argv[1]);
527
528 fprintf(stderr,"Default byte order, using single-element functions.\n");
529 if ( find_io_block(iobuf,&item_header) < 0 )
530 {
531 Error("*** Finding I/O block 1 failed");
532 exit(1);
533 }
534 if ( read_io_block(iobuf,&item_header) < 0 )
535 {
536 Error("*** Reading I/O block 1 failed");
537 exit(1);
538 }
539 if ( read_test1(&cdata1,iobuf) < 0 )
540 {
541 Error("*** Read test 1 failed");
542 exit(1);
543 }
544 if ( datacmp(&tdata,&cdata1) != 1 )
545 {
546 Error("*** Data from read test 1 does not match.");
547 ok = 0;
548 }
549
550 fprintf(stderr,"Default byte order, using mainly vector functions.\n");
551 if ( find_io_block(iobuf,&item_header) < 0 )
552 {
553 Error("*** Finding I/O block 1 failed");
554 exit(1);
555 }
556 if ( read_io_block(iobuf,&item_header) < 0 )
557 {
558 Error("*** Reading I/O block 1 failed");
559 exit(1);
560 }
561 if ( read_test2(&cdata2,iobuf) < 0 )
562 {
563 Error("*** Read test 2 failed");
564 exit(1);
565 }
566 if ( datacmp(&tdata,&cdata2) != 1 )
567 {
568 Error("*** Data from read test 2 does not match");
569 ok = 0;
570 }
571 fprintf(stderr,"Reversed byte order, using single-element functions.\n");
572 if ( find_io_block(iobuf,&item_header) < 0 )
573 {
574 Error("*** Finding I/O block 3 failed");
575 exit(1);
576 }
577 if ( read_io_block(iobuf,&item_header) < 0 )
578 {
579 Error("*** Reading I/O block 3 failed");
580 exit(1);
581 }
582 if ( read_test1(&cdata1,iobuf) < 0 )
583 {
584 Error("*** Read test 3 failed");
585 exit(1);
586 }
587 if ( datacmp(&tdata,&cdata1) != 1 )
588 {
589 Error("*** Data from read test 3 does not match.");
590 ok = 0;
591 }
592
593 fprintf(stderr,"Normal byte order, using single-element functions.\n");
594 if ( find_io_block(iobuf,&item_header) < 0 )
595 {
596 Error("*** Finding I/O block 4 failed");
597 exit(1);
598 }
599 if ( read_io_block(iobuf,&item_header) < 0 )
600 {
601 Error("*** Reading I/O block 4 failed");
602 exit(1);
603 }
604 if ( read_test1(&cdata2,iobuf) < 0 )
605 {
606 Error("*** Read test 4 failed");
607 exit(1);
608 }
609 if ( datacmp(&tdata,&cdata2) != 1 )
610 {
611 Error("*** Data from read test 4 does not match");
612 ok = 0;
613 }
614
615 Information("Read tests done\n");
616
617 if ( ok )
618 Information("Everthing is ok. Congratulations!\n");
619
620 if ( care_long )
621 {
622 Information("Note: on this machine you should care about the sign propagation");
623 Information("of 'LONG' (INT32) data elements to long integer variables.\n");
624 }
625
626 if ( care_int )
627 {
628 Information("Note: on this machine you should care about the sign propagation");
629 Information("of 'LONG' (INT32) data elements to 32 bit integer variables.\n");
630 }
631
632 if ( care_short )
633 {
634 Information("Note: on this machine you should care about the sign propagation");
635 Information("of 'SHORT' data elements to integer or long integer variables.\n");
636 }
637
638#ifdef HAVE_64BIT_INT
639 Information("On this machine you can read and write 64-bit integers but you should");
640 Information("be aware that this feature is not available on all systems otherwise");
641 Information("supported by eventio.");
642#ifdef SIXTY_FOUR_BITS
643 Information("These 64-bit integers are native types.\n");
644#else
645 Information("These 64-bit integers are implemented through the C compiler.\n");
646#endif
647#else
648 Information("On this system no 64-bit integers are supported.\n");
649#endif
650
651 return 0;
652}
653
Note: See TracBrowser for help on using the repository browser.