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 ASSERT(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]);
130 static void test1(BattFsSuper *disk)
132 pgcnt_t ref[PAGE_COUNT];
133 kprintf("Test1: disk new\n");
135 FILE *fpt = fopen(test_filename, "w+");
137 for (int i = 0; i < FILE_SIZE; i++)
140 for (int i = 0; i < PAGE_COUNT; i++)
141 ref[i] = PAGE_COUNT - i - 1;
143 testCheck(disk, ref);
144 kprintf("Test1: passed\n");
147 static void test2(BattFsSuper *disk)
149 pgcnt_t ref[PAGE_COUNT];
150 kprintf("Test2: disk full with 1 contiguos file\n");
153 fp = fopen(test_filename, "w+");
155 for (int i = 0; i < PAGE_COUNT; i++)
157 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
162 testCheck(disk, ref);
163 kprintf("Test2: passed\n");
167 static void test3(BattFsSuper *disk)
169 pgcnt_t ref[PAGE_COUNT];
170 kprintf("Test3: disk half full with 1 contiguos file, rest unformatted\n");
173 fp = fopen(test_filename, "w+");
175 for (int i = 0; i < PAGE_COUNT / 2; i++)
177 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
180 fseek(fp, FILE_SIZE / 2, SEEK_SET);
181 for (int i = FILE_SIZE / 2; i < FILE_SIZE; i++)
185 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
187 ref[i] = PAGE_COUNT + PAGE_COUNT / 2 - i - 1;
191 testCheck(disk, ref);
192 kprintf("Test3: passed\n");
195 static void test4(BattFsSuper *disk)
197 pgcnt_t ref[PAGE_COUNT];
198 kprintf("Test4: disk half full with 1 contiguos file, rest marked free\n");
201 fp = fopen(test_filename, "w+");
203 for (int i = 0; i < PAGE_COUNT / 2; i++)
205 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
208 for (int i = PAGE_COUNT / 2; i < PAGE_COUNT; i++)
210 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
216 testCheck(disk, ref);
217 kprintf("Test4: passed\n");
220 static void test5(BattFsSuper *disk)
222 pgcnt_t ref[PAGE_COUNT];
223 kprintf("Test5: disk 1/3 full with 1 contiguos file, 1/3 marked free, rest unformatted\n");
226 fp = fopen(test_filename, "w+");
228 for (int i = 0; i < FILE_SIZE; i++)
231 for (int i = 0; i < PAGE_COUNT / 3; i++)
233 battfs_writeTestBlock(disk, i, 0, 0, 0, i, MARK_PAGE_VALID);
236 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3); i++)
238 battfs_writeTestBlock(disk, i, 0, 0, 0, i, i);
239 ref[i + PAGE_COUNT / 3 + 1] = i;
243 for (int i = PAGE_COUNT / 3; i < 2 * (PAGE_COUNT / 3) + 1; i++)
244 ref[i] = PAGE_COUNT + PAGE_COUNT / 3 - i - 1;
246 testCheck(disk, ref);
247 kprintf("Test5: passed\n");
250 static void test6(BattFsSuper *disk)
253 kprintf("Test6: 1 file with 1 old seq num, 1 free block\n");
256 fp = fopen(test_filename, "w+");
258 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
259 battfs_writeTestBlock(disk, 1, 0, 0, 0, 1, MARK_PAGE_VALID);
260 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
261 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
269 testCheck(disk, ref);
270 kprintf("Test6: passed\n");
273 static void test7(BattFsSuper *disk)
276 kprintf("Test7: 1 file with 1 old seq num, 1 free block\n");
279 fp = fopen(test_filename, "w+");
281 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, MARK_PAGE_VALID);
282 battfs_writeTestBlock(disk, 1, 0, 1, 0, 1, MARK_PAGE_VALID);
283 battfs_writeTestBlock(disk, 2, 0, 0, 0, 1, MARK_PAGE_VALID);
284 battfs_writeTestBlock(disk, 3, 0, 0, 0, 0, 123);
292 testCheck(disk, ref);
293 kprintf("Test7: passed\n");
296 static void test8(BattFsSuper *disk)
299 kprintf("Test8: 1 file with 1 old seq num, 1 free block\n");
302 fp = fopen(test_filename, "w+");
304 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
305 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
306 battfs_writeTestBlock(disk, 2, 0, 1, 0, 1, MARK_PAGE_VALID);
307 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
316 testCheck(disk, ref);
317 kprintf("Test8: passed\n");
320 static void test9(BattFsSuper *disk)
323 kprintf("Test9: 2 file with old seq num, 2 free block\n");
326 fp = fopen(test_filename, "w+");
328 battfs_writeTestBlock(disk, 0, 0, 0, 0, 0, 1235);
329 battfs_writeTestBlock(disk, 1, 0, 0, 0, 0, MARK_PAGE_VALID);
330 battfs_writeTestBlock(disk, 2, 0, 3, 0, 1, MARK_PAGE_VALID);
331 battfs_writeTestBlock(disk, 3, 0, 0, 0, 1, MARK_PAGE_VALID);
332 battfs_writeTestBlock(disk, 4, 0, 0, 0, 0, 1236);
333 battfs_writeTestBlock(disk, 5, 4, 0, 0, 0, MARK_PAGE_VALID);
334 battfs_writeTestBlock(disk, 6, 4, 1, 0, 1, MARK_PAGE_VALID);
335 battfs_writeTestBlock(disk, 7, 4, 0, 0, 1, MARK_PAGE_VALID);
348 testCheck(disk, ref);
349 kprintf("Test9: passed\n");
352 static void test10(BattFsSuper *disk)
356 kprintf("Test10: open file test, inode 0 and inode 4\n");
358 fp = fopen(test_filename, "w+");
360 #define PAGE_FILL 116
365 battfs_writeTestBlock(disk, 0, INODE, 0, PAGE_FILL, 0, 1235);
366 battfs_writeTestBlock(disk, 1, INODE, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
367 battfs_writeTestBlock(disk, 2, INODE, 3, PAGE_FILL, 1, MARK_PAGE_VALID);
368 battfs_writeTestBlock(disk, 3, INODE, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
369 battfs_writeTestBlock(disk, 4, INODE2, 0, PAGE_FILL, 0, 1236);
370 battfs_writeTestBlock(disk, 5, INODE2, 0, PAGE_FILL, 0, MARK_PAGE_VALID);
371 battfs_writeTestBlock(disk, 6, INODE2, 1, PAGE_FILL, 1, MARK_PAGE_VALID);
372 battfs_writeTestBlock(disk, 7, INODE2, 0, PAGE_FILL, 1, MARK_PAGE_VALID);
376 ASSERT(battfs_init(disk));
378 ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE));
379 ASSERT(fd1.fd.size == PAGE_FILL * 2);
380 ASSERT(fd1.fd.seek_pos == 0);
381 ASSERT(fd1.mode == MODE);
382 ASSERT(fd1.inode == INODE);
383 ASSERT(fd1.start == &disk->page_array[0]);
384 ASSERT(fd1.disk == disk);
385 ASSERT(LIST_HEAD(&disk->file_opened_list) == &fd1.link);
387 ASSERT(battfs_fileopen(disk, &fd2, INODE2, MODE));
388 ASSERT(fd2.fd.size == PAGE_FILL * 2);
389 ASSERT(fd2.fd.seek_pos == 0);
390 ASSERT(fd2.mode == MODE);
391 ASSERT(fd2.inode == INODE2);
392 ASSERT(fd2.start == &disk->page_array[2]);
393 ASSERT(fd2.disk == disk);
394 ASSERT(LIST_HEAD(&disk->file_opened_list)->succ == &fd2.link);
396 ASSERT(kfile_close(&fd1.fd) == 0);
397 ASSERT(kfile_close(&fd2.fd) == 0);
398 ASSERT(LIST_EMPTY(&disk->file_opened_list));
399 ASSERT(battfs_close(disk));
401 kprintf("Test10: passed\n");
412 disk.open = disk_open;
413 disk.read = disk_page_read;
414 disk.write = disk_page_write;
415 disk.erase = disk_page_erase;
416 disk.close = disk_close;
427 kprintf("All test passed!\n");