File: test.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (158 lines) | stat: -rw-r--r-- 4,472 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 ****************************************************************************
 *
 * MODULE:       Vector library 
 *              
 * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
 *               Update to GRASS 5.7 Radim Blazek.
 *
 * PURPOSE:      Test portable r/w functions 
 *
 * COPYRIGHT:    (C) 2001-2009 by the GRASS Development Team
 *
 *               This program is free software under the GNU General Public
 *               License (>=v2). Read the file COPYING that comes with GRASS
 *               for details.
 *
 *****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <grass/Vect.h>

/* Test portable r/w functions */

#define D_TEST 1.3333
#define L_TEST 123456789
#define S_TEST 12345
#define C_TEST 123

int main()
{
    int i, j;
    int err;
    int byte_order;
    struct Port_info port;
    GVFILE fp;

    double db, td[] = { -(PORT_DOUBLE_MAX), -(D_TEST), -(PORT_DOUBLE_MIN),
	0, PORT_DOUBLE_MIN, D_TEST, PORT_DOUBLE_MAX
    };
    float fb, tf[] = { -(PORT_FLOAT_MAX), -(D_TEST), -(PORT_FLOAT_MIN),
	0, PORT_FLOAT_MIN, D_TEST, PORT_FLOAT_MAX
    };
    long lb, tl[] = { PORT_LONG_MIN, -(L_TEST), 0, L_TEST, PORT_LONG_MAX };
    int ib, ti[] = { PORT_INT_MIN, -(L_TEST), 0, L_TEST, PORT_INT_MAX };
    short sb, ts[] = { PORT_SHORT_MIN, -(S_TEST), 0, S_TEST, PORT_SHORT_MAX };
    char cb, tc[] = { PORT_CHAR_MIN, -(C_TEST), 0, C_TEST, PORT_CHAR_MAX };

    err = EXIT_SUCCESS;
    
    if (NULL == (fp.file = fopen("test.tmp", "wb+"))) {
	G_fatal_error("Unable tp open test.tmp file");
    }
    fp.loaded = 0;

    dig_set_cur_port(&port);

    byte_order = ENDIAN_LITTLE;
    for (i = 0; i < 2; i++) {
	dig_init_portable(&(port), byte_order);
	for (j = 0; j < 7; j++) {
	    dig_fseek(&fp, 0, SEEK_CUR);
	    fprintf(fp.file, "double  ");
	    dig__fwrite_port_D(&(td[j]), 1, &fp);
	    dig_fseek(&fp, -(PORT_DOUBLE), SEEK_CUR);
	    dig__fread_port_D(&db, 1, &fp);
	    dig_fflush(&fp);
	    if (db != td[j]) {
		G_warning("Error in read/write portable double, byte_order = %d"
			  " Written: %.16e3E Read: %.16e3E",
			  byte_order, td[j], db);
		err = EXIT_FAILURE;
	    }
	}
	for (j = 0; j < 7; j++) {
	    dig_fseek(&fp, 0, SEEK_CUR);
	    fprintf(fp.file, "float       ");
	    dig__fwrite_port_F(&(tf[j]), 1, &fp);
	    dig_fseek(&fp, -(PORT_FLOAT), SEEK_CUR);
	    dig__fread_port_F(&fb, 1, &fp);
	    dig_fflush(&fp);
	    if (fb != tf[j]) {
		G_warning("Error in read/write portable float, byte_order = %d"
			  " Written: %.8e3E Read: %.8e3E",
			  byte_order, tf[j], fb);
		err = EXIT_FAILURE;
	    }
	}

	for (j = 0; j < 5; j++) {
	    dig_fseek(&fp, 0, SEEK_CUR);
	    fprintf(fp.file, "long        ");
	    dig__fwrite_port_L(&(tl[j]), 1, &fp);
	    dig_fseek(&fp, -(PORT_LONG), SEEK_CUR);
	    dig__fread_port_L(&lb, 1, &fp);
	    dig_fflush(&fp);
	    if (lb != tl[j]) {
		G_warning("Error in read/write portable long, byte_order = %d"
			  " Written: %lu Read: %lu", 
			  byte_order, (long unsigned) tl[j], (long unsigned) lb);
		err = EXIT_FAILURE;
	    }
	}

	for (j = 0; j < 5; j++) {
	    dig_fseek(&fp, 0, SEEK_CUR);
	    fprintf(fp.file, "int         ");
	    dig__fwrite_port_I(&(ti[j]), 1, &fp);
	    dig_fseek(&fp, -(PORT_INT), SEEK_CUR);
	    dig__fread_port_I(&ib, 1, &fp);
	    dig_fflush(&fp);
	    if (ib != ti[j]) {
		G_warning("Error in read/write portable int, byte_order = %d"
			  " Written: %d Read: %d", 
			  byte_order, ti[j], ib);
		
		err = EXIT_FAILURE;
	    }
	}

	for (j = 0; j < 5; j++) {
	    dig_fseek(&fp, 0, SEEK_CUR);
	    fprintf(fp.file, "short         ");
	    dig__fwrite_port_S(&(ts[j]), 1, &fp);
	    dig_fseek(&fp, -(PORT_SHORT), SEEK_CUR);
	    dig__fread_port_S(&sb, 1, &fp);
	    dig_fflush(&fp);
	    if (sb != ts[j]) {
		G_warning("Error in read/write portable short, byte_order = %d"
			  " Written: %d Read: %d",
			  byte_order,  ts[j], sb);
		
		err = EXIT_FAILURE;
	    }
	}
	for (j = 0; j < 5; j++) {
	    dig_fseek(&fp, 0, SEEK_CUR);
	    fprintf(fp.file, "char           ");
	    dig__fwrite_port_C(&(tc[j]), 1, &fp);
	    dig_fseek(&fp, -(PORT_CHAR), SEEK_CUR);
	    dig__fread_port_C(&cb, 1, &fp);
	    dig_fflush(&fp);
	    if (cb != tc[j]) {
		G_warning("Error in read/write portable char, byte_order = %d"
			  " Written: %d Read: %d",
			  byte_order, tc[j], cb);
		err = EXIT_FAILURE;
	    }


	}
	byte_order = ENDIAN_BIG;
    }

    fclose(fp.file);

    exit(err);
}