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