1 //=================================================================
5 // Testcase for C library file I/O functions
7 //=================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2003 Gary Thomas
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.
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
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.
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.
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.
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####
43 // Author(s): Gary Thomas
46 // Description: Contains testcode for C library file I/O functions
49 //####DESCRIPTIONEND####
53 #include <pkgconf/libc_stdio.h> // Configuration header
57 #include <stdio.h> // All the stdio functions
58 #include <errno.h> // errno
59 #include <cyg/infra/testcase.h> // Testcase API
61 #include <cyg/fileio/fileio.h>
63 #define NUM_TEST_LINES 100
65 #define TEST_FILE "test.file"
67 static char errmsg[256];
72 test( CYG_ADDRWORD data )
74 int err, i, lines, len, total_len, expected_len;
78 err = mount("", TEST_DIR, "ramfs");
80 CYG_TEST_FAIL_FINISH("Can't mount '/' file system");
83 err = chdir(TEST_DIR);
85 CYG_TEST_FAIL_FINISH("Can't chdir('/')");
88 fp = fopen(TEST_FILE, "w");
89 if (fp == (FILE *)NULL) {
90 CYG_TEST_FAIL_FINISH("Can't create test file");
93 for (i = 0; i < NUM_TEST_LINES; i++) {
94 len = fprintf(fp, "This is line: %4d\n", i+1);
96 sprintf(errmsg, "Error writing data - line: %d, len: %d\n", i+1, len);
97 CYG_TEST_FAIL_FINISH(errmsg);
103 CYG_TEST_FAIL_FINISH("Error closingfile");
106 fp = fopen(TEST_FILE, "r");
107 if (fp == (FILE *)NULL) {
108 CYG_TEST_FAIL_FINISH("Can't open test file");
113 while ((cp = fgets(buf, sizeof(buf), fp)) != NULL) {
115 total_len += strlen(buf);
117 sprintf(errmsg, "Read %d lines, %d bytes", lines, total_len);
118 CYG_TEST_INFO(errmsg);
119 expected_len = total_len;
121 if (lines != NUM_TEST_LINES) {
122 sprintf(errmsg, "Read %d lines, not %d", lines, NUM_TEST_LINES);
123 CYG_TEST_FAIL_FINISH(errmsg);
127 CYG_TEST_FAIL_FINISH("ferror() set");
131 CYG_TEST_FAIL_FINISH("feof() not set");
136 CYG_TEST_FAIL_FINISH("Error closingfile");
139 fp = fopen(TEST_FILE, "r");
140 if (fp == (FILE *)NULL) {
141 CYG_TEST_FAIL_FINISH("Can't open test file");
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
150 while ((len = fread(buf, sizeof(buf), 1, fp)) == 1) {
152 total_len += sizeof(buf);
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);
159 CYG_TEST_FAIL_FINISH("ferror() set");
163 CYG_TEST_FAIL_FINISH("feof() not set");
166 if (expected_len != total_len) {
167 if (total_len > expected_len) {
168 CYG_TEST_FAIL_FINISH("Too many characters read");
170 // There should still be more characters left to read,
171 // but less than sizeof(buf)
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);
185 CYG_TEST_FAIL_FINISH("Error closingfile");
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");
194 main(int argc, char *argv[])
198 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C "
199 "library stdio file I/O functions");