]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/language/c/libc/stdio/v2_0/tests/fileio.c
Initial revision
[karo-tx-redboot.git] / packages / language / c / libc / stdio / v2_0 / tests / fileio.c
1 //=================================================================
2 //
3 //        fileio.c
4 //
5 //        Testcase for C library file I/O functions
6 //
7 //=================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2003 Gary Thomas
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //=================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):       Gary Thomas
44 // Contributors:    
45 // Date:            2003-03-06
46 // Description:     Contains testcode for C library file I/O functions
47 //
48 //
49 //####DESCRIPTIONEND####
50
51 // CONFIGURATION
52
53 #include <pkgconf/libc_stdio.h>   // Configuration header
54
55 // INCLUDES
56
57 #include <stdio.h>                 // All the stdio functions
58 #include <errno.h>                 // errno
59 #include <cyg/infra/testcase.h>    // Testcase API
60 #include <unistd.h>
61 #include <cyg/fileio/fileio.h>
62
63 #define NUM_TEST_LINES 100
64 #define TEST_DIR "/"
65 #define TEST_FILE "test.file"
66
67 static char errmsg[256];
68
69 // FUNCTIONS
70
71 static void
72 test( CYG_ADDRWORD data )
73 {
74     int err, i, lines, len, total_len, expected_len;
75     FILE *fp;
76     char buf[512], *cp;
77
78     err = mount("", TEST_DIR, "ramfs");
79     if (err < 0) {
80         CYG_TEST_FAIL_FINISH("Can't mount '/' file system");
81     }
82
83     err = chdir(TEST_DIR);
84     if (err < 0) {
85         CYG_TEST_FAIL_FINISH("Can't chdir('/')");
86     }
87
88     fp = fopen(TEST_FILE, "w");
89     if (fp == (FILE *)NULL) {
90         CYG_TEST_FAIL_FINISH("Can't create test file");
91     }
92
93     for (i = 0;  i < NUM_TEST_LINES;  i++) {
94         len = fprintf(fp, "This is line: %4d\n", i+1);
95         if (len != 19) {
96             sprintf(errmsg, "Error writing data - line: %d, len: %d\n", i+1, len);
97             CYG_TEST_FAIL_FINISH(errmsg);
98         }
99     }
100
101     err = fclose(fp);
102     if (err) {
103         CYG_TEST_FAIL_FINISH("Error closingfile");
104     }
105
106     fp = fopen(TEST_FILE, "r");
107     if (fp == (FILE *)NULL) {
108         CYG_TEST_FAIL_FINISH("Can't open test file");
109     }
110
111     lines = 0;
112     total_len = 0;
113     while ((cp = fgets(buf, sizeof(buf), fp)) != NULL) {
114         lines++;
115         total_len += strlen(buf);
116     }
117     sprintf(errmsg, "Read %d lines, %d bytes", lines, total_len);
118     CYG_TEST_INFO(errmsg);
119     expected_len = total_len;
120
121     if (lines != NUM_TEST_LINES) {        
122         sprintf(errmsg, "Read %d lines, not %d", lines, NUM_TEST_LINES);
123         CYG_TEST_FAIL_FINISH(errmsg);
124     }
125
126     if (ferror(fp)) {
127         CYG_TEST_FAIL_FINISH("ferror() set");
128     }
129
130     if (!feof(fp)) {
131         CYG_TEST_FAIL_FINISH("feof() not set");
132     }
133
134     err = fclose(fp);
135     if (err) {
136         CYG_TEST_FAIL_FINISH("Error closingfile");
137     }
138
139     fp = fopen(TEST_FILE, "r");
140     if (fp == (FILE *)NULL) {
141         CYG_TEST_FAIL_FINISH("Can't open test file");
142     }
143
144     // This tests whether or not it is possible to detect EOF separate
145     // from an error and recover from it.  The buffer size is [probably]
146     // such that fread() will not be able to read the entire file in
147     // 512 byte chunks.
148     lines = 0;
149     total_len = 0;
150     while ((len = fread(buf, sizeof(buf), 1, fp)) == 1) {
151         lines++;
152         total_len += sizeof(buf);
153     }
154     sprintf(errmsg, "Read %d chunks, %d bytes, err: %d, eof: %d", 
155             lines, total_len, ferror(fp), feof(fp));
156     CYG_TEST_INFO(errmsg);
157
158     if (ferror(fp)) {
159         CYG_TEST_FAIL_FINISH("ferror() set");
160     }
161
162     if (!feof(fp)) {
163         CYG_TEST_FAIL_FINISH("feof() not set");
164     }
165
166     if (expected_len != total_len) {
167         if (total_len > expected_len) {
168             CYG_TEST_FAIL_FINISH("Too many characters read");
169         }
170         // There should still be more characters left to read, 
171         // but less than sizeof(buf)
172         clearerr(fp);
173         rewind(fp);
174         fseek(fp, total_len, SEEK_SET);
175         len = fread(buf, 1, sizeof(buf), fp);
176         if (len != (expected_len - total_len)) {
177             sprintf(errmsg, "Wrong number of residual bytes - read %d, should be %d", 
178                     len, expected_len-total_len);
179             CYG_TEST_FAIL_FINISH(errmsg);
180         }
181     }
182
183     err = fclose(fp);
184     if (err) {
185         CYG_TEST_FAIL_FINISH("Error closingfile");
186     }
187
188     CYG_TEST_PASS("Stdio file I/O tests completed");
189     CYG_TEST_FINISH("Finished tests from testcase " __FILE__
190                     " for C library stdio file I/O functions");
191 } // test()
192
193 int
194 main(int argc, char *argv[])
195 {
196     CYG_TEST_INIT();
197
198     CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C "
199                   "library stdio file I/O functions");
200
201     test(0);
202
203     return 0;
204
205 } // main()
206
207 // EOF fileio.c