/** * \file * * * \brief FatFs test. * * \author Luca Ottaviano * * $test$: cp bertos/cfg/cfg_fat.h $cfgdir/ * $test$: echo "#undef CONFIG_FAT_USE_MKFS" >> $cfgdir/cfg_fat.h * $test$: echo "#define CONFIG_FAT_USE_MKFS 1" >> $cfgdir/cfg_fat.h * */ #include "fat.h" #include "fatfs/ff.h" #include "fatfs/diskio.h" #include /* avoid compiler warnings... */ int fatfile_testSetup(void); int fatfile_testTearDown(void); int fatfile_testRun(void); static FATFS file_system; int fatfile_testSetup(void) { FRESULT err; err = f_mount(0, &file_system); ASSERT(err == FR_OK); err = f_mkfs(0, 0, 512); ASSERT(err == FR_OK); return 0; } int fatfile_testTearDown(void) { FRESULT err; err = f_mount(0, 0); ASSERT(err == FR_OK); return 0; } int fatfile_testRun(void) { FRESULT fat_err; FatFile file_handler; const int SIZE = 10; int write[SIZE], read[SIZE]; fat_err = fatfile_open(&file_handler, "foo.txt", FA_WRITE | FA_CREATE_ALWAYS); ASSERT(fat_err == FR_OK); for (int i = 0; i < SIZE; ++i) { write[i] = i; size_t count = kfile_write(&file_handler.fd, &i, sizeof(int)); ASSERT(count == sizeof(int)); } /* test error function */ int tmp; if (kfile_read(&file_handler.fd, &tmp, sizeof(int)) < sizeof(int)) ASSERT(kfile_error(&file_handler.fd) == FR_DENIED); kfile_clearerr(&file_handler.fd); ASSERT(file_handler.error_code == FR_OK); int err = 0; err = kfile_close(&file_handler.fd); ASSERT(err == 0); fat_err = fatfile_open(&file_handler, "foo.txt", FA_READ); ASSERT(fat_err == FR_OK); for (int i = 0; i < SIZE; ++ i) { size_t count = kfile_read(&file_handler.fd, &read[i], sizeof(int)); ASSERT(count == sizeof(int)); /* check for correctness */ ASSERT(read[i] == write[i]); } /* test kfile_seek() */ ASSERT(kfile_seek(&file_handler.fd, -(sizeof(int) * SIZE * 2), KSM_SEEK_CUR) == 0); ASSERT(kfile_seek(&file_handler.fd, sizeof(int), KSM_SEEK_END) == EOF); ASSERT(kfile_close(&file_handler.fd) == 0); fatfile_open(&file_handler, "foo.txt", FA_READ | FA_WRITE); ASSERT((size_t)kfile_seek(&file_handler.fd, sizeof(int), KSM_SEEK_END) == sizeof(int) * (SIZE + 1)); ASSERT(kfile_seek(&file_handler.fd, -SIZE, KSM_SEEK_SET) == 0); return 0; } TEST_MAIN(fatfile);