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_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 = !((fd[0] = fopen("/fs/" "a", "wb")) != NULL); 00107 TEST_ASSERT_EQUAL(0, res); 00108 res = !((fd[1] = fopen("/fs/" "b", "wb")) != NULL); 00109 TEST_ASSERT_EQUAL(0, res); 00110 res = !((fd[2] = fopen("/fs/" "c", "wb")) != NULL); 00111 TEST_ASSERT_EQUAL(0, res); 00112 res = !((fd[3] = fopen("/fs/" "d", "wb")) != NULL); 00113 TEST_ASSERT_EQUAL(0, res); 00114 00115 for (int i = 0; i < 10; i++) { 00116 res = fwrite((const void*)"a", 1, 1, fd[0]); 00117 TEST_ASSERT_EQUAL(1, res); 00118 res = fwrite((const void*)"b", 1, 1, fd[1]); 00119 TEST_ASSERT_EQUAL(1, res); 00120 res = fwrite((const void*)"c", 1, 1, fd[2]); 00121 TEST_ASSERT_EQUAL(1, res); 00122 res = fwrite((const void*)"d", 1, 1, fd[3]); 00123 TEST_ASSERT_EQUAL(1, res); 00124 } 00125 00126 fclose(fd[0]); 00127 fclose(fd[1]); 00128 fclose(fd[2]); 00129 fclose(fd[3]); 00130 res = !((dd[0] = opendir("/fs/" "/")) != NULL); 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 res = ed->d_type; 00137 TEST_ASSERT_EQUAL(DT_DIR, res); 00138 res = ((ed = readdir(dd[0])) != NULL); 00139 TEST_ASSERT_EQUAL(1, res); 00140 res = strcmp(ed->d_name, ".."); 00141 TEST_ASSERT_EQUAL(0, res); 00142 res = ed->d_type; 00143 TEST_ASSERT_EQUAL(DT_DIR, res); 00144 res = ((ed = readdir(dd[0])) != NULL); 00145 TEST_ASSERT_EQUAL(1, res); 00146 res = strcmp(ed->d_name, "a"); 00147 TEST_ASSERT_EQUAL(0, res); 00148 res = ed->d_type; 00149 TEST_ASSERT_EQUAL(DT_REG, res); 00150 00151 res = ((ed = readdir(dd[0])) != NULL); 00152 TEST_ASSERT_EQUAL(1, res); 00153 res = strcmp(ed->d_name, "b"); 00154 TEST_ASSERT_EQUAL(0, res); 00155 res = ed->d_type; 00156 TEST_ASSERT_EQUAL(DT_REG, res); 00157 00158 res = ((ed = readdir(dd[0])) != NULL); 00159 TEST_ASSERT_EQUAL(1, res); 00160 res = strcmp(ed->d_name, "c"); 00161 TEST_ASSERT_EQUAL(0, res); 00162 res = ed->d_type; 00163 TEST_ASSERT_EQUAL(DT_REG, res); 00164 00165 res = ((ed = readdir(dd[0])) != NULL); 00166 TEST_ASSERT_EQUAL(1, res); 00167 res = strcmp(ed->d_name, "d"); 00168 TEST_ASSERT_EQUAL(0, res); 00169 res = ed->d_type; 00170 TEST_ASSERT_EQUAL(DT_REG, res); 00171 00172 res = ((ed = readdir(dd[0])) != NULL); 00173 TEST_ASSERT_EQUAL(0, res); 00174 res = closedir(dd[0]); 00175 TEST_ASSERT_EQUAL(0, res); 00176 res = !((fd[0] = fopen("/fs/" "a", "rb")) != NULL); 00177 TEST_ASSERT_EQUAL(0, res); 00178 res = !((fd[1] = fopen("/fs/" "b", "rb")) != NULL); 00179 TEST_ASSERT_EQUAL(0, res); 00180 res = !((fd[2] = fopen("/fs/" "c", "rb")) != NULL); 00181 TEST_ASSERT_EQUAL(0, res); 00182 res = !((fd[3] = fopen("/fs/" "d", "rb")) != NULL); 00183 TEST_ASSERT_EQUAL(0, res); 00184 00185 for (int i = 0; i < 10; i++) { 00186 res = fread(buffer, 1, 1, fd[0]); 00187 TEST_ASSERT_EQUAL(1, res); 00188 res = buffer[0]; 00189 TEST_ASSERT_EQUAL('a', res); 00190 res = fread(buffer, 1, 1, fd[1]); 00191 TEST_ASSERT_EQUAL(1, res); 00192 res = buffer[0]; 00193 TEST_ASSERT_EQUAL('b', res); 00194 res = fread(buffer, 1, 1, fd[2]); 00195 TEST_ASSERT_EQUAL(1, res); 00196 res = buffer[0]; 00197 TEST_ASSERT_EQUAL('c', res); 00198 res = fread(buffer, 1, 1, fd[3]); 00199 TEST_ASSERT_EQUAL(1, res); 00200 res = buffer[0]; 00201 TEST_ASSERT_EQUAL('d', res); 00202 } 00203 00204 fclose(fd[0]); 00205 fclose(fd[1]); 00206 fclose(fd[2]); 00207 fclose(fd[3]); 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 = !((fd[0] = fopen("/fs/" "e", "wb")) != NULL); 00225 TEST_ASSERT_EQUAL(0, res); 00226 00227 for (int i = 0; i < 5; i++) { 00228 res = fwrite((const void*)"e", 1, 1, fd[0]); 00229 TEST_ASSERT_EQUAL(1, res); 00230 } 00231 res = remove("/fs/" "a"); 00232 TEST_ASSERT_EQUAL(0, res); 00233 res = remove("/fs/" "b"); 00234 TEST_ASSERT_EQUAL(0, res); 00235 res = remove("/fs/" "c"); 00236 TEST_ASSERT_EQUAL(0, res); 00237 res = remove("/fs/" "d"); 00238 TEST_ASSERT_EQUAL(0, res); 00239 00240 for (int i = 0; i < 5; i++) { 00241 res = fwrite((const void*)"e", 1, 1, fd[0]); 00242 TEST_ASSERT_EQUAL(1, res); 00243 } 00244 00245 fclose(fd[0]); 00246 res = !((dd[0] = opendir("/fs/" "/")) != NULL); 00247 TEST_ASSERT_EQUAL(0, res); 00248 res = ((ed = readdir(dd[0])) != NULL); 00249 TEST_ASSERT_EQUAL(1, res); 00250 res = strcmp(ed->d_name, "."); 00251 TEST_ASSERT_EQUAL(0, res); 00252 res = ed->d_type; 00253 TEST_ASSERT_EQUAL(DT_DIR, res); 00254 res = ((ed = readdir(dd[0])) != NULL); 00255 TEST_ASSERT_EQUAL(1, res); 00256 res = strcmp(ed->d_name, ".."); 00257 TEST_ASSERT_EQUAL(0, res); 00258 res = ed->d_type; 00259 TEST_ASSERT_EQUAL(DT_DIR, res); 00260 res = ((ed = readdir(dd[0])) != NULL); 00261 TEST_ASSERT_EQUAL(1, res); 00262 res = strcmp(ed->d_name, "e"); 00263 TEST_ASSERT_EQUAL(0, res); 00264 res = ed->d_type; 00265 TEST_ASSERT_EQUAL(DT_REG, res); 00266 00267 res = ((ed = readdir(dd[0])) != NULL); 00268 TEST_ASSERT_EQUAL(0, res); 00269 res = closedir(dd[0]); 00270 TEST_ASSERT_EQUAL(0, res); 00271 res = !((fd[0] = fopen("/fs/" "e", "rb")) != NULL); 00272 TEST_ASSERT_EQUAL(0, res); 00273 00274 for (int i = 0; i < 10; i++) { 00275 res = fread(buffer, 1, 1, fd[0]); 00276 TEST_ASSERT_EQUAL(1, res); 00277 res = buffer[0]; 00278 TEST_ASSERT_EQUAL('e', res); 00279 } 00280 00281 fclose(fd[0]); 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 = !((fd[0] = fopen("/fs/" "e", "wb")) != NULL); 00299 TEST_ASSERT_EQUAL(0, res); 00300 res = !((fd[1] = fopen("/fs/" "f", "wb")) != NULL); 00301 TEST_ASSERT_EQUAL(0, res); 00302 res = !((fd[2] = fopen("/fs/" "g", "wb")) != NULL); 00303 TEST_ASSERT_EQUAL(0, res); 00304 00305 for (int i = 0; i < 5; i++) { 00306 res = fwrite((const void*)"e", 1, 1, fd[0]); 00307 TEST_ASSERT_EQUAL(1, res); 00308 res = fwrite((const void*)"f", 1, 1, fd[1]); 00309 TEST_ASSERT_EQUAL(1, res); 00310 res = fwrite((const void*)"g", 1, 1, fd[2]); 00311 TEST_ASSERT_EQUAL(1, res); 00312 } 00313 res = remove("/fs/" "f"); 00314 TEST_ASSERT_EQUAL(0, res); 00315 00316 for (int i = 0; i < 5; i++) { 00317 res = fwrite((const void*)"e", 1, 1, fd[0]); 00318 TEST_ASSERT_EQUAL(1, res); 00319 res = fwrite((const void*)"f", 1, 1, fd[1]); 00320 TEST_ASSERT_EQUAL(1, res); 00321 res = fwrite((const void*)"g", 1, 1, fd[2]); 00322 TEST_ASSERT_EQUAL(1, res); 00323 } 00324 00325 fclose(fd[0]); 00326 fclose(fd[1]); 00327 fclose(fd[2]); 00328 res = !((dd[0] = opendir("/fs/" "/")) != NULL); 00329 TEST_ASSERT_EQUAL(0, res); 00330 res = ((ed = readdir(dd[0])) != NULL); 00331 TEST_ASSERT_EQUAL(1, res); 00332 res = strcmp(ed->d_name, "."); 00333 TEST_ASSERT_EQUAL(0, res); 00334 res = ed->d_type; 00335 TEST_ASSERT_EQUAL(DT_DIR, res); 00336 res = ((ed = readdir(dd[0])) != NULL); 00337 TEST_ASSERT_EQUAL(1, res); 00338 res = strcmp(ed->d_name, ".."); 00339 TEST_ASSERT_EQUAL(0, res); 00340 res = ed->d_type; 00341 TEST_ASSERT_EQUAL(DT_DIR, res); 00342 res = ((ed = readdir(dd[0])) != NULL); 00343 TEST_ASSERT_EQUAL(1, res); 00344 res = strcmp(ed->d_name, "e"); 00345 TEST_ASSERT_EQUAL(0, res); 00346 res = ed->d_type; 00347 TEST_ASSERT_EQUAL(DT_REG, res); 00348 00349 res = ((ed = readdir(dd[0])) != NULL); 00350 TEST_ASSERT_EQUAL(1, res); 00351 res = strcmp(ed->d_name, "g"); 00352 TEST_ASSERT_EQUAL(0, res); 00353 res = ed->d_type; 00354 TEST_ASSERT_EQUAL(DT_REG, res); 00355 00356 res = ((ed = readdir(dd[0])) != NULL); 00357 TEST_ASSERT_EQUAL(0, res); 00358 res = closedir(dd[0]); 00359 TEST_ASSERT_EQUAL(0, res); 00360 res = !((fd[0] = fopen("/fs/" "e", "rb")) != NULL); 00361 TEST_ASSERT_EQUAL(0, res); 00362 res = !((fd[1] = fopen("/fs/" "g", "rb")) != NULL); 00363 TEST_ASSERT_EQUAL(0, res); 00364 00365 for (int i = 0; i < 10; i++) { 00366 res = fread(buffer, 1, 1, fd[0]); 00367 TEST_ASSERT_EQUAL(1, res); 00368 res = buffer[0]; 00369 TEST_ASSERT_EQUAL('e', res); 00370 res = fread(buffer, 1, 1, fd[1]); 00371 TEST_ASSERT_EQUAL(1, res); 00372 res = buffer[0]; 00373 TEST_ASSERT_EQUAL('g', res); 00374 } 00375 00376 fclose(fd[0]); 00377 fclose(fd[1]); 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 }
Generated on Tue Jul 12 2022 13:24:53 by
1.7.2