]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/jffs2/v2_0/tests/jffs2_3.c
Initial revision
[karo-tx-redboot.git] / packages / fs / jffs2 / v2_0 / tests / jffs2_3.c
1 //==========================================================================
2 //
3 //      jffs2_3.c
4 //
5 //      Test garbage collect on a filesystem
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2005 Andrew Lunn
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 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
40 //
41 // Author(s):           asl
42 // Contributors:        asl
43 // Date:                2005-01-16
44 // Purpose:             Test garbage collect on a filesystem
45 // Description:         This test creates and deletes files in order
46 //                      to test the garbage collection code.
47 //                      
48 //####DESCRIPTIONEND####
49 //
50 //==========================================================================
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <stdlib.h>
57
58 #include <cyg/fileio/fileio.h>
59 #include <cyg/io/flash.h>
60 #include <cyg/crc/crc.h>
61
62 #include <cyg/infra/testcase.h>
63 #include <cyg/infra/diag.h>            // HAL polled output
64
65 //==========================================================================
66
67 #define ITERATIONS 1000000
68 #define NELEM(_x_) (sizeof(_x_)/sizeof(*(_x_)))
69
70 #define SHOW_RESULT( _fn, _res ) \
71 diag_printf("FAIL: " #_fn "() returned %d %s\n", _res, \
72             (unsigned long) _res<0?strerror(errno):"");
73
74 //==========================================================================
75 // file creation, deletion and testing functions
76
77 static void create_file(int i)
78 {
79   cyg_int32 buffer[1020];
80   char name[16];
81   cyg_uint32 j;
82   int fd, err;
83   
84   sprintf(name,"test%07d",i);
85
86   fd = creat(name, S_IRWXU);
87   if (fd == -1) SHOW_RESULT( creat, fd );
88   
89   for (j=1; j < NELEM(buffer); j++) {
90     buffer[j] = rand();
91   }
92   
93   buffer[0] = 0;
94   buffer[0] = cyg_posix_crc32((unsigned char *)buffer, sizeof(buffer));
95   
96   err = write(fd, buffer, sizeof(buffer));
97   if (err == -1) SHOW_RESULT( write, err );
98   
99   err = close(fd);
100   if (err == -1) SHOW_RESULT( close, err );
101 }
102
103 static void delete_file(int i)
104 {
105   char name[16];
106   int err;
107   
108   sprintf(name,"test%07d",i);
109
110   err = unlink(name);
111   if (err == -1) SHOW_RESULT( unlink, err );
112 }
113
114 static void check_file(int i)
115 {
116   char name[16];
117   int err, fd;
118   cyg_int32 buffer[1020];
119   cyg_uint32 crc;
120   
121   sprintf(name,"test%07d",i);
122
123   fd = open(name, O_RDONLY);
124   if (fd == -1) SHOW_RESULT( open, fd );
125   
126   err = read(fd, buffer, sizeof(buffer));
127   if (err == -1) SHOW_RESULT( read, fd );
128   
129   crc = buffer[0];
130   buffer[0] = 0;
131   
132   if (crc != cyg_posix_crc32((unsigned char *)buffer, sizeof(buffer))) {
133     CYG_TEST_FAIL("File corrupt");
134   }
135
136   err = close(fd);
137   if (err == -1) SHOW_RESULT( read, fd );
138 }
139
140
141 //==========================================================================
142 // main
143
144 int main( int argc, char **argv )
145 {
146     int err, iteration;
147     struct mallinfo minfo
148 ;
149     CYG_TEST_INIT();
150
151     // --------------------------------------------------------------
152
153     CYG_TEST_INFO("mount /");    
154     err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
155     if( err < 0 ) SHOW_RESULT( mount, err );    
156     
157     chdir ("/");
158     
159     iteration=0;
160     create_file(iteration);
161     while (iteration < ITERATIONS) {
162       if (!(iteration % 1000)) {
163         minfo = mallinfo();
164         diag_printf("<INFO> Iteration %07d fordblks = %7d\n", 
165                     iteration, minfo.fordblks);
166       }
167       iteration++;
168       create_file(iteration);
169       check_file(iteration-1);
170       delete_file(iteration-1);
171       check_file(iteration);
172     }
173     
174     CYG_TEST_INFO("umount /");
175     err = umount( "/" );
176     if( err < 0 ) SHOW_RESULT( umount, err );    
177     
178     CYG_TEST_PASS_FINISH("jffs2_3");
179 }