]> www.wagner.pp.ru Git - oss/fgis.git/blob - lib/test/test_epplib.c
First checked in version
[oss/fgis.git] / lib / test / test_epplib.c
1 /*
2  * Test program for epp library. It reads file, given as argument
3  * (while not stdin? to simplify running under debugger)
4  * and performs specified operations
5  * After each operation some fields of epp structure are printed
6  * Availiable operations
7  * read file - open epp file for reading
8  * create file width height offsite bits - create file (write-only mode)
9  * load file - opens file for random read-write access
10  * save - saves loaded file
11  * reset - resets write-only file to read-only mode 
12  * get col row - return value
13  * put col row value - change value of pixel
14  * line col1 col2 row value - change value of several adjanced pixels
15  * # text    - comment
16  * NOTE: space after hash mark is mandantory
17  */
18 #include <epp.h>
19 #include <string.h>
20 #include <stdlib.h>
21 char filename[1024]; /* name of currently used epp-file */
22 FILE *f; /*file with test command. */
23 char *error_code[]={"OK","ME_POINT_OUTSIDE","ME_INVALID_MODE",
24   "ME_READ_ERROR","ME_WRITE_ERROR","ME_INVALID_PUT","ME_OUT_OF_MEMORY",
25   "ME_ACCESS_DENIED","ME_NO_FILE","ME_INVALID_FILE","ME_CREATE_ERROR",NULL};
26 /* prints value of global map_error varable. If it is 
27    related with file-system error, also
28    prints system error description */
29 void print_result()
30 {
31   if (map_error>ME_OUT_OF_MEMORY) {
32      perror(error_code[map_error]);
33   } else {
34      printf("%s\n",error_code[map_error]);
35   }
36     map_error=0;
37 }
38 /*
39  * Prints verbosely mode field of EPP structure.
40  * Note: if it prints something hexadecimal, it should be considered
41  * error in library!
42  */
43 char *epp_flags[]={"MAP_INPUT","MAP_OUTPUT","MAP_LOADED","MAP_CACHED",
44    NULL,NULL,NULL,"MAP_MODIFIED",
45    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
46    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
47    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
48 };
49 /*
50  * Prints verbosely mode field of EPP structure.
51  * Note: if it prints something hexadecimal, it should be considered
52  * error in library!
53  */
54 void print_flags(EPP *epp)
55 {  int i, mask;
56    char separator=':';
57    printf("EPP mode");
58    for(i=0,mask=1;i<32;i++,mask<<=1) {
59        if (epp->mode&mask) { 
60           if (!epp_flags[i]) {
61              printf("%c 0x%X",separator,mask);
62           } else {
63              printf("%c %s",separator,epp_flags[i]);
64           }
65           separator=',';
66        }
67    }
68    printf("\n");
69 }  
70 /*************************************************************************/
71 /* individual test procedures begin here                                 */
72 /************************************************************************/
73
74 /*
75  * testing epp_put 
76  */
77 void test_put(EPP *epp) 
78 {   int row,col,value;
79     if (!epp) {
80        fprintf(stderr,"Invalid test case - put before open");
81        exit(2);
82     }
83     if (fscanf(f,"%d %d %d",&col,&row,&value)!=3) {
84        fprintf(stderr,"Invalid line in input file\n");
85        exit(2);
86     }
87     printf("epp_put(%d,%d,%d):",col,row,value);
88     epp_put(epp, col, row, value);
89     printf("max=%d,min=%d ",epp->max, epp->min);
90 }
91 /* 
92  * testing epp_putline
93  */
94 void test_putline(EPP *epp)
95 {  int row,col,col2,value;
96    if (!epp) {
97        fprintf(stderr,"Invalid test case - putline before open");
98        exit(2);
99    }
100     if (fscanf(f,"%d %d %d %d",&col,&col2,&row,&value)!=4) {
101        fprintf(stderr,"Invalid line in input file\n"); 
102        exit(2);
103     }
104     printf("epp_putline(%d,%d,%d,%d):",col,col2,row,value); 
105      epp_putline(epp,col,col2,row,value); 
106     printf("max=%d,min=%d ",epp->max, epp->min);
107 }
108 /*
109  * testing epp_get
110  */
111 void test_get(EPP *epp)
112 {  int row,col;
113    if (!epp) {
114        fprintf(stderr,"Invalid test case - get before open");
115        exit(2);
116    }
117    if (fscanf(f,"%d %d",&row,&col)!=2) {
118        fprintf(stderr,"Invalid line in input file\n"); 
119        exit(2);
120    }
121    printf("epp_get(%d,%d)=%d ",col,row,epp_get(epp,col,row)); 
122 }
123 /*
124  * Testing save_epp
125  *
126  */
127 void test_save(EPP* epp) 
128 {
129    if (!epp) {
130        fprintf(stderr,"Invalid test case - save before load");
131        exit(2);
132    }
133   printf("Saving %s...",filename);
134   save_epp(epp);
135   print_result();
136   print_flags(epp);
137 }
138 /*
139  * testing open_epp
140  *
141  */
142
143 EPP *test_read(EPP *epp)
144 {
145   if (epp) close_epp(epp);
146   if (fscanf(f,"%s",filename)!=1) {
147     fprintf(stderr,"Missing filename for read command\n");
148     exit(2);
149   }
150   printf("Opening %s in read-only mode..",filename);
151   epp=open_epp(filename);
152   print_result();
153   if (!epp) {
154     printf("File wasn't opened.\n");
155     return NULL;
156   }
157   printf("%s header information:\n  bits per cell:%d\n  size:%dx%d\n  "
158      "offsite %d\n  values from: %d to %d\n  ",filename,epp->kind,
159    epp->lc-epp->fc,epp->lr-epp->fr,epp->offsite,epp->min,epp->max);
160   print_flags(epp);
161   return epp;
162 }
163 /*
164  *
165  * testing load_epp
166  *
167  */
168 EPP *test_load(EPP *epp)
169 {
170   if (epp) close_epp(epp);
171   if (fscanf(f,"%s",filename)!=1) {
172     fprintf(stderr,"Missing filename for load command\n");
173     exit(2);
174   }
175   printf("Loading %s...",filename);
176   epp=load_epp(filename);
177   print_result();
178   if (!epp) {
179     printf("File wasn't loaded.\n");
180     return NULL;
181   }
182   printf("%s header information:\n  bits per cell:%d\n  size:%dx%d\n  "
183      "offsite %d\n  values from: %d to %d\n  ",filename,epp->kind,
184    epp->lc-epp->fc,epp->lr-epp->fr,epp->offsite,epp->min,epp->max);
185   print_flags(epp);
186   return epp;
187 }
188 /*
189  *
190  * Test reset. Performs reset operation
191  *
192  */
193 void test_reset(EPP *epp)
194 {
195    if (!epp) {
196        fprintf(stderr,"Invalid test case - reset before read");
197        exit(2);
198    }
199   printf("Resetting file.\n Current line was %d\n Flags was:",
200        epp->currentline);
201   print_flags(epp);
202   
203   reset_epp(epp);
204   print_result();
205   printf("Current line now is:%d\n",epp->currentline);
206   printf("%s header information:\n  bits per cell:%d\n  size:%dx%d\n  "
207      "offsite %d\n  values from: %d to %d\n  ",filename,epp->kind,
208    epp->lc-epp->fc,epp->lr-epp->fr,epp->offsite,epp->min,epp->max);
209   print_flags(epp);
210 }
211 /*
212  * testing creat_epp;
213  *
214  */
215 EPP *test_create(EPP *epp)
216 { int width,height,bits,offsite;
217   if (epp) close_epp(epp);
218   if (fscanf(f,"%s %d %d %d %d",filename,&width,&height,&offsite,&bits)!=5) {
219     fprintf(stderr,"Wrong arguments for create command\n");
220     exit(2);
221   }
222   Create16bit= (bits!=8);
223   printf("Creating %d-bit file %s...",Create16bit?16:8,filename); 
224   epp=creat_epp(filename,1,1,width,height,0.5,0.5,width+0.5,height+0.5,
225        100,0,offsite);
226   print_result();
227   if (!epp) {
228     fprintf(stderr,"File is not created\n");
229     return NULL;
230   }
231   printf("%s header information:\n  bits per cell:%d\n  size:%dx%d\n  "
232      "offsite %d\n  values from: %d to %d\n  ",filename,epp->kind,
233    epp->lc-epp->fc,epp->lr-epp->fr,epp->offsite,epp->min,epp->max);
234   print_flags(epp);
235   return epp;
236 }
237 int main (int argc, char *argv[])
238 { EPP *epp=NULL;
239   char command[256];
240   if (argc!=2) {
241     fprintf(stderr,"Usage: test_load test-file");
242     exit(2);
243   }
244   f=fopen(argv[1],"r");
245   if (!f) {
246      perror(argv[0]);
247      exit(2);
248   }
249   while (!feof(f)) {
250     /* skip empty lines */
251     if (fscanf(f,"%s",command)!=1) { 
252        continue;
253     }
254     if (!strcmp(command,"put")) {
255         test_put(epp); 
256     } else if (!strcmp(command,"get")) {
257         test_get(epp);
258     } else if (!strcmp(command,"reset")) {
259         test_reset(epp);
260     } else if (!strcmp(command,"save")) {
261         test_save(epp);
262     } else if (!strcmp(command,"load"))  {
263         epp=test_load(epp);
264     } else if (!strcmp(command,"read")) {
265         epp=test_read(epp);
266     } else if (!strcmp(command,"create")) {
267         epp=test_create(epp);
268     } else if (!strcmp(command,"line")) {
269         test_putline(epp);  
270     } else if (!strcmp(command,"#")) {
271        char *newline;
272        fgets(command,255,f);
273        newline=strchr(command,'\n');
274        if (newline) *newline=0; 
275        printf("*** %s ***\n",command);
276        continue;
277      } else { 
278        fprintf(stderr,"Unknown command:%s",command);
279        continue;
280      }
281     print_result();
282   }
283   fclose(f);
284   if (epp) close_epp(epp);
285   return 0;
286 }