Add some tests.
[bertos.git] / app / battfs / battfs_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id: demo.c 18242 2007-10-08 17:35:23Z marco $
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * \brief BattFs Test.
38  */
39
40 #include <cfg/debug.h>
41 #include <fs/battfs.h>
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 FILE *fp;
48 const char test_filename[]="battfs_disk.bin";
49 const char *filename;
50
51 #define PAGE_SIZE 128
52 static bool disk_open(struct BattFsSuper *d)
53 {
54         fp = fopen(filename, "r+b");
55         ASSERT(fp);
56         fseek(fp, 0, SEEK_END);
57         d->page_size = PAGE_SIZE;
58         d->page_count = ftell(fp) / d->page_size;
59         d->page_array = malloc(d->page_count * sizeof(pgcnt_t));
60         TRACEMSG("page_size:%d, page_count:%d\n", d->page_size, d->page_count);
61         return (fp && d->page_array);
62 }
63
64 static size_t disk_page_read(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, void *buf, size_t size)
65 {
66         TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
67         fseek(fp, page * d->page_size + addr, SEEK_SET);
68         return fread(buf, 1, size, fp);
69 }
70
71 static size_t disk_page_write(struct BattFsSuper *d, pgcnt_t page, pgaddr_t addr, const void *buf, size_t size)
72 {
73         TRACEMSG("page:%d, addr:%d, size:%d\n", page, addr, size);
74         fseek(fp, page * d->page_size + addr, SEEK_SET);
75         return fwrite(buf, 1, size, fp);
76 }
77
78 static bool disk_page_erase(struct BattFsSuper *d, pgcnt_t page)
79 {
80         TRACEMSG("page:%d\n", page);
81         fseek(fp, page * d->page_size, SEEK_SET);
82
83         for (int i = 0; i < d->page_size; i++)
84                 if (fputc(0xff, fp) == EOF)
85                         return false;
86         return true;
87 }
88
89 static bool disk_close(struct BattFsSuper *d)
90 {
91         TRACE;
92         free(d->page_array);
93         return (fclose(fp) != EOF);
94 }
95
96 int main(int argc, char *argv[])
97 {
98         if (argc < 2)
99         {
100 /*              FILE *fpt = fopen(test_filename, "w+");
101                 for (int i = 0; i < 32768; i++)
102                         fputc(0xff, fpt);
103                 fclose(fpt);*/
104                 filename = test_filename;
105         }
106         else
107                 filename = argv[1];
108
109         BattFsSuper disk;
110         disk.open = disk_open;
111         disk.read = disk_page_read;
112         disk.write = disk_page_write;
113         disk.erase = disk_page_erase;
114         disk.close = disk_close;
115
116         if (battfs_init(&disk))
117         {
118                 kprintf("page_array:\n");
119                 for (pgcnt_t i = 0; i < disk.page_count; i++)
120                 {
121                         if (!(i % 16))
122                                 kputchar('\n');
123                         kprintf("%04d ", disk.page_array[i]);
124                 }
125                 kputchar('\n');
126
127                 for (pgcnt_t i = 0; i < disk.page_count; i++)
128                 {
129                         if (i < disk.page_count / 2)
130                         {
131                                 if (!battfs_writeTestBlock(&disk, i, i, i, i/3, 0, MARK_PAGE_VALID))
132                                 {
133                                         TRACEMSG("error writing:%d\n", i);
134                                         return 2;
135                                 }
136                         }
137                         else
138                         {
139                                 if (!battfs_writeTestBlock(&disk, i, i, i, i/3, 0, i))
140                                 {
141                                         TRACEMSG("error writing:%d\n", i);
142                                         return 2;
143                                 }
144                         }
145
146                 }
147                 return 0;
148         }
149         else
150                 return 1;
151 }