Add test for fatfs.
[bertos.git] / bertos / fs / fat_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 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief FatFs test.
34  *
35  * \version $Id$
36  * \author Luca Ottaviano <lottaviano@develer.com>
37  *
38  * $test$: cp bertos/cfg/cfg_fat.h $cfgdir/
39  * $test$: echo  "#undef CONFIG_FAT_USE_MKFS" >> $cfgdir/cfg_fat.h
40  * $test$: echo "#define CONFIG_FAT_USE_MKFS 1" >> $cfgdir/cfg_fat.h
41  *
42  */
43
44 #include "fat.h"
45
46 #include "fatfs/src/ff.h"
47 #include "fatfs/src/diskio.h"
48
49 #include <cfg/test.h>
50
51 /* avoid compiler warnings... */
52 int fatfile_testSetup(void);
53 int fatfile_testTearDown(void);
54 int fatfile_testRun(void);
55
56 static FATFS file_system;
57
58 int fatfile_testSetup(void)
59 {
60         FRESULT err;
61         err = f_mount(0, &file_system);
62         ASSERT(err == FR_OK);
63
64         err = f_mkfs(0, 0, 512);
65         ASSERT(err == FR_OK);
66         return 0;
67 }
68
69 int fatfile_testTearDown(void)
70 {
71         FRESULT err;
72         err = f_mount(0, 0);
73         ASSERT(err == FR_OK);
74         return 0;
75 }
76
77 int fatfile_testRun(void)
78 {
79         FRESULT fat_err;
80         FatFile file_handler;
81         const int SIZE = 10;
82         int write[SIZE], read[SIZE];
83
84         fat_err = fatfile_open(&file_handler, "foo.txt", FA_WRITE | FA_CREATE_ALWAYS);
85         ASSERT(fat_err == FR_OK);
86
87         for (int i = 0; i < SIZE; ++i)
88         {
89                 write[i] = i;
90                 size_t count = kfile_write(&file_handler.fd, &i, sizeof(int));
91                 ASSERT(count == sizeof(int));
92         }
93         /* test error function */
94         int tmp;
95         if (kfile_read(&file_handler.fd, &tmp, sizeof(int)) < sizeof(int))
96                 ASSERT(kfile_error(&file_handler.fd) == FR_DENIED);
97         kfile_clearerr(&file_handler.fd);
98         ASSERT(file_handler.error_code == FR_OK);
99
100
101         int err = 0;
102         err = kfile_close(&file_handler.fd);
103         ASSERT(err == 0);
104
105         fat_err = fatfile_open(&file_handler, "foo.txt", FA_READ);
106         ASSERT(fat_err == FR_OK);
107
108         for (int i = 0; i < SIZE; ++ i)
109         {
110                 size_t count = kfile_read(&file_handler.fd, &read[i], sizeof(int));
111                 ASSERT(count == sizeof(int));
112                 /* check for correctness */
113                 ASSERT(read[i] == write[i]);
114         }
115         /* test kfile_seek() */
116         ASSERT(kfile_seek(&file_handler.fd, -(sizeof(int) * SIZE * 2), KSM_SEEK_CUR) == 0);
117         ASSERT(kfile_seek(&file_handler.fd, sizeof(int), KSM_SEEK_END) == EOF);
118
119         ASSERT(kfile_close(&file_handler.fd) == 0);
120
121         fatfile_open(&file_handler, "foo.txt", FA_READ | FA_WRITE);
122         ASSERT((size_t)kfile_seek(&file_handler.fd, sizeof(int), KSM_SEEK_END) == sizeof(int) * (SIZE + 1));
123         ASSERT(kfile_seek(&file_handler.fd, -SIZE, KSM_SEEK_SET) == 0);
124
125         return 0;
126 }
127
128
129 TEST_MAIN(fatfile);