init
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 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 #include "mbed.h" 00017 #include "greentea-client/test_env.h" 00018 #include "unity.h" 00019 #include "utest.h" 00020 #include <stdlib.h> 00021 #include <errno.h> 00022 00023 using namespace utest::v1; 00024 00025 // test configuration 00026 #ifndef MBED_TEST_FILESYSTEM 00027 #define MBED_TEST_FILESYSTEM LittleFileSystem 00028 #endif 00029 00030 #ifndef MBED_TEST_FILESYSTEM_DECL 00031 #define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs") 00032 #endif 00033 00034 #ifndef MBED_TEST_BLOCKDEVICE 00035 #error [NOT_SUPPORTED] Non-volatile block device required 00036 #endif 00037 00038 #ifndef MBED_TEST_BLOCKDEVICE_DECL 00039 #define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd 00040 #endif 00041 00042 #ifndef MBED_TEST_FILES 00043 #define MBED_TEST_FILES 4 00044 #endif 00045 00046 #ifndef MBED_TEST_DIRS 00047 #define MBED_TEST_DIRS 4 00048 #endif 00049 00050 #ifndef MBED_TEST_BUFFER 00051 #define MBED_TEST_BUFFER 8192 00052 #endif 00053 00054 #ifndef MBED_TEST_TIMEOUT 00055 #define MBED_TEST_TIMEOUT 480 00056 #endif 00057 00058 00059 // declarations 00060 #define STRINGIZE(x) STRINGIZE2(x) 00061 #define STRINGIZE2(x) #x 00062 #define INCLUDE(x) STRINGIZE(x.h) 00063 00064 #include INCLUDE(MBED_TEST_FILESYSTEM) 00065 #include INCLUDE(MBED_TEST_BLOCKDEVICE) 00066 00067 MBED_TEST_FILESYSTEM_DECL; 00068 MBED_TEST_BLOCKDEVICE_DECL; 00069 00070 Dir dir[MBED_TEST_DIRS]; 00071 File file[MBED_TEST_FILES]; 00072 DIR *dd[MBED_TEST_DIRS]; 00073 FILE *fd[MBED_TEST_FILES]; 00074 struct dirent ent; 00075 struct dirent *ed; 00076 size_t size; 00077 uint8_t buffer[MBED_TEST_BUFFER]; 00078 uint8_t rbuffer[MBED_TEST_BUFFER]; 00079 uint8_t wbuffer[MBED_TEST_BUFFER]; 00080 00081 00082 // tests 00083 00084 void test_seek_tests() 00085 { 00086 int res = bd.init(); 00087 TEST_ASSERT_EQUAL(0, res); 00088 00089 { 00090 res = MBED_TEST_FILESYSTEM::format(&bd); 00091 TEST_ASSERT_EQUAL(0, res); 00092 res = fs.mount(&bd); 00093 TEST_ASSERT_EQUAL(0, res); 00094 res = mkdir("/fs/" "hello", 0777); 00095 TEST_ASSERT_EQUAL(0, res); 00096 for (int i = 0; i < 132; i++) { 00097 sprintf((char*)buffer, "/fs/" "hello/kitty%d", i); 00098 res = !((fd[0] = fopen((char*)buffer, 00099 "ab")) != NULL); 00100 TEST_ASSERT_EQUAL(0, res); 00101 00102 size = strlen("kittycatcat"); 00103 memcpy(buffer, "kittycatcat", size); 00104 for (int j = 0; j < 132; j++) { 00105 fwrite(buffer, 1, size, fd[0]); 00106 } 00107 res = fclose(fd[0]); 00108 TEST_ASSERT_EQUAL(0, res); 00109 } 00110 res = fs.unmount(); 00111 TEST_ASSERT_EQUAL(0, res); 00112 } 00113 00114 res = bd.deinit(); 00115 TEST_ASSERT_EQUAL(0, res); 00116 } 00117 00118 void test_simple_dir_seek() 00119 { 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 = !((dd[0] = opendir("/fs/" "hello")) != NULL); 00127 TEST_ASSERT_EQUAL(0, res); 00128 res = ((ed = readdir(dd[0])) != NULL); 00129 TEST_ASSERT_EQUAL(1, res); 00130 res = strcmp(ed->d_name, "."); 00131 TEST_ASSERT_EQUAL(0, res); 00132 res = ((ed = readdir(dd[0])) != NULL); 00133 TEST_ASSERT_EQUAL(1, res); 00134 res = strcmp(ed->d_name, ".."); 00135 TEST_ASSERT_EQUAL(0, res); 00136 00137 off_t pos; 00138 int i; 00139 for (i = 0; i < 4; i++) { 00140 sprintf((char*)buffer, "kitty%d", i); 00141 res = ((ed = readdir(dd[0])) != NULL); 00142 TEST_ASSERT_EQUAL(1, res); 00143 res = strcmp(ed->d_name, (char*)buffer); 00144 TEST_ASSERT_EQUAL(0, res); 00145 pos = telldir(dd[0]); 00146 } 00147 res = pos >= 0; 00148 TEST_ASSERT_EQUAL(1, res); 00149 00150 seekdir(dd[0], pos); 00151 sprintf((char*)buffer, "kitty%d", i); 00152 res = ((ed = readdir(dd[0])) != NULL); 00153 TEST_ASSERT_EQUAL(1, res); 00154 res = strcmp(ed->d_name, (char*)buffer); 00155 TEST_ASSERT_EQUAL(0, res); 00156 00157 rewinddir(dd[0]); 00158 sprintf((char*)buffer, "kitty%d", 0); 00159 res = ((ed = readdir(dd[0])) != NULL); 00160 TEST_ASSERT_EQUAL(1, res); 00161 res = strcmp(ed->d_name, "."); 00162 TEST_ASSERT_EQUAL(0, res); 00163 res = ((ed = readdir(dd[0])) != NULL); 00164 TEST_ASSERT_EQUAL(1, res); 00165 res = strcmp(ed->d_name, ".."); 00166 TEST_ASSERT_EQUAL(0, res); 00167 res = ((ed = readdir(dd[0])) != NULL); 00168 TEST_ASSERT_EQUAL(1, res); 00169 res = strcmp(ed->d_name, (char*)buffer); 00170 TEST_ASSERT_EQUAL(0, res); 00171 00172 seekdir(dd[0], pos); 00173 sprintf((char*)buffer, "kitty%d", i); 00174 res = ((ed = readdir(dd[0])) != NULL); 00175 TEST_ASSERT_EQUAL(1, res); 00176 res = strcmp(ed->d_name, (char*)buffer); 00177 TEST_ASSERT_EQUAL(0, res); 00178 res = closedir(dd[0]); 00179 TEST_ASSERT_EQUAL(0, res); 00180 res = fs.unmount(); 00181 TEST_ASSERT_EQUAL(0, res); 00182 } 00183 00184 res = bd.deinit(); 00185 TEST_ASSERT_EQUAL(0, res); 00186 } 00187 00188 void test_large_dir_seek() 00189 { 00190 int res = bd.init(); 00191 TEST_ASSERT_EQUAL(0, res); 00192 00193 { 00194 res = fs.mount(&bd); 00195 TEST_ASSERT_EQUAL(0, res); 00196 res = !((dd[0] = opendir("/fs/" "hello")) != NULL); 00197 TEST_ASSERT_EQUAL(0, res); 00198 res = ((ed = readdir(dd[0])) != NULL); 00199 TEST_ASSERT_EQUAL(1, res); 00200 res = strcmp(ed->d_name, "."); 00201 TEST_ASSERT_EQUAL(0, res); 00202 res = ((ed = readdir(dd[0])) != NULL); 00203 TEST_ASSERT_EQUAL(1, res); 00204 res = strcmp(ed->d_name, ".."); 00205 TEST_ASSERT_EQUAL(0, res); 00206 00207 off_t pos; 00208 int i; 00209 for (i = 0; i < 128; i++) { 00210 sprintf((char*)buffer, "kitty%d", i); 00211 res = ((ed = readdir(dd[0])) != NULL); 00212 TEST_ASSERT_EQUAL(1, res); 00213 res = strcmp(ed->d_name, (char*)buffer); 00214 TEST_ASSERT_EQUAL(0, res); 00215 pos = telldir(dd[0]); 00216 } 00217 res = pos >= 0; 00218 TEST_ASSERT_EQUAL(1, res); 00219 00220 seekdir(dd[0], pos); 00221 sprintf((char*)buffer, "kitty%d", i); 00222 res = ((ed = readdir(dd[0])) != NULL); 00223 TEST_ASSERT_EQUAL(1, res); 00224 res = strcmp(ed->d_name, (char*)buffer); 00225 TEST_ASSERT_EQUAL(0, res); 00226 00227 rewinddir(dd[0]); 00228 sprintf((char*)buffer, "kitty%d", 0); 00229 res = ((ed = readdir(dd[0])) != NULL); 00230 TEST_ASSERT_EQUAL(1, res); 00231 res = strcmp(ed->d_name, "."); 00232 TEST_ASSERT_EQUAL(0, res); 00233 res = ((ed = readdir(dd[0])) != NULL); 00234 TEST_ASSERT_EQUAL(1, res); 00235 res = strcmp(ed->d_name, ".."); 00236 TEST_ASSERT_EQUAL(0, res); 00237 res = ((ed = readdir(dd[0])) != NULL); 00238 TEST_ASSERT_EQUAL(1, res); 00239 res = strcmp(ed->d_name, (char*)buffer); 00240 TEST_ASSERT_EQUAL(0, res); 00241 00242 seekdir(dd[0], pos); 00243 sprintf((char*)buffer, "kitty%d", i); 00244 res = ((ed = readdir(dd[0])) != NULL); 00245 TEST_ASSERT_EQUAL(1, res); 00246 res = strcmp(ed->d_name, (char*)buffer); 00247 TEST_ASSERT_EQUAL(0, res); 00248 res = closedir(dd[0]); 00249 TEST_ASSERT_EQUAL(0, res); 00250 res = fs.unmount(); 00251 TEST_ASSERT_EQUAL(0, res); 00252 } 00253 00254 res = bd.deinit(); 00255 TEST_ASSERT_EQUAL(0, res); 00256 } 00257 00258 void test_simple_file_seek() 00259 { 00260 int res = bd.init(); 00261 TEST_ASSERT_EQUAL(0, res); 00262 00263 { 00264 res = fs.mount(&bd); 00265 TEST_ASSERT_EQUAL(0, res); 00266 res = !((fd[0] = fopen("/fs/" "hello/kitty42", "rb")) != NULL); 00267 TEST_ASSERT_EQUAL(0, res); 00268 00269 off_t pos; 00270 size = strlen("kittycatcat"); 00271 for (int i = 0; i < 4; i++) { 00272 res = fread(buffer, 1, size, fd[0]); 00273 TEST_ASSERT_EQUAL(size, res); 00274 res = memcmp(buffer, "kittycatcat", size); 00275 TEST_ASSERT_EQUAL(0, res); 00276 pos = ftell(fd[0]); 00277 } 00278 res = pos >= 0; 00279 TEST_ASSERT_EQUAL(1, res); 00280 res = fseek(fd[0], pos, SEEK_SET); 00281 TEST_ASSERT_EQUAL(0, res); 00282 res = fread(buffer, 1, size, fd[0]); 00283 TEST_ASSERT_EQUAL(size, res); 00284 res = memcmp(buffer, "kittycatcat", size); 00285 TEST_ASSERT_EQUAL(0, res); 00286 00287 rewind(fd[0]); 00288 res = fread(buffer, 1, size, fd[0]); 00289 TEST_ASSERT_EQUAL(size, res); 00290 res = memcmp(buffer, "kittycatcat", size); 00291 TEST_ASSERT_EQUAL(0, res); 00292 res = fseek(fd[0], pos, SEEK_SET); 00293 TEST_ASSERT_EQUAL(0, res); 00294 res = fread(buffer, 1, size, fd[0]); 00295 TEST_ASSERT_EQUAL(size, res); 00296 res = memcmp(buffer, "kittycatcat", size); 00297 TEST_ASSERT_EQUAL(0, res); 00298 res = fseek(fd[0], -size, SEEK_CUR); 00299 TEST_ASSERT_EQUAL(0, res); 00300 res = fread(buffer, 1, size, fd[0]); 00301 TEST_ASSERT_EQUAL(size, res); 00302 res = memcmp(buffer, "kittycatcat", size); 00303 TEST_ASSERT_EQUAL(0, res); 00304 res = fseek(fd[0], -size, SEEK_END); 00305 TEST_ASSERT_EQUAL(0, res); 00306 res = fread(buffer, 1, size, fd[0]); 00307 TEST_ASSERT_EQUAL(size, res); 00308 res = memcmp(buffer, "kittycatcat", size); 00309 TEST_ASSERT_EQUAL(0, res); 00310 00311 res = fseek(fd[0], 0, SEEK_CUR); 00312 TEST_ASSERT_EQUAL(0, res); 00313 res = fclose(fd[0]); 00314 TEST_ASSERT_EQUAL(0, res); 00315 res = fs.unmount(); 00316 TEST_ASSERT_EQUAL(0, res); 00317 } 00318 00319 res = bd.deinit(); 00320 TEST_ASSERT_EQUAL(0, res); 00321 } 00322 00323 void test_large_file_seek() 00324 { 00325 int res = bd.init(); 00326 TEST_ASSERT_EQUAL(0, res); 00327 00328 { 00329 res = fs.mount(&bd); 00330 TEST_ASSERT_EQUAL(0, res); 00331 res = !((fd[0] = fopen("/fs/" "hello/kitty42", "rb")) != NULL); 00332 TEST_ASSERT_EQUAL(0, res); 00333 00334 off_t pos; 00335 size = strlen("kittycatcat"); 00336 for (int i = 0; i < 128; i++) { 00337 res = fread(buffer, 1, size, fd[0]); 00338 TEST_ASSERT_EQUAL(size, res); 00339 res = memcmp(buffer, "kittycatcat", size); 00340 TEST_ASSERT_EQUAL(0, res); 00341 pos = ftell(fd[0]); 00342 } 00343 res = pos >= 0; 00344 TEST_ASSERT_EQUAL(1, res); 00345 res = fseek(fd[0], pos, SEEK_SET); 00346 TEST_ASSERT_EQUAL(0, res); 00347 res = fread(buffer, 1, size, fd[0]); 00348 TEST_ASSERT_EQUAL(size, res); 00349 res = memcmp(buffer, "kittycatcat", size); 00350 TEST_ASSERT_EQUAL(0, res); 00351 00352 rewind(fd[0]); 00353 res = fread(buffer, 1, size, fd[0]); 00354 TEST_ASSERT_EQUAL(size, res); 00355 res = memcmp(buffer, "kittycatcat", size); 00356 TEST_ASSERT_EQUAL(0, res); 00357 res = fseek(fd[0], pos, SEEK_SET); 00358 TEST_ASSERT_EQUAL(0, res); 00359 res = fread(buffer, 1, size, fd[0]); 00360 TEST_ASSERT_EQUAL(size, res); 00361 res = memcmp(buffer, "kittycatcat", size); 00362 TEST_ASSERT_EQUAL(0, res); 00363 res = fseek(fd[0], -size, SEEK_CUR); 00364 TEST_ASSERT_EQUAL(0, res); 00365 res = fread(buffer, 1, size, fd[0]); 00366 TEST_ASSERT_EQUAL(size, res); 00367 res = memcmp(buffer, "kittycatcat", size); 00368 TEST_ASSERT_EQUAL(0, res); 00369 res = fseek(fd[0], -size, SEEK_END); 00370 TEST_ASSERT_EQUAL(0, res); 00371 res = fread(buffer, 1, size, fd[0]); 00372 TEST_ASSERT_EQUAL(size, res); 00373 res = memcmp(buffer, "kittycatcat", size); 00374 TEST_ASSERT_EQUAL(0, res); 00375 00376 res = fseek(fd[0], 0, SEEK_CUR); 00377 TEST_ASSERT_EQUAL(0, res); 00378 res = fclose(fd[0]); 00379 TEST_ASSERT_EQUAL(0, res); 00380 res = fs.unmount(); 00381 TEST_ASSERT_EQUAL(0, res); 00382 } 00383 00384 res = bd.deinit(); 00385 TEST_ASSERT_EQUAL(0, res); 00386 } 00387 00388 void test_simple_file_seek_and_write() 00389 { 00390 int res = bd.init(); 00391 TEST_ASSERT_EQUAL(0, res); 00392 00393 { 00394 res = fs.mount(&bd); 00395 TEST_ASSERT_EQUAL(0, res); 00396 res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL); 00397 TEST_ASSERT_EQUAL(0, res); 00398 00399 off_t pos; 00400 size = strlen("kittycatcat"); 00401 for (int i = 0; i < 4; i++) { 00402 res = fread(buffer, 1, size, fd[0]); 00403 TEST_ASSERT_EQUAL(size, res); 00404 res = memcmp(buffer, "kittycatcat", size); 00405 TEST_ASSERT_EQUAL(0, res); 00406 pos = ftell(fd[0]); 00407 } 00408 res = pos >= 0; 00409 TEST_ASSERT_EQUAL(1, res); 00410 00411 memcpy(buffer, "doggodogdog", size); 00412 res = fseek(fd[0], pos, SEEK_SET); 00413 TEST_ASSERT_EQUAL(0, res); 00414 res = fwrite(buffer, 1, size, fd[0]); 00415 TEST_ASSERT_EQUAL(size, res); 00416 res = fseek(fd[0], pos, SEEK_SET); 00417 TEST_ASSERT_EQUAL(0, res); 00418 res = fread(buffer, 1, size, fd[0]); 00419 TEST_ASSERT_EQUAL(size, res); 00420 res = memcmp(buffer, "doggodogdog", size); 00421 TEST_ASSERT_EQUAL(0, res); 00422 00423 rewind(fd[0]); 00424 res = fread(buffer, 1, size, fd[0]); 00425 TEST_ASSERT_EQUAL(size, res); 00426 res = memcmp(buffer, "kittycatcat", size); 00427 TEST_ASSERT_EQUAL(0, res); 00428 res = fseek(fd[0], pos, SEEK_SET); 00429 TEST_ASSERT_EQUAL(0, res); 00430 res = fread(buffer, 1, size, fd[0]); 00431 TEST_ASSERT_EQUAL(size, res); 00432 res = memcmp(buffer, "doggodogdog", size); 00433 TEST_ASSERT_EQUAL(0, res); 00434 res = fseek(fd[0], -size, SEEK_END); 00435 TEST_ASSERT_EQUAL(0, res); 00436 res = fread(buffer, 1, size, fd[0]); 00437 TEST_ASSERT_EQUAL(size, res); 00438 res = memcmp(buffer, "kittycatcat", size); 00439 TEST_ASSERT_EQUAL(0, res); 00440 00441 res = fseek(fd[0], 0, SEEK_CUR); 00442 TEST_ASSERT_EQUAL(0, res); 00443 res = fclose(fd[0]); 00444 TEST_ASSERT_EQUAL(0, res); 00445 res = fs.unmount(); 00446 TEST_ASSERT_EQUAL(0, res); 00447 } 00448 00449 res = bd.deinit(); 00450 TEST_ASSERT_EQUAL(0, res); 00451 } 00452 00453 void test_large_file_seek_and_write() 00454 { 00455 int res = bd.init(); 00456 TEST_ASSERT_EQUAL(0, res); 00457 00458 { 00459 res = fs.mount(&bd); 00460 TEST_ASSERT_EQUAL(0, res); 00461 res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL); 00462 TEST_ASSERT_EQUAL(0, res); 00463 00464 off_t pos; 00465 size = strlen("kittycatcat"); 00466 for (int i = 0; i < 128; i++) { 00467 res = fread(buffer, 1, size, fd[0]); 00468 TEST_ASSERT_EQUAL(size, res); 00469 if (i != 4) { 00470 res = memcmp(buffer, "kittycatcat", size); 00471 TEST_ASSERT_EQUAL(0, res); 00472 } 00473 pos = ftell(fd[0]); 00474 } 00475 res = pos >= 0; 00476 TEST_ASSERT_EQUAL(1, res); 00477 00478 memcpy(buffer, "doggodogdog", size); 00479 res = fseek(fd[0], pos, SEEK_SET); 00480 TEST_ASSERT_EQUAL(0, res); 00481 res = fwrite(buffer, 1, size, fd[0]); 00482 TEST_ASSERT_EQUAL(size, res); 00483 res = fseek(fd[0], pos, SEEK_SET); 00484 TEST_ASSERT_EQUAL(0, res); 00485 res = fread(buffer, 1, size, fd[0]); 00486 TEST_ASSERT_EQUAL(size, res); 00487 res = memcmp(buffer, "doggodogdog", size); 00488 TEST_ASSERT_EQUAL(0, res); 00489 00490 rewind(fd[0]); 00491 res = fread(buffer, 1, size, fd[0]); 00492 TEST_ASSERT_EQUAL(size, res); 00493 res = memcmp(buffer, "kittycatcat", size); 00494 TEST_ASSERT_EQUAL(0, res); 00495 res = fseek(fd[0], pos, SEEK_SET); 00496 TEST_ASSERT_EQUAL(0, res); 00497 res = fread(buffer, 1, size, fd[0]); 00498 TEST_ASSERT_EQUAL(size, res); 00499 res = memcmp(buffer, "doggodogdog", size); 00500 TEST_ASSERT_EQUAL(0, res); 00501 res = fseek(fd[0], -size, SEEK_END); 00502 TEST_ASSERT_EQUAL(0, res); 00503 res = fread(buffer, 1, size, fd[0]); 00504 TEST_ASSERT_EQUAL(size, res); 00505 res = memcmp(buffer, "kittycatcat", size); 00506 TEST_ASSERT_EQUAL(0, res); 00507 00508 res = fseek(fd[0], 0, SEEK_CUR); 00509 TEST_ASSERT_EQUAL(0, res); 00510 res = fclose(fd[0]); 00511 TEST_ASSERT_EQUAL(0, res); 00512 res = fs.unmount(); 00513 TEST_ASSERT_EQUAL(0, res); 00514 } 00515 00516 res = bd.deinit(); 00517 TEST_ASSERT_EQUAL(0, res); 00518 } 00519 00520 void test_boundary_seek_and_write() 00521 { 00522 int res = bd.init(); 00523 TEST_ASSERT_EQUAL(0, res); 00524 00525 { 00526 res = fs.mount(&bd); 00527 TEST_ASSERT_EQUAL(0, res); 00528 res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL); 00529 TEST_ASSERT_EQUAL(0, res); 00530 00531 size = strlen("hedgehoghog"); 00532 const off_t offsets[] = {512, 1020, 513, 1021, 511, 1019}; 00533 00534 for (int i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) { 00535 off_t off = offsets[i]; 00536 memcpy(buffer, "hedgehoghog", size); 00537 res = fseek(fd[0], off, SEEK_SET); 00538 TEST_ASSERT_EQUAL(0, res); 00539 res = fwrite(buffer, 1, size, fd[0]); 00540 TEST_ASSERT_EQUAL(size, res); 00541 res = fseek(fd[0], off, SEEK_SET); 00542 TEST_ASSERT_EQUAL(0, res); 00543 res = fread(buffer, 1, size, fd[0]); 00544 TEST_ASSERT_EQUAL(size, res); 00545 res = memcmp(buffer, "hedgehoghog", size); 00546 TEST_ASSERT_EQUAL(0, res); 00547 res = fseek(fd[0], 0, SEEK_SET); 00548 TEST_ASSERT_EQUAL(0, res); 00549 res = fread(buffer, 1, size, fd[0]); 00550 TEST_ASSERT_EQUAL(size, res); 00551 res = memcmp(buffer, "kittycatcat", size); 00552 TEST_ASSERT_EQUAL(0, res); 00553 res = fflush(fd[0]); 00554 TEST_ASSERT_EQUAL(0, res); 00555 } 00556 res = fclose(fd[0]); 00557 TEST_ASSERT_EQUAL(0, res); 00558 res = fs.unmount(); 00559 TEST_ASSERT_EQUAL(0, res); 00560 } 00561 00562 res = bd.deinit(); 00563 TEST_ASSERT_EQUAL(0, res); 00564 } 00565 00566 void test_out_of_bounds_seek() 00567 { 00568 int res = bd.init(); 00569 TEST_ASSERT_EQUAL(0, res); 00570 00571 { 00572 res = fs.mount(&bd); 00573 TEST_ASSERT_EQUAL(0, res); 00574 res = !((fd[0] = fopen("/fs/" "hello/kitty42", "r+b")) != NULL); 00575 TEST_ASSERT_EQUAL(0, res); 00576 00577 size = strlen("kittycatcat"); 00578 res = fseek(fd[0], 0, SEEK_END); 00579 TEST_ASSERT_EQUAL(0, res); 00580 res = ftell(fd[0]); 00581 TEST_ASSERT_EQUAL(132*size, res); 00582 res = fseek(fd[0], (132+4)*size, 00583 SEEK_SET); 00584 TEST_ASSERT_EQUAL(0, res); 00585 res = fread(buffer, 1, size, fd[0]); 00586 TEST_ASSERT_EQUAL(0, res); 00587 00588 memcpy(buffer, "porcupineee", size); 00589 res = fwrite(buffer, 1, size, fd[0]); 00590 TEST_ASSERT_EQUAL(size, res); 00591 res = fseek(fd[0], (132+4)*size, 00592 SEEK_SET); 00593 TEST_ASSERT_EQUAL(0, res); 00594 res = fread(buffer, 1, size, fd[0]); 00595 TEST_ASSERT_EQUAL(size, res); 00596 res = memcmp(buffer, "porcupineee", size); 00597 TEST_ASSERT_EQUAL(0, res); 00598 res = fseek(fd[0], 132*size, 00599 SEEK_SET); 00600 TEST_ASSERT_EQUAL(0, res); 00601 res = fread(buffer, 1, size, fd[0]); 00602 TEST_ASSERT_EQUAL(size, res); 00603 res = memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size); 00604 TEST_ASSERT_EQUAL(0, res); 00605 res = fclose(fd[0]); 00606 TEST_ASSERT_EQUAL(0, res); 00607 res = fs.unmount(); 00608 TEST_ASSERT_EQUAL(0, res); 00609 } 00610 00611 res = bd.deinit(); 00612 TEST_ASSERT_EQUAL(0, res); 00613 } 00614 00615 00616 00617 // test setup 00618 utest::v1::status_t test_setup(const size_t number_of_cases) 00619 { 00620 GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); 00621 return verbose_test_setup_handler(number_of_cases); 00622 } 00623 00624 Case cases[] = { 00625 Case("Seek tests", test_seek_tests), 00626 Case("Simple dir seek", test_simple_dir_seek), 00627 Case("Large dir seek", test_large_dir_seek), 00628 Case("Simple file seek", test_simple_file_seek), 00629 Case("Large file seek", test_large_file_seek), 00630 Case("Simple file seek and write", test_simple_file_seek_and_write), 00631 Case("Large file seek and write", test_large_file_seek_and_write), 00632 Case("Boundary seek and write", test_boundary_seek_and_write), 00633 Case("Out-of-bounds seek", test_out_of_bounds_seek), 00634 }; 00635 00636 Specification specification(test_setup, cases); 00637 00638 int main() 00639 { 00640 return !Harness::run(specification); 00641 }
Generated on Tue Jul 12 2022 13:24:53 by
1.7.2