GRASS Programmer's Manual  6.4.1(2011)
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:      Test portable r/w functions 
00010  *
00011  * COPYRIGHT:    (C) 2001-2009 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 <stdlib.h>
00019 #include <stdio.h>
00020 #include <grass/Vect.h>
00021 
00022 /* Test portable r/w functions */
00023 
00024 #define D_TEST 1.3333
00025 #define L_TEST 123456789
00026 #define S_TEST 12345
00027 #define C_TEST 123
00028 
00029 int main()
00030 {
00031     int i, j;
00032     int err;
00033     int byte_order;
00034     struct Port_info port;
00035     GVFILE fp;
00036 
00037     double db, td[] = { -(PORT_DOUBLE_MAX), -(D_TEST), -(PORT_DOUBLE_MIN),
00038         0, PORT_DOUBLE_MIN, D_TEST, PORT_DOUBLE_MAX
00039     };
00040     float fb, tf[] = { -(PORT_FLOAT_MAX), -(D_TEST), -(PORT_FLOAT_MIN),
00041         0, PORT_FLOAT_MIN, D_TEST, PORT_FLOAT_MAX
00042     };
00043     long lb, tl[] = { PORT_LONG_MIN, -(L_TEST), 0, L_TEST, PORT_LONG_MAX };
00044     int ib, ti[] = { PORT_INT_MIN, -(L_TEST), 0, L_TEST, PORT_INT_MAX };
00045     short sb, ts[] = { PORT_SHORT_MIN, -(S_TEST), 0, S_TEST, PORT_SHORT_MAX };
00046     char cb, tc[] = { PORT_CHAR_MIN, -(C_TEST), 0, C_TEST, PORT_CHAR_MAX };
00047 
00048     err = EXIT_SUCCESS;
00049     
00050     if (NULL == (fp.file = fopen("test.tmp", "wb+"))) {
00051         G_fatal_error("Unable tp open test.tmp file");
00052     }
00053     fp.loaded = 0;
00054 
00055     dig_set_cur_port(&port);
00056 
00057     byte_order = ENDIAN_LITTLE;
00058     for (i = 0; i < 2; i++) {
00059         dig_init_portable(&(port), byte_order);
00060         for (j = 0; j < 7; j++) {
00061             dig_fseek(&fp, 0, SEEK_CUR);
00062             fprintf(fp.file, "double  ");
00063             dig__fwrite_port_D(&(td[j]), 1, &fp);
00064             dig_fseek(&fp, -(PORT_DOUBLE), SEEK_CUR);
00065             dig__fread_port_D(&db, 1, &fp);
00066             dig_fflush(&fp);
00067             if (db != td[j]) {
00068                 G_warning("Error in read/write portable double, byte_order = %d"
00069                           " Written: %.16e3E Read: %.16e3E",
00070                           byte_order, td[j], db);
00071                 err = EXIT_FAILURE;
00072             }
00073         }
00074         for (j = 0; j < 7; j++) {
00075             dig_fseek(&fp, 0, SEEK_CUR);
00076             fprintf(fp.file, "float       ");
00077             dig__fwrite_port_F(&(tf[j]), 1, &fp);
00078             dig_fseek(&fp, -(PORT_FLOAT), SEEK_CUR);
00079             dig__fread_port_F(&fb, 1, &fp);
00080             dig_fflush(&fp);
00081             if (fb != tf[j]) {
00082                 G_warning("Error in read/write portable float, byte_order = %d"
00083                           " Written: %.8e3E Read: %.8e3E",
00084                           byte_order, tf[j], fb);
00085                 err = EXIT_FAILURE;
00086             }
00087         }
00088 
00089         for (j = 0; j < 5; j++) {
00090             dig_fseek(&fp, 0, SEEK_CUR);
00091             fprintf(fp.file, "long        ");
00092             dig__fwrite_port_L(&(tl[j]), 1, &fp);
00093             dig_fseek(&fp, -(PORT_LONG), SEEK_CUR);
00094             dig__fread_port_L(&lb, 1, &fp);
00095             dig_fflush(&fp);
00096             if (lb != tl[j]) {
00097                 G_warning("Error in read/write portable long, byte_order = %d"
00098                           " Written: %lu Read: %lu", 
00099                           byte_order, (long unsigned) tl[j], (long unsigned) lb);
00100                 err = EXIT_FAILURE;
00101             }
00102         }
00103 
00104         for (j = 0; j < 5; j++) {
00105             dig_fseek(&fp, 0, SEEK_CUR);
00106             fprintf(fp.file, "int         ");
00107             dig__fwrite_port_I(&(ti[j]), 1, &fp);
00108             dig_fseek(&fp, -(PORT_INT), SEEK_CUR);
00109             dig__fread_port_I(&ib, 1, &fp);
00110             dig_fflush(&fp);
00111             if (ib != ti[j]) {
00112                 G_warning("Error in read/write portable int, byte_order = %d"
00113                           " Written: %d Read: %d", 
00114                           byte_order, ti[j], ib);
00115                 
00116                 err = EXIT_FAILURE;
00117             }
00118         }
00119 
00120         for (j = 0; j < 5; j++) {
00121             dig_fseek(&fp, 0, SEEK_CUR);
00122             fprintf(fp.file, "short         ");
00123             dig__fwrite_port_S(&(ts[j]), 1, &fp);
00124             dig_fseek(&fp, -(PORT_SHORT), SEEK_CUR);
00125             dig__fread_port_S(&sb, 1, &fp);
00126             dig_fflush(&fp);
00127             if (sb != ts[j]) {
00128                 G_warning("Error in read/write portable short, byte_order = %d"
00129                           " Written: %d Read: %d",
00130                           byte_order,  ts[j], sb);
00131                 
00132                 err = EXIT_FAILURE;
00133             }
00134         }
00135         for (j = 0; j < 5; j++) {
00136             dig_fseek(&fp, 0, SEEK_CUR);
00137             fprintf(fp.file, "char           ");
00138             dig__fwrite_port_C(&(tc[j]), 1, &fp);
00139             dig_fseek(&fp, -(PORT_CHAR), SEEK_CUR);
00140             dig__fread_port_C(&cb, 1, &fp);
00141             dig_fflush(&fp);
00142             if (cb != tc[j]) {
00143                 G_warning("Error in read/write portable char, byte_order = %d"
00144                           " Written: %d Read: %d",
00145                           byte_order, tc[j], cb);
00146                 err = EXIT_FAILURE;
00147             }
00148 
00149 
00150         }
00151         byte_order = ENDIAN_BIG;
00152     }
00153 
00154     fclose(fp.file);
00155 
00156     exit(err);
00157 }
00158 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines