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