GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
diglib/test.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * MODULE: Vector library
5  *
6  * AUTHOR(S): Original author CERL, probably Dave Gerdes.
7  * Update to GRASS 5.7 Radim Blazek.
8  *
9  * PURPOSE: Test portable r/w functions
10  *
11  * COPYRIGHT: (C) 2001-2009 by the GRASS Development Team
12  *
13  * This program is free software under the GNU General Public
14  * License (>=v2). Read the file COPYING that comes with GRASS
15  * for details.
16  *
17  *****************************************************************************/
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <grass/Vect.h>
21 
22 /* Test portable r/w functions */
23 
24 #define D_TEST 1.3333
25 #define L_TEST 123456789
26 #define S_TEST 12345
27 #define C_TEST 123
28 
29 int main()
30 {
31  int i, j;
32  int err;
33  int byte_order;
34  struct Port_info port;
35  GVFILE fp;
36 
37  double db, td[] = { -(PORT_DOUBLE_MAX), -(D_TEST), -(PORT_DOUBLE_MIN),
38  0, PORT_DOUBLE_MIN, D_TEST, PORT_DOUBLE_MAX
39  };
40  float fb, tf[] = { -(PORT_FLOAT_MAX), -(D_TEST), -(PORT_FLOAT_MIN),
41  0, PORT_FLOAT_MIN, D_TEST, PORT_FLOAT_MAX
42  };
43  long lb, tl[] = { PORT_LONG_MIN, -(L_TEST), 0, L_TEST, PORT_LONG_MAX };
44  int ib, ti[] = { PORT_INT_MIN, -(L_TEST), 0, L_TEST, PORT_INT_MAX };
45  short sb, ts[] = { PORT_SHORT_MIN, -(S_TEST), 0, S_TEST, PORT_SHORT_MAX };
46  char cb, tc[] = { PORT_CHAR_MIN, -(C_TEST), 0, C_TEST, PORT_CHAR_MAX };
47 
48  err = EXIT_SUCCESS;
49 
50  if (NULL == (fp.file = fopen("test.tmp", "wb+"))) {
51  G_fatal_error("Unable tp open test.tmp file");
52  }
53  fp.loaded = 0;
54 
55  dig_set_cur_port(&port);
56 
57  byte_order = ENDIAN_LITTLE;
58  for (i = 0; i < 2; i++) {
59  dig_init_portable(&(port), byte_order);
60  for (j = 0; j < 7; j++) {
61  dig_fseek(&fp, 0, SEEK_CUR);
62  fprintf(fp.file, "double ");
63  dig__fwrite_port_D(&(td[j]), 1, &fp);
64  dig_fseek(&fp, -(PORT_DOUBLE), SEEK_CUR);
65  dig__fread_port_D(&db, 1, &fp);
66  dig_fflush(&fp);
67  if (db != td[j]) {
68  G_warning("Error in read/write portable double, byte_order = %d"
69  " Written: %.16e3E Read: %.16e3E",
70  byte_order, td[j], db);
71  err = EXIT_FAILURE;
72  }
73  }
74  for (j = 0; j < 7; j++) {
75  dig_fseek(&fp, 0, SEEK_CUR);
76  fprintf(fp.file, "float ");
77  dig__fwrite_port_F(&(tf[j]), 1, &fp);
78  dig_fseek(&fp, -(PORT_FLOAT), SEEK_CUR);
79  dig__fread_port_F(&fb, 1, &fp);
80  dig_fflush(&fp);
81  if (fb != tf[j]) {
82  G_warning("Error in read/write portable float, byte_order = %d"
83  " Written: %.8e3E Read: %.8e3E",
84  byte_order, tf[j], fb);
85  err = EXIT_FAILURE;
86  }
87  }
88 
89  for (j = 0; j < 5; j++) {
90  dig_fseek(&fp, 0, SEEK_CUR);
91  fprintf(fp.file, "long ");
92  dig__fwrite_port_L(&(tl[j]), 1, &fp);
93  dig_fseek(&fp, -(PORT_LONG), SEEK_CUR);
94  dig__fread_port_L(&lb, 1, &fp);
95  dig_fflush(&fp);
96  if (lb != tl[j]) {
97  G_warning("Error in read/write portable long, byte_order = %d"
98  " Written: %lu Read: %lu",
99  byte_order, (long unsigned) tl[j], (long unsigned) lb);
100  err = EXIT_FAILURE;
101  }
102  }
103 
104  for (j = 0; j < 5; j++) {
105  dig_fseek(&fp, 0, SEEK_CUR);
106  fprintf(fp.file, "int ");
107  dig__fwrite_port_I(&(ti[j]), 1, &fp);
108  dig_fseek(&fp, -(PORT_INT), SEEK_CUR);
109  dig__fread_port_I(&ib, 1, &fp);
110  dig_fflush(&fp);
111  if (ib != ti[j]) {
112  G_warning("Error in read/write portable int, byte_order = %d"
113  " Written: %d Read: %d",
114  byte_order, ti[j], ib);
115 
116  err = EXIT_FAILURE;
117  }
118  }
119 
120  for (j = 0; j < 5; j++) {
121  dig_fseek(&fp, 0, SEEK_CUR);
122  fprintf(fp.file, "short ");
123  dig__fwrite_port_S(&(ts[j]), 1, &fp);
124  dig_fseek(&fp, -(PORT_SHORT), SEEK_CUR);
125  dig__fread_port_S(&sb, 1, &fp);
126  dig_fflush(&fp);
127  if (sb != ts[j]) {
128  G_warning("Error in read/write portable short, byte_order = %d"
129  " Written: %d Read: %d",
130  byte_order, ts[j], sb);
131 
132  err = EXIT_FAILURE;
133  }
134  }
135  for (j = 0; j < 5; j++) {
136  dig_fseek(&fp, 0, SEEK_CUR);
137  fprintf(fp.file, "char ");
138  dig__fwrite_port_C(&(tc[j]), 1, &fp);
139  dig_fseek(&fp, -(PORT_CHAR), SEEK_CUR);
140  dig__fread_port_C(&cb, 1, &fp);
141  dig_fflush(&fp);
142  if (cb != tc[j]) {
143  G_warning("Error in read/write portable char, byte_order = %d"
144  " Written: %d Read: %d",
145  byte_order, tc[j], cb);
146  err = EXIT_FAILURE;
147  }
148 
149 
150  }
151  byte_order = ENDIAN_BIG;
152  }
153 
154  fclose(fp.file);
155 
156  exit(err);
157 }
158 
#define C_TEST
Definition: diglib/test.c:27
int dig_set_cur_port(struct Port_info *port)
Definition: portable.c:640
int dig__fread_port_S(short *buf, int cnt, GVFILE *fp)
Definition: portable.c:277
#define S_TEST
Definition: diglib/test.c:26
int dig__fwrite_port_D(double *buf, int cnt, GVFILE *fp)
Definition: portable.c:370
const char * err
Definition: g3dcolor.c:50
void dig_init_portable(struct Port_info *port, int byte_order)
Definition: portable.c:568
int dig__fread_port_L(long *buf, int cnt, GVFILE *fp)
Definition: portable.c:137
int dig__fwrite_port_I(int *buf, int cnt, GVFILE *fp)
Definition: portable.c:465
int dig_fseek(GVFILE *file, long offset, int whence)
Set GVFILE position.
Definition: file.c:60
int main(int argc, char *argv[])
Definition: gem/main.c:302
int dig__fwrite_port_L(long *buf, int cnt, GVFILE *fp)
Definition: portable.c:422
int dig__fread_port_D(double *buf, int cnt, GVFILE *fp)
Definition: portable.c:75
int dig__fread_port_F(float *buf, int cnt, GVFILE *fp)
Definition: portable.c:106
#define D_TEST
Definition: diglib/test.c:24
int dig__fread_port_C(char *buf, int cnt, GVFILE *fp)
Definition: portable.c:347
return NULL
Definition: dbfopen.c:1394
int dig_fflush(GVFILE *file)
Flush GVFILE.
Definition: file.c:102
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
fclose(fd)
int dig__fwrite_port_F(float *buf, int cnt, GVFILE *fp)
Definition: portable.c:396
int dig__fwrite_port_C(char *buf, int cnt, GVFILE *fp)
Definition: portable.c:558
int dig__fwrite_port_S(short *buf, int cnt, GVFILE *fp)
Definition: portable.c:508
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
#define L_TEST
Definition: diglib/test.c:25
int dig__fread_port_I(int *buf, int cnt, GVFILE *fp)
Definition: portable.c:207