diglib/test.c

Go to the documentation of this file.
00001 /*
00002  ****************************************************************************
00003  *
00004  * MODULE:       Vector library 
00005  *              
00006  * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
00007  *               Update to GRASS 5.7 Radim Blazek.
00008  *
00009  * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
00010  *
00011  * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012  *
00013  *               This program is free software under the GNU General Public
00014  *              License (>=v2). Read the file COPYING that comes with GRASS
00015  *              for details.
00016  *
00017  *****************************************************************************/
00018 #include <stdio.h>
00019 #include <grass/Vect.h>
00020 
00021 /* Test portable r/w functions */
00022 
00023 #define D_TEST 1.3333
00024 #define L_TEST 123456789
00025 #define S_TEST 12345
00026 #define C_TEST 123
00027 
00028 int main(int argc, char **argv)
00029 {
00030     int i, j;
00031     int err = 0;
00032     int byte_order;
00033     struct Port_info port;
00034     GVFILE fp;
00035 
00036     double db, td[] = { -(PORT_DOUBLE_MAX), -(D_TEST), -(PORT_DOUBLE_MIN),
00037         0, PORT_DOUBLE_MIN, D_TEST, PORT_DOUBLE_MAX
00038     };
00039     float fb, tf[] = { -(PORT_FLOAT_MAX), -(D_TEST), -(PORT_FLOAT_MIN),
00040         0, PORT_FLOAT_MIN, D_TEST, PORT_FLOAT_MAX
00041     };
00042     long lb, tl[] = { PORT_LONG_MIN, -(L_TEST), 0, L_TEST, PORT_LONG_MAX };
00043     int ib, ti[] = { PORT_INT_MIN, -(L_TEST), 0, L_TEST, PORT_INT_MAX };
00044     short sb, ts[] = { PORT_SHORT_MIN, -(S_TEST), 0, S_TEST, PORT_SHORT_MAX };
00045     char cb, tc[] = { PORT_CHAR_MIN, -(C_TEST), 0, C_TEST, PORT_CHAR_MAX };
00046 
00047 
00048     if (NULL == (fp.file = fopen("test.tmp", "wb+"))) {
00049         fprintf(stderr, "ERROR, cannot open test.tmp file.\n");
00050         return (1);
00051     }
00052     fp.loaded = 0;
00053 
00054     dig_set_cur_port(&port);
00055 
00056     byte_order = ENDIAN_LITTLE;
00057     for (i = 0; i < 2; i++) {
00058         dig_init_portable(&(port), byte_order);
00059         for (j = 0; j < 7; j++) {
00060             dig_fseek(&fp, 0, SEEK_CUR);
00061             fprintf(fp.file, "double  ");
00062             dig__fwrite_port_D(&(td[j]), 1, &fp);
00063             dig_fseek(&fp, -(PORT_DOUBLE), SEEK_CUR);
00064             dig__fread_port_D(&db, 1, &fp);
00065             dig_fflush(&fp);
00066             if (db != td[j]) {
00067                 fprintf(stderr,
00068                         "ERROR in read/write portable double, byte_order = %d\n",
00069                         byte_order);
00070                 fprintf(stderr, "  Written: %.16e3E\n  Read   : %.16e3E\n",
00071                         td[j], db);
00072                 err = 1;
00073             }
00074         }
00075         for (j = 0; j < 7; j++) {
00076             dig_fseek(&fp, 0, SEEK_CUR);
00077             fprintf(fp.file, "float       ");
00078             dig__fwrite_port_F(&(tf[j]), 1, &fp);
00079             dig_fseek(&fp, -(PORT_FLOAT), SEEK_CUR);
00080             dig__fread_port_F(&fb, 1, &fp);
00081             dig_fflush(&fp);
00082             if (fb != tf[j]) {
00083                 fprintf(stderr,
00084                         "ERROR in read/write portable float, byte_order = %d\n",
00085                         byte_order);
00086                 fprintf(stderr, "  Written: %.8e3E\n  Read   : %.8e3E\n",
00087                         tf[j], fb);
00088                 err = 1;
00089             }
00090         }
00091 
00092         for (j = 0; j < 5; j++) {
00093             dig_fseek(&fp, 0, SEEK_CUR);
00094             fprintf(fp.file, "long        ");
00095             dig__fwrite_port_L(&(tl[j]), 1, &fp);
00096             dig_fseek(&fp, -(PORT_LONG), SEEK_CUR);
00097             dig__fread_port_L(&lb, 1, &fp);
00098             dig_fflush(&fp);
00099             if (lb != tl[j]) {
00100                 fprintf(stderr,
00101                         "ERROR in read/write portable long, byte_order = %d\n",
00102                         byte_order);
00103                 fprintf(stderr, "  Written: %ld\n  Read   : %ld\n", tl[j],
00104                         lb);
00105                 err = 1;
00106             }
00107         }
00108 
00109         for (j = 0; j < 5; j++) {
00110             dig_fseek(&fp, 0, SEEK_CUR);
00111             fprintf(fp.file, "int         ");
00112             dig__fwrite_port_I(&(ti[j]), 1, &fp);
00113             dig_fseek(&fp, -(PORT_INT), SEEK_CUR);
00114             dig__fread_port_I(&ib, 1, &fp);
00115             dig_fflush(&fp);
00116             if (ib != ti[j]) {
00117                 fprintf(stderr,
00118                         "ERROR in read/write portable int, byte_order = %d\n",
00119                         byte_order);
00120                 fprintf(stderr, "  Written: %d\n  Read   : %d\n", ti[j], ib);
00121                 err = 1;
00122             }
00123         }
00124 
00125         for (j = 0; j < 5; j++) {
00126             dig_fseek(&fp, 0, SEEK_CUR);
00127             fprintf(fp.file, "short         ");
00128             dig__fwrite_port_S(&(ts[j]), 1, &fp);
00129             dig_fseek(&fp, -(PORT_SHORT), SEEK_CUR);
00130             dig__fread_port_S(&sb, 1, &fp);
00131             dig_fflush(&fp);
00132             if (sb != ts[j]) {
00133                 fprintf(stderr,
00134                         "ERROR in read/write portable short, byte_order = %d\n",
00135                         byte_order);
00136                 fprintf(stderr, "  Written: %d\n  Read   : %d\n", ts[j], sb);
00137                 err = 1;
00138             }
00139         }
00140         for (j = 0; j < 5; j++) {
00141             dig_fseek(&fp, 0, SEEK_CUR);
00142             fprintf(fp.file, "char           ");
00143             dig__fwrite_port_C(&(tc[j]), 1, &fp);
00144             dig_fseek(&fp, -(PORT_CHAR), SEEK_CUR);
00145             dig__fread_port_C(&cb, 1, &fp);
00146             dig_fflush(&fp);
00147             if (cb != tc[j]) {
00148                 fprintf(stderr,
00149                         "ERROR in read/write portable char, byte_order = %d\n",
00150                         byte_order);
00151                 fprintf(stderr, "  Written: %d\n  Read   : %d\n", tc[j], cb);
00152                 err = 1;
00153             }
00154 
00155 
00156         }
00157         byte_order = ENDIAN_BIG;
00158     }
00159 
00160     fclose(fp.file);
00161 
00162     return (err);
00163 }

Generated on Thu Jul 16 13:21:18 2009 for GRASS Programmer's Manual by  doxygen 1.5.6