ELEC351 SUBMISSION - Same as on the DLE
Embed:
(wiki syntax)
Show/hide line numbers
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 static char file_counter = 0; 00068 const char *filenames[] = {"smallavacado", "mediumavacado", "largeavacado", 00069 "blockfile", "bigblockfile", "hello", ".", ".."}; 00070 00071 // tests 00072 00073 void test_file_tests() { 00074 int res = bd.init(); 00075 TEST_ASSERT_EQUAL(0, res); 00076 00077 { 00078 res = MBED_TEST_FILESYSTEM::format(&bd); 00079 TEST_ASSERT_EQUAL(0, res); 00080 } 00081 00082 res = bd.deinit(); 00083 TEST_ASSERT_EQUAL(0, res); 00084 } 00085 00086 void test_simple_file_test() { 00087 int res = bd.init(); 00088 TEST_ASSERT_EQUAL(0, res); 00089 00090 { 00091 res = fs.mount(&bd); 00092 TEST_ASSERT_EQUAL(0, res); 00093 res = file[0].open(&fs, "hello", O_WRONLY | O_CREAT); 00094 TEST_ASSERT_EQUAL(0, res); 00095 size = strlen("Hello World!\n"); 00096 memcpy(wbuffer, "Hello World!\n", size); 00097 res = file[0].write(wbuffer, size); 00098 TEST_ASSERT_EQUAL(size, res); 00099 res = file[0].close(); 00100 TEST_ASSERT_EQUAL(0, res); 00101 res = file[0].open(&fs, "hello", O_RDONLY); 00102 TEST_ASSERT_EQUAL(0, res); 00103 size = strlen("Hello World!\n"); 00104 res = file[0].read(rbuffer, size); 00105 TEST_ASSERT_EQUAL(size, res); 00106 res = memcmp(rbuffer, wbuffer, size); 00107 TEST_ASSERT_EQUAL(0, res); 00108 res = file[0].close(); 00109 TEST_ASSERT_EQUAL(0, res); 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 template <int file_size, int write_size, int read_size> 00119 void test_write_file_test() { 00120 int res = bd.init(); 00121 TEST_ASSERT_EQUAL(0, res); 00122 00123 { 00124 size_t size = file_size; 00125 size_t chunk = write_size; 00126 srand(0); 00127 res = fs.mount(&bd); 00128 TEST_ASSERT_EQUAL(0, res); 00129 res = file[0].open(&fs, filenames[file_counter], O_WRONLY | O_CREAT); 00130 TEST_ASSERT_EQUAL(0, res); 00131 for (size_t i = 0; i < size; i += chunk) { 00132 chunk = (chunk < size - i) ? chunk : size - i; 00133 for (size_t b = 0; b < chunk; b++) { 00134 buffer[b] = rand() & 0xff; 00135 } 00136 res = file[0].write(buffer, chunk); 00137 TEST_ASSERT_EQUAL(chunk, res); 00138 } 00139 res = file[0].close(); 00140 TEST_ASSERT_EQUAL(0, res); 00141 res = fs.unmount(); 00142 TEST_ASSERT_EQUAL(0, res); 00143 } 00144 00145 { 00146 size_t size = file_size; 00147 size_t chunk = read_size; 00148 srand(0); 00149 res = fs.mount(&bd); 00150 TEST_ASSERT_EQUAL(0, res); 00151 res = file[0].open(&fs, filenames[file_counter], O_RDONLY); 00152 TEST_ASSERT_EQUAL(0, res); 00153 for (size_t i = 0; i < size; i += chunk) { 00154 chunk = (chunk < size - i) ? chunk : size - i; 00155 res = file[0].read(buffer, chunk); 00156 TEST_ASSERT_EQUAL(chunk, res); 00157 for (size_t b = 0; b < chunk && i+b < size; b++) { 00158 res = buffer[b]; 00159 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00160 } 00161 } 00162 res = file[0].close(); 00163 TEST_ASSERT_EQUAL(0, res); 00164 res = fs.unmount(); 00165 TEST_ASSERT_EQUAL(0, res); 00166 } 00167 00168 file_counter++; 00169 res = bd.deinit(); 00170 TEST_ASSERT_EQUAL(0, res); 00171 } 00172 00173 void test_non_overlap_check() { 00174 int res = bd.init(); 00175 TEST_ASSERT_EQUAL(0, res); 00176 00177 { 00178 size_t size = 32; 00179 size_t chunk = 29; 00180 srand(0); 00181 res = fs.mount(&bd); 00182 TEST_ASSERT_EQUAL(0, res); 00183 res = file[0].open(&fs, "smallavacado", O_RDONLY); 00184 TEST_ASSERT_EQUAL(0, res); 00185 for (size_t i = 0; i < size; i += chunk) { 00186 chunk = (chunk < size - i) ? chunk : size - i; 00187 res = file[0].read(buffer, chunk); 00188 TEST_ASSERT_EQUAL(chunk, res); 00189 for (size_t b = 0; b < chunk && i+b < size; b++) { 00190 res = buffer[b]; 00191 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00192 } 00193 } 00194 res = file[0].close(); 00195 TEST_ASSERT_EQUAL(0, res); 00196 res = fs.unmount(); 00197 TEST_ASSERT_EQUAL(0, res); 00198 } 00199 00200 { 00201 size_t size = 8192; 00202 size_t chunk = 29; 00203 srand(0); 00204 res = fs.mount(&bd); 00205 TEST_ASSERT_EQUAL(0, res); 00206 res = file[0].open(&fs, "mediumavacado", O_RDONLY); 00207 TEST_ASSERT_EQUAL(0, res); 00208 for (size_t i = 0; i < size; i += chunk) { 00209 chunk = (chunk < size - i) ? chunk : size - i; 00210 res = file[0].read(buffer, chunk); 00211 TEST_ASSERT_EQUAL(chunk, res); 00212 for (size_t b = 0; b < chunk && i+b < size; b++) { 00213 res = buffer[b]; 00214 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00215 } 00216 } 00217 res = file[0].close(); 00218 TEST_ASSERT_EQUAL(0, res); 00219 res = fs.unmount(); 00220 TEST_ASSERT_EQUAL(0, res); 00221 } 00222 00223 { 00224 size_t size = 262144; 00225 size_t chunk = 29; 00226 srand(0); 00227 res = fs.mount(&bd); 00228 TEST_ASSERT_EQUAL(0, res); 00229 res = file[0].open(&fs, "largeavacado", O_RDONLY); 00230 TEST_ASSERT_EQUAL(0, res); 00231 for (size_t i = 0; i < size; i += chunk) { 00232 chunk = (chunk < size - i) ? chunk : size - i; 00233 res = file[0].read(buffer, chunk); 00234 TEST_ASSERT_EQUAL(chunk, res); 00235 for (size_t b = 0; b < chunk && i+b < size; b++) { 00236 res = buffer[b]; 00237 TEST_ASSERT_EQUAL(rand() & 0xff, res); 00238 } 00239 } 00240 res = file[0].close(); 00241 TEST_ASSERT_EQUAL(0, res); 00242 res = fs.unmount(); 00243 TEST_ASSERT_EQUAL(0, res); 00244 } 00245 00246 res = bd.deinit(); 00247 TEST_ASSERT_EQUAL(0, res); 00248 } 00249 00250 void test_dir_check() { 00251 00252 int res = bd.init(); 00253 TEST_ASSERT_EQUAL(0, res); 00254 00255 { 00256 res = fs.mount(&bd); 00257 TEST_ASSERT_EQUAL(0, res); 00258 res = dir[0].open(&fs, "/"); 00259 TEST_ASSERT_EQUAL(0, res); 00260 int numFiles = sizeof(filenames)/sizeof(filenames[0]); 00261 // Check the filenames in directory 00262 while(1) { 00263 res = dir[0].read(&ent); 00264 if (0 == res) { 00265 break; 00266 } 00267 for (int i=0; i < numFiles ; i++) { 00268 res = strcmp(ent.d_name, filenames[i]); 00269 if (0 == res) { 00270 res = ent.d_type; 00271 if ((DT_REG != res) && (DT_DIR != res)) { 00272 TEST_ASSERT(1); 00273 } 00274 break; 00275 } 00276 else if( i == numFiles) { 00277 TEST_ASSERT_EQUAL(0, res); 00278 } 00279 } 00280 } 00281 res = dir[0].close(); 00282 TEST_ASSERT_EQUAL(0, res); 00283 res = fs.unmount(); 00284 TEST_ASSERT_EQUAL(0, res); 00285 } 00286 00287 res = bd.deinit(); 00288 TEST_ASSERT_EQUAL(0, res); 00289 } 00290 00291 00292 // test setup 00293 utest::v1::status_t test_setup(const size_t number_of_cases) { 00294 GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); 00295 return verbose_test_setup_handler(number_of_cases); 00296 } 00297 00298 Case cases[] = { 00299 Case("File tests", test_file_tests), 00300 Case("Simple file test", test_simple_file_test), 00301 Case("Small file test", test_write_file_test<32, 31, 29>), 00302 Case("Medium file test", test_write_file_test<8192, 31, 29>), 00303 Case("Large file test", test_write_file_test<262144, 31, 29>), 00304 Case("Block Size file test", test_write_file_test<9000, 512, 512>), 00305 Case("Multiple block size file test", test_write_file_test<26215, MBED_TEST_BUFFER, MBED_TEST_BUFFER>), 00306 Case("Non-overlap check", test_non_overlap_check), 00307 Case("Dir check", test_dir_check), 00308 }; 00309 00310 Specification specification(test_setup, cases); 00311 00312 int main() { 00313 return !Harness::run(specification); 00314 }
Generated on Mon Aug 22 2022 02:21:38 by
1.7.2