]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/ram/v2_0/tests/ramfs2.c
Initial revision
[karo-tx-redboot.git] / packages / fs / ram / v2_0 / tests / ramfs2.c
1 //==========================================================================
2 //
3 //      ramfs2.c
4 //
5 //      Test fseek on a filesystem
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2004 Andrew Lunn
12 // Copyright (C) 2004 eCosCentric Limited
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 //
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
43 //
44 // Author(s):           asl
45 // Contributors:        asl
46 // Date:                2004-03-29
47 // Purpose:             Test fseek on a filesystem
48 // Description:         This test uses the ramfs to check out the fseek
49 //                      operation on a filesystem.
50 //                      
51 //                      
52 //                      
53 //                      
54 //                      
55 //              
56 //
57 //####DESCRIPTIONEND####
58 //
59 //==========================================================================
60 #include <stdio.h>
61 #include <unistd.h>
62 #include <fcntl.h>
63 #include <errno.h>
64 #include <string.h>
65
66 #include <cyg/fileio/fileio.h>
67
68 #include <cyg/infra/testcase.h>
69 #include <cyg/infra/diag.h>            // HAL polled output
70 //==========================================================================
71
72 #define SHOW_RESULT( _fn, _res ) \
73 diag_printf("FAIL: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
74
75 //==========================================================================
76
77 char buf[1024];
78 char buf1[1024];
79
80 //==========================================================================
81 // main
82
83 int main( int argc, char **argv )
84 {
85     int err;
86     FILE *stream;
87     long pos;
88     int i;
89     
90     CYG_TEST_INIT();
91
92     // --------------------------------------------------------------
93
94     CYG_TEST_INFO("mount /");    
95     err = mount( "", "/", "ramfs" );
96     if( err < 0 ) SHOW_RESULT( mount, err );    
97     
98     CYG_TEST_INFO("creating /fseek");    
99     stream = fopen("/fseek","w+");
100     if (!stream) {
101       diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno));
102       CYG_TEST_FINISH("done");          \
103     }
104
105     /* Write a buffer full of cyclic numbers */
106     for (i = 0; i < sizeof(buf); i++) {
107       buf[i] = i % 256;
108     }
109     
110     CYG_TEST_INFO("writing test pattern");    
111     err=fwrite(buf,sizeof(buf), 1, stream);
112     if ( err < 0 ) SHOW_RESULT( fwrite, err );
113     
114     /* The current position should be the same size as the buffer */
115     pos = ftell(stream);
116     
117     if (pos < 0) SHOW_RESULT( ftell, pos );
118     if (pos != sizeof(buf))
119       diag_printf("<FAIL>: ftell is not telling the truth.");
120     
121     CYG_TEST_INFO("fseek()ing to beginning and writing");    
122
123     /* Seek back to the beginning of the file */
124     err = fseek(stream, 0, SEEK_SET);
125     if ( err < 0 ) SHOW_RESULT( fwrite, err );
126
127     pos = ftell(stream);
128     
129     if (pos < 0) SHOW_RESULT( ftell, pos );
130     if (pos != 0) CYG_TEST_FAIL("ftell is not telling the truth");
131
132     /* Write 4 zeros to the beginning of the file */
133     for (i = 0; i < 4; i++) {
134       buf[i] = 0;
135     }
136
137     err = fwrite(buf, 4, 1, stream);
138     if ( err < 0 ) SHOW_RESULT( fwrite, err );
139
140     /* Check the pointer is at 4 */
141     pos = ftell(stream);
142     
143     if (pos < 0) SHOW_RESULT( ftell, pos );
144     if (pos != 4)  CYG_TEST_FAIL("ftell is not telling the truth");
145
146     CYG_TEST_INFO("closing file");
147     
148     /* Close the file, open it up again and read it back */
149     err = fclose(stream);
150     if (err != 0) SHOW_RESULT( fclose, err );
151
152     CYG_TEST_INFO("open file /fseek");
153     stream = fopen("/fseek", "r+");
154     if (!stream) {
155       diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
156     }
157
158     err = fread(buf1,sizeof(buf1),1, stream);
159     if (err != 1) SHOW_RESULT( fread, err );
160     
161     CYG_TEST_INFO("Comparing contents");
162     if (memcmp(buf, buf1, sizeof(buf1))) {
163       CYG_TEST_FAIL("File contents inconsistent");
164     }
165     
166     err = fclose(stream);
167     if (err != 0) SHOW_RESULT( fclose, err );
168
169     CYG_TEST_INFO("umount /");    
170     err = umount( "/" );
171     if( err < 0 ) SHOW_RESULT( umount, err );    
172     
173     CYG_TEST_PASS_FINISH("ramfs2");
174 }