From c6a054bac114ad22d85c52479d55382ac7f2a09f Mon Sep 17 00:00:00 2001 From: batt Date: Thu, 21 May 2009 13:12:22 +0000 Subject: [PATCH] Add test for fatfs. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2697 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/fs/fat_test.c | 129 +++++++++++++++++++++++++++++++++++++++++++ test/run_tests.sh | 2 +- 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 bertos/fs/fat_test.c diff --git a/bertos/fs/fat_test.c b/bertos/fs/fat_test.c new file mode 100644 index 00000000..35125193 --- /dev/null +++ b/bertos/fs/fat_test.c @@ -0,0 +1,129 @@ +/** + * \file + * + * + * \brief FatFs test. + * + * \version $Id$ + * \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/src/ff.h" +#include "fatfs/src/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); diff --git a/test/run_tests.sh b/test/run_tests.sh index 633525d2..c6e34c1f 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -27,7 +27,7 @@ TESTS=${TESTS:-`find . \ -o -name "*_test.c" -print` } TESTOUT="testout" -SRC_LIST="bertos/algo/ramp.c bertos/drv/kdebug.c bertos/drv/timer.c bertos/fs/battfs.c bertos/kern/coop.c bertos/kern/idle.c bertos/kern/kfile.c bertos/kern/monitor.c bertos/kern/proc.c bertos/kern/signal.c bertos/kern/sem.c bertos/mware/event.c bertos/mware/formatwr.c bertos/mware/hex.c bertos/mware/sprintf.c bertos/os/hptime.c bertos/struct/kfile_fifo.c bertos/emul/switch_ctx_emul.S" +SRC_LIST="bertos/algo/ramp.c bertos/drv/kdebug.c bertos/drv/timer.c bertos/fs/battfs.c bertos/kern/coop.c bertos/kern/idle.c bertos/kern/kfile.c bertos/kern/monitor.c bertos/kern/proc.c bertos/kern/signal.c bertos/kern/sem.c bertos/mware/event.c bertos/mware/formatwr.c bertos/mware/hex.c bertos/mware/sprintf.c bertos/os/hptime.c bertos/struct/kfile_fifo.c bertos/fs/fatfs/src/ff.c bertos/emul/diskio_emul.c bertos/fs/fat.c bertos/emul/switch_ctx_emul.S" buildout='/dev/null' runout='/dev/null' -- 2.25.1