Icons are changed
[gnuplot] / src / bf_test.c
1 #ifndef lint
2 static char *RCSid() { return RCSid("$Id: bf_test.c,v 1.9.4.1 2007/10/02 18:20:30 sfeam Exp $"); }
3 #endif
4
5
6 /*
7  * Test routines for binary files
8  * cc bf_test.c -o bf_test binary_files.o -lm
9  *
10  * Copyright (c) 1992 Robert K. Cunningham, MIT Lincoln Laboratory
11  *
12  */
13
14 /* Note that this file is not compiled into gnuplot, and so
15  * its more-restrictive copyright need not apply to gnuplot
16  * as a whole. (I think.)
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #define GPFAR /**/
24
25 #include "syscfg.h"
26 #include "stdfn.h"
27 #include "binary.h"
28
29 /* we declare these here instead of including more header files */
30 void int_error __PROTO((int, const char *));
31 void FreeHelp __PROTO((void));
32
33 /* static functions */
34 static float function __PROTO((int p, double x, double y));
35
36
37 typedef struct {
38   float xmin, xmax;
39   float ymin, ymax;
40 } range;
41
42 #define NUM_PLOTS 2
43 static range TheRange[] = {{-3,3,-2,2},
44                            {-3,3,-3,3},
45                            {-3,3,-3,3}}; /* Sampling rate causes this to go from -3:6*/
46
47 /*---- Stubs to make this work without including huge libraries ----*/
48 void
49 int_error(int dummy, const char *error_text)
50 {
51     (void) dummy;               /* avoid -Wunused warning */
52     fprintf(stderr, "Fatal error..\n%s\n...now exiting to system ...\n",
53             error_text);
54     exit(EXIT_FAILURE);
55 }
56
57
58 void
59 FreeHelp()
60 {
61 }
62
63 /*---- End of stubs ----*/
64
65
66 static float
67 function(int p, double x, double y)
68 {
69     float t = 0;                        /* HBB 990828: initialize */
70
71     switch (p) {
72     case 0:
73         t = 1.0 / (x * x + y * y + 1.0);
74         break;
75     case 1:
76         t = sin(x * x + y * y) / (x * x + y * y);
77         if (t > 1.0)
78             t = 1.0;
79         break;
80     case 2:
81         t = sin(x * x + y * y) / (x * x + y * y);
82         /* sinc modulated sinc */
83         t *= sin(4. * (x * x + y * y)) / (4. * (x * x + y * y));
84         if (t > 1.0)
85             t = 1.0;
86         break;
87     default:
88         fprintf(stderr, "Unknown function\n");
89         break;
90     }
91     return t;
92 }
93
94 #define ISOSAMPLES 5.0
95
96 int
97 main(void)
98 {
99     int plot;
100     int i, j;
101     float x, y;
102     float *rt, *ct;
103     float **m;
104     int xsize, ysize;
105     char buf[256];
106     FILE *fout;
107 /*  Create a few standard test interfaces */
108
109     for (plot = 0; plot < NUM_PLOTS; plot++) {
110         xsize = (TheRange[plot].xmax - TheRange[plot].xmin) * ISOSAMPLES + 1;
111         ysize = (TheRange[plot].ymax - TheRange[plot].ymin) * ISOSAMPLES + 1;
112
113         rt = alloc_vector(0, xsize - 1);
114         ct = alloc_vector(0, ysize - 1);
115         m = matrix(0, xsize - 1, 0, ysize - 1);
116
117         for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
118             ct[j] = y;
119         }
120
121         for (x = TheRange[plot].xmin, i = 0; i < xsize; i++, x += 1.0 / (double) ISOSAMPLES) {
122             rt[i] = x;
123             for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
124                 m[i][j] = function(plot, x, y);
125             }
126         }
127
128         sprintf(buf, "binary%d", plot + 1);
129         if (!(fout = fopen(buf, "wb")))
130             int_error(0, "Could not open file");
131         else {
132             fwrite_matrix(fout, m, 0, xsize - 1, 0, ysize - 1, rt, ct);
133         }
134         free_vector(rt, 0);
135         free_vector(ct, 0);
136         free_matrix(m, 0, xsize - 1, 0);
137     }
138
139     /* Show that it's ok to vary sampling rate, as long as x1<x2, y1<y2... */
140     xsize = (TheRange[plot].xmax - TheRange[plot].xmin) * ISOSAMPLES + 1;
141     ysize = (TheRange[plot].ymax - TheRange[plot].ymin) * ISOSAMPLES + 1;
142
143     rt = alloc_vector(0, xsize - 1);
144     ct = alloc_vector(0, ysize - 1);
145     m = matrix(0, xsize - 1, 0, ysize - 1);
146
147     for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
148         ct[j] = y > 0 ? 2 * y : y;
149     }
150     for (x = TheRange[plot].xmin, i = 0; i < xsize; i++, x += 1.0 / (double) ISOSAMPLES) {
151         rt[i] = x > 0 ? 2 * x : x;
152         for (y = TheRange[plot].ymin, j = 0; j < ysize; j++, y += 1.0 / (double) ISOSAMPLES) {
153             m[i][j] = function(plot, x, y);
154         }
155     }
156
157     sprintf(buf, "binary%d", plot + 1);
158     if (!(fout = fopen(buf, "wb")))
159         int_error(0, "Could not open file");
160     else {
161         fwrite_matrix(fout, m, 0, xsize - 1, 0, ysize - 1, rt, ct);
162     }
163     free_vector(rt, 0);
164     free_vector(ct, 0);
165     free_matrix(m, 0, xsize - 1, 0);
166
167     return EXIT_SUCCESS;
168 }