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_TIMEOUT 00053 #define MBED_TEST_TIMEOUT 120 00054 #endif 00055 00056 00057 // declarations 00058 #define STRINGIZE(x) STRINGIZE2(x) 00059 #define STRINGIZE2(x) #x 00060 #define INCLUDE(x, ext) STRINGIZE(x.ext) 00061 00062 #include INCLUDE(MBED_TEST_FILESYSTEM, h) 00063 #include INCLUDE(MBED_TEST_BLOCKDEVICE, hpp) 00064 00065 MBED_TEST_FILESYSTEM_DECL; 00066 MBED_TEST_BLOCKDEVICE_DECL; 00067 00068 Dir dir[MBED_TEST_DIRS]; 00069 File file[MBED_TEST_FILES]; 00070 DIR *dd[MBED_TEST_DIRS]; 00071 FILE *fd[MBED_TEST_FILES]; 00072 struct dirent ent; 00073 struct dirent *ed; 00074 size_t size; 00075 uint8_t buffer[MBED_CONF_RZ_SDHI_TEST_BUFFER]; 00076 00077 00078 // tests 00079 00080 void test_directory_tests() 00081 { 00082 int res = bd.init(); 00083 TEST_ASSERT_EQUAL(0, res); 00084 00085 { 00086 res = MBED_TEST_FILESYSTEM::format(&bd); 00087 TEST_ASSERT_EQUAL(0, res); 00088 } 00089 00090 res = bd.deinit(); 00091 TEST_ASSERT_EQUAL(0, res); 00092 } 00093 00094 void test_root_directory() 00095 { 00096 int res = bd.init(); 00097 TEST_ASSERT_EQUAL(0, res); 00098 00099 { 00100 res = fs.mount(&bd); 00101 TEST_ASSERT_EQUAL(0, res); 00102 res = dir[0].open(&fs, "/"); 00103 TEST_ASSERT_EQUAL(0, res); 00104 res = dir[0].close(); 00105 TEST_ASSERT_EQUAL(0, res); 00106 res = fs.unmount(); 00107 TEST_ASSERT_EQUAL(0, res); 00108 } 00109 00110 res = bd.deinit(); 00111 TEST_ASSERT_EQUAL(0, res); 00112 } 00113 00114 void test_directory_creation() 00115 { 00116 int res = bd.init(); 00117 TEST_ASSERT_EQUAL(0, res); 00118 00119 { 00120 res = fs.mount(&bd); 00121 TEST_ASSERT_EQUAL(0, res); 00122 res = fs.mkdir("potato", 0777); 00123 TEST_ASSERT_EQUAL(0, res); 00124 res = fs.unmount(); 00125 TEST_ASSERT_EQUAL(0, res); 00126 } 00127 00128 res = bd.deinit(); 00129 TEST_ASSERT_EQUAL(0, res); 00130 } 00131 00132 void test_file_creation() 00133 { 00134 int res = bd.init(); 00135 TEST_ASSERT_EQUAL(0, res); 00136 00137 { 00138 res = fs.mount(&bd); 00139 TEST_ASSERT_EQUAL(0, res); 00140 res = file[0].open(&fs, "burito", O_CREAT | O_WRONLY); 00141 TEST_ASSERT_EQUAL(0, res); 00142 res = file[0].close(); 00143 TEST_ASSERT_EQUAL(0, res); 00144 res = fs.unmount(); 00145 TEST_ASSERT_EQUAL(0, res); 00146 } 00147 00148 res = bd.deinit(); 00149 TEST_ASSERT_EQUAL(0, res); 00150 } 00151 00152 void dir_file_check(char *list[], uint32_t elements) 00153 { 00154 int res; 00155 while (1) { 00156 res = dir[0].read(&ent); 00157 if (0 == res) { 00158 break; 00159 } 00160 for (int i = 0; i < elements ; i++) { 00161 res = strcmp(ent.d_name, list[i]); 00162 if (0 == res) { 00163 res = ent.d_type; 00164 if ((DT_DIR != res) && (DT_REG != res)) { 00165 TEST_ASSERT(1); 00166 } 00167 break; 00168 } else if (i == elements) { 00169 TEST_ASSERT_EQUAL(0, res); 00170 } 00171 } 00172 } 00173 } 00174 00175 void test_directory_iteration() 00176 { 00177 int res = bd.init(); 00178 TEST_ASSERT_EQUAL(0, res); 00179 00180 res = fs.mount(&bd); 00181 TEST_ASSERT_EQUAL(0, res); 00182 res = dir[0].open(&fs, "/"); 00183 TEST_ASSERT_EQUAL(0, res); 00184 char *dir_list[] = {"potato", "burito", ".", ".."}; 00185 00186 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00187 00188 res = dir[0].close(); 00189 TEST_ASSERT_EQUAL(0, res); 00190 res = fs.unmount(); 00191 TEST_ASSERT_EQUAL(0, res); 00192 res = bd.deinit(); 00193 TEST_ASSERT_EQUAL(0, res); 00194 } 00195 00196 void test_directory_failures() 00197 { 00198 int res = bd.init(); 00199 TEST_ASSERT_EQUAL(0, res); 00200 00201 { 00202 res = fs.mount(&bd); 00203 TEST_ASSERT_EQUAL(0, res); 00204 res = fs.mkdir("potato", 0777); 00205 TEST_ASSERT_EQUAL(-EEXIST, res); 00206 res = dir[0].open(&fs, "tomato"); 00207 TEST_ASSERT_EQUAL(-ENOTDIR, res); 00208 res = dir[0].open(&fs, "burito"); 00209 TEST_ASSERT_NOT_EQUAL(0, res); 00210 res = file[0].open(&fs, "tomato", O_RDONLY); 00211 TEST_ASSERT_EQUAL(-ENOENT, res); 00212 res = file[0].open(&fs, "potato", O_RDONLY); 00213 TEST_ASSERT_NOT_EQUAL(0, res); 00214 res = fs.unmount(); 00215 TEST_ASSERT_EQUAL(0, res); 00216 } 00217 00218 res = bd.deinit(); 00219 TEST_ASSERT_EQUAL(0, res); 00220 } 00221 00222 void test_nested_directories() 00223 { 00224 int res = bd.init(); 00225 TEST_ASSERT_EQUAL(0, res); 00226 00227 { 00228 res = fs.mount(&bd); 00229 TEST_ASSERT_EQUAL(0, res); 00230 res = fs.mkdir("potato/baked", 0777); 00231 TEST_ASSERT_EQUAL(0, res); 00232 res = fs.mkdir("potato/sweet", 0777); 00233 TEST_ASSERT_EQUAL(0, res); 00234 res = fs.mkdir("potato/fried", 0777); 00235 TEST_ASSERT_EQUAL(0, res); 00236 res = fs.unmount(); 00237 TEST_ASSERT_EQUAL(0, res); 00238 } 00239 00240 { 00241 res = fs.mount(&bd); 00242 TEST_ASSERT_EQUAL(0, res); 00243 res = dir[0].open(&fs, "/"); 00244 TEST_ASSERT_EQUAL(0, res); 00245 char *dir_list[] = {"potato", "baked", "sweet", "fried", ".", ".."}; 00246 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00247 res = dir[0].close(); 00248 TEST_ASSERT_EQUAL(0, res); 00249 res = fs.unmount(); 00250 TEST_ASSERT_EQUAL(0, res); 00251 } 00252 00253 res = bd.deinit(); 00254 TEST_ASSERT_EQUAL(0, res); 00255 } 00256 00257 void test_multi_block_directory() 00258 { 00259 int res = bd.init(); 00260 TEST_ASSERT_EQUAL(0, res); 00261 00262 { 00263 res = fs.mount(&bd); 00264 TEST_ASSERT_EQUAL(0, res); 00265 res = fs.mkdir("cactus", 0777); 00266 TEST_ASSERT_EQUAL(0, res); 00267 for (int i = 0; i < 128; i++) { 00268 sprintf((char *)buffer, "cactus/test%d", i); 00269 res = fs.mkdir((char *)buffer, 0777); 00270 TEST_ASSERT_EQUAL(0, res); 00271 } 00272 res = fs.unmount(); 00273 TEST_ASSERT_EQUAL(0, res); 00274 } 00275 00276 { 00277 res = fs.mount(&bd); 00278 TEST_ASSERT_EQUAL(0, res); 00279 res = dir[0].open(&fs, "cactus"); 00280 TEST_ASSERT_EQUAL(0, res); 00281 00282 #if (MBED_TEST_FILESYSTEM != FATFileSystem) 00283 char *dir_list[] = {".", ".."}; 00284 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00285 #endif 00286 00287 for (int i = 0; i < 128; i++) { 00288 sprintf((char *)buffer, "test%d", i); 00289 res = dir[0].read(&ent); 00290 TEST_ASSERT_EQUAL(1, res); 00291 res = strcmp(ent.d_name, (char *)buffer); 00292 TEST_ASSERT_EQUAL(0, res); 00293 } 00294 res = dir[0].read(&ent); 00295 TEST_ASSERT_EQUAL(0, res); 00296 res = dir[0].close(); 00297 TEST_ASSERT_EQUAL(0, res); 00298 res = fs.unmount(); 00299 TEST_ASSERT_EQUAL(0, res); 00300 } 00301 00302 res = bd.deinit(); 00303 TEST_ASSERT_EQUAL(0, res); 00304 } 00305 00306 void test_directory_remove() 00307 { 00308 int res = bd.init(); 00309 TEST_ASSERT_EQUAL(0, res); 00310 00311 { 00312 res = fs.mount(&bd); 00313 TEST_ASSERT_EQUAL(0, res); 00314 res = fs.remove("potato"); 00315 TEST_ASSERT_NOT_EQUAL(0, res); 00316 res = fs.remove("potato/sweet"); 00317 TEST_ASSERT_EQUAL(0, res); 00318 res = fs.remove("potato/baked"); 00319 TEST_ASSERT_EQUAL(0, res); 00320 res = fs.remove("potato/fried"); 00321 TEST_ASSERT_EQUAL(0, res); 00322 res = dir[0].open(&fs, "potato"); 00323 TEST_ASSERT_EQUAL(0, res); 00324 00325 #if (MBED_TEST_FILESYSTEM != FATFileSystem) 00326 char *dir_list[] = {".", ".."}; 00327 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00328 #endif 00329 00330 res = dir[0].read(&ent); 00331 TEST_ASSERT_EQUAL(0, res); 00332 res = dir[0].close(); 00333 TEST_ASSERT_EQUAL(0, res); 00334 res = fs.remove("potato"); 00335 TEST_ASSERT_EQUAL(0, res); 00336 res = fs.unmount(); 00337 TEST_ASSERT_EQUAL(0, res); 00338 } 00339 00340 { 00341 res = fs.mount(&bd); 00342 TEST_ASSERT_EQUAL(0, res); 00343 res = dir[0].open(&fs, "/"); 00344 TEST_ASSERT_EQUAL(0, res); 00345 char *dir_list[] = {"burito", "cactus", ".", ".."}; 00346 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00347 res = dir[0].close(); 00348 TEST_ASSERT_EQUAL(0, res); 00349 res = fs.unmount(); 00350 TEST_ASSERT_EQUAL(0, res); 00351 } 00352 00353 res = bd.deinit(); 00354 TEST_ASSERT_EQUAL(0, res); 00355 } 00356 00357 void test_directory_rename() 00358 { 00359 int res = bd.init(); 00360 TEST_ASSERT_EQUAL(0, res); 00361 00362 { 00363 res = fs.mount(&bd); 00364 TEST_ASSERT_EQUAL(0, res); 00365 res = fs.mkdir("coldpotato", 0777); 00366 TEST_ASSERT_EQUAL(0, res); 00367 res = fs.mkdir("coldpotato/baked", 0777); 00368 TEST_ASSERT_EQUAL(0, res); 00369 res = fs.mkdir("coldpotato/sweet", 0777); 00370 TEST_ASSERT_EQUAL(0, res); 00371 res = fs.mkdir("coldpotato/fried", 0777); 00372 TEST_ASSERT_EQUAL(0, res); 00373 res = fs.unmount(); 00374 TEST_ASSERT_EQUAL(0, res); 00375 } 00376 00377 { 00378 res = fs.mount(&bd); 00379 TEST_ASSERT_EQUAL(0, res); 00380 res = fs.rename("coldpotato", "hotpotato"); 00381 TEST_ASSERT_EQUAL(0, res); 00382 res = fs.unmount(); 00383 TEST_ASSERT_EQUAL(0, res); 00384 } 00385 00386 { 00387 res = fs.mount(&bd); 00388 TEST_ASSERT_EQUAL(0, res); 00389 res = dir[0].open(&fs, "hotpotato"); 00390 TEST_ASSERT_EQUAL(0, res); 00391 char *dir_list[] = {"baked", "sweet", "fried", ".", ".."}; 00392 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00393 res = dir[0].close(); 00394 TEST_ASSERT_EQUAL(0, res); 00395 res = fs.unmount(); 00396 TEST_ASSERT_EQUAL(0, res); 00397 } 00398 00399 { 00400 res = fs.mount(&bd); 00401 TEST_ASSERT_EQUAL(0, res); 00402 res = fs.mkdir("warmpotato", 0777); 00403 TEST_ASSERT_EQUAL(0, res); 00404 res = fs.mkdir("warmpotato/mushy", 0777); 00405 TEST_ASSERT_EQUAL(0, res); 00406 res = fs.rename("hotpotato", "warmpotato"); 00407 TEST_ASSERT_NOT_EQUAL(0, res); 00408 res = fs.remove("warmpotato/mushy"); 00409 TEST_ASSERT_EQUAL(0, res); 00410 res = fs.remove("warmpotato"); 00411 TEST_ASSERT_EQUAL(0, res); 00412 res = fs.rename("hotpotato", "warmpotato"); 00413 TEST_ASSERT_EQUAL(0, res); 00414 res = fs.unmount(); 00415 TEST_ASSERT_EQUAL(0, res); 00416 } 00417 00418 { 00419 res = fs.mount(&bd); 00420 TEST_ASSERT_EQUAL(0, res); 00421 res = dir[0].open(&fs, "warmpotato"); 00422 TEST_ASSERT_EQUAL(0, res); 00423 char *dir_list[] = {"baked", "sweet", "fried", ".", ".."}; 00424 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00425 res = dir[0].close(); 00426 TEST_ASSERT_EQUAL(0, res); 00427 res = fs.unmount(); 00428 TEST_ASSERT_EQUAL(0, res); 00429 } 00430 00431 { 00432 res = fs.mount(&bd); 00433 TEST_ASSERT_EQUAL(0, res); 00434 res = fs.mkdir("coldpotato", 0777); 00435 TEST_ASSERT_EQUAL(0, res); 00436 res = fs.rename("warmpotato/baked", "coldpotato/baked"); 00437 TEST_ASSERT_EQUAL(0, res); 00438 res = fs.rename("warmpotato/sweet", "coldpotato/sweet"); 00439 TEST_ASSERT_EQUAL(0, res); 00440 res = fs.rename("warmpotato/fried", "coldpotato/fried"); 00441 TEST_ASSERT_EQUAL(0, res); 00442 res = fs.remove("coldpotato"); 00443 TEST_ASSERT_NOT_EQUAL(0, res); 00444 res = fs.remove("warmpotato"); 00445 TEST_ASSERT_EQUAL(0, res); 00446 res = fs.unmount(); 00447 TEST_ASSERT_EQUAL(0, res); 00448 } 00449 00450 { 00451 res = fs.mount(&bd); 00452 TEST_ASSERT_EQUAL(0, res); 00453 res = dir[0].open(&fs, "coldpotato"); 00454 TEST_ASSERT_EQUAL(0, res); 00455 char *dir_list[] = {"baked", "sweet", "fried", ".", ".."}; 00456 dir_file_check(dir_list, (sizeof(dir_list) / sizeof(dir_list[0]))); 00457 res = dir[0].close(); 00458 TEST_ASSERT_EQUAL(0, res); 00459 res = fs.unmount(); 00460 TEST_ASSERT_EQUAL(0, res); 00461 } 00462 00463 res = bd.deinit(); 00464 TEST_ASSERT_EQUAL(0, res); 00465 } 00466 00467 00468 00469 // test setup 00470 utest::v1::status_t test_setup(const size_t number_of_cases) 00471 { 00472 GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); 00473 return verbose_test_setup_handler(number_of_cases); 00474 } 00475 00476 Case cases[] = { 00477 Case("Directory tests", test_directory_tests), 00478 Case("Root directory", test_root_directory), 00479 Case("Directory creation", test_directory_creation), 00480 Case("File creation", test_file_creation), 00481 Case("Directory iteration", test_directory_iteration), 00482 Case("Directory failures", test_directory_failures), 00483 Case("Nested directories", test_nested_directories), 00484 Case("Multi-block directory", test_multi_block_directory), 00485 Case("Directory remove", test_directory_remove), 00486 Case("Directory rename", test_directory_rename), 00487 }; 00488 00489 Specification specification(test_setup, cases); 00490 00491 int main() 00492 { 00493 return !Harness::run(specification); 00494 }
Generated on Sun Jul 17 2022 03:41:56 by
1.7.2