Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2016 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "greentea-client/test_env.h" 00019 #include "unity.h" 00020 #include "utest.h" 00021 #include <stdlib.h> 00022 #include <errno.h> 00023 00024 using namespace utest::v1; 00025 00026 // test configuration 00027 #ifndef MBED_TEST_FILESYSTEM 00028 #define MBED_TEST_FILESYSTEM FATFileSystem 00029 #endif 00030 00031 #ifndef MBED_TEST_FILESYSTEM_DECL 00032 #define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs") 00033 #endif 00034 00035 #ifndef MBED_TEST_BLOCKDEVICE 00036 #define MBED_TEST_BLOCKDEVICE RZ_SDHIBlockDevice 00037 #define MBED_TEST_BLOCKDEVICE_DECL SDHIBlockDevice bd(MBED_CONF_RZ_SDHI_CH); 00038 #endif 00039 00040 #ifndef MBED_TEST_BLOCKDEVICE_DECL 00041 #define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd 00042 #endif 00043 00044 #ifndef MBED_TEST_FILES 00045 #define MBED_TEST_FILES 4 00046 #endif 00047 00048 #ifndef MBED_TEST_DIRS 00049 #define MBED_TEST_DIRS 4 00050 #endif 00051 00052 #ifndef MBED_TEST_BUFFER 00053 #define MBED_TEST_BUFFER 8192 00054 #endif 00055 00056 #ifndef MBED_TEST_TIMEOUT 00057 #define MBED_TEST_TIMEOUT 120 00058 #endif 00059 00060 00061 // declarations 00062 #define STRINGIZE(x) STRINGIZE2(x) 00063 #define STRINGIZE2(x) #x 00064 #define INCLUDE(x, ext) STRINGIZE(x.ext) 00065 00066 #include INCLUDE(MBED_TEST_FILESYSTEM, h) 00067 #include INCLUDE(MBED_TEST_BLOCKDEVICE, hpp) 00068 00069 MBED_TEST_FILESYSTEM_DECL; 00070 MBED_TEST_BLOCKDEVICE_DECL; 00071 00072 Dir dir[MBED_TEST_DIRS]; 00073 File file[MBED_TEST_FILES]; 00074 DIR *dd[MBED_TEST_DIRS]; 00075 FILE *fd[MBED_TEST_FILES]; 00076 struct dirent ent; 00077 struct dirent *ed; 00078 uint8_t buffer[MBED_TEST_BUFFER]; 00079 uint8_t rbuffer[MBED_TEST_BUFFER]; 00080 uint8_t wbuffer[MBED_TEST_BUFFER]; 00081 00082 static char file_counter = 0; 00083 const char *filenames[] = {"smallavacado", "mediumavacado", "largeavacado", 00084 "blockfile", "bigblockfile", "hello", ".", ".." 00085 }; 00086 00087 // tests 00088 00089 void test_file_tests() 00090 { 00091 int res = bd.init(); 00092 TEST_ASSERT_EQUAL(0, res); 00093 00094 { 00095 res = MBED_TEST_FILESYSTEM::format(&bd); 00096 TEST_ASSERT_EQUAL(0, res); 00097 } 00098 00099 res = bd.deinit(); 00100 TEST_ASSERT_EQUAL(0, res); 00101 } 00102 00103 void test_simple_file_test() 00104 { 00105 int res = bd.init(); 00106 TEST_ASSERT_EQUAL(0, res); 00107 00108 { 00109 size_t size; 00110 res = fs.mount(&bd); 00111 TEST_ASSERT_EQUAL(0, res); 00112 res = file[0].open(&fs, "hello", O_WRONLY | O_CREAT); 00113 TEST_ASSERT_EQUAL(0, res); 00114 size = strlen("Hello World!\n"); 00115 memcpy(wbuffer, "Hello World!\n", size); 00116 res = file[0].write(wbuffer, size); 00117 TEST_ASSERT_EQUAL(size, res); 00118 res = file[0].close(); 00119 TEST_ASSERT_EQUAL(0, res); 00120 res = file[0].open(&fs, "hello", O_RDONLY); 00121 TEST_ASSERT_EQUAL(0, res); 00122 size = strlen("Hello World!\n"); 00123 res = file[0].read(rbuffer, size); 00124 TEST_ASSERT_EQUAL(size, res); 00125 res = memcmp(rbuffer, wbuffer, size); 00126 TEST_ASSERT_EQUAL(0, res); 00127 res = file[0].close(); 00128 TEST_ASSERT_EQUAL(0, res); 00129 res = fs.unmount(); 00130 TEST_ASSERT_EQUAL(0, res); 00131 } 00132 00133 res = bd.deinit(); 00134 TEST_ASSERT_EQUAL(0, res); 00135 } 00136 00137 template <int file_size, int write_size, int read_size> 00138 void test_write_file_test() 00139 { 00140 int res = bd.init(); 00141 TEST_ASSERT_EQUAL(0, res); 00142 00143 { 00144 size_t size = file_size; 00145 size_t chunk = write_size; 00146 srand(0); 00147 res = fs.mount(&bd); 00148 TEST_ASSERT_EQUAL(0, res); 00149 res = file[0].open(&fs, filenames[file_counter], O_WRONLY | O_CREAT); 00150 TEST_ASSERT_EQUAL(0, res); 00151 for (size_t i = 0; i < size; i += chunk) { 00152 chunk = (chunk < size - i) ? chunk : size - i; 00153 for (size_t b = 0; b < chunk; b++) { 00154 buffer[b] = rand() & 0xff; 00155 } 00156 res = file[0].write(buffer, chunk); 00157 TEST_ASSERT_EQUAL(chunk, res); 00158 } 00159 res = file[0].close(); 00160 TEST_ASSERT_EQUAL(0, res); 00161 res = fs.unmount(); 00162 TEST_ASSERT_EQUAL(0, res); 00163 } 00164 00165 { 00166 size_t size = file_size; 00167 size_t chunk = read_size; 00168 srand(0); 00169 res = fs.mount(&bd); 00170 TEST_ASSERT_EQUAL(0, res); 00171 res = file[0].open(&fs, filenames[file_counter], O_RDONLY); 00172 TEST_ASSERT_EQUAL(0, res); 00173 for (size_t i = 0; i < size; i += chunk) { 00174 chunk = (chunk < size - i) ? chunk : size - i; 00175 res = file[0].read(buffer, chunk); 00176 TEST_ASSERT_EQUAL(chunk, res); 00177 for (size_t b = 0; b < chunk && i + b < size; b++) { 00178 res = buffer[b]; 00179 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00180 } 00181 } 00182 res = file[0].close(); 00183 TEST_ASSERT_EQUAL(0, res); 00184 res = fs.unmount(); 00185 TEST_ASSERT_EQUAL(0, res); 00186 } 00187 00188 file_counter++; 00189 res = bd.deinit(); 00190 TEST_ASSERT_EQUAL(0, res); 00191 } 00192 00193 void test_non_overlap_check() 00194 { 00195 int res = bd.init(); 00196 TEST_ASSERT_EQUAL(0, res); 00197 00198 { 00199 size_t size = 32; 00200 size_t chunk = 29; 00201 srand(0); 00202 res = fs.mount(&bd); 00203 TEST_ASSERT_EQUAL(0, res); 00204 res = file[0].open(&fs, "smallavacado", O_RDONLY); 00205 TEST_ASSERT_EQUAL(0, res); 00206 for (size_t i = 0; i < size; i += chunk) { 00207 chunk = (chunk < size - i) ? chunk : size - i; 00208 res = file[0].read(buffer, chunk); 00209 TEST_ASSERT_EQUAL(chunk, res); 00210 for (size_t b = 0; b < chunk && i + b < size; b++) { 00211 res = buffer[b]; 00212 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00213 } 00214 } 00215 res = file[0].close(); 00216 TEST_ASSERT_EQUAL(0, res); 00217 res = fs.unmount(); 00218 TEST_ASSERT_EQUAL(0, res); 00219 } 00220 00221 { 00222 size_t size = 8192; 00223 size_t chunk = 29; 00224 srand(0); 00225 res = fs.mount(&bd); 00226 TEST_ASSERT_EQUAL(0, res); 00227 res = file[0].open(&fs, "mediumavacado", O_RDONLY); 00228 TEST_ASSERT_EQUAL(0, res); 00229 for (size_t i = 0; i < size; i += chunk) { 00230 chunk = (chunk < size - i) ? chunk : size - i; 00231 res = file[0].read(buffer, chunk); 00232 TEST_ASSERT_EQUAL(chunk, res); 00233 for (size_t b = 0; b < chunk && i + b < size; b++) { 00234 res = buffer[b]; 00235 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00236 } 00237 } 00238 res = file[0].close(); 00239 TEST_ASSERT_EQUAL(0, res); 00240 res = fs.unmount(); 00241 TEST_ASSERT_EQUAL(0, res); 00242 } 00243 00244 { 00245 size_t size = 262144; 00246 size_t chunk = 29; 00247 srand(0); 00248 res = fs.mount(&bd); 00249 TEST_ASSERT_EQUAL(0, res); 00250 res = file[0].open(&fs, "largeavacado", O_RDONLY); 00251 TEST_ASSERT_EQUAL(0, res); 00252 for (size_t i = 0; i < size; i += chunk) { 00253 chunk = (chunk < size - i) ? chunk : size - i; 00254 res = file[0].read(buffer, chunk); 00255 TEST_ASSERT_EQUAL(chunk, res); 00256 for (size_t b = 0; b < chunk && i + b < size; b++) { 00257 res = buffer[b]; 00258 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00259 } 00260 } 00261 res = file[0].close(); 00262 TEST_ASSERT_EQUAL(0, res); 00263 res = fs.unmount(); 00264 TEST_ASSERT_EQUAL(0, res); 00265 } 00266 00267 res = bd.deinit(); 00268 TEST_ASSERT_EQUAL(0, res); 00269 } 00270 00271 void test_dir_check() 00272 { 00273 00274 int res = bd.init(); 00275 TEST_ASSERT_EQUAL(0, res); 00276 00277 { 00278 res = fs.mount(&bd); 00279 TEST_ASSERT_EQUAL(0, res); 00280 res = dir[0].open(&fs, "/"); 00281 TEST_ASSERT_EQUAL(0, res); 00282 int numFiles = sizeof(filenames) / sizeof(filenames[0]); 00283 // Check the filenames in directory 00284 while (1) { 00285 res = dir[0].read(&ent); 00286 if (0 == res) { 00287 break; 00288 } 00289 for (int i = 0; i < numFiles ; i++) { 00290 res = strcmp(ent.d_name, filenames[i]); 00291 if (0 == res) { 00292 res = ent.d_type; 00293 if ((DT_REG != res) && (DT_DIR != res)) { 00294 TEST_ASSERT(1); 00295 } 00296 break; 00297 } else if (i == numFiles) { 00298 TEST_ASSERT_EQUAL(0, res); 00299 } 00300 } 00301 } 00302 res = dir[0].close(); 00303 TEST_ASSERT_EQUAL(0, res); 00304 res = fs.unmount(); 00305 TEST_ASSERT_EQUAL(0, res); 00306 } 00307 00308 res = bd.deinit(); 00309 TEST_ASSERT_EQUAL(0, res); 00310 } 00311 00312 00313 // test setup 00314 utest::v1::status_t test_setup(const size_t number_of_cases) 00315 { 00316 GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); 00317 return verbose_test_setup_handler(number_of_cases); 00318 } 00319 00320 Case cases[] = { 00321 Case("File tests", test_file_tests), 00322 Case("Simple file test", test_simple_file_test), 00323 Case("Small file test", test_write_file_test<32, 31, 29>), 00324 Case("Medium file test", test_write_file_test<8192, 31, 29>), 00325 Case("Large file test", test_write_file_test<262144, 31, 29>), 00326 Case("Block Size file test", test_write_file_test<9000, 512, 512>), 00327 Case("Multiple block size file test", test_write_file_test<26215, MBED_TEST_BUFFER, MBED_TEST_BUFFER>), 00328 Case("Non-overlap check", test_non_overlap_check), 00329 Case("Dir check", test_dir_check), 00330 }; 00331 00332 Specification specification(test_setup, cases); 00333 00334 int main() 00335 { 00336 return !Harness::run(specification); 00337 }
Generated on Sun Jul 17 2022 03:41:56 by
1.7.2