init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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_file_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     }
00093 
00094     res = bd.deinit();
00095     TEST_ASSERT_EQUAL(0, res);
00096 }
00097 
00098 void test_simple_file_test()
00099 {
00100     int res = bd.init();
00101     TEST_ASSERT_EQUAL(0, res);
00102 
00103     {
00104         res = fs.mount(&bd);
00105         TEST_ASSERT_EQUAL(0, res);
00106         res = file[0].open(&fs, "hello", O_WRONLY | O_CREAT);
00107         TEST_ASSERT_EQUAL(0, res);
00108         size = strlen("Hello World!\n");
00109         memcpy(wbuffer, "Hello World!\n", size);
00110         res = file[0].write(wbuffer, size);
00111         TEST_ASSERT_EQUAL(size, res);
00112         res = file[0].close();
00113         TEST_ASSERT_EQUAL(0, res);
00114         res = file[0].open(&fs, "hello", O_RDONLY);
00115         TEST_ASSERT_EQUAL(0, res);
00116         size = strlen("Hello World!\n");
00117         res = file[0].read(rbuffer, size);
00118         TEST_ASSERT_EQUAL(size, res);
00119         res = memcmp(rbuffer, wbuffer, size);
00120         TEST_ASSERT_EQUAL(0, res);
00121         res = file[0].close();
00122         TEST_ASSERT_EQUAL(0, res);
00123         res = fs.unmount();
00124         TEST_ASSERT_EQUAL(0, res);
00125     }
00126 
00127     res = bd.deinit();
00128     TEST_ASSERT_EQUAL(0, res);
00129 }
00130 
00131 void test_small_file_test()
00132 {
00133     int res = bd.init();
00134     TEST_ASSERT_EQUAL(0, res);
00135 
00136     {
00137         size_t size = 32;
00138         size_t chunk = 31;
00139         srand(0);
00140         res = fs.mount(&bd);
00141         TEST_ASSERT_EQUAL(0, res);
00142         res = file[0].open(&fs, "smallavacado", O_WRONLY | O_CREAT);
00143         TEST_ASSERT_EQUAL(0, res);
00144         for (size_t i = 0; i < size; i += chunk) {
00145             chunk = (chunk < size - i) ? chunk : size - i;
00146             for (size_t b = 0; b < chunk; b++) {
00147                 buffer[b] = rand() & 0xff;
00148             }
00149             res = file[0].write(buffer, chunk);
00150             TEST_ASSERT_EQUAL(chunk, res);
00151         }
00152         res = file[0].close();
00153         TEST_ASSERT_EQUAL(0, res);
00154         res = fs.unmount();
00155         TEST_ASSERT_EQUAL(0, res);
00156     }
00157 
00158     {
00159         size_t size = 32;
00160         size_t chunk = 29;
00161         srand(0);
00162         res = fs.mount(&bd);
00163         TEST_ASSERT_EQUAL(0, res);
00164         res = file[0].open(&fs, "smallavacado", O_RDONLY);
00165         TEST_ASSERT_EQUAL(0, res);
00166         for (size_t i = 0; i < size; i += chunk) {
00167             chunk = (chunk < size - i) ? chunk : size - i;
00168             res = file[0].read(buffer, chunk);
00169             TEST_ASSERT_EQUAL(chunk, res);
00170             for (size_t b = 0; b < chunk && i+b < size; b++) {
00171                 res = buffer[b];
00172                 TEST_ASSERT_EQUAL(rand() & 0xff, res);
00173             }
00174         }
00175         res = file[0].close();
00176         TEST_ASSERT_EQUAL(0, res);
00177         res = fs.unmount();
00178         TEST_ASSERT_EQUAL(0, res);
00179     }
00180 
00181     res = bd.deinit();
00182     TEST_ASSERT_EQUAL(0, res);
00183 }
00184 
00185 void test_medium_file_test()
00186 {
00187     int res = bd.init();
00188     TEST_ASSERT_EQUAL(0, res);
00189 
00190     {
00191         size_t size = 8192;
00192         size_t chunk = 31;
00193         srand(0);
00194         res = fs.mount(&bd);
00195         TEST_ASSERT_EQUAL(0, res);
00196         res = file[0].open(&fs, "mediumavacado", O_WRONLY | O_CREAT);
00197         TEST_ASSERT_EQUAL(0, res);
00198         for (size_t i = 0; i < size; i += chunk) {
00199             chunk = (chunk < size - i) ? chunk : size - i;
00200             for (size_t b = 0; b < chunk; b++) {
00201                 buffer[b] = rand() & 0xff;
00202             }
00203             res = file[0].write(buffer, chunk);
00204             TEST_ASSERT_EQUAL(chunk, res);
00205         }
00206         res = file[0].close();
00207         TEST_ASSERT_EQUAL(0, res);
00208         res = fs.unmount();
00209         TEST_ASSERT_EQUAL(0, res);
00210     }
00211 
00212     {
00213         size_t size = 8192;
00214         size_t chunk = 29;
00215         srand(0);
00216         res = fs.mount(&bd);
00217         TEST_ASSERT_EQUAL(0, res);
00218         res = file[0].open(&fs, "mediumavacado", O_RDONLY);
00219         TEST_ASSERT_EQUAL(0, res);
00220         for (size_t i = 0; i < size; i += chunk) {
00221             chunk = (chunk < size - i) ? chunk : size - i;
00222             res = file[0].read(buffer, chunk);
00223             TEST_ASSERT_EQUAL(chunk, res);
00224             for (size_t b = 0; b < chunk && i+b < size; b++) {
00225                 res = buffer[b];
00226                 TEST_ASSERT_EQUAL(rand() & 0xff, res);
00227             }
00228         }
00229         res = file[0].close();
00230         TEST_ASSERT_EQUAL(0, res);
00231         res = fs.unmount();
00232         TEST_ASSERT_EQUAL(0, res);
00233     }
00234 
00235     res = bd.deinit();
00236     TEST_ASSERT_EQUAL(0, res);
00237 }
00238 
00239 void test_large_file_test()
00240 {
00241     int res = bd.init();
00242     TEST_ASSERT_EQUAL(0, res);
00243 
00244     {
00245         size_t size = 262144;
00246         size_t chunk = 31;
00247         srand(0);
00248         res = fs.mount(&bd);
00249         TEST_ASSERT_EQUAL(0, res);
00250         res = file[0].open(&fs, "largeavacado", O_WRONLY | O_CREAT);
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             for (size_t b = 0; b < chunk; b++) {
00255                 buffer[b] = rand() & 0xff;
00256             }
00257             res = file[0].write(buffer, chunk);
00258             TEST_ASSERT_EQUAL(chunk, res);
00259         }
00260         res = file[0].close();
00261         TEST_ASSERT_EQUAL(0, res);
00262         res = fs.unmount();
00263         TEST_ASSERT_EQUAL(0, res);
00264     }
00265 
00266     {
00267         size_t size = 262144;
00268         size_t chunk = 29;
00269         srand(0);
00270         res = fs.mount(&bd);
00271         TEST_ASSERT_EQUAL(0, res);
00272         res = file[0].open(&fs, "largeavacado", O_RDONLY);
00273         TEST_ASSERT_EQUAL(0, res);
00274         for (size_t i = 0; i < size; i += chunk) {
00275             chunk = (chunk < size - i) ? chunk : size - i;
00276             res = file[0].read(buffer, chunk);
00277             TEST_ASSERT_EQUAL(chunk, res);
00278             for (size_t b = 0; b < chunk && i+b < size; b++) {
00279                 res = buffer[b];
00280                 TEST_ASSERT_EQUAL(rand() & 0xff, res);
00281             }
00282         }
00283         res = file[0].close();
00284         TEST_ASSERT_EQUAL(0, res);
00285         res = fs.unmount();
00286         TEST_ASSERT_EQUAL(0, res);
00287     }
00288 
00289     res = bd.deinit();
00290     TEST_ASSERT_EQUAL(0, res);
00291 }
00292 
00293 void test_non_overlap_check()
00294 {
00295     int res = bd.init();
00296     TEST_ASSERT_EQUAL(0, res);
00297 
00298     {
00299         size_t size = 32;
00300         size_t chunk = 29;
00301         srand(0);
00302         res = fs.mount(&bd);
00303         TEST_ASSERT_EQUAL(0, res);
00304         res = file[0].open(&fs, "smallavacado", O_RDONLY);
00305         TEST_ASSERT_EQUAL(0, res);
00306         for (size_t i = 0; i < size; i += chunk) {
00307             chunk = (chunk < size - i) ? chunk : size - i;
00308             res = file[0].read(buffer, chunk);
00309             TEST_ASSERT_EQUAL(chunk, res);
00310             for (size_t b = 0; b < chunk && i+b < size; b++) {
00311                 res = buffer[b];
00312                 TEST_ASSERT_EQUAL(rand() & 0xff, res);
00313             }
00314         }
00315         res = file[0].close();
00316         TEST_ASSERT_EQUAL(0, res);
00317         res = fs.unmount();
00318         TEST_ASSERT_EQUAL(0, res);
00319     }
00320 
00321     {
00322         size_t size = 8192;
00323         size_t chunk = 29;
00324         srand(0);
00325         res = fs.mount(&bd);
00326         TEST_ASSERT_EQUAL(0, res);
00327         res = file[0].open(&fs, "mediumavacado", O_RDONLY);
00328         TEST_ASSERT_EQUAL(0, res);
00329         for (size_t i = 0; i < size; i += chunk) {
00330             chunk = (chunk < size - i) ? chunk : size - i;
00331             res = file[0].read(buffer, chunk);
00332             TEST_ASSERT_EQUAL(chunk, res);
00333             for (size_t b = 0; b < chunk && i+b < size; b++) {
00334                 res = buffer[b];
00335                 TEST_ASSERT_EQUAL(rand() & 0xff, res);
00336             }
00337         }
00338         res = file[0].close();
00339         TEST_ASSERT_EQUAL(0, res);
00340         res = fs.unmount();
00341         TEST_ASSERT_EQUAL(0, res);
00342     }
00343 
00344     {
00345         size_t size = 262144;
00346         size_t chunk = 29;
00347         srand(0);
00348         res = fs.mount(&bd);
00349         TEST_ASSERT_EQUAL(0, res);
00350         res = file[0].open(&fs, "largeavacado", O_RDONLY);
00351         TEST_ASSERT_EQUAL(0, res);
00352         for (size_t i = 0; i < size; i += chunk) {
00353             chunk = (chunk < size - i) ? chunk : size - i;
00354             res = file[0].read(buffer, chunk);
00355             TEST_ASSERT_EQUAL(chunk, res);
00356             for (size_t b = 0; b < chunk && i+b < size; b++) {
00357                 res = buffer[b];
00358                 TEST_ASSERT_EQUAL(rand() & 0xff, res);
00359             }
00360         }
00361         res = file[0].close();
00362         TEST_ASSERT_EQUAL(0, res);
00363         res = fs.unmount();
00364         TEST_ASSERT_EQUAL(0, res);
00365     }
00366 
00367     res = bd.deinit();
00368     TEST_ASSERT_EQUAL(0, res);
00369 }
00370 
00371 void test_dir_check()
00372 {
00373     int res = bd.init();
00374     TEST_ASSERT_EQUAL(0, res);
00375 
00376     {
00377         res = fs.mount(&bd);
00378         TEST_ASSERT_EQUAL(0, res);
00379         res = dir[0].open(&fs, "/");
00380         TEST_ASSERT_EQUAL(0, res);
00381         res = dir[0].read(&ent);
00382         TEST_ASSERT_EQUAL(1, res);
00383         res = dir[0].read(&ent);
00384         TEST_ASSERT_EQUAL(1, res);
00385         res = dir[0].read(&ent);
00386         TEST_ASSERT_EQUAL(1, res);
00387         res = strcmp(ent.d_name, "hello");
00388         TEST_ASSERT_EQUAL(0, res);
00389         res = ent.d_type;
00390         TEST_ASSERT_EQUAL(DT_REG, res);
00391     
00392         res = dir[0].read(&ent);
00393         TEST_ASSERT_EQUAL(1, res);
00394         res = strcmp(ent.d_name, "smallavacado");
00395         TEST_ASSERT_EQUAL(0, res);
00396         res = ent.d_type;
00397         TEST_ASSERT_EQUAL(DT_REG, res);
00398     
00399         res = dir[0].read(&ent);
00400         TEST_ASSERT_EQUAL(1, res);
00401         res = strcmp(ent.d_name, "mediumavacado");
00402         TEST_ASSERT_EQUAL(0, res);
00403         res = ent.d_type;
00404         TEST_ASSERT_EQUAL(DT_REG, res);
00405     
00406         res = dir[0].read(&ent);
00407         TEST_ASSERT_EQUAL(1, res);
00408         res = strcmp(ent.d_name, "largeavacado");
00409         TEST_ASSERT_EQUAL(0, res);
00410         res = ent.d_type;
00411         TEST_ASSERT_EQUAL(DT_REG, res);
00412     
00413         res = dir[0].read(&ent);
00414         TEST_ASSERT_EQUAL(0, res);
00415         res = dir[0].close();
00416         TEST_ASSERT_EQUAL(0, res);
00417         res = fs.unmount();
00418         TEST_ASSERT_EQUAL(0, res);
00419     }
00420 
00421     res = bd.deinit();
00422     TEST_ASSERT_EQUAL(0, res);
00423 }
00424 
00425 
00426 
00427 // test setup
00428 utest::v1::status_t test_setup(const size_t number_of_cases)
00429 {
00430     GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
00431     return verbose_test_setup_handler(number_of_cases);
00432 }
00433 
00434 Case cases[] = {
00435     Case("File tests", test_file_tests),
00436     Case("Simple file test", test_simple_file_test),
00437     Case("Small file test", test_small_file_test),
00438     Case("Medium file test", test_medium_file_test),
00439     Case("Large file test", test_large_file_test),
00440     Case("Non-overlap check", test_non_overlap_check),
00441     Case("Dir check", test_dir_check),
00442 };
00443 
00444 Specification specification(test_setup, cases);
00445 
00446 int main()
00447 {
00448     return !Harness::run(specification);
00449 }