Seeed / Mbed OS Wio_3G-example-sd-driver

Fork of Wio_3G-example-sd-driver by Toyomasa Watarai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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_seek_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         res = fs.mount(&bd);
00078         TEST_ASSERT_EQUAL(0, res);
00079         res = fs.mkdir("hello", 0777);
00080         TEST_ASSERT_EQUAL(0, res);
00081         for (int i = 0; i < 132; i++) {
00082             sprintf((char*)buffer, "hello/kitty%d", i);
00083             res = file[0].open(&fs, (char*)buffer,
00084                     O_WRONLY | O_CREAT | O_APPEND);
00085             TEST_ASSERT_EQUAL(0, res);
00086     
00087             size = strlen("kittycatcat");
00088             memcpy(buffer, "kittycatcat", size);
00089             for (int j = 0; j < 132; j++) {
00090                 file[0].write(buffer, size);
00091             }
00092             res = file[0].close();
00093             TEST_ASSERT_EQUAL(0, res);
00094         }
00095         res = fs.unmount();
00096         TEST_ASSERT_EQUAL(0, res);
00097     }
00098 
00099     res = bd.deinit();
00100     TEST_ASSERT_EQUAL(0, res);
00101 }
00102 
00103 void test_simple_dir_seek() {
00104     int res = bd.init();
00105     TEST_ASSERT_EQUAL(0, res);
00106 
00107     {
00108         res = fs.mount(&bd);
00109         TEST_ASSERT_EQUAL(0, res);
00110         res = dir[0].open(&fs, "hello");
00111         TEST_ASSERT_EQUAL(0, res);
00112 #if (MBED_TEST_FILESYSTEM != FATFileSystem)
00113         res = dir[0].read(&ent);
00114         TEST_ASSERT_EQUAL(1, res);
00115         res = strcmp(ent.d_name, ".");
00116         TEST_ASSERT_EQUAL(0, res);
00117         res = dir[0].read(&ent);
00118         TEST_ASSERT_EQUAL(1, res);
00119         res = strcmp(ent.d_name, "..");
00120         TEST_ASSERT_EQUAL(0, res);
00121 #endif
00122 
00123         off_t pos;
00124         int i;
00125         for (i = 0; i < 4; i++) {
00126             sprintf((char*)buffer, "kitty%d", i);
00127             res = dir[0].read(&ent);
00128             TEST_ASSERT_EQUAL(1, res);
00129             res = strcmp(ent.d_name, (char*)buffer);
00130             TEST_ASSERT_EQUAL(0, res);
00131             pos = dir[0].tell();
00132         }
00133         res = pos >= 0;
00134         TEST_ASSERT_EQUAL(1, res);
00135     
00136         dir[0].seek(pos);
00137         sprintf((char*)buffer, "kitty%d", i);
00138         res = dir[0].read(&ent);
00139         TEST_ASSERT_EQUAL(1, res);
00140         res = strcmp(ent.d_name, (char*)buffer);
00141         TEST_ASSERT_EQUAL(0, res);
00142     
00143         dir[0].rewind();
00144         sprintf((char*)buffer, "kitty%d", 0);
00145 #if (MBED_TEST_FILESYSTEM != FATFileSystem)
00146         res = dir[0].read(&ent);
00147         TEST_ASSERT_EQUAL(1, res);
00148         res = strcmp(ent.d_name, ".");
00149         TEST_ASSERT_EQUAL(0, res);
00150         res = dir[0].read(&ent);
00151         TEST_ASSERT_EQUAL(1, res);
00152         res = strcmp(ent.d_name, "..");
00153         TEST_ASSERT_EQUAL(0, res);
00154 #endif
00155         res = dir[0].read(&ent);
00156         TEST_ASSERT_EQUAL(1, res);
00157         res = strcmp(ent.d_name, (char*)buffer);
00158         TEST_ASSERT_EQUAL(0, res);
00159     
00160         dir[0].seek(pos);
00161         sprintf((char*)buffer, "kitty%d", i);
00162         res = dir[0].read(&ent);
00163         TEST_ASSERT_EQUAL(1, res);
00164         res = strcmp(ent.d_name, (char*)buffer);
00165         TEST_ASSERT_EQUAL(0, res);
00166         res = dir[0].close();
00167         TEST_ASSERT_EQUAL(0, res);
00168         res = fs.unmount();
00169         TEST_ASSERT_EQUAL(0, res);
00170     }
00171 
00172     res = bd.deinit();
00173     TEST_ASSERT_EQUAL(0, res);
00174 }
00175 
00176 void test_large_dir_seek() {
00177     int res = bd.init();
00178     TEST_ASSERT_EQUAL(0, res);
00179 
00180     {
00181         res = fs.mount(&bd);
00182         TEST_ASSERT_EQUAL(0, res);
00183         res = dir[0].open(&fs, "hello");
00184         TEST_ASSERT_EQUAL(0, res);
00185 #if (MBED_TEST_FILESYSTEM != FATFileSystem)
00186         res = dir[0].read(&ent);
00187         TEST_ASSERT_EQUAL(1, res);
00188         res = strcmp(ent.d_name, ".");
00189         TEST_ASSERT_EQUAL(0, res);
00190         res = dir[0].read(&ent);
00191         TEST_ASSERT_EQUAL(1, res);
00192         res = strcmp(ent.d_name, "..");
00193         TEST_ASSERT_EQUAL(0, res);
00194 #endif
00195 
00196         off_t pos;
00197         int i;
00198         for (i = 0; i < 128; i++) {
00199             sprintf((char*)buffer, "kitty%d", i);
00200             res = dir[0].read(&ent);
00201             TEST_ASSERT_EQUAL(1, res);
00202             res = strcmp(ent.d_name, (char*)buffer);
00203             TEST_ASSERT_EQUAL(0, res);
00204             pos = dir[0].tell();
00205         }
00206         res = pos >= 0;
00207         TEST_ASSERT_EQUAL(1, res);
00208     
00209         dir[0].seek(pos);
00210         sprintf((char*)buffer, "kitty%d", i);
00211         res = dir[0].read(&ent);
00212         TEST_ASSERT_EQUAL(1, res);
00213         res = strcmp(ent.d_name, (char*)buffer);
00214         TEST_ASSERT_EQUAL(0, res);
00215     
00216         dir[0].rewind();
00217         sprintf((char*)buffer, "kitty%d", 0);
00218 #if (MBED_TEST_FILESYSTEM != FATFileSystem)
00219         res = dir[0].read(&ent);
00220         TEST_ASSERT_EQUAL(1, res);
00221         res = strcmp(ent.d_name, ".");
00222         TEST_ASSERT_EQUAL(0, res);
00223         res = dir[0].read(&ent);
00224         TEST_ASSERT_EQUAL(1, res);
00225         res = strcmp(ent.d_name, "..");
00226         TEST_ASSERT_EQUAL(0, res);
00227 #endif
00228         res = dir[0].read(&ent);
00229         TEST_ASSERT_EQUAL(1, res);
00230         res = strcmp(ent.d_name, (char*)buffer);
00231         TEST_ASSERT_EQUAL(0, res);
00232     
00233         dir[0].seek(pos);
00234         sprintf((char*)buffer, "kitty%d", i);
00235         res = dir[0].read(&ent);
00236         TEST_ASSERT_EQUAL(1, res);
00237         res = strcmp(ent.d_name, (char*)buffer);
00238         TEST_ASSERT_EQUAL(0, res);
00239         res = dir[0].close();
00240         TEST_ASSERT_EQUAL(0, res);
00241         res = fs.unmount();
00242         TEST_ASSERT_EQUAL(0, res);
00243     }
00244 
00245     res = bd.deinit();
00246     TEST_ASSERT_EQUAL(0, res);
00247 }
00248 
00249 void test_simple_file_seek() {
00250     int res = bd.init();
00251     TEST_ASSERT_EQUAL(0, res);
00252 
00253     {
00254         res = fs.mount(&bd);
00255         TEST_ASSERT_EQUAL(0, res);
00256         res = file[0].open(&fs, "hello/kitty42", O_RDONLY);
00257         TEST_ASSERT_EQUAL(0, res);
00258     
00259         off_t pos;
00260         size = strlen("kittycatcat");
00261         for (int i = 0; i < 4; i++) {
00262             res = file[0].read(buffer, size);
00263             TEST_ASSERT_EQUAL(size, res);
00264             res = memcmp(buffer, "kittycatcat", size);
00265             TEST_ASSERT_EQUAL(0, res);
00266             pos = file[0].tell();
00267         }
00268         res = pos >= 0;
00269         TEST_ASSERT_EQUAL(1, res);
00270         res = file[0].seek(pos, SEEK_SET);
00271         TEST_ASSERT_EQUAL(pos, res);
00272         res = file[0].read(buffer, size);
00273         TEST_ASSERT_EQUAL(size, res);
00274         res = memcmp(buffer, "kittycatcat", size);
00275         TEST_ASSERT_EQUAL(0, res);
00276     
00277         file[0].rewind();
00278         res = file[0].read(buffer, size);
00279         TEST_ASSERT_EQUAL(size, res);
00280         res = memcmp(buffer, "kittycatcat", size);
00281         TEST_ASSERT_EQUAL(0, res);
00282         res = file[0].seek(pos, SEEK_SET);
00283         TEST_ASSERT_EQUAL(pos, res);
00284         res = file[0].read(buffer, size);
00285         TEST_ASSERT_EQUAL(size, res);
00286         res = memcmp(buffer, "kittycatcat", size);
00287         TEST_ASSERT_EQUAL(0, res);
00288         res = file[0].seek(-size, SEEK_CUR);
00289         TEST_ASSERT_EQUAL(pos, res);
00290         res = file[0].read(buffer, size);
00291         TEST_ASSERT_EQUAL(size, res);
00292         res = memcmp(buffer, "kittycatcat", size);
00293         TEST_ASSERT_EQUAL(0, res);
00294         res = file[0].seek(-size, SEEK_END) >= 0;
00295         TEST_ASSERT_EQUAL(1, res);
00296         res = file[0].read(buffer, size);
00297         TEST_ASSERT_EQUAL(size, res);
00298         res = memcmp(buffer, "kittycatcat", size);
00299         TEST_ASSERT_EQUAL(0, res);
00300     
00301         size_t size = file[0].size();
00302         res = file[0].seek(0, SEEK_CUR);
00303         TEST_ASSERT_EQUAL(size, res);
00304         res = file[0].close();
00305         TEST_ASSERT_EQUAL(0, res);
00306         res = fs.unmount();
00307         TEST_ASSERT_EQUAL(0, res);
00308     }
00309 
00310     res = bd.deinit();
00311     TEST_ASSERT_EQUAL(0, res);
00312 }
00313 
00314 void test_large_file_seek() {
00315     int res = bd.init();
00316     TEST_ASSERT_EQUAL(0, res);
00317 
00318     {
00319         res = fs.mount(&bd);
00320         TEST_ASSERT_EQUAL(0, res);
00321         res = file[0].open(&fs, "hello/kitty42", O_RDONLY);
00322         TEST_ASSERT_EQUAL(0, res);
00323     
00324         off_t pos;
00325         size = strlen("kittycatcat");
00326         for (int i = 0; i < 128; i++) {
00327             res = file[0].read(buffer, size);
00328             TEST_ASSERT_EQUAL(size, res);
00329             res = memcmp(buffer, "kittycatcat", size);
00330             TEST_ASSERT_EQUAL(0, res);
00331             pos = file[0].tell();
00332         }
00333         res = pos >= 0;
00334         TEST_ASSERT_EQUAL(1, res);
00335         res = file[0].seek(pos, SEEK_SET);
00336         TEST_ASSERT_EQUAL(pos, res);
00337         res = file[0].read(buffer, size);
00338         TEST_ASSERT_EQUAL(size, res);
00339         res = memcmp(buffer, "kittycatcat", size);
00340         TEST_ASSERT_EQUAL(0, res);
00341     
00342         file[0].rewind();
00343         res = file[0].read(buffer, size);
00344         TEST_ASSERT_EQUAL(size, res);
00345         res = memcmp(buffer, "kittycatcat", size);
00346         TEST_ASSERT_EQUAL(0, res);
00347         res = file[0].seek(pos, SEEK_SET);
00348         TEST_ASSERT_EQUAL(pos, res);
00349         res = file[0].read(buffer, size);
00350         TEST_ASSERT_EQUAL(size, res);
00351         res = memcmp(buffer, "kittycatcat", size);
00352         TEST_ASSERT_EQUAL(0, res);
00353         res = file[0].seek(-size, SEEK_CUR);
00354         TEST_ASSERT_EQUAL(pos, res);
00355         res = file[0].read(buffer, size);
00356         TEST_ASSERT_EQUAL(size, res);
00357         res = memcmp(buffer, "kittycatcat", size);
00358         TEST_ASSERT_EQUAL(0, res);
00359         res = file[0].seek(-size, SEEK_END) >= 0;
00360         TEST_ASSERT_EQUAL(1, res);
00361         res = file[0].read(buffer, size);
00362         TEST_ASSERT_EQUAL(size, res);
00363         res = memcmp(buffer, "kittycatcat", size);
00364         TEST_ASSERT_EQUAL(0, res);
00365     
00366         size_t size = file[0].size();
00367         res = file[0].seek(0, SEEK_CUR);
00368         TEST_ASSERT_EQUAL(size, res);
00369         res = file[0].close();
00370         TEST_ASSERT_EQUAL(0, res);
00371         res = fs.unmount();
00372         TEST_ASSERT_EQUAL(0, res);
00373     }
00374 
00375     res = bd.deinit();
00376     TEST_ASSERT_EQUAL(0, res);
00377 }
00378 
00379 void test_simple_file_seek_and_write() {
00380     int res = bd.init();
00381     TEST_ASSERT_EQUAL(0, res);
00382 
00383     {
00384         res = fs.mount(&bd);
00385         TEST_ASSERT_EQUAL(0, res);
00386         res = file[0].open(&fs, "hello/kitty42", O_RDWR);
00387         TEST_ASSERT_EQUAL(0, res);
00388     
00389         off_t pos;
00390         size = strlen("kittycatcat");
00391         for (int i = 0; i < 4; i++) {
00392             res = file[0].read(buffer, size);
00393             TEST_ASSERT_EQUAL(size, res);
00394             res = memcmp(buffer, "kittycatcat", size);
00395             TEST_ASSERT_EQUAL(0, res);
00396             pos = file[0].tell();
00397         }
00398         res = pos >= 0;
00399         TEST_ASSERT_EQUAL(1, res);
00400     
00401         memcpy(buffer, "doggodogdog", size);
00402         res = file[0].seek(pos, SEEK_SET);
00403         TEST_ASSERT_EQUAL(pos, res);
00404         res = file[0].write(buffer, size);
00405         TEST_ASSERT_EQUAL(size, res);
00406         res = file[0].seek(pos, SEEK_SET);
00407         TEST_ASSERT_EQUAL(pos, res);
00408         res = file[0].read(buffer, size);
00409         TEST_ASSERT_EQUAL(size, res);
00410         res = memcmp(buffer, "doggodogdog", size);
00411         TEST_ASSERT_EQUAL(0, res);
00412     
00413         file[0].rewind();
00414         res = file[0].read(buffer, size);
00415         TEST_ASSERT_EQUAL(size, res);
00416         res = memcmp(buffer, "kittycatcat", size);
00417         TEST_ASSERT_EQUAL(0, res);
00418         res = file[0].seek(pos, SEEK_SET);
00419         TEST_ASSERT_EQUAL(pos, res);
00420         res = file[0].read(buffer, size);
00421         TEST_ASSERT_EQUAL(size, res);
00422         res = memcmp(buffer, "doggodogdog", size);
00423         TEST_ASSERT_EQUAL(0, res);
00424         res = file[0].seek(-size, SEEK_END) >= 0;
00425         TEST_ASSERT_EQUAL(1, res);
00426         res = file[0].read(buffer, size);
00427         TEST_ASSERT_EQUAL(size, res);
00428         res = memcmp(buffer, "kittycatcat", size);
00429         TEST_ASSERT_EQUAL(0, res);
00430     
00431         size_t size = file[0].size();
00432         res = file[0].seek(0, SEEK_CUR);
00433         TEST_ASSERT_EQUAL(size, res);
00434         res = file[0].close();
00435         TEST_ASSERT_EQUAL(0, res);
00436         res = fs.unmount();
00437         TEST_ASSERT_EQUAL(0, res);
00438     }
00439 
00440     res = bd.deinit();
00441     TEST_ASSERT_EQUAL(0, res);
00442 }
00443 
00444 void test_large_file_seek_and_write() {
00445     int res = bd.init();
00446     TEST_ASSERT_EQUAL(0, res);
00447 
00448     {
00449         res = fs.mount(&bd);
00450         TEST_ASSERT_EQUAL(0, res);
00451         res = file[0].open(&fs, "hello/kitty42", O_RDWR);
00452         TEST_ASSERT_EQUAL(0, res);
00453     
00454         off_t pos;
00455         size = strlen("kittycatcat");
00456         for (int i = 0; i < 128; i++) {
00457             res = file[0].read(buffer, size);
00458             TEST_ASSERT_EQUAL(size, res);
00459             if (i != 4) {
00460                 res = memcmp(buffer, "kittycatcat", size);
00461                 TEST_ASSERT_EQUAL(0, res);
00462             }
00463             pos = file[0].tell();
00464         }
00465         res = pos >= 0;
00466         TEST_ASSERT_EQUAL(1, res);
00467     
00468         memcpy(buffer, "doggodogdog", size);
00469         res = file[0].seek(pos, SEEK_SET);
00470         TEST_ASSERT_EQUAL(pos, res);
00471         res = file[0].write(buffer, size);
00472         TEST_ASSERT_EQUAL(size, res);
00473         res = file[0].seek(pos, SEEK_SET);
00474         TEST_ASSERT_EQUAL(pos, res);
00475         res = file[0].read(buffer, size);
00476         TEST_ASSERT_EQUAL(size, res);
00477         res = memcmp(buffer, "doggodogdog", size);
00478         TEST_ASSERT_EQUAL(0, res);
00479     
00480         file[0].rewind();
00481         res = file[0].read(buffer, size);
00482         TEST_ASSERT_EQUAL(size, res);
00483         res = memcmp(buffer, "kittycatcat", size);
00484         TEST_ASSERT_EQUAL(0, res);
00485         res = file[0].seek(pos, SEEK_SET);
00486         TEST_ASSERT_EQUAL(pos, res);
00487         res = file[0].read(buffer, size);
00488         TEST_ASSERT_EQUAL(size, res);
00489         res = memcmp(buffer, "doggodogdog", size);
00490         TEST_ASSERT_EQUAL(0, res);
00491         res = file[0].seek(-size, SEEK_END) >= 0;
00492         TEST_ASSERT_EQUAL(1, res);
00493         res = file[0].read(buffer, size);
00494         TEST_ASSERT_EQUAL(size, res);
00495         res = memcmp(buffer, "kittycatcat", size);
00496         TEST_ASSERT_EQUAL(0, res);
00497     
00498         size_t size = file[0].size();
00499         res = file[0].seek(0, SEEK_CUR);
00500         TEST_ASSERT_EQUAL(size, res);
00501         res = file[0].close();
00502         TEST_ASSERT_EQUAL(0, res);
00503         res = fs.unmount();
00504         TEST_ASSERT_EQUAL(0, res);
00505     }
00506 
00507     res = bd.deinit();
00508     TEST_ASSERT_EQUAL(0, res);
00509 }
00510 
00511 void test_boundary_seek_and_write() {
00512     int res = bd.init();
00513     TEST_ASSERT_EQUAL(0, res);
00514 
00515     {
00516         res = fs.mount(&bd);
00517         TEST_ASSERT_EQUAL(0, res);
00518         res = file[0].open(&fs, "hello/kitty42", O_RDWR);
00519         TEST_ASSERT_EQUAL(0, res);
00520     
00521         size = strlen("hedgehoghog");
00522         const off_t offsets[] = {512, 1020, 513, 1021, 511, 1019};
00523     
00524         for (int i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) {
00525             off_t off = offsets[i];
00526             memcpy(buffer, "hedgehoghog", size);
00527             res = file[0].seek(off, SEEK_SET);
00528             TEST_ASSERT_EQUAL(off, res);
00529             res = file[0].write(buffer, size);
00530             TEST_ASSERT_EQUAL(size, res);
00531             res = file[0].seek(off, SEEK_SET);
00532             TEST_ASSERT_EQUAL(off, res);
00533             res = file[0].read(buffer, size);
00534             TEST_ASSERT_EQUAL(size, res);
00535             res = memcmp(buffer, "hedgehoghog", size);
00536             TEST_ASSERT_EQUAL(0, res);
00537             res = file[0].seek(0, SEEK_SET);
00538             TEST_ASSERT_EQUAL(0, res);
00539             res = file[0].read(buffer, size);
00540             TEST_ASSERT_EQUAL(size, res);
00541             res = memcmp(buffer, "kittycatcat", size);
00542             TEST_ASSERT_EQUAL(0, res);
00543             res = file[0].sync();
00544             TEST_ASSERT_EQUAL(0, res);
00545         }
00546         res = file[0].close();
00547         TEST_ASSERT_EQUAL(0, res);
00548         res = fs.unmount();
00549         TEST_ASSERT_EQUAL(0, res);
00550     }
00551 
00552     res = bd.deinit();
00553     TEST_ASSERT_EQUAL(0, res);
00554 }
00555 
00556 void test_out_of_bounds_seek() {
00557     int res = bd.init();
00558     TEST_ASSERT_EQUAL(0, res);
00559 
00560     {
00561         res = fs.mount(&bd);
00562         TEST_ASSERT_EQUAL(0, res);
00563         res = file[0].open(&fs, "hello/kitty42", O_RDWR);
00564         TEST_ASSERT_EQUAL(0, res);
00565     
00566         size = strlen("kittycatcat");
00567         res = file[0].size();
00568         TEST_ASSERT_EQUAL(132*size, res);
00569         res = file[0].seek((132+4)*size,
00570                 SEEK_SET);
00571         TEST_ASSERT_EQUAL((132+4)*size, res);
00572         res = file[0].read(buffer, size);
00573         TEST_ASSERT_EQUAL(0, res);
00574     
00575         memcpy(buffer, "porcupineee", size);
00576         res = file[0].write(buffer, size);
00577         TEST_ASSERT_EQUAL(size, res);
00578         res = file[0].seek((132+4)*size,
00579                 SEEK_SET);
00580         TEST_ASSERT_EQUAL((132+4)*size, res);
00581         res = file[0].read(buffer, size);
00582         TEST_ASSERT_EQUAL(size, res);
00583         res = memcmp(buffer, "porcupineee", size);
00584         TEST_ASSERT_EQUAL(0, res);
00585         res = file[0].seek(132*size,
00586                 SEEK_SET);
00587         TEST_ASSERT_EQUAL(132*size, res);
00588         res = file[0].read(buffer, size);
00589         TEST_ASSERT_EQUAL(size, res);
00590 #if (MBED_TEST_FILESYSTEM != FATFileSystem)
00591         // FatFs does not guarantee empty expanded buffer
00592         res = memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size);
00593         TEST_ASSERT_EQUAL(0, res);
00594 #endif
00595         res = file[0].close();
00596         TEST_ASSERT_EQUAL(0, res);
00597         res = fs.unmount();
00598         TEST_ASSERT_EQUAL(0, res);
00599     }
00600 
00601     res = bd.deinit();
00602     TEST_ASSERT_EQUAL(0, res);
00603 }
00604 
00605 
00606 
00607 // test setup
00608 utest::v1::status_t test_setup(const size_t number_of_cases) {
00609     GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
00610     return verbose_test_setup_handler(number_of_cases);
00611 }
00612 
00613 Case cases[] = {
00614     Case("Seek tests", test_seek_tests),
00615     Case("Simple dir seek", test_simple_dir_seek),
00616     Case("Large dir seek", test_large_dir_seek),
00617     Case("Simple file seek", test_simple_file_seek),
00618     Case("Large file seek", test_large_file_seek),
00619     Case("Simple file seek and write", test_simple_file_seek_and_write),
00620     Case("Large file seek and write", test_large_file_seek_and_write),
00621     Case("Boundary seek and write", test_boundary_seek_and_write),
00622     Case("Out-of-bounds seek", test_out_of_bounds_seek),
00623 };
00624 
00625 Specification specification(test_setup, cases);
00626 
00627 int main() {
00628     return !Harness::run(specification);
00629 }