From 96a5722d5be21779a7f58e16995de9a39ab91bad Mon Sep 17 00:00:00 2001 From: asterix Date: Fri, 9 Jan 2009 17:43:34 +0000 Subject: [PATCH] Refactor kfile test adding the beckend to be compile and run on pc. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2155 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/kern/kfile_test.c | 140 ++++++++++++++++++++++++++++++++------- 1 file changed, 117 insertions(+), 23 deletions(-) diff --git a/bertos/kern/kfile_test.c b/bertos/kern/kfile_test.c index 8874209c..21fc3d3b 100644 --- a/bertos/kern/kfile_test.c +++ b/bertos/kern/kfile_test.c @@ -44,6 +44,7 @@ #include "cfg/cfg_kfile.h" #include +#include #include // Define logging setting (for cfg/log.h module). @@ -55,9 +56,90 @@ #include - MOD_DEFINE(kfile_test); +#define BUF_TEST_LEN 3209 + +typedef uint8_t fake_t; +fake_t test_buf[BUF_TEST_LEN]; +fake_t test_buf_save[BUF_TEST_LEN]; +fake_t test_disk[BUF_TEST_LEN]; + +KFile fd; + +static int fake_close(KFile *fd) +{ + (void)fd; + return 0; +} + +static size_t fake_read(KFile *fd, void *buf, size_t size) +{ + fake_t *dest = (fake_t *)buf; + size_t rd_len; + + rd_len = MIN((kfile_off_t)size, fd->size - fd->seek_pos); + + memcpy(dest, test_disk, size); + fd->seek_pos += rd_len; + + LOG_INFO("Read: real[%ld] expected[%ld]\n", rd_len, size); + + return rd_len; +} + +static size_t fake_write(KFile *fd, const void *buf, size_t size) +{ + fake_t *src = (fake_t *)buf; + size_t wr_len; + + wr_len = MIN((kfile_off_t)size, fd->size - fd->seek_pos); + + memcpy(test_disk, src, wr_len); + fd->seek_pos += wr_len; + + LOG_INFO("Write: real[%ld] expected[%ld]\n", wr_len, size); + + return wr_len; +} + +int fake_flush(KFile *fd) +{ + (void)fd; + + return 0; +} + +void fake_kfileInit(void) +{ + // Setup data flash programming functions. + fd.reopen = kfile_genericReopen; + fd.close = fake_close; + fd.read = fake_read; + fd.write = fake_write; + fd.seek = kfile_genericSeek; + fd.flush = fake_flush; + + fd.seek_pos = 0; + fd.size = BUF_TEST_LEN; + +} + +static void init_testBuf(void) +{ + #include + + kprintf("Init fake buffer..\n"); + for (int i = 0; i < BUF_TEST_LEN; i++) + { + test_disk[i] = random(); + kprintf("%d ", test_disk[i]); + } + kprintf("\nend\n"); + + memset(test_buf, 0, sizeof(test_buf)); + memset(test_buf_save, 0, sizeof(test_buf_save)); +} /** * KFile read/write subtest. @@ -99,28 +181,17 @@ static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t size) for (size_t i = 0; i < size; i++) if (buf[i] != (i & 0xff)) return false; - return true; -} -/** - * Setup all needed for kfile test - */ -int kfile_testSetUp(void) -{ - MOD_INIT(kfile_test); - LOG_INFO("Mod init..ok\n"); - - return 0; + return true; } - /** * KFile read/write test. * This function write and read \a test_buf long \a size * on \a fd handler. * \a save_buf can be NULL or a buffer where to save previous file content. */ -int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) +int kfile_testRunGeneric(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) { /* @@ -141,8 +212,8 @@ int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) */ if (save_buf) { + LOG_INFO("Saved content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size); kfile_read(fd, save_buf, size); - LOG_INFO("Saved content..form [%ld] to [%ld]\n", fd->seek_pos, fd->seek_pos + size); } /* TEST 1 BEGIN. */ @@ -172,12 +243,12 @@ int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) if (kfile_write(fd, save_buf, size) != size) goto kfile_test_end; - LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size); + LOG_INFO("Restore content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size); } /* TEST 1 END. */ /* TEST 2 BEGIN. */ - LOG_INFO("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , fd->size/2 + size); + LOG_INFO("Test 2: write from pos [%d] to [%ld]\n", fd->size/2 , fd->size/2 + size); /* * Go to half test size. @@ -192,7 +263,7 @@ int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) { kfile_read(fd, save_buf, size); kfile_seek(fd, -(kfile_off_t)size, KSM_SEEK_CUR); - LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size); + LOG_INFO("Saved content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size); } /* @@ -213,13 +284,13 @@ int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) if (kfile_write(fd, save_buf, size) != size) goto kfile_test_end; - LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + size); + LOG_INFO("Restore content..form [%d] to [%ld]\n", fd->seek_pos, fd->seek_pos + size); } /* TEST 2 END. */ /* TEST 3 BEGIN. */ - LOG_INFO("Test 3: write outside of fd->size limit [%lu]\n", fd->size); + LOG_INFO("Test 3: write outside of fd->size limit [%d]\n", fd->size); LOG_INFO("This test should FAIL!, you must see an assertion fail message.\n"); /* @@ -235,7 +306,7 @@ int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) { kfile_read(fd, save_buf, len); kfile_seek(fd, -len, KSM_SEEK_CUR); - LOG_INFO("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len); + LOG_INFO("Saved content..form [%d] to [%d]\n", fd->seek_pos, fd->seek_pos + len); } /* @@ -256,7 +327,7 @@ int kfile_testRun(KFile *fd, uint8_t *test_buf, uint8_t *save_buf, size_t size) if ((kfile_off_t)kfile_write(fd, save_buf, len) != len) goto kfile_test_end; - LOG_INFO("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len); + LOG_INFO("Restore content..form [%d] to [%d]\n", fd->seek_pos, fd->seek_pos + len); } /* TEST 3 END. */ @@ -270,12 +341,35 @@ kfile_test_end: return EOF; } + + + +/** + * Setup all needed for kfile test + */ +int kfile_testSetup(void) +{ + MOD_INIT(kfile_test); + LOG_INFO("Mod init..ok\n"); + + fake_kfileInit(); + init_testBuf(); + + return 0; +} + +int kfile_testRun(void) +{ + return kfile_testRunGeneric(&fd, test_buf, test_buf_save, BUF_TEST_LEN); +} + /** * End a dataflash Test. * (Unused) */ int kfile_testTearDown(void) { - /* */ return 0; } + +TEST_MAIN(kfile); -- 2.25.1