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_parallel_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_parallel_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, "a", O_WRONLY | O_CREAT);
00107         TEST_ASSERT_EQUAL(0, res);
00108         res = file[1].open(&fs, "b", O_WRONLY | O_CREAT);
00109         TEST_ASSERT_EQUAL(0, res);
00110         res = file[2].open(&fs, "c", O_WRONLY | O_CREAT);
00111         TEST_ASSERT_EQUAL(0, res);
00112         res = file[3].open(&fs, "d", O_WRONLY | O_CREAT);
00113         TEST_ASSERT_EQUAL(0, res);
00114     
00115         for (int i = 0; i < 10; i++) {
00116             res = file[0].write((const void*)"a", 1);
00117             TEST_ASSERT_EQUAL(1, res);
00118             res = file[1].write((const void*)"b", 1);
00119             TEST_ASSERT_EQUAL(1, res);
00120             res = file[2].write((const void*)"c", 1);
00121             TEST_ASSERT_EQUAL(1, res);
00122             res = file[3].write((const void*)"d", 1);
00123             TEST_ASSERT_EQUAL(1, res);
00124         }
00125     
00126         file[0].close();
00127         file[1].close();
00128         file[2].close();
00129         file[3].close();
00130         res = dir[0].open(&fs, "/");
00131         TEST_ASSERT_EQUAL(0, res);
00132         res = dir[0].read(&ent);
00133         TEST_ASSERT_EQUAL(1, res);
00134         res = strcmp(ent.d_name, ".");
00135         TEST_ASSERT_EQUAL(0, res);
00136         res = ent.d_type;
00137         TEST_ASSERT_EQUAL(DT_DIR, res);
00138         res = dir[0].read(&ent);
00139         TEST_ASSERT_EQUAL(1, res);
00140         res = strcmp(ent.d_name, "..");
00141         TEST_ASSERT_EQUAL(0, res);
00142         res = ent.d_type;
00143         TEST_ASSERT_EQUAL(DT_DIR, res);
00144         res = dir[0].read(&ent);
00145         TEST_ASSERT_EQUAL(1, res);
00146         res = strcmp(ent.d_name, "a");
00147         TEST_ASSERT_EQUAL(0, res);
00148         res = ent.d_type;
00149         TEST_ASSERT_EQUAL(DT_REG, res);
00150     
00151         res = dir[0].read(&ent);
00152         TEST_ASSERT_EQUAL(1, res);
00153         res = strcmp(ent.d_name, "b");
00154         TEST_ASSERT_EQUAL(0, res);
00155         res = ent.d_type;
00156         TEST_ASSERT_EQUAL(DT_REG, res);
00157     
00158         res = dir[0].read(&ent);
00159         TEST_ASSERT_EQUAL(1, res);
00160         res = strcmp(ent.d_name, "c");
00161         TEST_ASSERT_EQUAL(0, res);
00162         res = ent.d_type;
00163         TEST_ASSERT_EQUAL(DT_REG, res);
00164     
00165         res = dir[0].read(&ent);
00166         TEST_ASSERT_EQUAL(1, res);
00167         res = strcmp(ent.d_name, "d");
00168         TEST_ASSERT_EQUAL(0, res);
00169         res = ent.d_type;
00170         TEST_ASSERT_EQUAL(DT_REG, res);
00171     
00172         res = dir[0].read(&ent);
00173         TEST_ASSERT_EQUAL(0, res);
00174         res = dir[0].close();
00175         TEST_ASSERT_EQUAL(0, res);
00176         res = file[0].open(&fs, "a", O_RDONLY);
00177         TEST_ASSERT_EQUAL(0, res);
00178         res = file[1].open(&fs, "b", O_RDONLY);
00179         TEST_ASSERT_EQUAL(0, res);
00180         res = file[2].open(&fs, "c", O_RDONLY);
00181         TEST_ASSERT_EQUAL(0, res);
00182         res = file[3].open(&fs, "d", O_RDONLY);
00183         TEST_ASSERT_EQUAL(0, res);
00184     
00185         for (int i = 0; i < 10; i++) {
00186             res = file[0].read(buffer, 1);
00187             TEST_ASSERT_EQUAL(1, res);
00188             res = buffer[0];
00189             TEST_ASSERT_EQUAL('a', res);
00190             res = file[1].read(buffer, 1);
00191             TEST_ASSERT_EQUAL(1, res);
00192             res = buffer[0];
00193             TEST_ASSERT_EQUAL('b', res);
00194             res = file[2].read(buffer, 1);
00195             TEST_ASSERT_EQUAL(1, res);
00196             res = buffer[0];
00197             TEST_ASSERT_EQUAL('c', res);
00198             res = file[3].read(buffer, 1);
00199             TEST_ASSERT_EQUAL(1, res);
00200             res = buffer[0];
00201             TEST_ASSERT_EQUAL('d', res);
00202         }
00203     
00204         file[0].close();
00205         file[1].close();
00206         file[2].close();
00207         file[3].close();
00208         res = fs.unmount();
00209         TEST_ASSERT_EQUAL(0, res);
00210     }
00211 
00212     res = bd.deinit();
00213     TEST_ASSERT_EQUAL(0, res);
00214 }
00215 
00216 void test_parallel_remove_file_test()
00217 {
00218     int res = bd.init();
00219     TEST_ASSERT_EQUAL(0, res);
00220 
00221     {
00222         res = fs.mount(&bd);
00223         TEST_ASSERT_EQUAL(0, res);
00224         res = file[0].open(&fs, "e", O_WRONLY | O_CREAT);
00225         TEST_ASSERT_EQUAL(0, res);
00226     
00227         for (int i = 0; i < 5; i++) {
00228             res = file[0].write((const void*)"e", 1);
00229             TEST_ASSERT_EQUAL(1, res);
00230         }
00231         res = fs.remove("a");
00232         TEST_ASSERT_EQUAL(0, res);
00233         res = fs.remove("b");
00234         TEST_ASSERT_EQUAL(0, res);
00235         res = fs.remove("c");
00236         TEST_ASSERT_EQUAL(0, res);
00237         res = fs.remove("d");
00238         TEST_ASSERT_EQUAL(0, res);
00239     
00240         for (int i = 0; i < 5; i++) {
00241             res = file[0].write((const void*)"e", 1);
00242             TEST_ASSERT_EQUAL(1, res);
00243         }
00244     
00245         file[0].close();
00246         res = dir[0].open(&fs, "/");
00247         TEST_ASSERT_EQUAL(0, res);
00248         res = dir[0].read(&ent);
00249         TEST_ASSERT_EQUAL(1, res);
00250         res = strcmp(ent.d_name, ".");
00251         TEST_ASSERT_EQUAL(0, res);
00252         res = ent.d_type;
00253         TEST_ASSERT_EQUAL(DT_DIR, res);
00254         res = dir[0].read(&ent);
00255         TEST_ASSERT_EQUAL(1, res);
00256         res = strcmp(ent.d_name, "..");
00257         TEST_ASSERT_EQUAL(0, res);
00258         res = ent.d_type;
00259         TEST_ASSERT_EQUAL(DT_DIR, res);
00260         res = dir[0].read(&ent);
00261         TEST_ASSERT_EQUAL(1, res);
00262         res = strcmp(ent.d_name, "e");
00263         TEST_ASSERT_EQUAL(0, res);
00264         res = ent.d_type;
00265         TEST_ASSERT_EQUAL(DT_REG, res);
00266     
00267         res = dir[0].read(&ent);
00268         TEST_ASSERT_EQUAL(0, res);
00269         res = dir[0].close();
00270         TEST_ASSERT_EQUAL(0, res);
00271         res = file[0].open(&fs, "e", O_RDONLY);
00272         TEST_ASSERT_EQUAL(0, res);
00273     
00274         for (int i = 0; i < 10; i++) {
00275             res = file[0].read(buffer, 1);
00276             TEST_ASSERT_EQUAL(1, res);
00277             res = buffer[0];
00278             TEST_ASSERT_EQUAL('e', res);
00279         }
00280     
00281         file[0].close();
00282         res = fs.unmount();
00283         TEST_ASSERT_EQUAL(0, res);
00284     }
00285 
00286     res = bd.deinit();
00287     TEST_ASSERT_EQUAL(0, res);
00288 }
00289 
00290 void test_remove_inconveniently_test()
00291 {
00292     int res = bd.init();
00293     TEST_ASSERT_EQUAL(0, res);
00294 
00295     {
00296         res = fs.mount(&bd);
00297         TEST_ASSERT_EQUAL(0, res);
00298         res = file[0].open(&fs, "e", O_WRONLY | O_TRUNC);
00299         TEST_ASSERT_EQUAL(0, res);
00300         res = file[1].open(&fs, "f", O_WRONLY | O_CREAT);
00301         TEST_ASSERT_EQUAL(0, res);
00302         res = file[2].open(&fs, "g", O_WRONLY | O_CREAT);
00303         TEST_ASSERT_EQUAL(0, res);
00304     
00305         for (int i = 0; i < 5; i++) {
00306             res = file[0].write((const void*)"e", 1);
00307             TEST_ASSERT_EQUAL(1, res);
00308             res = file[1].write((const void*)"f", 1);
00309             TEST_ASSERT_EQUAL(1, res);
00310             res = file[2].write((const void*)"g", 1);
00311             TEST_ASSERT_EQUAL(1, res);
00312         }
00313         res = fs.remove("f");
00314         TEST_ASSERT_EQUAL(0, res);
00315     
00316         for (int i = 0; i < 5; i++) {
00317             res = file[0].write((const void*)"e", 1);
00318             TEST_ASSERT_EQUAL(1, res);
00319             res = file[1].write((const void*)"f", 1);
00320             TEST_ASSERT_EQUAL(1, res);
00321             res = file[2].write((const void*)"g", 1);
00322             TEST_ASSERT_EQUAL(1, res);
00323         }
00324     
00325         file[0].close();
00326         file[1].close();
00327         file[2].close();
00328         res = dir[0].open(&fs, "/");
00329         TEST_ASSERT_EQUAL(0, res);
00330         res = dir[0].read(&ent);
00331         TEST_ASSERT_EQUAL(1, res);
00332         res = strcmp(ent.d_name, ".");
00333         TEST_ASSERT_EQUAL(0, res);
00334         res = ent.d_type;
00335         TEST_ASSERT_EQUAL(DT_DIR, res);
00336         res = dir[0].read(&ent);
00337         TEST_ASSERT_EQUAL(1, res);
00338         res = strcmp(ent.d_name, "..");
00339         TEST_ASSERT_EQUAL(0, res);
00340         res = ent.d_type;
00341         TEST_ASSERT_EQUAL(DT_DIR, res);
00342         res = dir[0].read(&ent);
00343         TEST_ASSERT_EQUAL(1, res);
00344         res = strcmp(ent.d_name, "e");
00345         TEST_ASSERT_EQUAL(0, res);
00346         res = ent.d_type;
00347         TEST_ASSERT_EQUAL(DT_REG, res);
00348     
00349         res = dir[0].read(&ent);
00350         TEST_ASSERT_EQUAL(1, res);
00351         res = strcmp(ent.d_name, "g");
00352         TEST_ASSERT_EQUAL(0, res);
00353         res = ent.d_type;
00354         TEST_ASSERT_EQUAL(DT_REG, res);
00355     
00356         res = dir[0].read(&ent);
00357         TEST_ASSERT_EQUAL(0, res);
00358         res = dir[0].close();
00359         TEST_ASSERT_EQUAL(0, res);
00360         res = file[0].open(&fs, "e", O_RDONLY);
00361         TEST_ASSERT_EQUAL(0, res);
00362         res = file[1].open(&fs, "g", O_RDONLY);
00363         TEST_ASSERT_EQUAL(0, res);
00364     
00365         for (int i = 0; i < 10; i++) {
00366             res = file[0].read(buffer, 1);
00367             TEST_ASSERT_EQUAL(1, res);
00368             res = buffer[0];
00369             TEST_ASSERT_EQUAL('e', res);
00370             res = file[1].read(buffer, 1);
00371             TEST_ASSERT_EQUAL(1, res);
00372             res = buffer[0];
00373             TEST_ASSERT_EQUAL('g', res);
00374         }
00375     
00376         file[0].close();
00377         file[1].close();
00378         res = fs.unmount();
00379         TEST_ASSERT_EQUAL(0, res);
00380     }
00381 
00382     res = bd.deinit();
00383     TEST_ASSERT_EQUAL(0, res);
00384 }
00385 
00386 
00387 
00388 // test setup
00389 utest::v1::status_t test_setup(const size_t number_of_cases)
00390 {
00391     GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
00392     return verbose_test_setup_handler(number_of_cases);
00393 }
00394 
00395 Case cases[] = {
00396     Case("Parallel tests", test_parallel_tests),
00397     Case("Parallel file test", test_parallel_file_test),
00398     Case("Parallel remove file test", test_parallel_remove_file_test),
00399     Case("Remove inconveniently test", test_remove_inconveniently_test),
00400 };
00401 
00402 Specification specification(test_setup, cases);
00403 
00404 int main()
00405 {
00406     return !Harness::run(specification);
00407 }