4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
33 * \version $Id: demo.c 18242 2007-10-08 17:35:23Z marco $
35 * \author Francesco Sacchi <batt@develer.com>
40 #include <cfg/debug.h>
41 #include <fs/battfs.h>
47 #define FILE_SIZE 32768
49 #define PAGE_COUNT FILE_SIZE / PAGE_SIZE
52 const char test_filename[]="battfs_disk.bin";
55 static bool disk_open(struct BattFsSuper *d)
57 fp = fopen(test_filename, "r+b");
59 fseek(fp, 0, SEEK_END);
60 d->page_size = PAGE_SIZE;
61 d->page_count = ftell(fp) / d->page_size;
62 d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
63 //TRACEMSG("page_size:%d, page_count:%d\n", d->page_size, d->page_count);
64 return (fp && d->page_array);
67 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
69 //TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
70 fseek(fp, page * d->page_size + addr, SEEK_SET);
71 return fread(buf, 1, size, fp);
74 static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t size)
76 //TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
77 fseek(fp, page * d->page_size + addr, SEEK_SET);
78 return fwrite(buf, 1, size, fp);
81 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
83 //TRACEMSG("page:%d\n", page);
84 fseek(fp, page * d->page_size, SEEK_SET);
86 for (int i = 0; i < d->page_size; i++)
87 if (fputc(0xff, fp) == EOF)
92 static bool disk_close(struct BattFsSuper *d)
96 return (fclose(fp) != EOF);
99 static void testCheck(BattFsSuper *disk, pgcnt_t *reference)
101 if (battfs_init(disk))
103 for (int i = 0; i < disk->page_count; i++)
105 if (disk->page_array[i] != reference[i])
107 kprintf("Error at addr %d: page_array read", i);
108 for (pgcnt_t i = 0; i < disk->page_count; i++)
112 kprintf("%04d ", disk->page_array[i]);
115 kprintf("Expected:");
116 for (pgcnt_t i = 0; i < disk->page_count; i++)
120 kprintf("%04d ", reference[i]);
131 kprintf("Error in battfs_init\n");
136 static void test1(BattFsSuper *disk)
138 pgcnt_t ref[PAGE_COUNT];
139 kprintf("Test1: disk new\n");
141 FILE *fpt = fopen(test_filename, "w+");
143 for (int i = 0; i < FILE_SIZE; i++)
146 for (int i = 0; i < PAGE_COUNT; i++)
147 ref[i] = PAGE_COUNT - i - 1;
149 testCheck(disk, ref);
150 kprintf("Test1: passed\n");
153 static void test2(BattFsSuper *disk)
155 pgcnt_t ref[PAGE_COUNT];
156 kprintf("Test2: disk full with 1 contiguos file\n");
159 fp = fopen(test_filename, "w+");
161 for (int i = 0; i < PAGE_COUNT; i++)
163 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
168 testCheck(disk, ref);
169 kprintf("Test2: passed\n");
173 static void test3(BattFsSuper *disk)
175 pgcnt_t ref[PAGE_COUNT];
176 kprintf("Test3: disk half full with 1 contiguos file, rest unformatted\n");
179 fp = fopen(test_filename, "w+");
181 for (int i = 0; i < PAGE_COUNT / 2; i++)
183 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
186 fseek(fp, FILE_SIZE / 2, SEEK_SET);
187 for (int i = FILE_SIZE / 2; i < FILE_SIZE; i++)
191 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
193 ref[i] = PAGE_COUNT + PAGE_COUNT / 2 - i - 1;
197 testCheck(disk, ref);
198 kprintf("Test3: passed\n");
201 static void test4(BattFsSuper *disk)
203 pgcnt_t ref[PAGE_COUNT];
204 kprintf("Test4: disk half full with 1 contiguos file, rest marked free\n");
207 fp = fopen(test_filename, "w+");
209 for (int i = 0; i < PAGE_COUNT / 2; i++)
211 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
214 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
216 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
222 testCheck(disk, ref);
223 kprintf("Test4: passed\n");
226 static void test5(BattFsSuper *disk)
228 pgcnt_t ref[PAGE_COUNT];
229 kprintf("Test5: disk 1/3 full with 1 contiguos file, 1/3 marked free, rest unformatted\n");
232 fp = fopen(test_filename, "w+");
234 for (int i = 0; i < FILE_SIZE; i++)
237 for (int i = 0; i < PAGE_COUNT / 3; i++)
239 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
242 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
244 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
245 ref[i + PAGE_COUNT / 3 + 1] = i;
249 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3) + 1; i++)
250 ref[i] = PAGE_COUNT + PAGE_COUNT / 3 - i - 1;
252 testCheck(disk, ref);
253 kprintf("Test5: passed\n");
256 static void test6(BattFsSuper *disk)
259 kprintf("Test6: 1 file with 1 old seq num, 1 free block\n");
262 fp = fopen(test_filename, "w+");
264 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
265 battfs_writeTestBlock(disk, 1, 0, 0, 0, 1, MARK_PAGE_VALID);
266 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
267 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
275 testCheck(disk, ref);
276 kprintf("Test6: passed\n");
279 static void test7(BattFsSuper *disk)
282 kprintf("Test7: 1 file with 1 old seq num, 1 free block\n");
285 fp = fopen(test_filename, "w+");
287 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
288 battfs_writeTestBlock(disk, 1, 0, 1, 0, 1, MARK_PAGE_VALID);
289 battfs_writeTestBlock(disk, 2, 0, 0, 0, 1, MARK_PAGE_VALID);
290 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
298 testCheck(disk, ref);
299 kprintf("Test7: passed\n");
302 static void test8(BattFsSuper *disk)
305 kprintf("Test8: 1 file with 1 old seq num, 1 free block\n");
308 fp = fopen(test_filename, "w+");
310 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
311 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
312 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
313 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
322 testCheck(disk, ref);
323 kprintf("Test8: passed\n");
326 static void test9(BattFsSuper *disk)
329 kprintf("Test9: 2 file with old seq num, 2 free block\n");
332 fp = fopen(test_filename, "w+");
334 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
335 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
336 battfs_writeTestBlock(disk, 2, 0, 3, 0, 1, MARK_PAGE_VALID);
337 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
338 battfs_writeTestBlock(disk, 4, 0, 0, 0, 0, 1236);
339 battfs_writeTestBlock(disk, 5, 4, 0, 0, 0, MARK_PAGE_VALID);
340 battfs_writeTestBlock(disk, 6, 4, 1, 0, 1, MARK_PAGE_VALID);
341 battfs_writeTestBlock(disk, 7, 4, 0, 0, 1, MARK_PAGE_VALID);
354 testCheck(disk, ref);
355 kprintf("Test9: passed\n");
365 disk.open = disk_open;
366 disk.read = disk_page_read;
367 disk.write = disk_page_write;
368 disk.erase = disk_page_erase;
369 disk.close = disk_close;
379 kprintf("All test passed!\n");